// JavaScript Document

    if (GBrowserIsCompatible()) {
      // this variable will collect the html which will eventually be placed in the side_bar
      
	  var side_bar_html = "";
    
      // arrays to hold copies of the markers and html used by the side_bar
      var gmarkers = [];
      var htmls = [];
      var i = 0;
 		
	  var to_htmls = [];   //array to hold direction 
      var from_htmls = [];   //array to hold from direction
	  
	  var map;
	  
	  var baseIcon = new GIcon();
          baseIcon.iconSize=new GSize(20,20);
          baseIcon.shadowSize=new GSize(28,20);
          baseIcon.iconAnchor=new GPoint(16,32);
          baseIcon.infoWindowAnchor=new GPoint(16,0);
	
	  var farmicon = new GIcon(baseIcon, "images/icon56.png", null, "images/icon56s.png");
	  

		/*-------------------------------function that include driving diretion */
		// A function to create the marker and set up the event window
      


      // This function picks up the click and opens the corresponding info window
      function myclick(i) {
        					gmarkers[i].openInfoWindowHtml(htmls[i]);
      					  }
	  

 	 // functions that open the directions forms
      function tohere(i) {
        					gmarkers[i].openInfoWindowHtml(to_htmls[i]); //when click info from the data file is store here
      					  }
      function fromhere(i) {
       						 gmarkers[i].openInfoWindowHtml(from_htmls[i]);
      					   }
	  
	 
	 
	 
	 
	/*-------------------------------------------------------load map----------------------------------------*/  
	  
      // create the map
      var map = new GMap2(document.getElementById("map"));
      map.addControl(new GSmallMapControl());
      map.addControl(new GMapTypeControl());
	  
    // map.setCenter(new GLatLng(41.25922682850889, -73.311767578125), 10);  //center area
		
		
	function setMCenter(dname)
	{
		if(dname == 'Fairfield'){
			 map.setCenter(new GLatLng(41.25922682850889, -73.31176757812501), 10);}
		if(dname == 'Hartford' ){
			map.setCenter(new GLatLng(41.76926321969369, -72.689666748046881), 10);}	
		if(dname =='Litchfield'){
			map.setCenter(new GLatLng(41.83068856472101, -73.218383789062501), 10);}
		if(dname =='Tolland'){
			map.setCenter(new GLatLng(41.89487683210912, -72.439727783203120), 10);}
		if(dname == 'Middlesex'){
			map.setCenter(new GLatLng(41.47977575214486, -72.528991699218750), 10);}
		if(dname == 'Newlondon'){ 
			map.setCenter(new GLatLng(41.498292501398545, -72.15133666992188), 10);}
		if(dname =='Newhaven'){
			map.setCenter(new GLatLng(41.41904486310779, -72.958831787109380), 10);}
		if(dname =='Windham'){
			map.setCenter(new GLatLng(41.83478149415483, -72.162322998046880), 10);}
	}
	
 	/*------------read data file------------*/
 
 		// fn that loads XML file
  	function loadXMLDoc(dname)
	{
		
		var xmlDoc;
		// code for IE
		if (window.ActiveXObject)
		{
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		}

		// code for Mozilla, Firefox, Opera, etc.
		else if (document.implementation && document.implementation.createDocument)
		{
			xmlDoc=document.implementation.createDocument("","",null);
		}
		   //safari
      	/*else if(window.XMLHttpRequest)
        {
             var errorHappendHere = "Error handling XMLHttpRequest request";
             var d = new XMLHttpRequest();
             d.open("GET", dname, false);
             d.send(null);
             xmlDoc=d.responseXML;
			 return(xmlDoc);
		}*/
		else
		{
			alert('Your browser cannot handle this script');
		}
		xmlDoc.async=false;
		xmlDoc.load(dname);
		return(xmlDoc);
	}
     

	 // A function to read the data
      function readSymbol(symbol, tagNme) 
	  {
		  
		  	var xmlDoc = loadXMLDoc("Data.xml");
			var farms = xmlDoc.documentElement.getElementsByTagName(tagNme);
			
			//setMCenter(tagname);
			map.getInfoWindow().hide();           
			map.clearOverlays();
			
			//reset markers to empty
			gmarkers = [];
			
			// reset the side_bar
			side_bar_html="";
			//document.getElementById("side_bar").innerHTML = side_bar_html;
			
			var n = 0;
			for (var i = 0; i < farms.length; i++) 
			{
				var sFound = false;
				
				//alert(farms[i].getElementsByTagName("Symbols")[0].getElementsByTagName("Pri")[0].firstChild.nodeValue);
				
				if(farms[i].getElementsByTagName("Symbols")[0].getElementsByTagName("Pri")[0].firstChild.nodeValue == symbol)
				{
					sFound = true;		
				}
				if(farms[i].getElementsByTagName("Symbols")[0].getElementsByTagName("Sec")[0].firstChild.nodeValue == symbol)
				{
					sFound = true;		
				}
				if(farms[i].getElementsByTagName("Symbols")[0].getElementsByTagName("Thr")[0].firstChild.nodeValue == symbol)
				{
					sFound = true;		
				}
				if(farms[i].getElementsByTagName("Symbols")[0].getElementsByTagName("For")[0].firstChild.nodeValue == symbol)
				{
					sFound = true;		
				}
				if(farms[i].getElementsByTagName("Symbols")[0].getElementsByTagName("Fif")[0].firstChild.nodeValue == symbol)
				{
					sFound = true;		
				}
				if(farms[i].getElementsByTagName("Symbols")[0].getElementsByTagName("Six")[0].firstChild.nodeValue == symbol)
				{
					sFound = true;		
				}
					
				
				
				if (sFound == true)
				{
					// obtain the attribues of each marker
					//var lat = parseFloat(farms[i].childNodes(1).getValue().trim());
					var label = farms[i].getElementsByTagName("Business")[0].firstChild.nodeValue;
					var lat = parseFloat(farms[i].getElementsByTagName("Lat")[0].firstChild.nodeValue);
					var lng = parseFloat(farms[i].getElementsByTagName("Lon")[0].firstChild.nodeValue);
					var point = new GLatLng(lat,lng);
					var html = farms[i].getElementsByTagName("Html")[0].firstChild.nodeValue;
					var sIcon = new GIcon(baseIcon, "images/"+symbol+".gif", null, "images/icon56s.png");
					var marker = createMarker(point,label,html,sIcon, n);
						  
					map.addOverlay(marker);
					n++
				}
			}
			// put the assembled side_bar_html contents into the side_bar div
			document.getElementById("side_bar").innerHTML = side_bar_html;
			
      }// End of the function that reads county data for certain symbol
	
	  // A function to read the data
      function readCnty(tagNme) 
	  {  	 
	   		var xmlDoc = loadXMLDoc("Data.xml");

			// obtain the array of markers and loop through it 

			var farms = xmlDoc.documentElement.getElementsByTagName(tagNme);
			
			//setMCenter(tagname);
			
			setMCenter(tagNme);
		
			// hide the info window, otherwise it still stays open where the removed marker used to be
		
			map.getInfoWindow().hide();         
			map.clearOverlays();
					
			// empty the array of markers
			gmarkers = [];
			
			// reset the side_bar
			side_bar_html="";
			//document.getElementById("side_bar").innerHTML = side_bar_html;
 
			for (var i = 0; i < farms.length; i++) 
			{
				// obtain the attribues of each marker
				//var lat = parseFloat(farms[i].childNodes(1).getValue().trim());
				var label = farms[i].getElementsByTagName("Business")[0].firstChild.nodeValue;
	
				var lat = parseFloat(farms[i].getElementsByTagName("Lat")[0].firstChild.nodeValue);
				var lng = parseFloat(farms[i].getElementsByTagName("Lon")[0].firstChild.nodeValue);
				var point = new GLatLng(lat,lng);
			
				var html = farms[i].getElementsByTagName("Html")[0].firstChild.nodeValue;
				
				var marker = createMarker(point,label,html,farmicon, i);
					  
				map.addOverlay(marker);
			}
			// put the assembled side_bar_html contents into the side_bar div
			document.getElementById("side_bar").innerHTML = side_bar_html;
			
				 
      	}// End of the function that reads county data
     
	 	
		 
		function createMarker(point,name,html,icon, i) 
		{
			var marker = new GMarker(point,icon);
			
			//------driving direction----------      
			// The info window version with the "to here" form open, data from xml file are being loadd into to_htmls[i]
        	to_htmls[i] = html + '<br>Directions: <b>To here</b> - <a href="javascript:fromhere(' + i + ')">From here</a>' +
			   '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
			   '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
			   '<INPUT value="Get Directions" TYPE="SUBMIT">' +
			   '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + 
					  // "(" + name + ")" + 
           '"/>';
        	// The info window version with the "to here" form open, data from xml file are being loaded into from_htmls[i]
       	 	from_htmls[i] = html + '<br>Directions: <a href="javascript:tohere(' + i + ')">To here</a> - <b>From here</b>' +
			   '<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
			   '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
			   '<INPUT value="Get Directions" TYPE="SUBMIT">' +
			   '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
					  // "(" + name + ")" + 
			   '"/>';
        	// The inactive version of the direction info, once the to here or from here is click , information will be loaded into the window pane
        	html = html + '<br>Directions: <a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a>';

			//--------window info--------------------
			GEvent.addListener(marker, "click", function()
												{
													marker.openInfoWindowHtml(html);
												});
        
			// save the info we need to use later for the side_bar
        	gmarkers[i] = marker;
        	htmls[i] = html;
        	// add a line to the side_bar html
        	side_bar_html += '<a href="javascript:myclick(' + i + ')">' + name + '</a><br>';
        	
        	return marker;
      }
	  

      
    }

    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }

    

    //]]>
    
