/*
 * Author: Charles Lauzon
 * Name:   Functions.js
 * Date:   19 Octobre, 2009
 */
 
/********************************************************************
 * 																	*
 *							 GOOGLE MAP                             *
 *																	*
 ********************************************************************/
//Variable Declaration
var map = null;
var geocoder = null;
var address;
var myBulle = new Array();
var arrGoogleMap = new Array();
var i = 0;
var d = new Date();

var iconAppart = new GIcon();  
iconAppart.image = 'http://www.casavisite.com/images/icon_maison/icon_appart.gif'; 
iconAppart.iconSize = new GSize(30, 49); 
iconAppart.iconAnchor = new GPoint(15, 24); 
iconAppart.infoWindowAnchor = new GPoint(15, 24); 

var iconBungalow = new GIcon();  
iconBungalow.image = 'http://www.casavisite.com/images/icon_maison/icon_bungalow.gif'; 
iconBungalow.iconSize = new GSize(32, 24); 
iconBungalow.iconAnchor = new GPoint(16, 12); 
iconBungalow.infoWindowAnchor = new GPoint(16, 12); 

var iconChalet = new GIcon();  
iconChalet.image = 'http://www.casavisite.com/images/icon_maison/icon_chalet.gif'; 
iconChalet.iconSize = new GSize(40, 42); 
iconChalet.iconAnchor = new GPoint(20, 21); 
iconChalet.infoWindowAnchor = new GPoint(20, 21); 

var iconCottage = new GIcon();  
iconCottage.image = 'http://www.casavisite.com/images/icon_maison/icon_cottage.gif'; 
iconCottage.iconSize = new GSize(32, 33); 
iconCottage.iconAnchor = new GPoint(16, 16); 
iconCottage.infoWindowAnchor = new GPoint(16, 16); 

var iconMaisonMobile = new GIcon();  
iconMaisonMobile.image = 'http://www.casavisite.com/images/icon_maison/icon_maison-mobile.gif'; 
iconMaisonMobile.iconSize = new GSize(31, 25); 
iconMaisonMobile.iconAnchor = new GPoint(15, 12); 
iconMaisonMobile.infoWindowAnchor = new GPoint(15, 12); 

var iconPenthouse = new GIcon();  
iconPenthouse.image = 'http://www.casavisite.com/images/icon_maison/icon_loft.gif'; 
iconPenthouse.iconSize = new GSize(31, 54); 
iconPenthouse.iconAnchor = new GPoint(15, 26); 
iconPenthouse.infoWindowAnchor = new GPoint(15, 26); 

var iconPalierMultiple = new GIcon();  
iconPalierMultiple.image = 'http://www.casavisite.com/images/icon_maison/icon_splitlevel.gif'; 
iconPalierMultiple.iconSize = new GSize(40, 38); 
iconPalierMultiple.iconAnchor = new GPoint(20, 19); 
iconPalierMultiple.infoWindowAnchor = new GPoint(20, 19); 

var iconTerrain = new GIcon();  
iconTerrain.image = 'http://www.casavisite.com/images/icon_maison/icon_terrain.gif'; 
iconTerrain.iconSize = new GSize(40, 29); 
iconTerrain.iconAnchor = new GPoint(20, 19); 
iconTerrain.infoWindowAnchor = new GPoint(20, 19); 

var iconFermette = new GIcon();  
iconFermette.image = 'http://www.casavisite.com/images/icon_maison/icon_ferme.gif'; 
iconFermette.iconSize = new GSize(40, 38); 
iconFermette.iconAnchor = new GPoint(20, 19); 
iconFermette.infoWindowAnchor = new GPoint(20, 19); 

var customIcons = []; 
customIcons["AP"] = iconAppart; 
customIcons["B"] = iconBungalow; 
customIcons["CH"] = iconChalet; 
customIcons["CO"] = iconCottage; 
customIcons["CON"] = iconAppart; 
customIcons["L"] = iconAppart; 
customIcons["MM"] = iconMaisonMobile; 
customIcons["P"] = iconPenthouse; 
customIcons["PM"] = iconPalierMultiple; 
customIcons["2X"] = iconAppart; 
customIcons["3X"] = iconAppart; 
customIcons["MX"] = iconAppart; 
customIcons["T"] = iconTerrain; 
customIcons["F"] = iconFermette; 

