// JavaScript Document
var IE6 = false /*@cc_on || @_jscript_version <= 5.7 @*/;
var markers = [];
if (GBrowserIsCompatible()) {
	// A function to create the marker and set up the event window
	// Dont try to unroll this function. It has to be here for the function closure
	// Each instance of the function preserves the contends of a different instance
	// of the "marker" and "html" variables which will be needed later when the event triggers.
	function createMarker(point,html,id) {
		//var newIcon = MapIconMaker.createMarkerIcon({width: 32, height: 32, primaryColor: "#ff0000"});
		//var marker = new GMarker(store.latlng, );
		var marker = new GMarker(point);
		GEvent.addListener(marker, "mouseover", function() {
			marker.openInfoWindowHtml(html);
		});
		markers[id]=marker;
		return marker;
	}
	// Display the map, with some controls and set the initial location
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(centerLat,centerLng),centerHgt);
	map.enableScrollWheelZoom();
	if(IE6 == false){
		mapopacityTween = new OpacityTween(document.getElementById('map'),Tween.strongEaseOut, 0, 100, 3);
		mapopacityTween.start()
	}else {
		document.getElementById('map').style.display='block';
	}
	/*var directionsPanel = document.getElementById("route");
	var directions = new GDirections(map, directionsPanel);
	directions.load("from: 4615 N Lamar Blvd, Austin, TX 78756 to: 11301 Lakeline Blvd, Austin, TX 78717");*/
	document.getElementById("route")

	function getDirections(form,to) {
		//alert("directions is "+directions);
		if(directions == undefined) {

			from = "from: "+form.inputbox.value+" ";
			fromTo = from + to;
			directionsPanel = document.getElementById("route");
			directions = new GDirections(map, directionsPanel);
			directions.load(fromTo);
			//document.getElementById('route').style.display='block';
			GEvent.addListener(directions, "error", handleErrors);
			GEvent.addListener(directions, "load", onGDirectionsLoad);
		} else {
			map.setCenter(new GLatLng(centerLat,centerLng),centerHgt);
			document.getElementById('route').style.display='none';
			directions.clear();
			from = "from: "+form.inputbox.value+" ";
			fromTo = from + to;
			directionsPanel = document.getElementById("route");
			directions = new GDirections(map, directionsPanel);
			directions.load(fromTo);
			//document.getElementById('route').style.display='block';
			GEvent.addListener(directions, "error", handleErrors);
			GEvent.addListener(directions, "load", onGDirectionsLoad);
		}
	}
	function onGDirectionsLoad() {
		document.getElementById('route').style.display='block';
		if(IE6 == false){
			routeopacityTween = new OpacityTween(document.getElementById('route'),Tween.strongEaseOut, 0, 100, 3);
			routeopacityTween.start()
		}else {document.getElementById('route').style.display='block';
		}
	}

	function handleErrors() {
		var message = "An unknown error occurred.";
		if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		message = "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: " + directions.getStatus().code;
		else if (directions.getStatus().code == G_GEO_SERVER_ERROR)
		message = "A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + directions.getStatus().code;
		else if (directions.getStatus().code == G_GEO_MISSING_QUERY)
		message = "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: " + directions.getStatus().code;
		else if (directions.getStatus().code == G_GEO_BAD_KEY)
		message = "The given key is either invalid or does not match the domain for which it was given. \n Error code: " + directions.getStatus().code;
		else if (directions.getStatus().code == G_GEO_BAD_REQUEST)
		message = "A directions request could not be successfully parsed.\n Error code: " + directions.getStatus().code;
		document.getElementById('route').style.display='none';
		alert("Message from Google Maps: " + message);
	}
	function hideDIR() {
		map.clearOverlays();
		map.setCenter(new GLatLng(centerLat,centerLng),centerHgt);
		document.getElementById('route').style.display='none';
		directions.clear();
	}

	function drawCircle(lat, lng, radius, strokeColor, strokeWidth, strokeOpacity, fillColor, fillOpacity) {
		map.clearOverlays();
		var d2r = Math.PI/180;
		var r2d = 180/Math.PI;
		var Clat = radius * 0.014483;
		var Clng = Clat/Math.cos(lat * d2r);
		var Cpoints = [];
		for (var i=0; i < 33; i++) {
			var theta = Math.PI * (i/16);
			Cy = lat + (Clat * Math.sin(theta));
			Cx = lng + (Clng * Math.cos(theta));
			var P = new GPoint(Cx,Cy);
			Cpoints.push(P);
		}
		var polygon = new GPolygon(Cpoints, strokeColor, strokeWidth,strokeOpacity, fillColor,fillOpacity);
		map.addOverlay(polygon);
	}

	function myclick(id) { //used for all other regions
		markers[id].openInfoWindowHtml(locations[id][2]);
//	}
//	function myclick0(id) {//for Austin/Triangle
//		drawCircle(30.315691,-97.735147, 3.0, "#000080", 1, 0.75, "#0000FF", .5); 
//		markers[id].openInfoWindowHtml(locations[id][2]);
//	}
//	function myclick1(id) {// for Austin/Lakeline
//		drawCircle(30.479426,-97.799372, 5.0, "#000080", 1, 0.75, "#0000FF", .5); 
//		markers[id].openInfoWindowHtml(locations[id][2]);
//	}
//	function myclick2(id) {//for Austin/Southpark
//		drawCircle(30.161260,-97.791693, 4.0, "#000080", 1, 0.75, "#0000FF", .5); 
//		markers[id].openInfoWindowHtml(locations[id][2]);
//	}
//	function myclick3(id) {//for Austin/Downtown
//		drawCircle(30.274931,-97.741671, 3.5, "#000080", 1, 0.75, "#0000FF", .5); 
//		markers[id].openInfoWindowHtml(locations[id][2]);
//	}
//	function myclick4(id) {//for Austin/Georgetwon
//		drawCircle(30.632728,-97.693404, 6.0, "#000080", 1, 0.75, "#0000FF", .5); 
//		markers[id].openInfoWindowHtml(locations[id][2]);
//	}
//	function myclick5(id) {//for Austin/New Braunfels
//		drawCircle(29.698311,-98.096574, 0.0, "#000080", 1, 0.75, "#0000FF", .5); 
//		markers[id].openInfoWindowHtml(locations[id][2]);
//	}
//	function myclick6(id) {//for Arkansas/Bentonville
//		drawCircle(36.335286,-94.202543, 7.0, "#000080", 1, 0.75, "#0000FF", .5); 
//		markers[id].openInfoWindowHtml(locations[id][2]);
//	}
//	function myclick7(id) {//for Florida/Hollywood
//		drawCircle(26.010966,-80.173906, 5.0, "#000080", 1, 0.75, "#0000FF", .5); 
//		markers[id].openInfoWindowHtml(locations[id][2]);
//	}
//	function myclick10(id) {//Test diaplay all Austin delivery areas
//		map.clearOverlays();
//		drawCircle(30.315691,-97.735147, 3.0, "#000080", 1, 0.75, "#0000FF", .5); //TR
//		drawCircle(30.479426,-97.799372, 5.0, "#000080", 1, 0.75, "#6b4033", .5); //LL
//		drawCircle(30.161260,-97.791693, 4.0, "#000080", 1, 0.75, "#33CC33", .5); //SP
//		drawCircle(30.274931,-97.741671, 3.5, "#000080", 1, 0.75, "#0099CC", .5); //DT
//		drawCircle(30.632728,-97.693404, 6.0, "#000080", 1, 0.75, "#33CC00", .5); //GT
//		drawCircle(36.335286,-94.202543, 7.0, "#000080", 1, 0.75, "#0000FF", .5); //BV
//		drawCircle(26.010966,-80.173906, 5.0, "#000080", 1, 0.75, "#0000FF", .5); //HW
//		markers[id].openInfoWindowHtml(locations[id][2]);
	}
	// Set up markers with info windows

	for (var i=0; i<locations.length; i++) {
		id = i;
		point = new GLatLng(locations[i][0],locations[i][1]);
		marker = createMarker(point,locations[i][2],id)
		map.addOverlay(marker);

	}
}

// display a warning if the browser was not compatible
else {
	alert("Sorry, the Google Maps API is not compatible with this browser");
}
//]]>