/*
*	Interactive map functions.  Uses google map to draw and display the map.
*   Uses variables defined in the index page: sNumbers, sLats, sLongs, sNames, sRegions, sStatuss
*/
var map, cluster, icon, eventListeners=[], markersArray=[], markersID=[], markersDisplay=[];

function statusAlert(){
	if (document.getElementById("poStatus").checked == 0){
		alert('In some browsers disabling this option can cause this application to run slowly. \nIf you experience problems you can view this data in an alternative method at \nhttp:\/\/www.meds-sdmm.dfo-mpo.gc.ca\/isdm-gdsi\/twl-mne\/inventory-inventaire\/index-eng.htm.');	
	}
}
//both showList and hideList are used to display or hide the StationList
function showList(){
	document.getElementById("divShowStationList").style.display="none";
	document.getElementById("divStationList").style.display = "";
	document.getElementById("formSList").value = "true";
}
//both showList and hideList are used to display or hide the StationList
function hideList(){
	document.getElementById("divShowStationList").style.display="";
	document.getElementById("divStationList").style.display = "none";
	document.getElementById("formSList").value = "false";
}
//used for updating the table that displays a list of all the stations
function updateStationList(){
	var newHTML = "";
	var counter = 0;
	newHTML = newHTML+'<table cellspacing="0" cellpadding="0" class="stationTable" style="margin: auto;">';
	newHTML = newHTML+'<thead>';
        newHTML = newHTML+'<tr>';
			newHTML = newHTML+'<th>Station</th>';
            newHTML = newHTML+'<th>Nom de station</th>';
            newHTML = newHTML+'<th>LAT</th>';
            newHTML = newHTML+'<th>LONG</th>';
            newHTML = newHTML+'<th>Référence verticale</th>';
            newHTML = newHTML+'<th>Fuseau horaire</th>';
            newHTML = newHTML+'<th>Région</th>';
        newHTML = newHTML+'</tr>';
		newHTML = newHTML+'</thead>';
		newHTML = newHTML+'<tbody>';
	for (var x =0;x<sNumbers.length;x++){
		if(markersDisplay[x]===true) {
			counter = counter +1;
			newHTML = newHTML+'<tr>';
				newHTML = newHTML+'<td><a href=\"\/isdm-gdsi\/twl-mne\/benchmarks-reperes\/station-fra.asp?T1='+sNumbers[x]+'&amp;region='+sRegions[x]+'&amp;ref=maps-cartes\" title=\"Afficher les repères\" \">'+sNumbers[x]+'<\/a><\/td>';
				newHTML = newHTML+'<td>'+sNames[x]+'</td>';
				newHTML = newHTML+'<td>'+sLats[x]+'</td>';
				newHTML = newHTML+'<td>'+sLongs[x]+'</td>';
				newHTML = newHTML+'<td>'+sDatums[x]+'</td>';
				newHTML = newHTML+'<td>'+sTime_zones[x]+'</td>';
				if (sRegions[x] == "ATL") {
				   newHTML = newHTML+'<td>Atlantique</td>';
				}
				else if (sRegions[x] == "LAU") {
					newHTML = newHTML+'<td>Québec</td>';
				}
                else if (sRegions[x] == "PAC") {
					newHTML = newHTML+'<td>Pacifique</td>';
				}
                else if (sRegions[x] == "CA"){
					newHTML = newHTML+'<td>Centrale &amp; Arctique</td>';
				}
			newHTML = newHTML+'</tr>';
		}
	}
	newHTML = newHTML+"</tbody>";
	newHTML = newHTML + "</table>";
	document.getElementById("tableStationList").innerHTML = newHTML;
	//alert("Added "+counter+" markers to the map.");
}

