﻿var map;
var directionsInfo;
var storeWindows;
var storeMarkers;
var localSearch = new GlocalSearch();

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);

function directions(fromPoint, toPoint) {
    map.clearOverlays();
    directionsPanel = document.getElementById("googleDrivingDirectionsText");
    if (directionsInfo == undefined) {
        directionsInfo = new GDirections(map, directionsPanel);
    }
    directionsInfo.load("from: " + fromPoint + " to: " + toPoint);
}

function usePointFromPostcode(postcode, toPoint, callbackFunction) {
    localSearch.setSearchCompleteCallback(null,
		function() {
		    if (localSearch.results[0]) {
		        document.getElementById("directionsError").style.display = 'none';
		        var resultLat = localSearch.results[0].lat;
		        var resultLng = localSearch.results[0].lng;
		        var point = new GLatLng(resultLat, resultLng);
		        callbackFunction(point, toPoint);
		    } else {
		        document.getElementById("directionsError").style.display = 'block';
		    }
		});
    localSearch.execute(postcode + ", UK");
}

function placeMarkerAtPoint(point) {
    var marker = new GMarker(point, icon);
    map.addOverlay(marker);
}

function setCenterToPoint(point) {
    map.setCenter(point, 17);
}

function showPointLatLng(point) {
    alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
}

function mapLoad() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
    }
}

function getStoreCoordinates() {
    var pt;
    if (document.getElementById("mapStoreBC")) //brighton
    {
        pt = new GLatLng(50.8247, -0.1526);
    }
    else if (document.getElementById("mapStoreBA")) //bristol
    {
        pt = new GLatLng(51.4529, -2.6001);
    }
    else if (document.getElementById("mapStoreTF")) //dublin
    {
        pt = new GLatLng(53.3425, -6.2625);
    }
    else if (document.getElementById("mapStoreAC")) //dundee
    {
        pt = new GLatLng(56.4604, -2.9684);
    }
    else if (document.getElementById("mapStoreAN")) //edinburgh
    {
        pt = new GLatLng(55.9503, -3.2091);
    }
    else if (document.getElementById("mapStoreBT")) //exeter
    {
        pt = new GLatLng(50.7271, -3.5236);
    }
    else if (document.getElementById("mapStoreUB")) //lincoln
    {
        pt = new GLatLng(53.2261, -0.5426);
    }
    else if (document.getElementById("mapStoreBP")) //london argents
    {
        pt = new GLatLng(51.5154, -0.1297);
    }
    else if (document.getElementById("mapStoreCD")) //london rose morris
    {
        pt = new GLatLng(51.515, -0.1296);
    }
    else if (document.getElementById("mapStoreAK")) //norwich
    {
        pt = new GLatLng(52.6308, 1.2907);
    }
    else if (document.getElementById("mapStoreTT")) //nottingham
    {
        pt = new GLatLng(52.9565, -1.1474);
    }
    else if (document.getElementById("mapStoreAL")) //poole
    {
        pt = new GLatLng(50.7182, -1.9825);
    }
    else if (document.getElementById("mapStoreZD")) //portsmouth
    {
        pt = new GLatLng(50.79906, -1.091215);
    }
    else if (document.getElementById("mapStoreUO")) //salisbury
    {
        pt = new GLatLng(51.0683, -1.7937);
    }
    else if (document.getElementById("mapStoreAP")) //stratford
    {
        pt = new GLatLng(52.1924, -1.7098);
    }
    else if (document.getElementById("mapStoreAR")) //truro
    {
        pt = new GLatLng(50.2626, -5.0541);
    }
    else if (document.getElementById("mapStoreTJ")) //york
    {
        pt = new GLatLng(53.9602, -1.0856);
    }
    else if (document.getElementById("mapStoreAX")) //Leeds
    {
        pt = new GLatLng(53.799548, -1.545821);
    }
    else if (document.getElementById("mapStoreZG")) //Hatfield
    {
        pt = new GLatLng(51.761347, -0.240381);
    }
    return pt;
}