var arrMarkers = [];
var myBulle = [];
var myList = [];
 
function btnGoogleMap_onClick(VariableCoder){
	document.getElementById("cboOrderBy").style.visibility = "hidden";
	
	//Show Picture Viewer
	document.getElementById("googlemap_shadow").style.visibility = "visible"; //Shadow
	document.getElementById("googlemap").style.visibility = "visible"; //Box
	document.getElementById("googlemap_loading").style.visibility = "visible"; //Loading
	
	if(GBrowserIsCompatible() && arrMarkers.length == 0){ 
		map = new GMap2(document.getElementById("map")); 
		map.setCenter(new GLatLng(45.4957, -73.6317), 9);
		map.setUIToDefault();
	
		//http://www.residencetemporaire.com/new/phpsqlajax_genxml2.php?arrGoogleMaps=a%3A13%3A%7Bi%3A0%3Bs%3A1%3A%221%22%3Bi%3A1%3Bs%3A1%3A%222%22%3Bi%3A2%3Bs%3A1%3A%223%22%3Bi%3A3%3Bs%3A1%3A%224%22%3Bi%3A4%3Bs%3A1%3A%225%22%3Bi%3A5%3Bs%3A1%3A%227%22%3Bi%3A6%3Bs%3A1%3A%228%22%3Bi%3A7%3Bs%3A1%3A%229%22%3Bi%3A8%3Bs%3A2%3A%2210%22%3Bi%3A9%3Bs%3A2%3A%2211%22%3Bi%3A10%3Bs%3A2%3A%2212%22%3Bi%3A11%3Bs%3A2%3A%2213%22%3Bi%3A12%3Bs%3A2%3A%2214%22%3B%7D&v=12&s=0&o=DISTANCE%20ASC
		var pageURL = "systems/phpsqlajax_genxml.php?hStart="+document.fResultat.hStart.value;
		pageURL += "&rdtView="+document.fResultat.rdtView.value;
		pageURL += "&hOrderBy="+document.fResultat.hOrderBy.value;
		pageURL += "&cboPays="+document.fResultat.cboPays.value;
		if(document.fResultat.cboPays.value == "CA"){
			pageURL += "&cboProvince="+document.fResultat.cboProvince.value;
		}else if(document.fResultat.cboPays.value == "US"){
			pageURL += "&cboEtat="+document.fResultat.cboEtat.value;
		}
		pageURL += "&txtVille="+document.fResultat.txtVille.value;
		pageURL += "&rdtDistance="+document.fResultat.rdtDistance.value;
		if(document.fResultat.txtAnneeDe.value != ""){ pageURL += "&txtAnneeDe="+document.fResultat.txtAnneeDe.value; }
		if(document.fResultat.txtAnneeA.value != ""){ pageURL += "&txtAnneeA="+document.fResultat.txtAnneeA.value; }
		if(document.fResultat.txtPrixDe.value != ""){ pageURL += "&txtPrixDe="+document.fResultat.txtPrixDe.value; }
		if(document.fResultat.txtPrixA.value != ""){ pageURL += "&txtPrixA="+document.fResultat.txtPrixA.value; }
		if(document.fResultat.cboChambre.value != ""){ pageURL += "&cboChambre="+document.fResultat.cboChambre.value; }
		if(document.fResultat.cboSalleBain.value != ""){ pageURL += "&cboSalleBain="+document.fResultat.cboSalleBain.value; }
		if(document.fResultat.cboSalleDeau.value != ""){ pageURL += "&cboSalleDeau="+document.fResultat.cboSalleDeau.value; }
		if(document.fResultat.cboFoyer.value != ""){ pageURL += "&cboFoyer="+document.fResultat.cboFoyer.value; }
		if(document.fResultat.chkStyle1Maison != null || document.fResultat.chkStyle1Maison != undefined && document.fResultat.chkStyle1Maison.value == "B OR CO OR PM"){ pageURL += "&chkStyle1Maison="+document.fResultat.chkStyle1Maison.value; }
		if(document.fResultat.chkStyle1Condo != null || document.fResultat.chkStyle1Condo != undefined && document.fResultat.chkStyle1Condo.value == "CON OR L OR P"){ pageURL += "&chkStyle1Condo="+document.fResultat.chkStyle1Condo.value; }
		if(document.fResultat.chkStyle1Multiplex != null || document.fResultat.chkStyle1Multiplex != undefined && document.fResultat.chkStyle1Multiplex.value == "2X OR 3X OR MX"){ pageURL += "&chkStyle1Multiplex="+document.fResultat.chkStyle1Multiplex.value; }
		if(document.fResultat.chkStyle1Terrain != null || document.fResultat.chkStyle1Terrain != undefined && document.fResultat.chkStyle1Terrain.value == "T"){ pageURL += "&chkStyle1Terrain="+document.fResultat.chkStyle1Terrain.value; }
		if(document.fResultat.chkStyle2Appartement != null || document.fResultat.chkStyle2Appartement != undefined && document.fResultat.chkStyle2Appartement.value == "AP"){ pageURL += "&chkStyle2Appartement="+document.fResultat.chkStyle2Appartement.value; }
		if(document.fResultat.chkStyle2Bungalow != null || document.fResultat.chkStyle2Bungalow != undefined && document.fResultat.chkStyle2Bungalow.value == "B"){ pageURL += "&chkStyle2Bungalow="+document.fResultat.chkStyle2Bungalow.value; }
		if(document.fResultat.chkStyle2Chalet != null || document.fResultat.chkStyle2Chalet != undefined && document.fResultat.chkStyle2Chalet.value == "CH"){ pageURL += "&chkStyle2Chalet="+document.fResultat.chkStyle2Chalet.value; }
		if(document.fResultat.chkStyle2Cottage != null || document.fResultat.chkStyle2Cottage != undefined && document.fResultat.chkStyle2Cottage.value == "CO"){ pageURL += "&chkStyle2Cottage="+document.fResultat.chkStyle2Cottage.value; }
		if(document.fResultat.chkStyle2Condo != null || document.fResultat.chkStyle2Condo != undefined && document.fResultat.chkStyle2Condo.value == "CON OR L OR P"){ pageURL += "&chkStyle2Condo="+document.fResultat.chkStyle2Condo.value; }
		if(document.fResultat.chkStyle2MaisonMobile != null || document.fResultat.chkStyle2MaisonMobile != undefined && document.fResultat.chkStyle2MaisonMobile.value == "MM"){ pageURL += "&chkStyle2MaisonMobile="+document.fResultat.chkStyle2MaisonMobile.value; }
		if(document.fResultat.chkStyle2PalierMultiple != null || document.fResultat.chkStyle2PalierMultiple != undefined && document.fResultat.chkStyle2PalierMultiple.value == "PM"){ pageURL += "&chkStyle2PalierMultiple="+document.fResultat.chkStyle2PalierMultiple.value; }
		if(document.fResultat.chkStyle2Duplex != null || document.fResultat.chkStyle2Duplex != undefined && document.fResultat.chkStyle2Duplex.value == "2X"){ pageURL += "&chkStyle2Duplex="+document.fResultat.chkStyle2Duplex.value; }
		if(document.fResultat.chkStyle2Triplex != null || document.fResultat.chkStyle2Triplex != undefined && document.fResultat.chkStyle2Triplex.value == "3X"){ pageURL += "&chkStyle2Triplex="+document.fResultat.chkStyle2Triplex.value; }
		if(document.fResultat.chkStyle2Multiplex != null || document.fResultat.chkStyle2Multiplex != undefined && document.fResultat.chkStyle2Multiplex.value == "MX"){ pageURL += "&chkStyle2Multiplex="+document.fResultat.chkStyle2Multiplex.value; }
		if(document.fResultat.chkStyle2Terrain != null || document.fResultat.chkStyle2Terrain != undefined && document.fResultat.chkStyle2Terrain.value == "T"){ pageURL += "&chkStyle2Terrain="+document.fResultat.chkStyle2Terrain.value; }
		if(document.fResultat.chkStyle2Fermette != null || document.fResultat.chkStyle2Fermette != undefined && document.fResultat.chkStyle2Fermette.value == "F"){ pageURL += "&chkStyle2Fermette="+document.fResultat.chkStyle2Fermette.value; }
		if(document.fResultat.chkTypeIsole != null || document.fResultat.chkTypeIsole != undefined && document.fResultat.chkTypeIsole.value == "I"){ pageURL += "&chkTypeIsole="+document.fResultat.chkTypeIsole.value; }
		if(document.fResultat.chkTypeJumele != null || document.fResultat.chkTypeJumele != undefined && document.fResultat.chkTypeJumele.value == "J"){ pageURL += "&chkTypeJumele="+document.fResultat.chkTypeJumele.value; }
		if(document.fResultat.chkTypeEnRange != null || document.fResultat.chkTypeEnRange != undefined && document.fResultat.chkTypeEnRange.value == "R"){ pageURL += "&chkTypeEnRange="+document.fResultat.chkTypeEnRange.value; }
		if(document.fResultat.chkTypeNA != null || document.fResultat.chkTypeNA != undefined && document.fResultat.chkTypeNA.value == "N"){ pageURL += "&chkTypeNA="+document.fResultat.chkTypeNA.value; }
		if(document.fResultat.chkGarageAucun != null || document.fResultat.chkGarageAucun != undefined && document.fResultat.chkGarageAucun.value == "0"){ pageURL += "&chkGarageAucun="+document.fResultat.chkGarageAucun.value; }
		if(document.fResultat.chkGarageSimple != null || document.fResultat.chkGarageSimple != undefined && document.fResultat.chkGarageSimple.value == "1"){ pageURL += "&chkGarageSimple="+document.fResultat.chkGarageSimple.value; }
		if(document.fResultat.chkGarageDouble != null || document.fResultat.chkGarageDouble != undefined && document.fResultat.chkGarageDouble.value == "2"){ pageURL += "&chkGarageDouble="+document.fResultat.chkGarageDouble.value; }
		if(document.fResultat.chkGarageTriple != null || document.fResultat.chkGarageTriple != undefined && document.fResultat.chkGarageTriple.value == "3"){ pageURL += "&chkGarageTriple="+document.fResultat.chkGarageTriple.value; }
		if(document.fResultat.chkPiscineAucune != null || document.fResultat.chkPiscineAucune != undefined && document.fResultat.chkPiscineAucune.value == "N"){ pageURL += "&chkPiscineAucune="+document.fResultat.chkPiscineAucune.value; }
		if(document.fResultat.chkPiscineHorsTerre != null || document.fResultat.chkPiscineHorsTerre != undefined && document.fResultat.chkPiscineHorsTerre.value == "H"){ pageURL += "&chkPiscineHorsTerre="+document.fResultat.chkPiscineHorsTerre.value; }
		if(document.fResultat.chkPiscineCreusee != null || document.fResultat.chkPiscineCreusee != undefined && document.fResultat.chkPiscineCreusee.value == "C"){ pageURL += "&chkPiscineCreusee="+document.fResultat.chkPiscineCreusee.value; }
		if(document.fResultat.chkVuePitoresque != null || document.fResultat.chkVuePitoresque != undefined){ pageURL += "&chkVuePitoresque="+document.fResultat.chkVuePitoresque.value; }
		if(document.fResultat.chkBergeRiveLac != null || document.fResultat.chkBergeRiveLac != undefined){ pageURL += "&chkBergeRiveLac="+document.fResultat.chkBergeRiveLac.value; }
		if(document.fResultat.chkMezanine != null || document.fResultat.chkMezanine != undefined){ pageURL += "&chkMezanine="+document.fResultat.chkMezanine.value; }
		if(document.fResultat.chkClimtisation != null || document.fResultat.chkClimtisation != undefined){ pageURL += "&chkClimtisation="+document.fResultat.chkClimtisation.value; }
		if(document.fResultat.chkAscenseur != null || document.fResultat.chkAscenseur != undefined){ pageURL += "&chkAscenseur="+document.fResultat.chkAscenseur.value; }
		if(document.fResultat.chkPiscineCreusee != null || document.fResultat.chkPiscineCreusee != undefined){ pageURL += "&chkPiscineCreusee="+document.fResultat.chkPiscineCreusee.value; }
		if(document.fResultat.chkRevenu != null || document.fResultat.chkRevenu != undefined){ pageURL += "&chkRevenu="+document.fResultat.chkRevenu.value; }
		if(document.fResultat.cboDelaiDoccupation.value != ""){ pageURL += "&cboDelaiDoccupation="+document.fResultat.cboDelaiDoccupation.value; }
		if(document.fResultat.rbtAireHabitable != null || document.fResultat.rbtAireHabitable != undefined){ pageURL += "&rbtAireHabitable="+document.fResultat.rbtAireHabitable.value; }
		if(document.fResultat.txtAireHabitableMin.value != ""){ pageURL += "&txtAireHabitableMin="+document.fResultat.txtAireHabitableMin.value; }
		if(document.fResultat.txtAireHabitableMax.value != ""){ pageURL += "&txtAireHabitableMax="+document.fResultat.txtAireHabitableMax.value; }
		if(document.fResultat.rbtSuperficieTerrain != null || document.fResultat.rbtSuperficieTerrain != undefined){ pageURL += "&rbtSuperficieTerrain="+document.fResultat.rbtSuperficieTerrain.value; }
		if(document.fResultat.txtSuperficieTerrainMin.value != ""){ pageURL += "&txtSuperficieTerrainMin="+document.fResultat.txtSuperficieTerrainMin.value; }
		if(document.fResultat.txtSuperficieTerrainMax.value != ""){ pageURL += "&txtSuperficieTerrainMax="+document.fResultat.txtSuperficieTerrainMax.value; }
		//GDownloadUrl("systems/phpsqlajax_genxml.php?arrGoogleMaps="+document.getElementById("hGoogleMaps").value+"&v="+document.getElementById("rdtView").value+"&s="+document.getElementById("hStart").value+"&o="+document.getElementById("hOrderBy").value+"&d="+d, function(data){ 
		GDownloadUrl(pageURL+"&d="+d, function(data){ 
			var xml = GXml.parse(data); 
			var markers = xml.documentElement.getElementsByTagName("marker");
			for(var i = 0; i < markers.length; i++){ 
				var name = markers[i].getAttribute("NoMaison"); 
				var address = markers[i].getAttribute("Address"); 
				var type = markers[i].getAttribute("Style"); 
				var point = new GLatLng(parseFloat(markers[i].getAttribute("Lat")), parseFloat(markers[i].getAttribute("Long"))); 
				var marker = new GMarker(point, customIcons[type]); 
				map.addOverlay(marker);
				arrMarkers[i] = marker;
				
				myBulle[i] = "<table class='myBulle' width='250' height='255' cellpadding='0' cellspacing='0' border='0'><tr height='55' valign='top'><td width='230'>#"+markers[i].getAttribute("NoMaison")+" - "+markers[i].getAttribute("Contact")+"<br>"+markers[i].getAttribute("Address")+"<br>"+markers[i].getAttribute("Ville")+", "+markers[i].getAttribute("Province")+", "+markers[i].getAttribute("CodePostal")+"</td><td align='right'><span id='myBulle_close' onClick='document.getElementById(\"googlemap_bulle\").style.display=\"none\";' onMouseOver='this.style.color=\"#FF0000\"' onMouseOut='this.style.color=\"#FFFFFF\"' style='cursor:pointer;'>X</span></td></tr><tr><td colspan='2'><img src='http://www.casavisit.com/photos/"+markers[i].getAttribute("MaisonId")+"/"+markers[i].getAttribute("Photo")+"' width='250' height='200' onClick='btnMaison_onClick(\""+markers[i].getAttribute("Encrypt_MaisonId")+"\", "+markers[i].getAttribute("Fiche")+", "+markers[i].getAttribute("Distance")+", "+markers[i].getAttribute("Start")+");' /></td></tr></table>";
				
				var myWrap = document.getElementById("wrap");
				if(document.getElementById("rdtView").value == 12){
					myList[i] = "<table class='myList' width='219' height='51' cellpadding='0' cellspacing='0' border='0'><tr height='51'><td width='65'><img src='http://www.casavisit.com/photos/"+markers[i].getAttribute("MaisonId")+"/"+markers[i].getAttribute("Photo")+"' width='60' height='51' /></td><td>#"+markers[i].getAttribute("NoMaison")+" - "+markers[i].getAttribute("Contact")+"<br>"+markers[i].getAttribute("Address")+"<br>"+markers[i].getAttribute("Ville")+", "+markers[i].getAttribute("Province")+"</td></tr></table>";				
					myWrap.className = "wrap12";
				}else{
					myList[i] = "<table class='myList' width='219' height='25' cellpadding='0' cellspacing='0' border='0'><tr height='25'><td width='219'>#"+markers[i].getAttribute("NoMaison")+" - "+markers[i].getAttribute("Ville")+", "+markers[i].getAttribute("Province")+"</td></tr></table>";
					myWrap.className = "wrap24";
				}
			} 
		}); 
		//Add DoubleClick on Map Event
		//Hidden message
		GEvent.addListener(map, "dblclick", function(){
			document.getElementById("googlemap_bulle").style.display = "none";
		});
		
		GEvent.addListener(map, "zoomend", function(){
			document.getElementById("googlemap_bulle").style.display = "none";
		});		
		
		setTimeout("ini()", 2000);

	}else{
		//Hide Loading
		document.getElementById("googlemap_loading").style.visibility = "hidden"; //Loading
	}
}

