/******************************************************************************************************************
**
**							FONCTIONS JAVASCRIPT POUR GOOGLE MAP SUR L ECRAN HOME VACANCES
**
*******************************************************************************************************************/




var GestionMapHome = function()
{
	//================================
	// 		variables privées
	//================================
	
	//================================
	// 		méthodes privées
	//================================
	var initMapHome = function ()
		{
			if (GBrowserIsCompatible()) {			
			    
			    //Initialisation des limites de déplacements
			    allowedBounds   = new GLatLngBounds(new GLatLng(coordcountrytab.FR.latboundsw,coordcountrytab.FR.longboundsw), new GLatLng(coordcountrytab.FR.latboundne,coordcountrytab.FR.longboundne));	
				
				
			    Amaxlg = allowedBounds.getNorthEast().lng();
				Amaxlt = allowedBounds.getNorthEast().lat();
				Aminlg = allowedBounds.getSouthWest().lng();
				Aminlt = allowedBounds.getSouthWest().lat();	  
			 
			    //Initialisation de la carte	    
			    map = new GMap2(document.getElementById("mapHome"));		
			    var zoomLevel = map.getBoundsZoomLevel(allowedBounds) + 1;
				map.setCenter(allowedBounds.getCenter(), zoomLevel);
			   
			    //Ajout des barres de controles
			    addDragZoomControl(map,'zooming');
			    map.addControl(new pvControlBar());
			    map.addControl(new pvTypeControlBar());
			    map.disableDoubleClickZoom();
			    
				//Niveau de zoom maximum (un pays entier), on ne se deplace pas
				map.disableDragging();
				zoomMax = map.getZoom();
				limitZoom(zoomMax);
				
			    // Limitation du mouvement		    
				allowedBoundsListener = GEvent.addListener(map, "moveend", function() {
					checkBounds();
					changeCountryMode=false; // Permet de débloquer le control du mouvement après le premier déplacement
					if(checkingMode){
						//Le timeout sert ici à la méthode panTo dans checkBounds
						//Sans cela updateMarkers() est appelée avant le repositionnement de la carte
						setTimeout (function(){checkingMode = false;}, 1000 );	
					}
				});	
				// Désactivation du drag en zoomMax
				zoomListener = GEvent.addListener(map, "zoomend", function() {						
					checkZoom();
					checkBounds();
				});	
					
			}// if GBrowserIsCompatible		
			else{
				document.getElementById("message").innerHTML = "Votre navigateur n'est pas compatible avec Google Maps !";			
			} 
		
		};
		
		var reloadCoord = function ()
			{
				var bounds	= map.getBounds();
				var northEastBounds = bounds.getNorthEast();
				var southWestBounds = bounds.getSouthWest();
				
				ltsw = southWestBounds.lat();
				lgsw = southWestBounds.lng();
				ltne = northEastBounds.lat();
				lgne = northEastBounds.lng();		
				
			}//reloadCoord
	
		var submitFormRecherche = function () {
			
			reloadCoord();
			paramRecherche = createParamRecherche('home');
			if(paramRecherche === false){
				return;
			}
			//var paramGeoloc = createGeolocRecherche(ltsw,lgsw,ltne,lgne);
			var paramGeoloc = '&ltne='+ltne+'&lgne='+lgne+'&ltsw='+ltsw+'&lgsw='+lgsw;
			window.location.href=(urlResult +'?'+ paramRecherche + paramGeoloc);
			
			return true;
		}// submitRecherche()
		
	return {
		//================================
		// 		variables publiques
		//================================
		
		//================================
		// 		méthodes publiques
		//================================
		init: function ()
			{
				initMapHome();
			}, // init
		
		submitRecherche: function ()
			{
				submitFormRecherche();
			},
		
		majTyh: function()
			{
				if(document.recherche.tous.checked === false){
					document.recherche.tyhApp.checked = false;
					document.recherche.tyhMsn.checked = false;
					document.recherche.tyhChl.checked = false;
					document.recherche.tyhHtl.checked = false;
					document.recherche.tyhGt.checked  = false;
					document.recherche.tyhMH.checked  = false;
					document.recherche.tyhHH.checked  = false;
					document.recherche.tyhCA.checked  = false;
				}else{
					document.recherche.tyhApp.checked = true;
					document.recherche.tyhMsn.checked = true;
					document.recherche.tyhChl.checked = true;
					document.recherche.tyhHtl.checked = true;
					document.recherche.tyhGt.checked  = true;
					document.recherche.tyhMH.checked  = true;
					document.recherche.tyhHH.checked  = true;
					document.recherche.tyhCA.checked  = true;
				}
			},
		
		centerOnLocation: function(libelle, element)
			{
				map.setCenter(new GLatLng(element.attributes['lat'].nodeValue, element.attributes['long'].nodeValue), 14);
			}	
		
			
			
	}; // public	
	
	
	
}();		

