/*
	Autore: Matteo Ossani, Danilo Cicognani
	Script: common.js
	Applicazione: CyclE-R
	Versione: 1.00
	Data: 04/05/2011
	Scopo: Funzionalita' di animazione JavaScript
	Copyright (c) 2011 CambiamentiWeb SAS e CicoNET di Danilo Cicognani
*/

//html5 for IE
document.createElement('header');
document.createElement('footer');
document.createElement('section');
document.createElement('article');
document.createElement('aside');
document.createElement('nav');

var map;
var layerPath;
var layerPathZoom;
var layerPOIShow;
var layerPOIHide;
var infoPOI;
var pixelOffset;

// Animazione proposte turistiche in home
var aryBox;
var intIndex = 0;
var tmrSwap = null;
function swap() {
	$(aryBox[intIndex]).fadeOut(500);
	if (++intIndex > aryBox.length - 1)
		intIndex = 0;
	$(aryBox[intIndex]).fadeIn(500);
	tmrSwap = window.setTimeout('swap()', 4000);
}

$(document).ready(function() {
	//login-panel
	$('.trigger').click(function() {
		$('.login-panel').toggle('fast');
		$(this).toggleClass('active');
		return false;
	});

	//POI
	$('.open-poi').click(function() {
		$(this).toggleClass('selected');
		$(this).next().toggle('fast');
		return false;
	}).next().hide();
	$('.poi-menu input').click(function() {
		if ($(this).attr('checked') != '')
			eval($(this).val()).setMap(map);
		else
			eval($(this).val()).setMap(null);
	});

	//LEGENDA
	$('.open-legenda').click(function() {
		$(this).toggleClass('selected');
		$(this).next().toggle('fast');
		return false;
	}).next().hide();

	// Google Map
	if ($('#map_canvas').length == 1) {
		var centro = new google.maps.LatLng(44.5764472, 11.3190156);
		var myOptions = {
			zoom: 8,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			center: centro
		};
		map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		layerPath = {map: map, preserveViewport: true, suppressInfoWindows: true};
		layerPathZoom = {map: map, suppressInfoWindows: true};
		layerPOIShow = {map: map, preserveViewport: true, suppressInfoWindows: true};
		layerPOIHide = {map: null, preserveViewport: true, suppressInfoWindows: true};
		pixelOffset = new google.maps.Size(0, 0);
		infoPOI = new google.maps.InfoWindow();
	}

	// Votes
	$('.staroff').hover(function() {
		if (! $(this).hasClass('starvoted')) {
			intStar = $(this).attr('rel');
			for (i = 1; i <= intStar; i++)
				$('.staroff[rel = ' + i + ']').addClass('staron');
		}
	}, function() {
		if (! $(this).hasClass('starvoted')) {
			$('.staroff').removeClass('staron');
		}
	});

	// Animazione proposte turistiche in home
	aryBox = $('div.package-box').toArray();
	if (aryBox.length) {
		tmrSwap = window.setTimeout('swap()', 4000);
		$('div.package-box').hover(function() {
			window.clearTimeout(tmrSwap);
		}, function() {
			tmrSwap = window.setTimeout('swap()', 4000);
		});
	}
});

function showPOIInfo(intPoi) {
	var latlng = $('#poi-info-' + intPoi).attr('rel');
	var poipos = new google.maps.LatLng(latlng.substring(0, latlng.indexOf(',')), latlng.substr(latlng.indexOf(',') + 1));
	if (typeof(infoPOI) != 'undefined')
		infoPOI.close();
	infoPOI.setOptions({
		content: $('#poi-info-' + intPoi).html(),
		pixelOffset: pixelOffset,
		position: poipos
	});
	infoPOI.open(map);
}

function vote(intPath, intVote) {
	if (! $('.staroff[rel = ' + intVote + ']').hasClass('starvoted')) {
		$('.staroff').addClass('starvoted');
		$objAjaxCall = $.ajax({
			'url': strVoteLink,
			'data': {'site': strSite, 'path': intPath, 'vote': intVote},
			'dataType': 'json',
			'type': 'POST',
			'success': function(data) {
				if (data[0].result == '1')
					$('#starthank').show();
				else if (data[0].result == '2')
					$('#staralready').show();
				else
					$('#starerror').show();
			},
			'error': function(data) {
				$('#starerror').show();
			}
		});
	}
}