function showStoreInfo(storeCode) {
    storeMarkers[storeCode].openInfoWindowHtml(storeWindows[storeCode]);
}

function load() {
    $('body').bind('keypress', function(e) {
        if (e.keyCode == 13) {
            e.preventDefault();
            $('#goDirections').click();
        }
    });

    if (document.getElementById("map")) {
        if (GBrowserIsCompatible()) {
            mapLoad();
            function createMarker(pt, html) {
                var marker = new GMarker(pt);
                GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });
                return marker;
            }

            var pt = getStoreCoordinates();

            if (pt) {
                map.setCenter(pt, 15);
                var mrkr = createMarker(pt, 'We are here')
                map.addOverlay(mrkr);
            }
            else {
                storeWindows = new Array();
                storeMarkers = new Array();
                map.setCenter(new GLatLng(54.6012, -2.2458), 5);

                // Set up markers with info windows
                pt = new GLatLng(50.8247, -0.1526);
                var markerHtml = '<div style="body">Musicroom Brighton<br /><a href="/brighton" class="map_icon">Store Information</a></div>';
                var marker = createMarker(pt, markerHtml);
                map.addOverlay(marker);
                storeWindows["BC"] = markerHtml;
                storeMarkers["BC"] = marker;

                pt = new GLatLng(51.4529, -2.6001);
                var markerHtml = '<div style="body">Musicroom Bristol<br /><a href="/bristol" class="map_icon">Store Information</a></div>';
                marker = createMarker(pt, markerHtml);
                map.addOverlay(marker);
                storeWindows["BA"] = markerHtml;
                storeMarkers["BA"] = marker;

                pt = new GLatLng(53.3428, -6.262);
                var markerHtml = '<div style="body">Dublin: McCullough Pigott<br /><a href="/dublin" class="map_icon">Store Information</a></div>';
                marker = createMarker(pt, markerHtml);
                map.addOverlay(marker);
                storeWindows["TF"] = markerHtml;
                storeMarkers["TF"] = marker;

                pt = new GLatLng(56.4604, -2.9684);
                var markerHtml = '<div style="body">Dundee: Kenny\'s Music<br /><a href="/dundee" class="map_icon">Store Information</a></div>';
                marker = createMarker(pt, markerHtml);
                map.addOverlay(marker);
                storeWindows["AC"] = markerHtml;
                storeMarkers["AC"] = marker;

                pt = new GLatLng(55.9503, -3.2091);
                var markerHtml = '<div style="body">Edinburgh: Rae Macintosh<br /><a href="/edinburgh" class="map_icon">Store Information</a></div>';
                marker = createMarker(pt, markerHtml);
                map.addOverlay(marker);
                storeWindows["AN"] = markerHtml;
                storeMarkers["AN"] = marker;

                pt = new GLatLng(50.7271, -3.5236);
                var markerHtml = '<div style="body">Musicroom Exeter<br /><a href="/exeter" class="map_icon">Store Information</a></div>';
                marker = createMarker(pt, markerHtml);
                map.addOverlay(marker);
                storeWindows["BT"] = markerHtml;
                storeMarkers["BT"] = marker;

                pt = new GLatLng(53.2261, -0.5426);
                var markerHtml = '<div style="body">Musicroom Lincoln<br /><a href="/lincoln" class="map_icon">Store Information</a></div>';
                marker = createMarker(pt, markerHtml)
                map.addOverlay(marker);
                storeWindows["UB"] = markerHtml;
                storeMarkers["UB"] = marker;

                pt = new GLatLng(51.5154, -0.1297);
                var markerHtml = '<div style="body">London: Argent\'s Music Shop<br /><a href="/argents" class="map_icon">Store Information</a><br/><br/>London: Rose Morris<br /><a href="/rosemorris" class="map_icon">Store Information</a></div>';
                marker = createMarker(pt, markerHtml);
                map.addOverlay(marker);
                storeWindows["BP"] = markerHtml;
                storeMarkers["BP"] = marker;
                storeWindows["CD"] = markerHtml;
                storeMarkers["CD"] = marker;

                pt = new GLatLng(52.6308, 1.2907);
                var markerHtml = '<div style="body">Musicroom Norwich<br /><a href="/norwich" class="map_icon">Store Information</a></div>';
                marker = createMarker(pt, markerHtml);
                map.addOverlay(marker);
                storeWindows["AK"] = markerHtml;
                storeMarkers["AK"] = marker;

                pt = new GLatLng(52.9565, -1.1474);
                var markerHtml = '<div style="body">Musicroom Nottingham<br /><a href="/nottingham" class="map_icon">Store Information</a></div>';
                marker = createMarker(pt, markerHtml);
                map.addOverlay(marker);
                storeWindows["TT"] = markerHtml;
                storeMarkers["TT"] = marker;

                pt = new GLatLng(51.0683, -1.7937);
                var markerHtml = '<div style="body">Musicroom Salisbury<br /><a href="/salisbury" class="map_icon">Store Information</a></div>';
                marker = createMarker(pt, markerHtml);
                map.addOverlay(marker);
                storeWindows["UO"] = markerHtml;
                storeMarkers["UO"] = marker;

                pt = new GLatLng(50.7182, -1.9825);
                var markerHtml = '<div style="body">Musicroom Poole<br /><a href="/poole" class="map_icon">Store Information</a></div>';
                marker = createMarker(pt, markerHtml);
                map.addOverlay(marker);
                storeWindows["AL"] = markerHtml;
                storeMarkers["AL"] = marker;

                pt = new GLatLng(52.1924, -1.7098);
                var markerHtml = '<div style="body">Musicroom Stratford<br /><a href="/stratford" class="map_icon">Store Information</a></div>';
                marker = createMarker(pt, markerHtml);
                map.addOverlay(marker);
                storeWindows["AP"] = markerHtml;
                storeMarkers["AP"] = marker;

                pt = new GLatLng(50.2626, -5.0541);
                var markerHtml = '<div style="body">Truro: City Music<br /><a href="/truro" class="map_icon">Store Information</a></div>';
                marker = createMarker(pt, markerHtml);
                map.addOverlay(marker);
                storeWindows["AR"] = markerHtml;
                storeMarkers["AR"] = marker;

                pt = new GLatLng(53.9602, -1.0856);
                var markerHtml = '<div style="body">York: Banks Music<br /><a href="/york" class="map_icon">Store Information</a></div>';
                marker = createMarker(pt, markerHtml);
                map.addOverlay(marker);
                storeWindows["TJ"] = markerHtml;
                storeMarkers["TJ"] = marker;

                pt = new GLatLng(50.79906, -1.091215);
                var markerHtml = '<div style="body">Musicroom Portsmouth<br /><a href="/portsmouth" class="map_icon">Store Information</a></div>';
                marker = createMarker(pt, markerHtml);
                map.addOverlay(marker);
                storeWindows["ZD"] = markerHtml;
                storeMarkers["ZD"] = marker;

                pt = new GLatLng(53.799548, -1.545821);
                var markerHtml = '<div style="body">Musicroom Leeds<br /><a href="/leeds" class="map_icon">Store Information</a></div>';
                marker = createMarker(pt, markerHtml);
                map.addOverlay(marker);
                storeWindows["AX"] = markerHtml;
                storeMarkers["AX"] = marker;

                pt = new GLatLng(51.761347, -0.240381);
                var markerHtml = '<div style="body">Musicroom Hatfield<br /><a href="/hatfield" class="map_icon">Store Information</a></div>';
                marker = createMarker(pt, markerHtml);
                map.addOverlay(marker);
                storeWindows["ZG"] = markerHtml;
                storeMarkers["ZG"] = marker;
            }
        }
        else {
            alert("Sorry, Google Maps is not compatible with this browser");
        }
    }
}
