var proxy = '/reserve/modules/maps/googlemap';
var map;
var gdir;
var geo;
var point;
var local = "en";
var info;
var name;
var maptype;
var mapzoom;
var showinfo = true;
function initialize() {
      if (GBrowserIsCompatible()) {
    	map = new GMap2(document.getElementById("map_canvas"));
	    map.setCenter(point, mapzoom);
	    map.setMapType(maptype);
    
        // Either Add points and HTML
        map.addOverlay(createMarker(point, info));
        
        // Add Map Controls
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.addControl(new GOverviewMapControl());

        if (document.getElementById("directionstext") != null) {
        	gdir = new GDirections(map, document.getElementById("directionstext"));
        	GEvent.addListener(gdir, "load", onGDirectionsLoad);
        	GEvent.addListener(gdir, "error", handleErrors);
        }
        
        if (showinfo == true)
			{map.openInfoWindowHtml(point, info);};
      }
    }
 
// Creates a marker at the given point
// Clicking the marker will show HTML buble
function createMarker(point, myHtml) {
      var marker = new GMarker(point);
      marker.value = 1;
      GEvent.addListener(marker,"click", function() {
        map.openInfoWindowHtml(point, myHtml);
      });
	  return marker;
    }
    
   
// Zoom
function zoomMap(level, rd) {
			document.getElementById('region2').checked = false;
			document.getElementById('region').checked = false;
			document.getElementById('street').checked = false;
			document.getElementById('suburb').checked = false;
			rd.checked = true;
			map.setZoom(level);
	}	

// ReCentre
function centerAndZoom() {
		map.returnToSavedPosition();
		}	

function getAjax(){
	var xmlHttp;
	try {
		xmlHttp = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
	} catch(e) {
    
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer 6+
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); // Internet Explorer 5.5+
			} catch (e) {
				xmlHttp = false; // no ajax support
			}
		}
	}
	return xmlHttp;
}

function xmlError(e) {
	alert(e); //there was an error, show the user
}

function promptPlaces(){
	if (document.getElementById('address').value.length > 3)
	{
		var xmlHttp = getAjax();
		xmlHttp.onreadystatechange=function(){
			if(xmlHttp.readyState==4){
				if (xmlHttp.responseText.length > 10) {
					document.getElementById('addresspopup').innerHTML=xmlHttp.responseText;
					document.getElementById('addresspopup').style.display='block';
				}
			}
		}
		xmlHttp.open('GET',proxy + '/proxyplaces.aspx?name='+document.getElementById('address').value,true);
		xmlHttp.send(null);
	}
}

function hideResults(){
	//setTimeout("document.getElementById('addresspopup').style.display='none';",300);
}

function setDirections(x,y,from){
	document.getElementById('progress').style.display='block';
	
	geo = new GLatLng(x, y);
	
	try {
			local = document.getElementById("locale").value;
			} catch (e) {
				var local = "en";
			}
		
	document.getElementById('address').value = from;
	document.getElementById("directionstext").innerHTML = ""; 
	gdir.loadFromWaypoints([ from + "@" + geo.y + "," + geo.x, name + "@" + point.y + "," + point.x ],{getPolyline:true,getSteps:true,"locale": local}); 
}

function changeLang() {
	if (geo == null)
		{
		try {
			local = document.getElementById("locale").value;
			} catch (e) {
				var local = "en";
			}
		}
	else
		{
	document.getElementById('progress').style.display='block';	
	var from = document.getElementById('address').value;
	try {
			local = document.getElementById("locale").value;
			} catch (e) {
				var local = "en";
			}
		document.getElementById('address').value = from;
		document.getElementById("directionstext").innerHTML = ""; 
		gdir.loadFromWaypoints([ from + "@" + point.y + "," + point.x, name + "@" + geo.y + "," + geo.x ],{getPolyline:true,getSteps:true,"locale": local}); 
		};
}

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_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\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 onGDirectionsLoad(){
	  // Use this function to access information about the latest load()
      // results.
		document.getElementById('progress').style.display='none';
		map.getInfoWindow().hide()
      // e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
	}