// JavaScript Document ----------
// Patrick Potvin ---------------
// www.DynamiqueGestion.com -----
// ------------------------------

var coordmap;
var coordDepart;
var google_map;
var mapAdresse;
var titreLocation = '';
var multiple = '';
var mapPrecedente = 0;

function AfficherGoogleMap(id, google_map, mapAdresse){
	
	if(google_map!=''){
		coordmap = google_map.replace(/\)/, '');
		coordmap = coordmap.replace(/\(/, '');
	}else{
		coordmap = '48.43, -71.08';
	}
	
	if(coordmap.indexOf(';')!=-1){
		coordmap = coordmap.split(';');
		coordDepart = coordmap[0].split(',');
		multiple = 1;
	}else{
		coordDepart = coordmap.split(',');
		coordmap = coordmap.split(',');
		multiple = 0;
	}	
	
	
	if(document.getElementById('p'+id)){
		document.getElementById('p'+id).innerHTML = '';
	}
	
	if(mapPrecedente > 0){
		document.getElementById('map'+mapPrecedente).style.display = 'none';
	}
	
	if(mapPrecedente == id){
		document.getElementById('map'+mapPrecedente).innerHTML = '';
		document.getElementById('map'+mapPrecedente).style.display = 'none';
		mapPrecedente = 0;
	}else{
		document.getElementById('map'+id).style.display = 'block';
		mapPrecedente = id;
		initialize(id, mapAdresse);
	}
	
}




var marker;
var map;
var geocoder;
function initialize(id, mapAdresse) {
  
 if (GBrowserIsCompatible()) { 

 var mapOptions = {    
 	googleBarOptions : {    
		showOnLoad : true,
  		//style : "new", 
  		adsOptions: {   
  			client: "partner-pub-4920083328295561",        
			channel: "5284829179",        
			adsafe: "high",        
			language: "fr"
		}    
	 }  
  }


  map = new GMap2(document.getElementById("map"+id));
  map.setCenter(new GLatLng(coordDepart[0], coordDepart[1]), 12);
  map.setUIToDefault();
  geocoder = new GClientGeocoder();
  var customUI = map.getDefaultUI();
  // Remove MapType.G_HYBRID_MAP
  customUI.maptypes.hybrid = false;
  map.setUI(customUI);
  map.enableGoogleBar();
  
  
  
/*  
  	var adPos = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(0, 0)); // Set the anchor position and GSize offset to your desired values.
	var publisher_id = 'ca-pub-4920083328295561';
	var adsManagerOptions = {  
		maxAdsOnMap : 4,  
		style: 'adunit',  
		// The channel field is optional - replace this field with a channel number   
		// for Google AdSense tracking  
		channel: '5284829179',  
		position: adPos
	};
	adsManager = new GAdsManager(map, publisher_id, adsManagerOptions);
	adsManager.enable();
*/

 }
 	if(multiple==0){
		showLocation(mapAdresse);
	}else{
		showMultipleLocation();
	}
	
}

function createMarker(point, number) {  
	var marker = new GMarker(point);  
	marker.value = number;  
	return marker;
}

function showMultipleLocation(){

	for (var i=0; i<coordmap.length; i++) { 
		crd = coordmap[i].split(',');
		//alert(crd[0]+','+crd[1]);
		var point = new GLatLng(crd[0], crd[1]);  
		map.addOverlay(createMarker(point, i+1));
	}
	map.setCenter(point, 12);
}

function addAddressToMap(response) {
  map.clearOverlays();
  if (!response || response.Status.code != 200) {
  
  	if(google_map != ""){
		point = new GLatLng(coordDepart[0],coordDepart[1]);
		marker = new GMarker(point);
		map.addOverlay(marker);
		//GEvent.addListener(marker, "click", function() { 
		//	marker.openInfoWindowHtml(titreLocation, mapAdresse); 
		//} );
		map.setCenter(point, 11);
	}else{
    	alert("Désolé, ce lieu n'a pas été trouvé sur la carte. Pour trouver ce lieu déplacez vous sur la carte!");
	}
	
  } else {
    place = response.Placemark[0];
    point = new GLatLng(place.Point.coordinates[1],
                        place.Point.coordinates[0]);
		
    marker = new GMarker(point);
    map.addOverlay(marker);//place.address
    //marker.openInfoWindowHtml("<%=Replace(titre, chr(34), "&quot;")%>", "<%=mapAdresse%>");
	
	//GEvent.addListener(marker, "click", function() { 
    //	marker.openInfoWindowHtml(titreLocation, mapAdresse); 
  	//} );
	map.setCenter(point, 11);
	
  }
}

function showLocation(address) {
  //var address = mapAdresse;
  geocoder.getLocations(address, addAddressToMap);
}

// findLocation() is used to enter the sample addresses into the form.
function findLocation(address) {
  address = address;
  showLocation();
}

if(window.addEventListener){
  window.addEventListener("unload", GUnload, false);
}else if(window.attachEvent){
  window.attachEvent("onunload", GUnload);
}else{
  window.onunload = GUnload;
}