function newMarker(markerLocation, title, markerIcon, info) {
	var marker=new GMarker(markerLocation, {title:title, icon:markerIcon});
	eventListeners.push(GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(info);
	}));
	return marker;
}
//clears and then updates all the markers on the map
function addMarkers(status, rid){
	markersArray=[];
	for(i=eventListeners.length-1; i>=0; i--){
		GEvent.removeListener(eventListeners[i]);
	}
	eventListeners=[];
	
	for (var i=0; i<sLats.length; i++) { //sLats.length
		markersDisplay[i] = false;
		if ((status == "on" && sStatuss[i]=="PO") || (status == "off")){
			if ((rid == "ALL") || (rid == sRegions[i])){
				var id = sNumbers[i];			
				var name = "#"+sNumbers[i] + ": " +sNames[i];
				var info = "<div style=\"width:300px;\">"
					info = info + "<strong>Station :<\/strong> "+sNumbers[i]+"<br \/>";
					info = info + "<strong>Nom de station :<\/strong> "+sNames[i]+"<br \/>";
					info = info + "<strong>Emplacement :<\/strong> "+sLats[i]+","+sLongs[i]+"<br \/>";
			
					if	(sRegions[i] == "ATL")		{ info = info + "<strong>Région :<\/strong> Atlantique<br \/>";}
					else if (sRegions[i] == "LAU")	{ info = info + "<strong>Région :<\/strong> Québec<br \/>";}
					else if (sRegions[i] == "CA")	{ info = info + "<strong>Région :<\/strong> Centrale &amp; Arctique<br \/>";}
					else if (sRegions[i] == "PAC")	{ info =info + "<strong>Région :<\/strong> Pacifique<br \/>";}
					
					info = info + "<strong>Référence verticale :<\/strong> "+sDatums[i]+"<br \/>";
					info = info + "<strong>Fuseau horaire :<\/strong> "+sTime_zones[i]+"<br \/>";
					info = info + '<br \/><div style="text-align:right;"><a href="\/isdm-gdsi\/twl-mne\/benchmarks-reperes\/station-fra.asp?T1='+sNumbers[i]+'&amp;region='+sRegions[i]+'&amp;ref=maps-cartes" title="Afficher les repères"><strong>Afficher les repères<\/strong><\/a><\/div><br \/>';

					info = info + "<\/div>"

				marker=newMarker(new GLatLng(sLats[i],sLongs[i]), name, icon, info);
				markersArray.push(marker);
				markersID.push(id);
				markersDisplay[i]=true;
			}
		}
	}
	cluster.removeMarkers();
	cluster.addMarkers(markersArray);
	cluster.fitMapToMarkers();
	map.savePosition();
}

function resetMarkers(){
	document.getElementById("poStatus").checked = 1;
	document.getElementById("region").selectedIndex = 4;
	map.setCenter(new GLatLng(70, -99.94), 2);
	addMarkers("on","ALL");
	hideList();
}
//called from the update button in the form.  redraws all the markers 
//and redos the station list.
function updateMarkers(){
	var status = "off"
	if (document.getElementById("poStatus").checked == 1){
		status = "on";
	}
	var newRid = document.getElementById("region").selectedIndex;
	var rid = "";
	if (newRid === 0)	  {rid = "ATL"; map.setCenter(new GLatLng(53,-70), 3);}
	else if (newRid == 1) {rid = "LAU"; map.setCenter(new GLatLng(50.03,-66.79), 4); }
	else if (newRid == 2) {rid = "CA"; map.setCenter(new GLatLng(70,-99.94), 2);}
	else if (newRid == 3) {rid = "PAC"; map.setCenter(new GLatLng(52.15,-128.09), 4);}
	else 				  {rid = "ALL"; map.setCenter(new GLatLng(70, -99.94), 2);}
	
	if (document.getElementById("divStationList").style === ""){
		showList();	
	}
	
	addMarkers(status,rid);
	updateStationList();
}

function initialize() {
	if (GBrowserIsCompatible()) {
		map=new GMap2(document.getElementById('divGoogleMaps'));
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl(true));
		map.addControl(new GScaleControl());;
		
		map.disableDoubleClickZoom();
		map.enableScrollWheelZoom();
		map.enableContinuousZoom();
		
		GEvent.addListener(map, 'zoomend', function() { map.closeInfoWindow(); });
		
		map.setCenter(new GLatLng(70, -99.94), 1);
		
		function myClusterClick(args) {
			cluster.defaultClickAction=function(){
				map.setCenter(args.clusterMarker.getLatLng(), map.getBoundsZoomLevel(args.clusterMarker.clusterGroupBounds))
				delete cluster.defaultClickAction;
			}
			var html='<div style="height:8em; overflow:auto; width:24em"><div style="font-size:1.5em;"><strong>'+args.clusteredMarkers.length+' emplacements :<\/strong><\/div><br \/>';
			for (i=0; i<args.clusteredMarkers.length; i++) {
				html+='<a href="javascript:cluster.triggerClick('+args.clusteredMarkers[i].index+')">'+args.clusteredMarkers[i].getTitle()+'<\/a><br \/>';
			}
			html+='<br \/><a href="javascript:void(0)" onclick="cluster.defaultClickAction()">Agrandir pour afficher ces emplacements<\/a><\/div>';
			//	args.clusterMarker.openInfoWindowHtml(html);
			map.openInfoWindowHtml(args.clusterMarker.getLatLng(), html);
		}
		//	create a ClusterMarker
		cluster=new ClusterMarker(map, {clusterMarkerTitle:'Cliquer pour afficher des renseignements sur les %count emplacements.' , clusterMarkerClick:myClusterClick });
		
		
		icon = new GIcon();
		icon.image = "../images/m-2.png";
		icon.iconSize = new GSize(12, 20);
		icon.iconAnchor = new GPoint(4, 4);
		icon.infoWindowAnchor = new GPoint(4, 4);      
		icon.shadow = ""; icon.shadowSize = new GSize(0, 0);

		updateMarkers();
	}
	else
	{
		alert("Désolé, la carte interactive n’est pas compatible avec votre fureteur.");
	}
}
