
function createMarker(point, myhtml) {
var myMarker = new GIcon();
myMarker.iconSize = new GSize(36, 32);
myMarker.shadowSize = new GSize(16, 16);
myMarker.iconAnchor = new GPoint(18, 32);
myMarker.infoWindowAnchor = new GPoint(5, 2);
myMarker.infoShadowAnchor = new GPoint(5, 2);
myMarker.image = "witch.gif";
markerOptions = { icon:myMarker };
var marker = new GMarker(point, markerOptions);
GEvent.addListener(marker, "click", function() {
  marker.openInfoWindowHtml(myhtml);
});
return marker;
}

var map;
var gdir;
var store1Dir;
var store2Dir;
var store3Dir;
		var numberOfStores=1;
		var names = new Array(numberOfStores);
		var lats = new Array(numberOfStores);
		var longs = new Array(numberOfStores);

		names[0] = "Nashoba Valley Ski Area";
		lats[0] = 42.542;
		longs[0] = -71.445;
		

var store1;
var store2;
var store3;

function gload() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
gdir= new GDirections(map, document.getElementById("route"));
store1Dir= new GDirections();
store2Dir= new GDirections();
store3Dir= new GDirections();
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(42.542,-71.445), 9);

GEvent.addListener(gdir, "load", onGDirectionsLoad);
GEvent.addListener(gdir, "error", handleErrors);
GEvent.addListener(gdir, "addoverlay", onAddOverlayDirection);

GEvent.addListener(store1Dir, "load", onTemp1Load);
GEvent.addListener(store1Dir, "error", handleTempErrors);
GEvent.addListener(store2Dir, "load", onTemp2Load);
GEvent.addListener(store2Dir, "error", handleTempErrors);
GEvent.addListener(store3Dir, "load", onTemp3Load);
GEvent.addListener(store3Dir, "error", handleTempErrors);

var point = new GLatLng(42.542,-71.445);
var html = '<h4><a href=\'http://www.skinashoba.com\'>Nashoba Valley Ski Area</a></h4>Westford, MA 01886';
map.addOverlay(createMarker(point,html));


}
}

function onAddOverlayDirection()
{
var i =0;
for(i =0; i< gdir.getNumGeocodes(); i++)
{
map.removeOverlay(gdir.getMarker(i));
}
}


function setDirections(fromAddress, toAddress, locale) {
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": locale });
    }

function handleErrors(){
if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_BAD_KEY)
     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
else alert("An unknown error occurred.");
}

function handleTempErrors(){
}

function onGDirectionsLoad(){ 
}

function onTemp1Load(){
	var div=document.getElementById("store1");
	var thisDist=store1Dir.getDistance().meters;
	thisDist=thisDist/1609.344;
	thisDist=thisDist.toFixed(1);
	/*div.innerHTML="<a href=\"javascript:mapStore("+store1+");\">"+thisDist+" mi to "+names[store1]+"</a>";*/
}
function onTemp2Load(){ 
	var div=document.getElementById("store2");
	var thisDist=store2Dir.getDistance().meters;
	thisDist=thisDist/1609.344;
	thisDist=thisDist.toFixed(1);
	div.innerHTML="<a href=\"javascript:mapStore("+store2+");\">"+thisDist+" mi to "+names[store2]+"</a>";
}
function onTemp3Load(){ 
	var div=document.getElementById("store3");
	var thisDist=store3Dir.getDistance().meters;
	thisDist=thisDist/1609.344;
	thisDist=thisDist.toFixed(1);
	div.innerHTML="<a href=\"javascript:mapStore("+store3+");\">"+thisDist+" mi to "+names[store3]+"</a>";
}
function mapStore(id){
	var start = document.getElementById("closeAddress").value;
	var end = names[id]+"@"+lats[id]+","+longs[id];
	document.getElementById("route").style.display='block';
	setDirections(start, end, 'en');
}



function closestDirections(){ 
	var start = document.getElementById("closeAddress").value;
	var geoCoder = new GClientGeocoder();
	geoCoder.getLatLng(start,  function(point) {
	if (!point) {
		alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.");
        } else {
		

		store1=0;
		store2=0;
		store3=0;
		var dist1=24999.9;
		var dist2=24999.9;
		var dist3=24999.9;
			
		
	for(var i=0; i< numberOfStores; i++)
	{
		var storePoint = new GLatLng(lats[i],longs[i]);
		var thisDistance = point.distanceFrom(storePoint)/1000;
		
		if(thisDistance<dist3){
			dist3=thisDistance;
			store3=i;
		}
		if(thisDistance<dist2){
			dist3=dist2;
			store3=store2;
			dist2=thisDistance;
			store2=i;
		}
		if(thisDistance<dist1){
			dist2=dist1;
			store2=store1;
			dist1=thisDistance;
			store1=i;
		}
	}


	var store = names[store1]+"@"+lats[store1]+","+longs[store1];
store1Dir.load("from: " + start + " to: " + store,{ "locale": 'en' });
document.getElementById("route").style.display='block';
	setDirections(start, store, 'en');
	}
   }); 
}