function ini(){
	$(arrMarkers).each(function(i,marker){
		$("<li />")
			.html(myList[i])
			.click(function(){
				displayPoint(marker, i);
			})
			.appendTo("#list");

		GEvent.addListener(marker, "click", function(){
			displayPoint(marker, i);
		});
	});
	
	$("#googlemap_bulle").appendTo(map.getPane(G_MAP_FLOAT_SHADOW_PANE));

	//Hide Loading
	document.getElementById("googlemap_loading").style.visibility = "hidden"; //Loading
}

function displayPoint(marker, index){
	//Show Loading
	document.getElementById("googlemap_loading").style.visibility = "visible"; //Loading
	$("#googlemap_bulle").hide();
	$("#googlemap_bulle").html(myBulle[index]);
	var moveEnd = GEvent.addListener(map, "moveend", function(){
		var markerOffset = map.fromLatLngToDivPixel(marker.getLatLng());
		$("#googlemap_bulle")
			.fadeIn()
			.css({ top:markerOffset.y, left:markerOffset.x });
	
		GEvent.removeListener(moveEnd);
	});
	map.panTo(marker.getLatLng());
	$("#googlemap_bulle").show();
	
	//Hide GoogleMaps Loading
	setTimeout("document.getElementById('googlemap_loading').style.visibility = 'hidden'", 1000);
}

function btnCloseGoogleMap_onClick(){
	//Show cboOrderBy
	document.getElementById("cboOrderBy").style.visibility = "visible";
	
	//Hide GoogleMaps
	document.getElementById("googlemap_shadow").style.visibility = "hidden"; //Shadow
	document.getElementById("googlemap").style.visibility = "hidden"; //Box
}
