﻿var map;
var storeNames = new Array('christchurch', 'dunedin', 'alexandra', 'barbadoes', 'bishopdale', 'citycentre', 'ferrymead', 'foodlands', 'hornby', 'kaikoura', 'moorehouse', 'newbrighton', 'queenstown', 'rangiora', 'rolleston', 'shirley', 'timaru', 'towerjunction', 'woolston');
var imgStoreMarker = "/images/locations/marker.png";
var imgStoreMarkerShadow = "/images/locations/markerShadow.png";

if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
    imgStoreMarker = "/images/locations/marker.gif";
    imgStoreMarkerShadow = "/images/blank.gif";
}

var storeArray = new Array();
storeArray['christchurch'] = { 'type': 'region', 'location': 'christchurch', 'name': 'Christchurch', 'address': '', 'phone': '', 'lat': '-43.531625', 'lng': '172.536628', 'offsetLat': '0', 'offsetLng': '0.005', 'zoom': '10' };
storeArray['dunedin'] = { 'type': 'region', 'location': 'dunedin', 'name': 'Dunedin', 'address': '', 'phone': '', 'lat': '-45.87456', 'lng': '170.503388', 'offsetLat': '0', 'offsetLng': '0.009', 'zoom': '13' };
storeArray['alexandra'] = { 'type':'store', 'location':'alexandra', 'name': 'Alexandra', 'address': '78-80 Centennial Avenue<br />Alexandra', 'phone': '03 448 7278', 'lat': '-45.2513390', 'lng': '169.3878480' };
storeArray['barbadoes'] = { 'type': 'store', 'location': 'christchurch', 'name': 'Barbadoes Street', 'address': '182 Barbadoes Street<br />Christchurch', 'phone': '03 366 6624', 'lat': '-43.535895', 'lng': '172.645911' };
storeArray['bishopdale'] = { 'type': 'store', 'location': 'christchurch', 'name': 'Bishopdale', 'address': '333 Harewood Road<br />Christchurch', 'phone': '03 359 6690', 'lat': '-43.4878030', 'lng': '172.5868450' };
storeArray['citycentre'] = { 'type': 'store', 'location': 'dunedin', 'name': 'City Centre', 'address': 'Corner Hanover & Great King Streets<br />Dunedin', 'phone': '03 477 4433', 'lat': '-45.870481', 'lng': '170.507791' };
storeArray['ferrymead'] = { 'type': 'store', 'location': 'christchurch', 'name': 'Ferrymead', 'address': 'Waterman Place<br />Christchurch', 'phone': '03 376 4167', 'lat': '-43.5555220', 'lng': '172.6974860' };
storeArray['foodlands'] = { 'type': 'store', 'location': 'dunedin', 'name': 'Foodlands', 'address': '185 Kaikorai Valley Road<br />Dunedin', 'phone': '03 453 5575', 'lat': '-45.8701110', 'lng': '170.4743720' };
storeArray['hornby'] = { 'type': 'store', 'location': 'christchurch', 'name': 'Hornby', 'address': '21 Shands Road<br />Christchurch', 'phone': '03 982 8992', 'lat': '-43.5454290', 'lng': '172.5247790' };
storeArray['kaikoura'] = { 'type': 'store', 'location': 'kaikoura', 'name': 'Kaikoura', 'address': '98 Edmund Avenue<br />Kaikoura', 'phone': '03 319 7919', 'lat': '-42.3916811', 'lng': '173.6801554' };
storeArray['moorehouse'] = { 'type': 'store', 'location': 'christchurch', 'name': 'Moorhouse', 'address': '305 Moorhouse Avenue<br />Christchurch', 'phone': '03 377 1160', 'lat': '-43.5397250', 'lng': '172.6390900' };
storeArray['newbrighton'] = { 'type': 'store', 'location': 'christchurch', 'name': 'New Brighton', 'address': '655 Pages Road<br />Christchurch', 'phone': '03 382 5026', 'lat': '-43.5094290', 'lng': '172.7202200' };
storeArray['queenstown'] = { 'type': 'store', 'location': 'queenstown', 'name': 'Queenstown', 'address': '6 &ndash; 10 Shotover Street<br />Queenstown', 'phone': '03 442 8750', 'lat': '-45.0305130', 'lng': '168.6612580' };
storeArray['rangiora'] = { 'type': 'store', 'location': 'rangiora', 'name': 'Rangiora', 'address': '16 Southbrook Road<br />Rangiora', 'phone': '03 313 6684', 'lat': '-43.3238450', 'lng': '172.5987200' };
storeArray['rolleston'] = { 'type': 'store', 'location': 'christchurch', 'name': 'Rolleston', 'address': 'Unit 2, Rolleston Drive<br />Christchurch', 'phone': '03 347 3025', 'lat': '-43.596852', 'lng': '172.383468' };
storeArray['shirley'] = { 'type': 'store', 'location': 'christchurch', 'name': 'Shirley', 'address': '199 Marshlands Road<br />Christchurch', 'phone': '03 385 5740', 'lat': '-43.493561', 'lng': '172.661133' };
storeArray['timaru'] = { 'type': 'store', 'location': 'timaru', 'name': 'Timaru', 'address': 'Corner Wai-iti Road & Sealy Street<br />Timaru', 'phone': '03 688 4186', 'lat': '-44.3904000', 'lng': '171.2271810' };
storeArray['towerjunction'] = { 'type': 'store', 'location': 'christchurch', 'name': 'Tower Junction', 'address': '55 Clarence Street<br />Christchurch', 'phone': '03 982 8990', 'lat': '-43.5397817', 'lng': '172.6061224' };
storeArray['woolston'] = { 'type': 'store', 'location': 'christchurch', 'name': 'Woolston', 'address': '8 Portman Street<br />Christchurch', 'phone': '03 982 3778', 'lat': '-43.5484030', 'lng': '172.6822250' };

