﻿var map;
var localSearch = new GlocalSearch();

// Create a base icon for all markers
var baseIcon = new GIcon(G_DEFAULT_ICON);
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(23, 35);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(12, 38);
baseIcon.infoWindowAnchor = new GPoint(12, 2);
baseIcon.image = MARKER_URL;

function toggleView(viewOption)
{
    if (viewOption == "map")
    {
        // Toggle view
        document.getElementById("branchesMapOuter").style.display = 'block';
        document.getElementById("branchesList").style.display = 'none';
    }
    else if (viewOption == "list")
    {
        // Toggle view
        document.getElementById("branchesMapOuter").style.display = 'none';
        document.getElementById("branchesList").style.display = 'block';
    }
}

function mapLoad()
{
    if (GBrowserIsCompatible())
    {
        map = new GMap2(document.getElementById("branchesMap"));
        map.setCenter(new GLatLng(51.5, -1.95), 7);
        map.setUIToDefault();

        // Add markers
        addBranchMarkers();
    }
}

function addBranchMarkers()
{
    // Add markers for each branch
    map.addOverlay(createMarker(new GLatLng(51.8157, -0.8168), 'Aylesbury PAT Testing', 'http://www.aylesburypat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.2625, -1.0840), 'Basingstoke PAT Testing', 'http://www.basingstokepat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.3814, -2.3575), 'Bath PAT Testing', 'http://www.bathpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(52.4830, -1.8936), 'Birmingham PAT Testing', 'http://www.birminghampat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(50.7217, -1.8785), 'Bournemouth PAT Testing', 'http://www.bournemouthpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.4553, -2.5919), 'Bristol PAT Testing', 'http://www.bristolpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.4813, -3.1805), 'Cardiff PAT Testing', 'http://www.cardiff-pattesting.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.8980, -2.0713), 'Cheltenham PAT Testing', 'http://www.cheltenhampat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(52.4058, -1.5127), 'Coventry PAT Testing', 'http://www.coventrypat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(50.7218, -3.5336), 'Exeter PAT Testing', 'http://www.exeterpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.2940, -0.7546), 'Farnborough PAT Testing', 'http://www.farnboroughpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.2365, -0.5703), 'Guildford PAT Testing', 'http://www.guildfordpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.8667, -2.2487), 'Gloucester PAT Testing', 'http://www.gloucesterpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.6288, -0.7495), 'High Wycombe PAT Testing', 'http://www.highwycombepat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(52.3861, -2.2469), 'Kidderminster PAT Testing', 'http://www.kidderminsterpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.8797, -0.4176), 'Luton PAT Testing', 'http://www.lutonpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.5228, -0.7202), 'Maidenhead PAT Testing', 'http://www.maidenheadpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(52.0353, -0.7042), 'Milton Keynes PAT Testing', 'http://www.miltonkeynespat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.4004, -1.3219), 'Newbury PAT Testing', 'http://www.newburypat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.5877, -2.9983), 'Newport PAT Testing', 'http://www.newportpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(52.2369, -0.8974), 'Northampton PAT Testing', 'http://www.northamptonpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.7523, -1.2558), 'Oxford PAT Testing', 'http://www.oxfordpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(50.3704, -4.1427), 'Plymouth PAT Testing', 'http://www.plymouthpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(50.7194, -1.9811), 'Poole PAT Testing', 'http://www.poolepat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(50.7989, -1.0912), 'Portsmouth PAT Testing', 'http://www.portsmouthpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(52.3066, -1.9440), 'Redditch PAT Testing', 'http://www.redditchpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.4550, -0.9691), 'Reading PAT Testing', 'http://www.readingpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(52.3732, -1.2617), 'Rugby PAT Testing', 'http://www.rugbypat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.0674, -1.7976), 'Salisbury PAT Testing', 'http://www.salisburypat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.5093, -0.5954), 'Slough PAT Testing', 'http://www.sloughpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(50.9050, -1.4032), 'Southampton PAT Testing', 'http://www.southamptonpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.7470, -2.2153), 'Stroud PAT Testing', 'http://www.stroudpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.5584, -1.7820), 'Swindon PAT Testing', 'http://www.swindonpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.0147, -3.1034), 'Taunton PAT Testing', 'http://www.tauntonpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(50.4518, -3.5579), 'Torbay PAT Testing', 'http://www.torbaypat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.3476, -2.9793), 'Weston-super-Mare PAT Testing', 'http://www.westonsupermarepat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.3168, -0.5591), 'Woking PAT Testing', 'http://www.wokingpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(52.1920, -2.2235), 'Worcester PAT Testing', 'http://www.worcesterpat-testing.co.uk/', '0800 612 6214'));

    map.addOverlay(createMarker(new GLatLng(51.2469, -0.7662), 'Aldershot PAT Testing', 'http://www.aldershotpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.2093, -1.4784), 'Andover PAT Testing', 'http://www.andoverpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(52.0600, -1.3404), 'Banbury PAT Testing', 'http://www.banburypat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.0806, -4.0582), 'Barnstaple PAT Testing', 'http://www.barnstaplepat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.4170, -0.7465), 'Bracknell PAT Testing', 'http://www.bracknellpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.1276, -3.0040), 'Bridgwater PAT Testing', 'http://www.bridgwaterpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(52.0560, -2.7175), 'Hereford PAT Testing', 'http://www.herefordpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(52.2920, -1.5358), 'Leamington Spa PAT Testing', 'http://www.leamingtonspapat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(50.6088, -2.4541), 'Weymouth PAT Testing', 'http://www.weymouthpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.0629, -1.3165), 'Winchester PAT Testing', 'http://www.winchesterpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.4110, -0.8349), 'Wokingham PAT Testing', 'http://www.wokinghampat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(50.9438, -2.6297), 'Yeovil PAT Testing', 'http://www.yeovilpat-testing.co.uk/', '0800 612 6214'));

    map.addOverlay(createMarker(new GLatLng(51.6702, -1.2849), 'Abingdon PAT Testing', 'http://www.abingdonpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.1717, -1.7789), 'Amesbury PAT Testing', 'http://www.amesburypat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.9022, -1.1542), 'Bicester PAT Testing', 'http://www.bicesterpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.3508, -2.2492), 'Bradford-on-Avon PAT Testing', 'http://www.bradford-on-avonpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.4353, -2.0027), 'Calne PAT Testing', 'http://www.calnepat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.4611, -2.1194), 'Chippenham PAT Testing', 'http://www.chippenhampat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.7142, -1.9665), 'Cirencester PAT Testing', 'http://www.cirencesterpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.4311, -2.1857), 'Corsham PAT Testing', 'http://www.corshampat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.3519, -1.9947), 'Devizes PAT Testing', 'http://www.devizespat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.6062, -1.2402), 'Didcot PAT Testing', 'http://www.didcotpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.2288, -2.3228), 'Frome PAT Testing', 'http://www.fromepat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.5848, -2.0995), 'Malmesbury PAT Testing', 'http://www.malmesburypat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.4210, -1.7302), 'Marlborough PAT Testing', 'http://www.marlboroughpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.3735, -2.1378), 'Melksham PAT Testing', 'http://www.melkshampat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.2404, -1.6643), 'Tidworth PAT Testing', 'http://www.tidworthpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.3201, -2.2080), 'Trowbridge PAT Testing', 'http://www.trowbridgepat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.5884, -1.4257), 'Wantage PAT Testing', 'http://www.wantagepat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.2049, -2.1812), 'Warminster PAT Testing', 'http://www.warminsterpat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.2602, -2.1855), 'Westbury PAT Testing', 'http://www.westburypat-testing.co.uk/', '0800 612 6214'));
    map.addOverlay(createMarker(new GLatLng(51.7837, -1.4855), 'Witney PAT Testing', 'http://www.witneypat-testing.co.uk/', '0800 612 6214'));
}

function createMarker(point, siteName, siteUrl, siteTel)
{
    // Set up our GMarkerOptions object
    var markerOptions = { icon:baseIcon };
    var marker = new GMarker(point, markerOptions);

    GEvent.addListener(marker, "click", function()
    {
        marker.openInfoWindowHtml("<p>" + siteName + "<br /><br />Tel: <strong>" + siteTel + "</strong><br /><br />Visit their website <a href='" + siteUrl + "' target='_blank' title='Visit " + siteName + "'>here</a>.</p>");
    });
    
    return marker;
}

function usePointFromPostcode(postcode)
{
    if (postcode != "")
    {
        // Clear all markers initially
        map.clearOverlays();

        // Re-add branch markers
        addBranchMarkers();

        // Hide map key
        document.getElementById("mapKey").style.visibility = "hidden";

        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);
		        placeMarkerAtPoint(point);

		        // Show map key
		        document.getElementById("mapKey").style.visibility = "";
		    }
		    else
		    {
		        alert("Postcode not found!");
		    }
		});

        localSearch.execute(postcode + ", UK");
    }
    else
        alert("Postcode is required.");
}

function placeMarkerAtPoint(point)
{
    // Create icon and mark the users postcode
    var icon = new GIcon();
    icon.image = "http://www.google.com/mapfiles/marker.png";
    icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    icon.iconSize = new GSize(20, 34);
    icon.shadowSize = new GSize(37, 34);
    icon.iconAnchor = new GPoint(10, 34);

    var marker = new GMarker(point, icon);
    map.addOverlay(marker);

    // Center map and zoom
    map.setCenter(point, 10);
}

function mapUnLoad()
{
    GUnload()
}

if (window.addEventListener)
    window.addEventListener("load", mapLoad, false);
else if (window.attachEvent)
    window.attachEvent("onload", mapLoad);

if (window.addEventListener)
    window.addEventListener("unload", mapLoad, false);
else if (window.attachEvent)
    window.attachEvent("onunload", mapLoad);