/**
 * Chargement de la carte et initialisation de l'autocomplétion
 */
function onLoad()
{
	GestionMapHome.init();
}


/**
 * Affichage du panel de précision localisation
 * @param {Object} xhr
 */
function displayList(xhr){
	var list = xhr.responseText;
	$('list_localisation').innerHTML = list;
	$('localisation_panel').style.display = 'block';
	$('autocomplete_choices').style.display = 'none';
	$('indicator1').style.display = 'none';	
}

/**
 * Centre sur la ville choisi dans la précision localisation
 * @param {Object} latitude
 * @param {Object} longitude
 */
function choiceLocation(latitude, longitude) {
	$('localisation_panel').style.display = 'none';
	$('autocomplete_choices').style.display = 'none';
	$('myCitylat').value = latitude;
	$('myCitylon').value = longitude;
	validcarte(null);
}

/**
 * Action lancer par le bouton Ok
 * Propose le panel de précision localisation
 */
function checklocalisation() {

	if ($('autocomplete').value.blank() ||  $('autocomplete').value.length < 3) {
		return;
	}
	$('myCitylat').value = '';
	$('myCitylon').value = '';
	$('indicator1').style.display = 'block';	
	var ajaxCheck = new Ajax.Request(serverUrlCheckLocalisation,
									      {
									        method: 'post',
									        asynchronous: !(/async=0/.test(window.location.search)),
											parameters: {'request' : $('autocomplete').value										        			 
									        			},

									        onSuccess: displayList.bind(this)
									      }
						);
	
}

// Autocomplétion
function loadAutocomp()
{
	
	if ($('autocomplete').value.blank()) {
		return;
	}	
	
	if($('autocomplete').value.length >= 2)
	{
		$('myCitylat').value = '';
		$('myCitylon').value = '';
		$('indicator1').style.display = 'block';	
		var ajaxCheck = new Ajax.Request('/communfo/homecommunfo/ajax/autocompletion',
			{
			method: 'post',
			asynchronous: !(/async=0/.test(window.location.search)),
			parameters: {value: $('autocomplete').value , map: true										        			 
			},

			onSuccess: displayAutocomp.bind(this)
			}
		);
	}
}
function displayAutocomp(xhr)
{
	var vlist = xhr.responseText;

/* Home Vacances 2009 04 */

	if(Prototype.Browser.IE)
	{
		contenu = '<div id="acm_cdr" class="acm_cdr" onmouseover="this.parentNode.style.display=\'block\'" onmouseleave="this.parentNode.style.display=\'none\'">';
	}
	else
	{
		contenu = '<div id="acm_cdr" class="acm_cdr" onmouseover="this.parentNode.style.display=\'block\'" onmouseout="this.parentNode.style.display=\'none\'">';
	}	
	contenu += vlist;
	contenu += '</div>';
	
	$('autocomplete_choices').innerHTML = contenu;
	$('autocomplete_choices').style.display = 'block';

	
	if(Prototype.Browser.IE)
	{
		ht = $('acm_cdr').getElementsByTagName('div')[0].offsetHeight;
		$('autocomplete_choices').style.top = (- ht - 25) + 'px';
	}
	else	
	{
		ht = $('acm_cdr').getElementsByTagName('div')[0].clientHeight;
		$('autocomplete_choices').style.top = (- ht - 25) + 'px';
	}
	$('localisation_panel').style.display = 'none';
	$('indicator1').style.display = 'none';	
/* FIN Home Vacances 2009 04 */

}

