var localSearch = new GlocalSearch();

var map;

var code = new Array();
var lat = new Array();
var long = new Array();
var address = new Array();
var url = new Array();
var name = new Array();
var postcode = new Array();

var icon = new GIcon();
icon.image = "/prdcons/images/imperial_website/images/red-dot.png";
icon.shadow = "/prdcons/images/imperial_website/images/shadow-red_MarkerA.png";
icon.iconSize = new GSize(20, 34);
icon.shadowSize = new GSize(38, 34);
icon.iconAnchor = new GPoint(10, 30);
icon.infoWindowAnchor = new GPoint(9, 1);

// Creates a marker at the given point with the given number label
function createMarker(seqn, thisCode, thisLat, thisLong, thisAddress, thisName) {
        // Create our "tiny" marker icon
        icon.image = "/prdcons/images/imperial_website/images/red_Marker_" + thisCode + ".png";

        var point = new GLatLng(thisLat, thisLong);
        var marker = new PdMarker(point,icon);
	marker.setTooltip(thisName);
	marker.setHoverImage("http://www.google.com/mapfiles/dd-start.png");
      GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(thisAddress);
      });
      return marker;
    }

function setCenterToLatLong(thisLat,thisLong, thisZoom)
{
        map = new GMap2(document.getElementById("map"));
	var mapTypeControl = new GLargeMapControl();
	var bottomLeft = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(10,35));
	map.addControl(mapTypeControl, bottomLeft);

	map.setCenter(new GLatLng(thisLat,thisLong), thisZoom);

	for (i=0; i<5; i++)
	{ 
		map.addOverlay(new createMarker(i, code[i], lat[i], long[i], address[i], name[i]));
	}

}

function goFrom(here, destLat, destLong) {
      dirmap = new GMap2(document.getElementById("map"));
      directionsPanel = document.getElementById("rightCol");
      // map.setCenter(here, 13);

	//Create an array to house the two GLatLng Objects
        var arrLocation = new Array(2);
        arrLocation[0] = here;
        arrLocation[1] = new GLatLng(destLat, destLong);


      x_directions = new GDirections(dirmap, directionsPanel);
      x_directions.loadFromWaypoints(arrLocation); 
    }

// function usePointFromPostcode() {
function usePointFromPostcode(postcode, hospLat, hospLong, callbackFunction) {

	localSearch.setSearchCompleteCallback(null, 
		function() {
			
			if (localSearch.results[0])
			{		
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				callbackFunction(point, hospLat, hospLong);
			}else{
				alert("Postcode not found!");
			}
		});	
		
	localSearch.execute(postcode + ", UK");
}

function getDirect(index) {
	usePointFromPostcode(document.getElementById('postcode').value, lat[index], long[index], goFrom);
}

function mapLoad(Latitude, Longitude, Zoom, MapType, numMarker) {
   if (GBrowserIsCompatible()) {
        // <$if not #active.site$>
        map = new GMap2(document.getElementById("map"));

	var mapTypeControl = new GLargeMapControl();
	var bottomLeft = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(10,35));
	map.addControl(mapTypeControl, bottomLeft);

        map.setCenter(new GLatLng(Latitude, Longitude), Zoom, MapType);
	//  <$endif$>
	for (i=0; i<numMarker; i++)
	{ 
		var marker = createMarker(i, code[i], lat[i], long[i], address[i], name[i]);
		map.addOverlay(marker);
	}
	
	map.zoomToMarkers();

  }
}