function displayIndividualStore(storeName) {
    addLinkHighlight(storeName);
    var myLatlng = new google.maps.LatLng(storeArray[storeName]['lat'], storeArray[storeName]['lng']);
    var myOptions = { zoom: 16, center: myLatlng, mapTypeControl: true, mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU }, navigationControl: true, navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL }, mapTypeId: google.maps.MapTypeId.ROADMAP};

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var companyLogo = new google.maps.MarkerImage(imgStoreMarker, new google.maps.Size(100, 50), new google.maps.Point(0, 0), new google.maps.Point(50, 50));
    var companyShadow = new google.maps.MarkerImage(imgStoreMarkerShadow, new google.maps.Size(140, 50), new google.maps.Point(0, 0), new google.maps.Point(50, 50));
    var companyMarker = new google.maps.Marker({ position: myLatlng, map: map, icon: companyLogo, shadow: companyShadow, title: "View details of Henry's, " + storeArray[storeName]['name'] });
    //var companyMarker = new google.maps.Marker({ position: myLatlng, map: map, title: "View details of Henry's, " + storeArray[storeName]['name'] });

    var contentString = '<div id="content">' +
            '<h2 id="firstHeading" class="firstHeading">Henry\'s &ndash; ' + storeArray[storeName]['name'] + '</h2>' + '<div id="bodyContent">' + '<p>' + storeArray[storeName]['address'] + '<br />Ph: ' + storeArray[storeName]['phone'] + '</p>' + '</div></div>';

    var infowindow = new google.maps.InfoWindow({ content: contentString });
    google.maps.event.addListener(companyMarker, 'mouseover', function () {
        infowindow.open(map, companyMarker);
    });

}

