var map;
var gdir;

var geocoder = null;
var addressMarker;

var error=null;

var lat;
var lng;

function Store(lat,lng,city,dest){
	this.latitude=lat;
	this.longitude=lng;
	this.city=city;
	this.destination=dest;
}
var stores=new Array();

stores[0]=new Store(33.7241,-118.07607,"Huntington Beach","16400 Pacific Coast Hwy,CA 92649");
stores[1]=new Store(35.19267882266177, -111.6589879989624,"FlagStaff","801 S Milton Ave,Flagstaff,Arizona");

hours=new Array();

hours[0]="<B>Trading Hours for Huntington Restaurant</B><BR>Daily Lunch ~ 11am-3:00pm (Mon-Sat)<BR>Dinner ~ 5pm-10pm (Everyday)<BR>Sunday Champagne Brunch: 11:30am-3:00pm";
hours[1]="<B>Trading Hours for FlagStaff Restaurant</B><BR>Daily Lunch ~ 11am-2:30pm (Mon-Sat)<BR>Dinner ~ 5pm-10pm (Everyday)<BR>Sunday Champagne Brunch: 11:00am-2:30pm";

var storeID=0;

function load(store){	         
    storeID=store;

    lat=stores[storeID].latitude;
    lng=stores[storeID].longitude;

	if (GBrowserIsCompatible()) {	
		 resetMap();//Closes Direction if open
		 loadGoogleMap(lat,lng);
	}else{
		 loadStaticMap();
	}
	
	//document.getElementById("hours").innerHTML=hours[storeID];		
	document.getElementById("city").innerHTML=stores[storeID].city;	
}

function loadGoogleMap(lat,lng){
    if(map){
		 GUnload();
	}

	map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());

	//Milton:Ok                             
	map.setCenter(new GLatLng(lat,lng), 12);	

	//add marker
	marker=new GMarker(new GLatLng(lat,lng));
	map.addOverlay(marker);

	GEvent.addListener(map,"click",function (marker){
			  marker.openInfoWindowHtml('<IMG SRC="img.jpg" WIDTH="184" HEIGHT="138" BORDER=0 ALT="">');
	});

	initialize(); //don't remove
}

function initialize() {
	if(!map){
		 map = new GMap2(document.getElementById("map")); 
	}
	
	gdir = new GDirections(map, document.getElementById("directions"));

	GEvent.addListener(gdir, "load", onGDirectionsLoad);
	GEvent.addListener(gdir, "error", handleErrors);
}


function getDirections(form){
	 
	if(!gdir){
	  initialize();
	}

	gdir.clear();


	var fromAddress=form.from.value;
	var toAddress=stores[storeID].destination;
	var locale="en_US";

	gdir.load("from: " + fromAddress + " to: " + toAddress,
			{ "locale": locale , "getSteps":true});
	 
}

function reverseDirections(form){
	if(!gdir){
	  initialize();
	}

	gdir.clear();


	var toAddress=form.from.value;
	var fromAddress=stores[storeID].destination;
	var locale="en_US";

	gdir.load("from: " + fromAddress + " to: " + toAddress,
			{ "locale": locale , "getSteps":true});
}

function resetMap(){
	if(gdir){
	 gdir.clear();      
	 map.setCenter(new GLatLng(lat,lng), 12);	
	}

	document.getElementById("directions").style.height="0px";
	document.getElementById("directions").style.visibility="hidden";
}

function handleErrors(){
   

	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS){
	     alert("Geographic location could be found for the specified addresses.\n The address is either relatively new, or it may be incorrect.\n Try one of the city names seen on the map");

	}

	document.getElementById("directions").style.height="0px";
	document.getElementById("directions").style.visibility="hidden";

	/**
	else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("Sorry,there was a problem processing the directions request.\nError 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 onGDirectionsLoad(){
	document.getElementById("directions").style.height="300px";
	document.getElementById("directions").style.visibility="visible";
}

function loadStaticMap(){
	var table='\n <table border="0" align="center" cellpadding="0" cellspacing="0">';
	table+='\n \t <tr><td><img src="map1.jpg"></td></tr>';
	table+='\n \t <tr><td><img src="map2.jpg"></td></tr>';
	table+='\n \t <tr><td><img src="map3.jpg"></td></tr>';
	table+='\n \t </table>';

	var panel=document.getElementById("map");
	alert(table);
	panel.innerHTML=table;
}
