//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("mapLocations"));
	    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);
        
        map.setMapType(G_HYBRID_MAP);

	    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.472208, -2.323292);
        var marker = CreateMarker(point, 'HI', '<table width="150" height="105" border="0" cellspacing="0" cellpadding="0"><tr><td width="140"><p>Ashbridge off Tenax Road, Trafford Park, M17 1BN</p></td>')
        map.addOverlay(marker);
        bounds.extend(point);
    }
    if (which == 2 || which == 0) {
        var point = new GLatLng(53.467396, -2.315905);
        var marker = CreateMarker(point, 'HI1', '<table width="150" height="105" border="0" cellspacing="0" cellpadding="0"><tr><td width="140"><p>Village Way, Trafford Park, Manchester M17 1BW</p></td>')
        map.addOverlay(marker);
        bounds.extend(point);
    }*/
    if (which == 3 || which == 0) {
        var point = new GLatLng(53.473849, -2.208424);
        var marker = CreateMarker(point, 'HI2', '<table width="150" height="105" border="0" cellspacing="0" cellpadding="0"><tr><td width="140"><p>Rondin Road, Manchester, M12 6BS</p></td>')
        map.addOverlay(marker);
        bounds.extend(point);
    }
    
    if (which == 4 || which == 0) {
        var point = new GLatLng(53.479235, -2.30795);
        var marker = CreateMarker(point, 'HI3', '<table width="150" height="105" border="0" cellspacing="0" cellpadding="0"><tr><td width="140"><p>Daniel Adamson Way, Salford, Manchester M5 2DE</p></td>')
        map.addOverlay(marker);
        bounds.extend(point);
    }
    if (which == 5 || which == 0) {
        var point = new GLatLng(53.486501, -2.294834);
        var marker = CreateMarker(point, 'HI4', '<table width="150" height="105" border="0" cellspacing="0" cellpadding="0"><tr><td width="140"><p>Langworthy Road, Salford, Manchester, M6 6JU</p></td>')
        map.addOverlay(marker);
        bounds.extend(point);
    }
    if (which == 6 || which == 0) {
        var point = new GLatLng(53.396588, -2.507024);
        var marker = CreateMarker(point, 'HI5', '<table width="150" height="105" border="0" cellspacing="0" cellpadding="0"><tr><td width="140"><p>Thelwall Viaduct, Manchester Road, Woolston, WA3 6DR</p></td>')
        map.addOverlay(marker);
        bounds.extend(point);
    }
    
    if (which == 7 || which == 0) {
        var point = new GLatLng(53.501864, -2.850802);
        var marker = CreateMarker(point, 'H6', '<table width="150" height="105" border="0" cellspacing="0" cellpadding="0"><tr><td width="140"><p>Stopgate Lane, Simonswood, Liverpool L33 4YV</p></td>')
        map.addOverlay(marker);
        bounds.extend(point);
    }
    if (which == 8 || which == 0) {
        var point = new GLatLng(53.648619, -2.298439);
        var marker = CreateMarker(point, 'HI7', '<table width="150" height="105" border="0" cellspacing="0" cellpadding="0"><tr><td width="140"><p>Fletcher Bank Quarry, Manchester Road, Ramsbottom, BL0 0DH</p></td>')
        map.addOverlay(marker);
        bounds.extend(point);
    }
    if (which == 9 || which == 0) {
        var point = new GLatLng(53.704354, -2.340066);
        var marker = CreateMarker(point, 'HI8', '<table width="150" height="105" border="0" cellspacing="0" cellpadding="0"><tr><td width="140"><p>Hutchbank Quarry, Hutchbank Road, Haslingden, BB4</p></td>')
        map.addOverlay(marker);
        bounds.extend(point);
    }
    if (which == 10 || which == 0) {
        var point = new GLatLng(53.438218, -2.42079);
        var marker = CreateMarker(point, 'HI9', '<table width="150" height="105" border="0" cellspacing="0" cellpadding="0"><tr><td width="140"><p>Head Office, Soapstone Way, Irlam, M44 6RA</p></td>')
        map.addOverlay(marker);
        bounds.extend(point);
    }
    if (which == 11 || which == 0) {
        var point = new GLatLng(53.434288, -2.418355);
        var marker = CreateMarker(point, 'HI10', '<table width="150" height="105" border="0" cellspacing="0" cellpadding="0"><tr><td width="140"><p>Irlam Wharf Road, Irlam, Salford, M44 5PN</p></td>')
        map.addOverlay(marker);
        bounds.extend(point);
    }
    
    if (which == 0) {
        map.setZoom(map.getBoundsZoomLevel(bounds));
    }
    else {
        map.setZoom(map.getBoundsZoomLevel(bounds) - 3);
    }
    
    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 = "App_Themes/std/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

