
function anchorTo( anchor ) {
	var urllocation = location.href; //find url parameter
	if(urllocation.indexOf("#") == -1 || urllocation.indexOf("#") > -1) {
		window.location.hash=anchor; 
	}
}


Effect.Transitions.Elastic = function(pos) {
   	return -1*Math.pow(4,-4*pos) * Math.sin((pos*3-1)*(3*Math.PI)/2) + 1;
};

function showBox(boxID){
	var box = document.getElementById(boxID);
	var button_close = document.getElementById('button_close_'+boxID);
	var button_open = document.getElementById('button_open_'+boxID);
	new Effect.BlindDown($(box),{duration:.3});
	button_open.style.display = 'none';
	button_close.style.display = ''; 
}
function hideBox(boxID){
	var box = document.getElementById(boxID);
	var button_close = document.getElementById('button_close_'+boxID);
	var button_open = document.getElementById('button_open_'+boxID);
	new Effect.BlindUp($(box), {duration:.3});
	button_open.style.display = '';
	button_close.style.display = 'none'; 
}

/**
 * Spinner
 */
function rePositionSpinner() {			
	var spinner = document.getElementById('a4jstatus');
	var position = document.documentElement.scrollTop + Math.floor( document.documentElement.clientHeight / 2 );
	//alert('position: '+ document.documentElement.scrollTop );	// DEBUG
	spinner.style.top = position + 'px';
}


/**
 * Invia form con tasto Invio
 */
function submitEnterPressed(e) {

 if ((window.event && window.event.keyCode == 13) || (e && e.which == 13)) {

	if ( (navigator.userAgent.indexOf("MSIE") == -1) && delEnterPressed ) { delEnterPressed = false; return; }

	var searchBt = document.getElementById('searchBarForm:searchBt1');
	if ( searchBt==null ) searchBt = document.getElementById('searchBarForm:searchBt2');
	if ( searchBt==null ) searchBt = document.getElementById('boxVerderForm:searchBt1');
	if ( searchBt!=null ) searchBt.click();
	 	 
	if (navigator.userAgent.indexOf("MSIE") != -1) {
   
           window.event.cancelBubble = true;
             window.event.returnValue = false;
   
   } else e.preventDefault();
   
 }
 
}
var delEnterPressed = false;
function suggestionSelect() { delEnterPressed = true; }


/**
 * Apre la finestra di Gmap
 */
function openGmapWindow( url ) {
	var w = 800, h = 550; // default sizes
	var	xPos = 250, yPos = 250;
	
	if (window.screen) {
		//w = window.screen.availWidth * 90 / 100;
		h = window.screen.availHeight * 70 / 100;
		yPos = window.screen.availWidth/2 - w/2;
		xPos = h/15;
	}
	hmap = h - 100;
    
    window.open(url+'&h='+hmap, 'mapWindow', 'scrollbars=yes, location=no,toolbar=no,resizable=1,width='+w+',height='+h+',top='+xPos+',left='+yPos);
}


/**
 * Prende la data di inizio e setta la data
 * fine al giorno successivo di una certa form.
 * ID input data inizio: inputDataInizio
 * ID input data fine: inputDataFine
 */
function setDataFineADomani( formID ) { 	
	
	// Estrazione e conversione input
	var input = document.getElementById(formID+':inputDataInizioInputDate').value;  			
	var anno = parseInt(input.substring(6,10),10);
	var mese = parseInt(input.substring(3,5),10)-1;
	var giorno = parseInt(input.substring(0,2),10);
	
	if ( giorno > 0 && giorno < 32 && mese >= 0 && mese < 13 && anno > 1000 ) {
		
		var annoFine = (giorno==31 && mese==11) ? anno+1 : anno; 
		 
		// Creazione della data fine
		var dataFine = new Date();	
		dataFine.setFullYear( anno , mese , giorno+1 );	
		
		// Conversione e scrittura nel campo form
		var giornoFine = dataFine.getDate();
		var meseFine = dataFine.getMonth()+1;
		var output = (giornoFine<10?'0'+giornoFine:giornoFine)+'/'+((meseFine)<10?'0'+meseFine:meseFine)+'/'+(annoFine);	
		document.getElementById(formID+':inputDataFineInputDate').value = output;
	
		//alert( 'Input: '+input+ '\nOutput: '+output );
	}
 	
}

/**
 * Esegue il controllo sulle date di checkIn e checkOut
 */
