var map;
var timesteps = new Array();
var gGeoXmlSources = new Array();
var availableSteps = 0;
var lastStep= -1;
var timeoutKmz;
var timeoutRedraw;	
 
 function animate() {    
      map.setZoom(5); 
	  map.panTo(new GLatLng(53.9, -127.5));
	 
}
function animate1() {    
      map.setZoom(5); 
	  map.panTo(new GLatLng(54.3, -114.7));
	  
}
function animate2() {    
      map.setZoom(5); 
	  map.panTo(new GLatLng(54.3, -106.7));
	 
}
function animate3() {    
    map.setZoom(5); 
	map.panTo(new GLatLng(54.3, -96.7));
	
}
function animate4() {    
      map.setZoom(5); 
	  map.panTo(new GLatLng(48.8, -85.7));
	   
}
function animate5() {    
      map.setZoom(5); 
	  map.panTo(new GLatLng(48.8, -70.7));
	  
} 
	function initialize() {
 
 	  if (GBrowserIsCompatible()) {
      
        map = new GMap2(document.getElementById("map_canvas"));
        //map.setCenter(new GLatLng( 60.496675,-98.65625),3);
        map.setCenter(new GLatLng( 60.9,-98.65625),3);
        map.addMapType(G_PHYSICAL_MAP);
        map.setMapType(G_PHYSICAL_MAP);
       	map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		
		 
		//map.enableScrollWheelZoom();
		//map.enableDoubleClickZoom();
		//map.enableContinuousZoom();
		//new GKeyboardHandler(map);

		//var mt = map.getMapTypes();
		//for (var i=2; i<mt.length; i++) {
		//	mt[i].getMinimumResolution = function() {return 3;}
		//	mt[i].getMaximumResolution = function() {return 15;}
		//}
		//var mycontrol = new GMapTypeControl();
		//map.addControl(mycontrol);
		//map.setMapType(G_HYBRID_MAP);
   		
   		getKMZFiles();
      }
      
    }
 
  	function parseFileList(fileList){
  		
  		
  		var debugConsole = document.getElementById("debugConsole");
		var tmpArray = fileList.split(';')
		var tmpGeoSources = new Array();
		var tmpTimeSteps = new Array();
		var geoXml;      	
		var currStr;
		var count = tmpArray.length;
		var tempStr;
			
		for(var i=0; i<count; i++){
			
			currStr = tmpArray[i];
		
			if(i<(count-1)){
				geoXml = new GGeoXml("http://theweatherman.ca/radar/"+tmpArray[i]);
				geoXml.hide();
				map.addOverlay(geoXml);
				tmpGeoSources.push(geoXml);
				tmpTimeSteps.push(currStr.substr(0,12));
				
			}else if(i==(count-1)){
				//do nothing
			}
		}
		
		timesteps = tmpTimeSteps;
		gGeoXmlSources = tmpGeoSources;
		availableSteps = timesteps.length;
		
		redrawSelectField();
		window.setTimeout("redrawMap()","5000");
  	}
  	
  	
  	
  	function getKMZFiles(){
		
		var xmlHttp = null;
		
		if (typeof XMLHttpRequest != 'undefined') {
		    xmlHttp = new XMLHttpRequest();
		}
		if (!xmlHttp) {
		    try {
		        xmlHttp  = new ActiveXObject("Msxml2.XMLHTTP");
		    } catch(e) {
		        try {
		            xmlHttp  = new ActiveXObject("Microsoft.XMLHTTP");
		        } catch(e) {
		            xmlHttp  = null;
		        }
		    }
		}
		if (xmlHttp) {
		    xmlHttp.open('GET', 'http://theweatherman.ca/cms2/components/com_radarmap/getKmlList.php', true);
		    xmlHttp.onreadystatechange = function () {
		        if (xmlHttp.readyState == 4) {
		            parseFileList(xmlHttp.responseText);
		            
		        }
		    };
		   xmlHttp.send(null);
		}
		if(timeoutKmz){
			window.clearTimeout(timeoutKmz);
		}
		//swindow.setTimeout("getKMZFiles()","600000");
		
  	}
  	
 
 	function redrawSelectField(){
 		var select = document.getElementById("pointsInTime");
 		var tmpOption;
 		var tmpDate;
 		var tmpTransMonth;
 		var minutesAgo;
 		
 		for(var k=0; k<select.options.length; k++){
 			select.remove(k);
 		}
 		
 		for(j=0; j<timesteps.length;j++){
 			
 			//Process date
 			//tmpDate = new Date();
 			//tmpDate.setUTCFullYear(timesteps[j].substring(0,4));
 			//tmpTransMonth = timesteps[j].substring(4,6);
 			//tmpTransMonth--;
 			//tmpDate.setUTCMonth(tmpTransMonth);
 			//tmpDate.setUTCDate(timesteps[j].substring(6,8));
 			//tmpDate.setUTCHours(timesteps[j].substring(8,10));
 			//tmpDate.setUTCMinutes(timesteps[j].substring(10));
 			//tmpDate.setUTCSeconds('00');
 			
 			//tmpDate = tmpDate.toLocaleString();
 			//tmpDate = tmpDate.toUTCString();
 			
 			//Process option
 			tmpOption = document.createElement('option');
 			//tmpOption.text=tmpDate;
 			minutesAgo = (timesteps.length-1-j)*10;
 			tmpOption.text= minutesAgo+" - "+(minutesAgo+10)+" minutes ago";
 			tmpOption.value = j;
 			
 			//Browser swtich
 			var browserName = navigator.appName;
 			if(browserName=='Microsoft Internet Explorer'){
 				select.add(tmpOption);
 			}else {
 				select.add(tmpOption,null);
 			}
 			
 		}
 		select.selectedIndex =timesteps.length-1;
 	}
 	
 	
 	function redrawMap(){
		
 		var select = document.getElementById('pointsInTime');
 		var stepTolLoad = 0;
 		var isAuto = document.forms['showMapControls'].loopControl.checked;
 		
 		stepToLoad = select.value;	
 		
 		if(isAuto){
 			if(lastStep < gGeoXmlSources.length-1){
 				stepToLoad = lastStep +1;
 				lastStep = stepToLoad
 			} else {
 				stepToLoad = 0;
 				lastStep = 0;
 			}
 			select.selectedIndex = stepToLoad;
 		}
 		
 		if(gGeoXmlSources.length>0 
 			&& stepToLoad <gGeoXmlSources.length
 			&& gGeoXmlSources[stepToLoad].hasLoaded()){
			
			for(j=0; j<gGeoXmlSources.length;j++){
				gGeoXmlSources[j].hide();
			}
			gGeoXmlSources[stepToLoad].show();	
		}
 		
 		if(timeoutRedraw){
 			window.clearTimeout(timeoutRedraw);
 		}
		timeoutRedraw = window.setTimeout("redrawMap()","800");
 	}
 	


