var gdir, fromAddress, toAddress;

function initialize(store) {
	if (GBrowserIsCompatible()) {
		var companyMarkerSize = new GSize(52,52);
		var companyMarkerImageEvans="images/map/evans.png";
		var companyLatLngEvans= new GLatLng(39.678587,-104.975736);
		var companyMarkerImageLogan="images/map/logan.png";
		var companyLatLngLogan= new GLatLng(39.711295,-104.982824);
		var companyMarkerImageWarehouse="images/map/warehouse.png";
		var companyLatLngWareHouse= new GLatLng(39.70339,-104.998398);
		var companyMarkerImageEdwards="images/map/edwards.png";
		var companyLatLngEdwards= new GLatLng(39.65183,-106.61798);
		
		// Settings
		if(store===1) {
			// var companyMarkerImage="images/map/evans.png";
			// var companyLatLng = new GLatLng(39.678587,-104.975736);
			var mapCenter = companyLatLngEvans;
			// var companyMarkerSize = new GSize(52,52);
			toAddress = "999 E Evans AVE Denver, CO 80210"
			
		}
		else if(store===2) {
			// var companyMarkerImage="images/map/logan.png";
			// var companyLatLng = new GLatLng(39.711295,-104.982824);
			var mapCenter = companyLatLngLogan;
			// var companyMarkerSize = new GSize(52,52);
			toAddress = "286 S Logan ST Denver, CO 80209"
			
		}
		else if(store===3) {
			// var companyMarkerImage="images/map/warehouse.png";
			// var companyLatLng = new GLatLng(39.70339,-104.998398);
			var mapCenter = companyLatLngWareHouse;
			// var companyMarkerSize = new GSize(52,52);
			toAddress = "730 S Jason St #21 Denver, CO 80223"
			
		}
		else if(store===4) {
			// var companyMarkerImage="images/map/edwards.png";
			// var companyLatLng = new GLatLng(39.65183,-106.61798);
			var mapCenter = companyLatLngEdwards;
			// var companyMarkerSize = new GSize(52,52);
			toAddress = "5 Murray Road Unit C-4 Edwards, CO 81632"
			
		}
		else console.log('nothing was received');
		
		var defaultZoomLevel = 13;
		// End Settings
		
		// Setup Elements
		map =new GMap2(document.getElementById("map_canvas"));
		gdir = new GDirections(map, document.getElementById("directions"));
		
		// Error Handler
		GEvent.addListener(gdir, "error", handleErrors);
		
		// Set Company Marker
		var companyMarkerEvans = createMarker(companyLatLngEvans, companyMarkerImageEvans, companyMarkerSize);
		var companyMarkerLogan = createMarker(companyLatLngLogan, companyMarkerImageLogan, companyMarkerSize);
		var companyMarkerWarehouse = createMarker(companyLatLngWareHouse, companyMarkerImageWarehouse, companyMarkerSize);
		var companyMarkerEdwards = createMarker(companyLatLngEdwards, companyMarkerImageEdwards, companyMarkerSize);
		
		// Set map center
		// map.setCenter(companyLatLng, defaultZoomLevel);
		map.setCenter(mapCenter, defaultZoomLevel);
		map.addOverlay(companyMarkerEvans);
		map.addOverlay(companyMarkerLogan);
		map.addOverlay(companyMarkerWarehouse);
		map.addOverlay(companyMarkerEdwards);
	}
}

function createMarker(latlng, imageURL, imageSize)
{
	var marker = new GIcon(G_DEFAULT_ICON, imageURL);
	marker.iconSize = imageSize;
	
	return new GMarker(latlng, {icon: marker });
}

function overlayDirections()
{
	fromAddress = document.getElementById("street").value + " " + document.getElementById("city").value + ", " + document.getElementById("state").options[document.getElementById("state").selectedIndex].value + " " + document.getElementById("zip").value; 
	console.log(fromAddress);	
	console.log(toAddress);
	console.log("from: " + fromAddress + "to: " + toAddress );
	gdir.load("from: " + fromAddress + " to: " + toAddress);
}

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.");
}