function choiceAutocomp(latitude, longitude, lib) 
{
	$('autocomplete_choices').style.display = 'none';	
	$('lo').value = lib;
	$('autocomplete').value = lib;
	$('myCitylat').value = latitude;
	$('myCitylon').value = longitude;
}



/* Home Vacances 2009 04 */

	function affPlusDeCrit(ahref)
	{
		obj = document.getElementById('plusdecrit');
		if(obj.style.display == 'none')
		{
			obj.style.display = 'block';
			ahref.className = 'va_ho_moins_crit';
			ahref.innerHTML = 'Masquer les critères';
		}
		else
		{
			obj.style.display = 'none';
			ahref.className = 'va_ho_plus_crit';
			ahref.innerHTML = 'Afficher plus de critères';
		}
	}
	
	last_open = '';
	function selectopen(obj)
	{

		objet = document.getElementById(obj);
		if(objet.style.display == 'none')
		{
			objet.style.display = 'block';
			if(last_open != '')
			{
				last_open.style.display = 'none';
			}
			last_open = objet;
		}
		else
		{
			objet.style.display = 'none';
			last_open = '';
		}	
		
	}
	function selectli(obj,valeur)
	{
		objet = obj.parentNode.parentNode.parentNode.parentNode;
		objet.style.display = 'none';
		objet.parentNode.getElementsByTagName('input')[0].value = valeur;
		objet.parentNode.getElementsByTagName('strong')[0].innerHTML = obj.innerHTML;
		last_open = ''; 
	}
	function selectchk(obj)
	{
		objet = obj.parentNode.parentNode.parentNode.parentNode.parentNode;
		
		valeurs = objet.getElementsByTagName('span');
		libelle = '';
		for(v=0 ; v < valeurs.length ; v++)
		{
			if( valeurs[v].parentNode.getElementsByTagName('input')[0].checked)
			{
				if(libelle != '') libelle += ', ';
				libelle+= valeurs[v].innerHTML;
			}
		}
		objet.parentNode.getElementsByTagName('strong')[0].innerHTML = libelle;
	}
	function provochk(obj)
	{
		if(obj.getElementsByTagName('input')[0].checked)
		{
			obj.getElementsByTagName('input')[0].checked = false;
		}
		else	
		{
			obj.getElementsByTagName('input')[0].checked = true;
		}
		selectchk(obj.getElementsByTagName('input')[0]);
	}
	
	function va_ho_selectTous()
	{
		tb = document.getElementById('cnt_tous').getElementsByTagName('input');
		checkall = true;
		for(i=0 ; i< tb.length; i++)
		{
			if(tb[i].checked == false) { checkall = false ; }
		}
		if(checkall)
		{
			for(j=0 ; j< tb.length; j++) { tb[j].checked = false; }
		}
		else
		{
			for(k=0 ; k< tb.length; k++) { tb[k].checked = true; }
		}
	}
	fS_zindex = 10000;
	formSelect = function() {
		this.label = '';
		this.id = '';
		this.defaut = '';
		this.valeur = '';
		this.libelle = '';
		this.name = '';
		this.tabindex = '';
		this.onclick = '';
		
		this.optGroup = '';
		this.optGroupPosition = 1;

		this.show = function() {
			if(this.name.empty())
				this.name = this.id;
			elt = '';
			
			elt+='	<label class="lig_label"><span>'+ this.label +'</span></label>';
			elt+='	<div class="div_select" style="z-index:' + fS_zindex-- +'">';
			elt+='		<input type="hidden" id="'+this.id+'" name="'+this.name+'" value="'+this.defautval+'" />';
			elt+='		<a tabindex="' + this.tabindex + '" href="#1" class="lig_select" onclick="selectopen(\'sel_'+this.id+'\');"><span><strong>'+this.defaut+'</strong></span></a>';
			elt+='		<dl id="sel_'+this.id+'" class="va_ho_slt_pop" style="display:none;">';
			elt+='			<dd class="va_ho_slt_pop_in">';
			elt+='				<ul class="lim">';
			
			for(i=0 ; i < this.valeur.length ; i++)
			{
				if( (i == this.optGroupPosition) && (this.optGroup != '') ){
					elt+='<li><div>'+this.optGroup.name+'</div></li>';
					for(j=0; j < this.optGroup.items.length; j++){
						(j%2 == 0)? cc='bgj': cc='bgb'; 
						elt+='					<li><a class="'+cc+'_tab" href="#1" onclick="selectli(this,\''+this.optGroup.items[j].value+'\');'+this.onclick+'">'+this.optGroup.items[j].libelle+'</a></li>';
					}
					elt+='<li><div></div></li>';
				}
				
				(i%2 == 0)? cc='bgj': cc='bgb'; 
				elt+='					<li><a class="'+cc+'" href="#1" onclick="selectli(this,\''+this.valeur[i]+'\');'+this.onclick+'">'+this.libelle[i]+'</a></li>';
			}
			elt+='				</ul>';
			elt+='			</dd>';
			elt+='		</dl>';
			elt+='	</div>';
			elt+='	<div class="fin"></div>';
				
			document.write(elt);
		};
		this.check = function() {

			elt = '';
			
			elt+='	<label class="lig_label"><span>'+ this.label +'</span></label>';
			elt+='	<div class="div_select" style="z-index:' + fS_zindex-- +'">';
			elt+='		<a tabindex="' + this.tabindex + '" href="#1" class="lig_select" onclick="selectopen(\'sel_'+this.id+'\')"><span><strong></strong></span></a>';
			elt+='		<dl id="sel_'+this.id+'" class="va_ho_slt_pop" style="display:none;">';
			elt+='			<dd class="va_ho_slt_pop_in">';
			elt+='				<ul class="lim">';
			
			for(i=0 ; i < this.valeur.length ; i++)
			{
				(i%2 == 0)? cc='bgj': cc='bgb'; 
				elt+='					<li><div class="'+cc+'"><input onclick="selectchk(this)" type="checkbox" class="chk" value="'+this.valeur[i]+'" name="'+this.name[i]+'" id="'+this.name[i]+'" /> <span onclick="provochk(this.parentNode)">'+this.libelle[i]+'</span></div></li>';
			}
			
			elt+='				</ul>';
			elt+='			</dd>';
			elt+='		</dl>';
			elt+='	</div>';
			elt+='	<div class="fin"></div>';
		
			document.write(elt);
		};
		this.simple = function() {
			if(this.name.empty())
				this.name = this.id;
			elt = '';
		
			elt+='	<div class="div_select" style="z-index:' + fS_zindex-- +'">';
			elt+='		<input type="hidden" id="'+this.id+'" name="'+this.name+'" value="'+this.defautval+'" />';
			elt+='		<a tabindex="' + this.tabindex + '" href="#1" class="lig_select" onclick="selectopen(\'sel_'+this.id+'\')"><span><strong>'+this.defaut+'</strong></span></a>';
			elt+='		<dl id="sel_'+this.id+'" class="va_ho_slt_pop" style="display:none;">';
			elt+='			<dd class="va_ho_slt_pop_in">';
			elt+='				<ul class="lim">';
			
			for(i=0 ; i < this.valeur.length ; i++)
			{
				(i%2 == 0)? cc='bgj': cc='bgb'; 
				elt+='					<li><a class="'+cc+'" href="#1" onclick="selectli(this,\''+this.valeur[i]+'\')">'+this.libelle[i]+'</a></li>';
			}
			elt+='				</ul>';
			elt+='			</dd>';
			elt+='		</dl>';
			elt+='	</div>';
		
			document.write(elt);
		}
	};


/* FIN Home Vacances 2009 04 */
