var map;
var html;

var icon = new GIcon();
icon.image = "http://labs.google.com/ridefinder/images/mm_20_yellow.png";
icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
icon.iconSize = new GSize(12, 20);
icon.shadowSize = new GSize(22, 20);
icon.iconAnchor = new GPoint(6, 20);
icon.infoWindowAnchor = new GPoint(5, 1);

var icon1 = new GIcon();
icon1.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";
icon1.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
icon1.iconSize = new GSize(12, 20);
icon1.shadowSize = new GSize(22, 20);
icon1.iconAnchor = new GPoint(6, 20);
icon1.infoWindowAnchor = new GPoint(5, 1);
	  
var icon2 = new GIcon();
icon2.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
icon2.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
icon2.iconSize = new GSize(12, 20);
icon2.shadowSize = new GSize(22, 20);
icon2.iconAnchor = new GPoint(6, 20);
icon2.infoWindowAnchor = new GPoint(5, 1);	  
	  
var icon3 = new GIcon();
icon3.image = "http://labs.google.com/ridefinder/images/mm_20_green.png";
icon3.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
icon3.iconSize = new GSize(12, 20);
icon3.shadowSize = new GSize(22, 20);
icon3.iconAnchor = new GPoint(6, 20);
icon3.infoWindowAnchor = new GPoint(5, 1);
	  
var icon4 = new GIcon();
icon4.image = "http://labs.google.com/ridefinder/images/mm_20_purple.png";
icon4.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
icon4.iconSize = new GSize(12, 20);
icon4.shadowSize = new GSize(22, 20);
icon4.iconAnchor = new GPoint(6, 20);
icon4.infoWindowAnchor = new GPoint(5, 1);	 	  
	  
function clearmap() {
    document.getElementById("pwait").innerHTML = "<img src='images/wait.gif' alt='' width='150' border='0'>";
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(42.82321, -76.938654), 9);

	map1('xml.asp?type=0X','');
}	  
	  
function map1(xmladdr,from) {
   if (GBrowserIsCompatible()) {
   	  document.getElementById("pwait").innerHTML = "<img src='images/wait.gif' alt='' width='150' border='0'>";
      var infoHtml;
	  var address;  

	  GDownloadUrl(xmladdr, function(data, responseCode) { 
         var xml = GXml.parse(data);
		 var markers = xml.documentElement.getElementsByTagName("name");

         for (var i = 0; i < markers.length; i++) {
		    address 	= markers[i].getAttribute("street") + ", " + 
					      markers[i].getAttribute("city") + ", " + 
						  markers[i].getAttribute("state") + " " + 
						  markers[i].getAttribute("zip");
					  
			var direction = markers[i].getAttribute("direction")
 
			infoHtml 	= "<font color=black size=2><b>" + 
						  markers[i].firstChild.nodeValue + "</b><br>" + 
						  markers[i].getAttribute("street") + "<br>" + 
						  markers[i].getAttribute("city") + ", " + 
						  markers[i].getAttribute("state") + " " + 
						  markers[i].getAttribute("zip") + "</font><br><br>";		 

		 	if(markers[i].getAttribute("type") == "0X") 
				showAddress(address,icon,infoHtml,i,direction,from,"1",markers[i].getAttribute("actID"));	
			else if(markers[i].getAttribute("type") == "1W") 	 
				showAddress(address,icon1,infoHtml,i,direction,from,"0",markers[i].getAttribute("actID"));	
			else if(markers[i].getAttribute("type") == "2G") 	 
				showAddress(address,icon3,infoHtml,i,direction,from,"0",markers[i].getAttribute("actID"));				
			else if(markers[i].getAttribute("type") == "3A") 	 
				showAddress(address,icon3,infoHtml,i,direction,from,"0",markers[i].getAttribute("actID"));				
			else if(markers[i].getAttribute("type") == "4R") 	 
				showAddress(address,icon2,infoHtml,i,direction,from,"0",markers[i].getAttribute("actID"));	
			else if(markers[i].getAttribute("type") == "5C") 	 
				showAddress(address,icon1,infoHtml,i,direction,from,"0",markers[i].getAttribute("actID"));	
		 }
		 document.getElementById("pwait").innerHTML = "";
      });	  
   } 
}

function map2(name,addr,phone,color,from) {
	var infoHtml;
	var direction 	= "to";
	var address		= addr;

	infoHtml 	= "<font color=black size=2><b>" + name + "</b><br>" + 
				  addr + "<br>" + 
				  "Phone Number: " + phone + "<br><br>";

 	if(color == "0") 
		showAddress(address,icon,infoHtml,0,direction,from,"1","0");	
	else if(color == "1") 	 
		showAddress(address,icon1,infoHtml,0,direction,from,"0","0");			 
	else if(color == "2") 	 
		showAddress(address,icon2,infoHtml,0,direction,from,"0","0");			 
	else if(color == "3") 	 
		showAddress(address,icon3,infoHtml,0,direction,from,"0","0");			 
	else if(color == "4") 	 
		showAddress(address,icon4,infoHtml,0,direction,from,"1","0");		
}

function showAddress(addr,icon,infoHtml,num,direction,from,loc,AID) {
   var geocoder = new GClientGeocoder();

   if (geocoder) {
      geocoder.getLatLng(
         addr,
         function(point) {
            if (!point) {
			/*
				alert(AID + " - " + addr + " not found");
			*/	
            } else {			   
               var marker = new GMarker(point, icon);

			   if (direction == "to") {
			      if(addr != "4136 High Banks, Geneva, NY 14456")
                  	infoHtml = infoHtml + '<font color=black><form action="http://maps.google.com/maps" method="get" target="_blank">' +
           		     					'Get Directions From Paradise On The Lake B&B:<br />';
				  else
                  	infoHtml = infoHtml + '<font color=black><form action="http://maps.google.com/maps" method="get" target="_blank">' +
           		     					'Enter Address For Directions:<br />';				  	
					 
				  if (from != "") 
				  	infoHtml = infoHtml + '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="'+from+'" /><br />';
				  else
				    if(addr != "4136 High Banks, Geneva, NY 14456")
				  		infoHtml = infoHtml + '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="4136 High Banks, Geneva, NY 14456" /><br />';
				 	else
						infoHtml = infoHtml + '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br />';
				 
           		    infoHtml = infoHtml + '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           		    					  '<input type="hidden" name="daddr" value="' + addr +
           		     					  '"/></form></font>';
			   } 			   
			   
			   if (direction == "from") {
                  infoHtml = infoHtml + '<font color=black><form action="http://maps.google.com/maps" method="get" target="_blank">' +
           		     'Enter your ending address:<br />' +
           		     '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="Num Street, Town, State" /><br />' +
           		     '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           		     '<input type="hidden" name="saddr" value="' + 
           		     parseFloat(point.lat()).toString() + ',' + 
           		     parseFloat(point.lng()).toString() + 
           		     '"/></form></font>';
			   } 			   
	   
			   GEvent.addListener(marker, "click", function() {
                  map.setCenter(point, 13);				
    		      marker.openInfoWindowHtml(infoHtml);
               });		

			   map.addOverlay(marker);				
			   if(loc == "1") 
			   	  marker.openInfoWindowHtml(infoHtml);	   
            }
         }
      );
   }
}
