//Google Maps
var map;
var tohere_functionpointer;
var fromhere_functionpointer;
var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];
var i = 0;

function load() {
    if (GBrowserIsCompatible()) {
        // functions that open the directions forms
        function tohere(i) {
            gmarkers[i].openInfoWindowHtml(to_htmls[i]);
        }
        function fromhere(i) {
            gmarkers[i].openInfoWindowHtml(from_htmls[i]);
        }
	  
	    tohere_functionpointer = tohere;
	    fromhere_functionpointer = fromhere;

	    // Display the map, with some controls and set the initial location
	    map = new GMap2(document.getElementById("map"));
	    map.addControl(new GLargeMapControl());
        map.addControl(new GHierarchicalMapTypeControl());
        map.addMapType(G_PHYSICAL_MAP);
        map.enableDoubleClickZoom();
        map.enableScrollWheelZoom();
	    
	    map.setCenter(new GLatLng(52.576350, -1.164551), 6);

	    AddMarker(0);
	    
		//prevent page scroll
		function wheelevent(e)
		{
		    e = window.event
			if (e.preventDefault)
			    {
		            e.preventDefault()
			    }
		    e.returnValue = false;
	    }
		GEvent.addDomListener(map.getContainer(), "DOMMouseScroll", wheelevent);
		map.getContainer().onmousewheel = wheelevent;
    }
    // display a warning if the browser was not compatible
    else {
        alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}

function AddMarker(which) {
    map.clearOverlays();

    var bounds = new GLatLngBounds();
    
    if (which == 1 || which == 0) {
        var point = new GLatLng(53.781568, -1.570090);
        var marker = CreateMarker(point, 'HI', '<table width="150" height="105" border="0" cellspacing="0" cellpadding="0"><tr><td width="140"><p>Rentokil Initial Business Centre, Unit 6, Maple Park Industrial Estate, Lowfields Avenue, Leeds, LS12 6HH</p></td>')
        map.addOverlay(marker);
        bounds.extend(point);
    }
    if (which == 2 || which == 0) {
        var point = new GLatLng(52.514308, -2.073101);
        var marker = CreateMarker(point, 'HI1', '<table width="150" height="105" border="0" cellspacing="0" cellpadding="0"><tr><td width="140"><p>Castlegate House, Castlegate House, Castlegate Way, Dudley,<br/>DY1 4RR</p></td>')
        map.addOverlay(marker);
        bounds.extend(point);
    }
    if (which == 3 || which == 0) {
        var point = new GLatLng(51.129195, -0.016769);
        var marker = CreateMarker(point, 'HI2', '<table width="150" height="105" border="0" cellspacing="0" cellpadding="0"><tr><td width="140"><p>Garland Court, Garland Court, Garland Road, East Grinstead,<br/>RH19 1DY</p></td>')
        map.addOverlay(marker);
        bounds.extend(point);
    }
    
    if (which == 0) {
        map.setZoom(map.getBoundsZoomLevel(bounds));
    }
    else {
        map.setZoom(map.getBoundsZoomLevel(bounds) - 6);
    }
    
    map.setCenter(bounds.getCenter());	
}

// A function to create the marker and set up the event window
function CreateMarker(point, name, html) {
    
    // Create Icon
    var icon = new GIcon();
    icon.image = "images/sitePinGoogle.png";
    icon.iconSize = new GSize(30, 37);
    icon.iconAnchor = new GPoint(25, 27);
    icon.infoWindowAnchor = new GPoint(33, 8);
    
    var marker = new GMarker(point, icon);
    
    // The info window version with the "directions to here" form open
    to_htmls[i] = html + '<table width="150" border="0" cellspacing="0" cellpadding="0"><tr><td width="150">Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
           '<input type="text" SIZE=17 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT">' +
           '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() +
    // "(" + name + ")" + 
           '"</td></tr></table>';
    // The inactive version with get directions to here link
    html = html + '<table width="150" border="0" cellspacing="0" cellpadding="0"><tr><td width="150"><a href="javascript:tohere_functionpointer(' + i + ')">Get Directions</a></td></tr></table>';

    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
    });
    gmarkers[i] = marker;
    htmls[i] = html;
    i++;
    return marker;
}
//End of Google Maps