function loadSthIslandMap() {
    var myLatlng = new google.maps.LatLng(-44.0977190, 170.8288830);
    var myOptions = { zoom: 6, center: myLatlng, mapTypeControl: true, mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU }, navigationControl: true, navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL }, mapTypeId: google.maps.MapTypeId.ROADMAP };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    setTimeout('loadSthIslandLocations()', 2000);
    
    }

    function loadSthIslandLocations() {
    var southIslandMap = new Array('christchurch', 'dunedin', 'alexandra', 'kaikoura', 'queenstown', 'rangiora', 'timaru');
    for (var i = 0; i < southIslandMap.length; i++) {
        var storeLatlng = new google.maps.LatLng(storeArray[southIslandMap[i]]['lat'], storeArray[southIslandMap[i]]['lng']);
        var companyLogo = new google.maps.MarkerImage(imgStoreMarker, new google.maps.Size(100, 50), new google.maps.Point(0, 0), new google.maps.Point(50, 50));
        var companyShadow = new google.maps.MarkerImage(imgStoreMarkerShadow, new google.maps.Size(140, 50), new google.maps.Point(0, 0), new google.maps.Point(50, 50));
        var companyMarker = new google.maps.Marker({ position: storeLatlng, map: map, icon: companyLogo, shadow: companyShadow, title: "View details of Henry's, " + storeArray[southIslandMap[i]]['name'], id: southIslandMap[i] });
        google.maps.event.addListener(companyMarker, 'click', function () {

            switch (this.id) {
                case 'dunedin':
                    cityStores(this.id);
                    break;
                case 'christchurch':
                    cityStores(this.id);
                    break;
                default:
                    displayIndividualStore(this.id);
                    break;
            }
        });
    }
    addLinkHighlight("southisland"); //jquery css styling of list of links
}


function cityStores(location) {

    var myLatlng = new google.maps.LatLng(eval(storeArray[location]['lat'] - storeArray[location]['offsetLat']), eval(storeArray[location]['lng'] - storeArray[location]['offsetLng']));
    var myOptions = { zoom: eval(storeArray[location]['zoom']), center: myLatlng, mapTypeControl: true, mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU }, 
        navigationControl: true, navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL }, mapTypeId: google.maps.MapTypeId.ROADMAP};
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    for (var i = 0; i < storeNames.length; i++) {
        if (storeArray[storeNames[i]]['location'] == location && storeArray[storeNames[i]]['type'] == 'store') {
            var storeLatlng = new google.maps.LatLng(storeArray[storeNames[i]]['lat'], storeArray[storeNames[i]]['lng']);
            var companyLogo = new google.maps.MarkerImage(imgStoreMarker, new google.maps.Size(100, 50), new google.maps.Point(0, 0), new google.maps.Point(50, 50));
            var companyShadow = new google.maps.MarkerImage(imgStoreMarkerShadow, new google.maps.Size(140, 50), new google.maps.Point(0, 0), new google.maps.Point(50, 50));

            var companyMarker = new google.maps.Marker({ position: storeLatlng, map: map, icon: companyLogo, shadow: companyShadow, title: "View details of Henry's, " + storeArray[storeNames[i]]['name'], id: storeNames[i] });
            google.maps.event.addListener(companyMarker, 'click', function () {
               displayIndividualStore(this.id);
            });
        }
   }
   addLinkHighlight(location);

}

function addLinkHighlight(aID) {
    $("#storeLinks a").removeClass("active");
    $("#storeLinks a#"+aID).addClass("active");
}


$(document).ready(function () {

   $("#storeLinks a").click(function (e) {
        e.preventDefault();
        addLinkHighlight($(this).attr("id"));
        switch ($(this).attr("id")) {
            case "southisland":
                loadSthIslandMap();
                break;
            case "christchurch":
                cityStores($(this).attr("id"));
                break;
            case "dunedin":
                cityStores($(this).attr("id"));
                break;
            default:
                displayIndividualStore($(this).attr("id"));
                break;
        }
    });
});

window.onload = loadSthIslandMap; 