function checkDate( formID ) {
	
	var checkIn = document.getElementById(formID+':inputDataInizioInputDate').value;
	var checkOut = document.getElementById(formID+':inputDataFineInputDate').value; 
	
	if ( checkIn == '' || checkOut == '' ) {
		//alert('ATTENZIONE: Inserire le date di arrivo e partenza in modo corretto.');
		return true;
	}

	// Estrazione e conversione input checkIn		
	var annoIn = parseInt(checkIn.substring(6,10),10);
	var meseIn = parseInt(checkIn.substring(3,5),10)-1;
	var giornoIn = parseInt(checkIn.substring(0,2),10);
	
	// Creazione della data fine
	var dataInizio = new Date();	
	dataInizio.setFullYear( annoIn , meseIn , giornoIn );	
	
	// Estrazione e conversione input checkOut 			
	var annoOut = parseInt(checkOut.substring(6,10),10);
	var meseOut = parseInt(checkOut.substring(3,5),10)-1;
	var giornoOut = parseInt(checkOut.substring(0,2),10);
	
	// Creazione della data fine
	var dataFine = new Date();	
	dataFine.setFullYear( annoOut , meseOut , giornoOut );	
	
	//alert( dataInizio.getTime() +' - ' + new Date().getTime() +' > ' + (dataInizio.getTime() < new Date().getTime()) )
	
	if ( dataInizio.getTime() < new Date().getTime() ) {
		alert('ATTENZIONE: La data di arrivo non puo essere precedente alla data odierna.');
		return false;
	}
	
	if ( dataFine.getTime() <= dataInizio.getTime()  ) {
		alert('ATTENZIONE: La data di partenza deve essere successiva alla data di arrivo.');
		return false;
	}
	
	if ( (dataFine.getTime()-dataInizio.getTime()) > 1000*60*60*24*30  ) {
		alert('ATTENZIONE: La data di partenza non puo essere superiore di 30 giorni alla data di arrivo.');
		return false;
	}
	
	return true; 
	
}


//******************************************
//************************* Quicklook box **
//******************************************
function hideAllQuicklook_box(mask){
	var allPageTags=document.getElementsByTagName("div");
	for (i=0; i<allPageTags.length; i++)
		if (allPageTags[i].className=='quicklook_box') 
			new Effect.Fade ($(allPageTags[i]),{duration:0.1});
	new Effect.Fade ($(mask),{duration:0.3});
}
function showQuicklook_box(quicklook){
	var quicklook = document.getElementById(quicklook);
	var mask = document.getElementById('mask');

	mask.style.height = document.body.offsetHeight + 'px';
	new Effect.Appear ($(mask),{duration:0.4, from:0.0, to:0.7});
	
	new Effect.Grow ($(quicklook),{duration:0.2});
}


//******************************************
//************** CARICAMENTO GMAP STATICA **
//******************************************
var cittaLat = 0;
var cittaLng = 0;
var strutture = new Array();
var count = 0;
function setPointStruttura( lat , lng ) {
	if (count==0) cittaLat=lat; cittaLng=lng;
	strutture[count] = {lat: lat, lng: lng};
	count = count + 1;
}
function setCittaLatLng(centro) {
	if (centro!='' ) {   		    	
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng( unescape(centro) , loadGMap );
	}
}
function loadGMap(point) {
	
	if (point) {
		cittaLat = point.lat();
		cittaLng = point.lng();
	}

	var mapDiv = document.getElementById('staticgmap');
	
	if (mapDiv != null) {								
		
		var urlString = ['http://maps.google.com/staticmap'];
		
		urlString.push('?size=252x330');
		urlString.push('&key='+gmapKey);
		
		if ( strutture.length > 0 ){
			urlString.push('&markers=');
			var markerString = [];
			for (var i = 0; i < strutture.length; i++) {
			  markerString.push(strutture[i].lat + ',' + strutture[i].lng + ',blue');
			}
			urlString.push(markerString.join('|'));
		} else {
			urlString.push('&zoom=10');
			urlString.push('&center='+cittaLat+','+cittaLng);
		}
		
		gmapImg = 'url(\'' + urlString.join('') + '\')';
	
		count=0;
		strutture = new Array();
		mapDiv.style.background = gmapImg;
		//mapDiv.innerHTML = urlString.join('');
	}
}

function showHideBoxHome(BoxId) {
	var box = document.getElementById(BoxId);			
	if ( box.style.display == 'none' ) new Effect.SlideDown ($(box),{duration:0.3});
	else new Effect.SlideUp ($(box),{duration:0.3});
}

function MostraNascondi(x) { if (document.getElementById(x)) { if (document.getElementById(x).style.display == '') { document.getElementById(x).style.display = 'none'; } else { document.getElementById(x).style.display = ''; } } }

