
function GetXmlHttpObject( )
{
	var xmlHttp = null;

	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest( );
	}
	catch( e )
	{
		// Internet Explorer
		try
		{
			xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP" );
		}
		catch( e )
		{
			try
			{
				xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
			}
			catch( e )
			{
			}
		}
	}

	return xmlHttp;
}

function trimAll(str) {
    str = str.replace(/\s+/, '');
    for (var i = str.length - 1; i > 0; i--) {
        if (/\S/.test(str.charAt(i))) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    return str;
}

function getLang(){
	
	var index = 0;
	var url = window.location.href;
	
	index = url.indexOf('.com/') + 5;
	return url.substr(index, 2);
		
}

var map;

window.addEvent('load', function(){
	if (GBrowserIsCompatible()) {
		var xmlHttp = GetXmlHttpObject();
		tagMap = $("map");
		map = new GMap2(tagMap);
		map.setUIToDefault();

		xmlHttp.onreadystatechange = function()
		{
			if( xmlHttp.readyState == 4 )
			{
				var rep = xmlHttp.responseText;
				var tab = rep.split("^^^");
				
				var bounds = new GLatLngBounds();
				
				for(var i = 0; i < tab.length; i++){
					
					var tab2 = tab[i].split("^^");
					var p = new GLatLng(tab2[0], tab2[1]);
					bounds.extend(p);
				}
				
				var zoomLevel = map.getBoundsZoomLevel(bounds);
				map.setCenter(bounds.getCenter(), zoomLevel);
				GEvent.addListener(map, "moveend", getStores);
			}
		}
		
		xmlHttp.open( "GET", "/ajax/googleMapAjax.php?action=init&lang=" + getLang(), true );
		xmlHttp.send( null );
		
	}
});

function showOnMap(){
	
	
	var codePostal = document.getElementById('codePostal').value;
	var ville = document.getElementById('ville').value;
	
	var geocoder = new GClientGeocoder();
	
	if(codePostal.length > 0)
		geocoder.getLatLng(codePostal + ", canada", getProximCode);
	else
		geocoder.getLatLng(ville + " city, canada", getProximVille);
	
	return false;
	
}

function stripAccents(mot){
	
	var accent = "ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ";
	var sansAccent = "AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn";
	var motStrip = "";
	for(var i = 0; i < mot.length; i++){
		var lettre = '';
		if(accent.indexOf(mot[i]) > 0){
			
			var index = accent.indexOf(mot[i]);
			lettre = sansAccent.substr(index, 1);
			
		}else{
			
			lettre = mot[i];
			
		}
		motStrip += lettre;
	}
	
	return motStrip;
	
}

function getProximCode(p){ 
	
	if (p != null) {
		var xmlHttp = GetXmlHttpObject();
		xmlHttp.onreadystatechange = function(){
			if (xmlHttp.readyState == 4) {
				var bounds = new GLatLngBounds();
				bounds.extend(p);
				var rep = xmlHttp.responseText;
				
				if (rep.indexOf('^^^') > 0) {
					var tab = rep.split("^^^");
					for (var i = 0; i < tab.length; i++) {
						var tab2 = tab[i].split("^^");
						bounds.extend(new GLatLng(tab2[0], tab2[1]));
					}
				}
				else {
					var tab = rep.split("^^");
					bounds.extend(new GLatLng(tab[0], tab[1]));
				}
				
				
				
				var zoomLevel = map.getBoundsZoomLevel(bounds);
				map.setCenter(p, zoomLevel - 1);
				

			}
		}
		
		
		xmlHttp.open("GET", "/ajax/googleMapAjax.php?action=getByProximCode&x=" + p.lat() + "&y=" + p.lng(), true);
		xmlHttp.send(null);
	}else{
		var codePostal = document.getElementById('codePostal').value;
		if(getLang() == "en"){
			
			alert("The postal code " + codePostal + " could not be found.");
			
		}else{
			
			alert("Le code postal " + codePostal + " n'a pas été trouvé.");
		}
	}
}

function getProximVille(p){
	
	if (p != null) {
		var xmlHttp = GetXmlHttpObject();
		
		xmlHttp.onreadystatechange = function(){
			if (xmlHttp.readyState == 4) {
				var bounds = new GLatLngBounds();
				bounds.extend(p);
				var rep = xmlHttp.responseText;
				
				if (rep.indexOf('^^^') > 0) {
					var tab = rep.split("^^^");
					for (var i = 0; i < tab.length; i++) {
						var tab2 = tab[i].split("^^");
						bounds.extend(new GLatLng(tab2[0], tab2[1]));
					}
				}
				else {
					var tab = rep.split("^^");
					bounds.extend(new GLatLng(tab[0], tab[1]));
				}
				
				
				
				var zoomLevel = map.getBoundsZoomLevel(bounds);
				map.setCenter(p, zoomLevel - 1);
				
			}
		}
		
		
		xmlHttp.open("GET", "/ajax/googleMapAjax.php?action=getByProximVille&x=" + p.lat() + "&y=" + p.lng(), true);
		xmlHttp.send(null);
	}else{
		
		var ville = document.getElementById('ville').value;
		if(getLang() == "en"){
			
			alert("The city name " + ville + " could not be found.");
			
		}else{
			
			alert("La ville " + ville + " n'a pas été trouvé.");
			
		}
		
	}
}

function getStores(){
	
	if(map.getZoom() > 6){
		var xmlHttp = GetXmlHttpObject();
		xmlHttp.onreadystatechange = function()
		{
			if( xmlHttp.readyState == 4 )
			{
				var rep = xmlHttp.responseText;
				var tab = rep.split("^^^");
				
				for(var i = 0; i < tab.length; i++){
						tab2 = tab[i].split("$$");
						
						if(tab2.length > 0){
						
							var p = new GLatLng(tab2[0], tab2[1]);
							
							var icon = new GIcon();
							icon.image = SKIN_URL + "img/pattes.gif";
							icon.iconSize = new GSize(30, 30);
							icon.iconAnchor = new GPoint(15, 15);
							icon.infoWindowAnchor = new GPoint(30, 0);
							
							var marker = new GMarker(p, {icon: icon, clickable: true, title: tab2[2]});
							marker.infosMagasin = tab2;
							
							map.addOverlay(marker);
							
							GEvent.addListener(marker, "click", function() {
								this.openInfoWindowHtml(getInfoWindow(this.infosMagasin), {maxWidth:600});
						    });
							
						}
				}
			}
		}	
		
		var bounds = map.getBounds();
		var sw = bounds.getSouthWest();
		var ne = bounds.getNorthEast();
		
		xmlHttp.open( "GET", "/ajax/googleMapAjax.php?action=getStores&lang=" + getLang() + "&minLong=" + sw.lng() + "&maxLong=" + ne.lng() + "&minLat=" + sw.lat() + "&maxLat=" + ne.lat(), true );
		xmlHttp.send( null );
	}else{
		
		map.clearOverlays();
		
	}
	
}


function getInfoWindow(tab){

	var html = "<div class=\"infosMagasin\">";
	html += "	<p style=\"float:left;\" class=\"adresse\">";
	if(tab.length > 2 && trimAll(tab[2]).length > 0)
		html += "	<span style=\"font-weight:bold;\" class=\"nom\">"+ tab[2] + "</span>";
	if(tab.length > 3 && trimAll(tab[3]).length > 0)
		html += "	<br /><span class=\"adresse\">" + tab[3] + "</span>";
	if(tab.length > 4 && trimAll(tab[4]).length > 0)
		html += "	<br /><span class=\"ville\">"+ tab[4] + "</span>";
	if(tab.length > 5 && trimAll(tab[5]).length > 0)
		html += "	<br /><span class=\"codePostal\">"+ tab[5] + "</span>";
	html +="	</p>";
	html +="	<p style=\"float:left;margin-left:15px;margin-right:15px;\" class=\"infoTel\">";
	if(tab.length > 6 && trimAll(tab[6]).length > 0)
		html += "	<span class=\"telephone\">Tel&nbsp;:" + tab[6] + "</span>";
	if(tab.length > 7 && trimAll(tab[7]).length > 0)
		html += "	<br /><span class=\"fax\">Fax&nbsp;:"+ tab[7] + "</span>";
	if(tab.length > 8 && trimAll(tab[8]).length > 0)
		html += "	<br /><span class=\"email\">"+ (getLang() == 'fr' ? "Courriel&nbsp;: ": "Email: ") + "<a href=\"mailto:" + tab[8] + "\">" + tab[8] + "</a></span>";
	if(tab.length > 9 && trimAll(tab[9]).length > 0)
		html += "	<br /><span class=\"email\">"+ (getLang() == 'fr' ? "Site web&nbsp;: ": "Website: ") + "<a href=\"http://"+ tab[9] + "\">" + tab[9] + "</a></span>";
	html += "	</p>";
	html +="</div>";

	return html;
	
	
}