var map;
var localSearch = new GlocalSearch();
// === Create an associative array of GIcons() ===
var gicons = [];
gicons["sr"] = new GIcon(G_DEFAULT_ICON, "/img/icon-man.png");
gicons["sr"].shadow = "img/icon_shadow.png";
gicons["sr"].iconSize = new GSize(40, 40);
gicons["sr"].shadowSize = new GSize(40, 40);
gicons["sr"].iconAnchor = new GPoint(-20, 20);
gicons["sr"].infoWindowAnchor = new GPoint(29, 0);
gicons["sr"].infoShadowAnchor = new GPoint(0, 0);


var csicon = new GIcon();
csicon.image = "img/icon-man.png"
csicon.shadow = "img/icon_shadow.png";
csicon.iconSize = new GSize(40, 40);
csicon.shadowSize = new GSize(40, 40);
csicon.iconAnchor = new GPoint(-20, 20);
csicon.infoWindowAnchor = new GPoint(29, 0);
csicon.infoShadowAnchor = new GPoint(0, 0);


csloclng=51.545057;
csloclat=-0.149777;


function changeClass(Elem, myClass) {
	var elem;
	if(document.getElementById) {
		var elem = document.getElementById(Elem);
	} else if (document.all){
		var elem = document.all[Elem];
	}
	elem.className = myClass;
}

function handleErrors(){
	   if (directions.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 (directions.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 (directions.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 (directions.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 (directions.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	   else alert("Not a valid postcode."+directions.getStatus().code);
	   
	}


function createMarker(point,html,icontype) {
var marker = new GMarker(point, csicon);
GEvent.addListener(marker, "click", function() {
  marker.openInfoWindowHtml(html);
});
return marker;
}

function wheelevent(e)
{
if (!e){
e = window.event
}
if (e.preventDefault){
e.preventDefault()
}
e.returnValue = false;
}
function addoverlays() {
		var point = new GLatLng(csloclng, csloclat);
		var marker = createMarker(point,'<div style="width:150px"<p><strong>CityStore Self Storage</strong><br>Malden Crescent<br>Camden<br>London<br>NW1 8BS</p></div>')
		map.addOverlay(marker);	
}
function mapLoad() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.enableContinuousZoom();
		//map.enableScrollWheelZoom();
		map.enableDoubleClickZoom
		map.setCenter(new GLatLng(loclng, loclat), 13);
		GEvent.addDomListener(map.getContainer(), "DOMMouseScroll", wheelevent);
		map.getContainer().onmousewheel = wheelevent; 
		addoverlays();
		if(typeof initload != "undefined") {
			loadlocation(loclat, loclng);
		}
	}
}
function resetmap(){
	if(( typeof( window[ 'directions' ] ) != "undefined" )){directions.clear()};
	map.setCenter(new GLatLng(loclng, loclat), 15);
	addoverlays();
	destination = $("#button").offset().top;
	$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100 );
}
function usePointFromPostcode(postcode, callbackFunction) {
	localSearch.setSearchCompleteCallback(null, 
		function() {
			//alert(localSearch.results[0]);
			if (localSearch.results[0])
			{		
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				callbackFunction(point);
			}else{
				alert("Postcode not found!");
			}
		});	
	localSearch.execute(postcode + ", UK");
}

function getdirs(point){
	if(document.getElementById('start').value!='') {
	usePointFromPostcode(document.getElementById('start').value, getdirs2);
	}
}
function onGDirectionsLoad(){ 
	m=directions.getDistance().meters;
	//alert(directions.getSummaryHtml());
	miles=Math.round(m/1609.344);
	changeClass('directionsPanel','shown');
	map.clearOverlays();
}
function getdirs2(point){
	lat2=point.lat();
	lng2=point.lng();
	dpanel = document.getElementById("directionsPanel");
	directions = new GDirections(map, dpanel);
	G_END_ICON = csicon; 
	GEvent.addListener(directions, "load", onGDirectionsLoad);
	 GEvent.addListener(directions, "error", handleErrors);
	directions.load(lat2 + "," + lng2 + " to " + csloclng + "," + csloclat)
	destination = $("#button").offset().top;
	$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100 );
}
function loadlocation(lat2, lng2){
	dpanel = document.getElementById("directionsPanel");
	directions = new GDirections(map, dpanel);
	GEvent.addListener(directions, "load", onGDirectionsLoad);
	GEvent.addListener(directions, "error", handleErrors);
	G_END_ICON = csicon; 
	directions.load(lng2 + "," + lat2 + " to " + csloclng + "," + csloclat);
	destination = $("#button").offset().top;
	//$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100 );
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}



