
  // declare some variables in global scope to Internet Exploder doesnt choke -.-
  var map = Object;
  var loc = new Array();
  
  /**
    * Loads the normal map on the map page, and adds overlays to it.

  function loadMap() {
    if (GBrowserIsCompatible()) {
      // make a new map 
      map = new GMap2(document.getElementById("map"));
      map.setCenter(new GLatLng(-31, 151), 6); // set center of map 
      //map.addControl(new GLargeMapControl());   // add a pan/zoom control
      map.addControl(new GSmallMapControl());   // add a pan/zoom control
      setAllMarkers(loc); // add all of our markers
      GEvent.addDomListener(document.getElementById("map"),"DOMMouseScroll", wheelZoom);
      GEvent.addDomListener(document.getElementById("map"), "mousewheel", wheelZoom);
      map.enableContinuousZoom();
    }
  }
    */  
  /** 
    * Mouse wheel zoom functionality
    */
  function wheelZoom(event) { 
    if (event.cancelable) {
      event.preventDefault();
    }
    if (event.detail < 0 || -event.wheelDelta < 0) {
      increaseZoomLevel();
    }
    else {
      decreaseZoomLevel();
    }
  }
  
// All of our locations
loc = [
  {town:"Wesley Hospital Ashfield",latitude:"-33.892262",longitude:"151.117068",address:"91 Milton St, Ashfield",postcode:"2131",sitecode:"",regioncode:""},
  {town:"Wesley Conference Centre",latitude:"-33.968396",longitude:"151.128646",address:"7 Blake Street, Kogarah",postcode:"2217",sitecode:"",regioncode:""},
];

  /**
    * put all markers on the map based on our array of locations
    */ 
  function setAllMarkers(loc){
    for (i=0;i<loc.length;i++) {
     l = loc[i];
     pt = new GLatLng(l.latitude, l.longitude);
     l.marker = createMarker(pt,i);                  
     map.addOverlay(l.marker);                       
    }
  }

  /**
    * Create a marker
    */
  function createMarker(point, num) {
    opts = new Object();
    opts.title = loc[num].town;
    var marker = new GMarker(point, opts);
    GEvent.addListener(marker, "click", function() {
        showInfoWindow(num);
    });
    return marker;
  }
  /** 
    * Show the info window for a marker
    */
  function showInfoWindow(num) {
    loc[num].marker.openInfoWindowHtml('<span class="iwstyle"><span class="town">'+loc[num].town+'</span><br/><address>'+loc[num].address.replace(",","<br/>").replace(",","<br/>")+'</address><br/><span class="iwstyle_links"><a href="javascript:zoomIn('+num+');">Zoom to streetlevel</a></span></span>');
  }
  /** 
    * Close the info window
    */
  function hideInfoWindow(num) {
    map.closeInfoWindow();
  }
  
  /** 
    * Hide all markers
    */
  function hideAllMarkers() {
    for (i=0;i<loc.length;i++) {
      loc[i].marker.hide();
    }
  }
  /** 
    * Show marker for given index in Locations array
    */
  function showMarkerForIndex(num) {
    loc[num].marker.show();
  }  
  /**
    * Show / Unhide all markers
    */
  function showAllMarkers() {
    for (i=0;i<loc.length;i++) {
      loc[i].marker.show();
    }
  }
  /**
    * Zoom to street level for index number (of locations array)
    */
  function zoomIn(num) {
    zoomToLocation(loc[num].latitude,loc[num].longitude,16);
  }
  /** 
    * Zoom out to state view for index number (of locations array);
    */
  function zoomOut(num) {
    zoomToLocation(loc[num].latitude,loc[num].longitude,6);
  }
  /**
    * Zoom to a particulat Latitude/Longitude with given zoomlevel
    */
  function zoomToLocation(lat, lon, zoomLevel){
      map.setCenter(new GLatLng(lat,lon),zoomLevel);
      document.getElementById('map').focus();
  }    
  /**
    * Set a marker on the map based on latitude and longitude
    */
  function setMarker(lat,lon) {
    point = new GLatLng(lat,lon);
    var marker = new GMarker(point);

    map.addOverlay(marker);
    return marker;
  }
	/** 
    * Increase zoom level by 1
    */
  function increaseZoomLevel() {
    var maxZoomLevel = 15; 
    var curZoomLevel = map.getZoom();
    if (curZoomLevel < maxZoomLevel) {
      map.setZoom(curZoomLevel+1);
    }
  }
  /** 
    * Decrease zoom level by 1
    */
  function decreaseZoomLevel() {
    var minZoomLevel = 1; 
    var curZoomLevel = map.getZoom();
    if (curZoomLevel > minZoomLevel) {
      map.setZoom(curZoomLevel-1);
    }
  }

/**
  * Loader method to blur from the menu to the map, this
  * prevents the menu from "sticking" open.
  */  
var sfBlur = function(targetid) {
  if (document.getElementById(targetid)) {
  	var sfEls = document.getElementById(targetid).getElementsByTagName('a');
  	for (var i=0; i<sfEls.length; i++) {
      var el = sfEls[i];
  		sfEls[i].onclick=function() {
        el.blur();
  			document.getElementById('map').focus();
  		}
  	}
  }
}

function sfInitBlur() {
  sfBlur('mapnav');
}
function initMapNav() {
	  sfHover("mapnav");
  	mcAccessible("mapnav");
}


function setDirections(fromAddress, toAddress, locale) {
      gdir.load("from: " + fromAddress + " to: " + toAddress,
                { "locale": locale });
    }

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.

      // e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
	}

	