﻿// Google Maps 3.0

var Maps = {};

Maps.map = null;
Maps.directionsService = null;
Maps.directionsDisplay = null;
// We save each marker of the shop here
Maps.markersArray = [];
// Counter for the markerArray
Maps.indexMarkers = 0;

Maps.Init = function() {
    Maps.directionsDisplay = new google.maps.DirectionsRenderer();
    var latlng = new google.maps.LatLng(0.00, 0.00);
    var myOptions = {
        zoom: 20,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    Maps.map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    Maps.directionsDisplay.setMap(Maps.map);
    if (document.getElementById("directionsPanel") != null) {
        Maps.directionsDisplay.setPanel(document.getElementById("directionsPanel"));
    }
}

Maps.AddShop = function(lng, lat, shopTypeId) {
    if (Maps.map == null) {
        Maps.Init();
    }

    var loc = new google.maps.LatLng(lat, lng);
    Maps.map.center = loc;
    Maps.map.zoom = 15;

    var iconPT = '';
    
    if (shopTypeId == 23 || shopTypeId == 24) {
        iconPT = _siteRoot + "/JS/Images/ProfileBaloonSmall.png";
        newShopName = 'Profile Tyrecenter';
    }
    else if (shopTypeId == 27) {
        newShopName = "abc Autoservice";
        iconPT = _siteRoot + "/JS/Images/ABCBaloonSmall.png";
    }
    else if (shopTypeId == 28) {
        newShopName = "JAMES Lease Service ";
        iconPT = _siteRoot + "/JS/Images/JamesBaloonSmall.png";
    }
    else if (shopTypeId == 29) {
        newShopName = "JAMES Auto <span style='font-family:Times New Roman'>&amp;</span> Service";
        iconPT = _siteRoot + "/JS/Images/JamesBaloonSmall.png";
    }
    else if (shopTypeId == 35) {
        iconPT = _siteRoot + "/JS/Images/PassionBaloonSmall.png";
    }
    else if (shopTypeId == 36) {
        iconPT = _siteRoot + "/JS/Images/DecondeBaloonSmall.png";
    }
    else if (shopTypeId == 39) {
        iconPT = _siteRoot + "/JS/Images/ZBaloonSmall.png";
    }
    else
        if (shopTypeId == 40) {
        iconPT = _siteRoot + "/JS/Images/AutoglazBaloonSmall.png";
    }

    marker = new google.maps.Marker({
        map: Maps.map,
        icon: iconPT,
        draggable: false,
        animation: google.maps.Animation.DROP,
        position: loc
    });
    google.maps.event.addListener(marker, 'click', toggleBounce);
}

Maps.AddShopDefaultIconA = function(lng, lat) {
    if (Maps.map == null) {
        Maps.Init();
    }

    var loc = new google.maps.LatLng(lat, lng);
    Maps.map.center = loc;
    Maps.map.zoom = 7;

    var iconPT = _siteRoot + "/JS/Images/Shop/icon_greenA.png";

    marker = new google.maps.Marker({
        map: Maps.map,
        icon: iconPT,
        draggable: false,
        animation: google.maps.Animation.DROP,
        position: loc
    });
    google.maps.event.addListener(marker, 'click', toggleBounce);
}

Maps.AddShopDefaultIconB = function(lng, lat) {
    if (Maps.map == null) {
        Maps.Init();
    }

    var loc = new google.maps.LatLng(lat, lng);
    Maps.map.center = loc;
    Maps.map.zoom = 7;

    var iconPT = _siteRoot + "/JS/Images/Shop/icon_greenB.png";

    marker = new google.maps.Marker({
        map: Maps.map,
        icon: iconPT,
        draggable: false,
        animation: google.maps.Animation.DROP,
        position: loc
    });
    google.maps.event.addListener(marker, 'click', toggleBounce);
}

Maps.AddShopForMap = function(lng, lat, shopName, shopOwner, shopCity, shopAdress, shopHouseNumber, ShopPostcode, shopID, shopTel, shopImage, shopType, shopTypeId) {
    if (Maps.map == null) {
        Maps.Init();
    }

    var loc = new google.maps.LatLng(lat, lng);
    Maps.map.center = loc;
    Maps.map.zoom = 8;

    var iconPT = '';
    var newShopName = '';

    if (shopTypeId == 23 || shopTypeId == 24) {
        iconPT = _siteRoot + "/JS/Images/ProfileBaloonSmall.png";
        newShopName = 'Profile Tyrecenter';
    }
    else if (shopTypeId == 27) {
        newShopName = "abc Autoservice";
        iconPT = _siteRoot + "/JS/Images/ABCBaloonSmall.png";
    }
    else if (shopTypeId == 28) {
        newShopName = "JAMES Lease Service";
        iconPT = _siteRoot + "/JS/Images/JamesBaloonSmall.png";
    }
    else if (shopTypeId == 29) {
        newShopName = "JAMES Auto <span style='font-family:Times New Roman'>&amp;</span> Service";
        iconPT = _siteRoot + "/JS/Images/JamesBaloonSmall.png";
    }
    else if (shopTypeId == 35) {
        iconPT = _siteRoot + "/JS/Images/PassionBaloonSmall.png";
    }
    else if (shopTypeId == 36) {
        iconPT = _siteRoot + "/JS/Images/DecondeBaloonSmall.png";
    }
    else if (shopTypeId == 39) {
        iconPT = _siteRoot + "/JS/Images/ZBaloonSmall.png";
    }
    else
        if (shopTypeId == 40) {
        iconPT = _siteRoot + "/JS/Images/AutoglazBaloonSmall.png";
    }

    var beginTable = '<table cellpadding="3" cellspacing="0" border="0">';
    var endTable = '</table>';
    var beginTd = '<td valing="top">';
    var endTd = '</td>';
    var beginTr = '<tr>';
    var endTr = '</tr>';
    var finalTable = '';

    finalTable += beginTable + beginTr + beginTd;

    var img = "<img alt=" + shopName + " src='" + shopImage + "' width='90px' border='0' />";

    finalTable += img;
    finalTable += endTd;

    newShopName += ' <b>' + shopName + '</b>';

    if (shopOwner == '') {
        finalTable += beginTd + "<span class='jamesBlue1 bold'>" + newShopName + "</span><br />" + shopAdress + " " + shopHouseNumber + "<br />" + ShopPostcode + " " + shopCity + "<br />" + "Tel: " + shopTel + "<br />" + "<a class='jamesBlue1' href='OnlinePlanning/OnlinePlanning.aspx?service=banden&shopid=" + shopID + "#onlinePlanning';'>Maak afspraak</a><br /><a href='ShopDirections.aspx?shopid=" + shopID + "'>Plan route</a>" + endTd + endTr + endTable;
    }
    else {
        finalTable += beginTd + "<span class='jamesBlue1 bold'>" + newShopName + "</span><br />" + shopOwner + "<br />" + shopAdress + " " + shopHouseNumber + "<br />" + ShopPostcode + " " + shopCity + "<br />" + "Tel: " + shopTel + "<br />" + "<a class='jamesBlue1' href='OnlinePlanning/OnlinePlanning.aspx?service=banden&shopid=" + shopID + "#onlinePlanning';'>Maak afspraak</a><br /><a href='ShopDirections.aspx?shopid=" + shopID + "'>Plan route</a>" + endTd + endTr + endTable;
    }

    var infowindow = new google.maps.InfoWindow({
        content: finalTable
    });

    var marker = new google.maps.Marker({
        map: Maps.map,
        icon: iconPT,
        draggable: false,
        animation: google.maps.Animation.DROP,
        position: loc
    });

    Maps.markersArray[Maps.indexMarkers] = marker;
    Maps.indexMarkers++;

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(Maps.map, marker);
    });
}

Maps.CalculateDistance = function(startLat, startLng, endLat, endLng) {
    Maps.directionsService = new google.maps.DirectionsService();
    var start = new google.maps.LatLng(startLng, startLat);
    var end = new google.maps.LatLng(endLng, endLat);
    var request = {
        origin: start,
        destination: end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    Maps.directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            Maps.directionsDisplay.setDirections(response);
        }
    });
}


// Animation for clicking the marker
function toggleBounce() {

    if (marker.getAnimation() != null) {
        marker.setAnimation(null);
    } else {
        marker.setAnimation(google.maps.Animation.BOUNCE);
    }
}

// Loads the map with all the shops
Maps.LoadMap = function () {
    $("#map_canvas").html('<div style="text-align:center;height:400px;"><img src="' + _appTheme + '/Images/OnlinePlanningImages/Loader/ajax-loader.gif" style="padding-top:200px;"/></div>');
    $.getJSON(_siteRoot + 'Ajax/ShopMap.aspx', function(data) {
        if (data != "null") {
            for (var i = 0; i < data.length; i++) {
                Maps.AddShopForMap(data[i].Lat,
                    data[i].Lng,
                    data[i].ShopName,
                    data[i].ShopOwner,
                    data[i].ShopCity,
                    data[i].ShopAdress,
                    data[i].ShopHouseNumber,
                    data[i].ShopPostcode,
                    data[i].ShopIDString,
                    data[i].ShopTelephone,
                    data[i].ShopImage,
                    data[i].ShopType,
                    data[i].ShopTypeId)
            }
        }
    });
}

// Loads the map with Profile shops
Maps.LoadBanden = function () {
    $.getJSON(_siteRoot + 'Ajax/ShopMap.aspx?service=banden', function(data) {
        if (data != "null") {
            for (var i = 0; i < data.length; i++) {
                Maps.AddShopForMap(data[i].Lat,
                    data[i].Lng,
                    data[i].ShopName,
                    data[i].ShopOwner,
                    data[i].ShopCity,
                    data[i].ShopAdress,
                    data[i].ShopHouseNumber,
                    data[i].ShopPostcode,
                    data[i].ShopIDString,
                    data[i].ShopTelephone,
                    data[i].ShopImage,
                    data[i].ShopType,
                    data[i].ShopTypeId)
            }
        }
    });
}

// Loads the map with Banden and Zr certificate
Maps.LoadOnderhoud = function () {
    $.getJSON(_siteRoot + 'Ajax/ShopMap.aspx?service=onderhoud', function(data) {
        if (data != "null") {
            for (var i = 0; i < data.length; i++) {
                Maps.AddShopForMap(data[i].Lat,
                data[i].Lng,
                data[i].ShopName,
                data[i].ShopOwner,
                data[i].ShopCity,
                data[i].ShopAdress,
                data[i].ShopHouseNumber,
                data[i].ShopPostcode,
                data[i].ShopIDString,
                data[i].ShopTelephone,
                data[i].ShopImage,
                data[i].ShopType,
                data[i].ShopTypeId)
            }
        }
    });
}

// Clear all the markers from the map
Maps.ClearOverlays = function() {
    Maps.indexMarkers = 0;
    for (i in Maps.markersArray) {
        Maps.markersArray[i].setMap(null);
    }
}

$(window).ready(function() {
    $('#option1').click(function() {
        if ($('#option1').is(':checked')) {
            Maps.ClearOverlays();
            Maps.LoadBanden();
        }
        else {
            if ($('#option2').is(':checked') == false && $('#option1').is(':checked') == false) {
                Maps.ClearOverlays();
                Maps.LoadMap();
            }
        }
    });
    $('#option2').click(function() {
        if ($('#option2').is(':checked')) {
            Maps.ClearOverlays();
            Maps.LoadOnderhoud();
        }
        else {
            if ($('#option1').is(':checked') == false && $('option2').is(':checked') == false) {
                Maps.ClearOverlays();
                Maps.LoadMap();
            }
        }
    })
});

// End Google Maps 3.0




// shop types:
//  23 - profile NL
//  24 - profile BE
//  29 - james
//  27 - ABC
//  28 - 123
//  35 - Passion Pneu
//  36 - Banden De Conde

var map = null;
var directions = null;
var currentLocation = null;
MapDistance = null;
function load(lat, lng) {
    try {
        if (GBrowserIsCompatible() && lat != "0" && lng != "0") {

            map = new GMap2(document.getElementById("map"));

            var point = new GLatLng(lat, lng);
            map.setCenter(point, 16);
            map.enableScrollWheelZoom();

            var zoom = new GSmallMapControl();
            map.addControl(zoom);

            var mapControl = new GMapTypeControl();
            map.addControl(mapControl);
        }
    }
    catch (exception) {
        //alert(exception);
    }
}

function loadWithZoom(lat, lng,zoomLevel) {
    try {
        if (GBrowserIsCompatible() && lat != "0" && lng != "0") {

            map = new GMap2(document.getElementById("map"));

            var point = new GLatLng(lat, lng);
            map.setCenter(point, 16);
            map.enableScrollWheelZoom();

            var zoom = new GSmallMapControl();
            map.addControl(zoom);

            var mapControl = new GMapTypeControl();
            map.addControl(mapControl);

            map.setZoom(zoomLevel);

        }
    }
    catch (exception) {
        //alert(exception);
    }
}
function getRoadDistance(fromLat, fromLng, toLat, toLng) {
    var directions2 = new GDirections();
    GEvent.addListener(directions2, "load", function() { distLoad(directions2); });
    if (directions2 != null) {
        var a = "Your location@" + fromLng + "," + fromLat;
        var b = "Destination@" + toLng + "," + toLat;
        var qry = "from: " + a + " to: " + b;
        directions2.load(qry, { getPolyline: true, getSteps: true });
        return 1;
    }
    return -1;
}

function distLoad(dir) {
    MapDistance = dir.getDistance().meters;
}

function loadDirections(control) {
    directions = new GDirections(map, document.getElementById(control));
}

function loadRoute(fromLat, fromLng, toLat, toLng, locale) {
    if (directions != null) {
        var a = "Uw lokatie@" + fromLng + "," + fromLat;
        var b = "Bestemming@" + toLng + "," + toLat;
        var qry = "from: " + a + " to: " + b;
        directions.load(qry, { getPolyline: true, "locale": locale });
    }
}
function handleNoFlash(errorCode) {
    if (errorCode == 603) {
        alert("Error: Flash doesn't appear to be supported by your browser");
        return;
    }
}


function addShop(lat, lng, shopName, shopAddress, shopNumber, postcode, shopCity, phone, shopLink, selected, type) {
    if (lat != 0) {
        var iconOptions = {};
        iconOptions.width = 32;
        iconOptions.height = 32;
        if (selected == true) {
            iconOptions.primaryColor = "#00CCCC";
        }
        else {
            iconOptions.primaryColor = "#000CCC";
        }
        iconOptions.cornerColor = "#FFFFFF";
        iconOptions.strokeColor = "#000000";
        var icon = MapIconMaker.createMarkerIcon(iconOptions);

        var iconPT = new GIcon();
        iconPT.image = _siteRoot + "/JS/Images/PTCBaloon.png";
        iconPT.shadow = _siteRoot + "/JS/Images/PTCBaloonShadow.png";
        iconPT.iconSize = new GSize(25, 41);
        iconPT.shadowSize = new GSize(30, 12);
        iconPT.iconAnchor = new GPoint(12, 40);
        iconPT.infoWindowAnchor = new GPoint(12, 0);

        var iconPTSmall = new GIcon();
        var shop_type = "";
        if (type == 23 || type == 24) {
            shop_type = "Profile Tyrecenter";
            iconPTSmall.image = _siteRoot + "/JS/Images/ProfileBaloonSmall.png";
        }
        else if (type == 27) {
            shop_type = "abc Autoservice";
            iconPTSmall.image = _siteRoot + "/JS/Images/ABCBaloonSmall.png";
        }
        else if (type == 28) {
            shop_type = "1,2,3/// AutoService";
            iconPTSmall.image = _siteRoot + "/JS/Images/123BaloonSmall.png";
        }
        else if (type == 29) {
            shop_type = "James Autoservice";
            iconPTSmall.image = _siteRoot + "/JS/Images/JamesBaloonSmall.png";
        }
        else if (type == 35) {
            iconPTSmall.image = _siteRoot + "/JS/Images/PassionBaloonSmall.png";
        }
        else if (type == 36) {
            iconPTSmall.image = _siteRoot + "/JS/Images/DecondeBaloonSmall.png";
        }
        else if (type == 39) {
            iconPTSmall.image = _siteRoot + "/JS/Images/ZBaloonSmall.png";
        }
        else 
            if (type == 40) {
                iconPTSmall.image = _siteRoot + "/JS/Images/AutoglazBaloonSmall.png";
            }
        
        iconPTSmall.shadow = _siteRoot + "/JS/Images/PTCBaloonShadow.png";
        iconPTSmall.iconSize = new GSize(35, 41);
        iconPTSmall.shadowSize = new GSize(30, 12);
        iconPTSmall.iconAnchor = new GPoint(18, 41);
        iconPTSmall.infoWindowAnchor = new GPoint(2, 0);

        var point = new GLatLng(lat, lng);
        var marker;
        if (selected == true)
            marker = new GMarker(point, iconPT);
        else
            marker = new GMarker(point, iconPTSmall);

        var shopInfo = '<div><div style="font-size:14px;">' + shop_type + '  <b>' + shopName + '</b></div>';
        shopInfo += '<div style="font-size:11px">' + shopAddress +' ' + shopNumber + '</div>';
        shopInfo += '<div style="font-size:11px"><b>' + postcode + ' ' + shopCity + '</b></div>';
        shopInfo += '<div style="font-size:11px">' + phone + '</div></div>';
        //if (selected == false) {
        shopInfo += '<div style="font-size:11px"><a href="' + _siteRoot + "OnlinePlanning/OnlinePlanning.aspx?services=banden&shopid=" + shopLink + '#onlinePlanning">Maak afspraak</a></div></div>';
        shopInfo += '<div style="font-size:11px"><a href="' + _siteRoot + "ShopDirections.aspx?shopid=" + shopLink + '#onlinePlanning">Plan route</a></div></div>';
        //}
        if (map != null) {
            map.addOverlay(marker);
        }
        //marker.openInfoWindowHtml(shopInfo);

        GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(shopInfo); });
    }
}

function addPosition(lat, lng, text, postcode) {
    if (lat != 0) {
        var point = new GLatLng(lat, lng);
        var marker = new GMarker(point);
        var shopInfo = '<div><div style="font-size:14px;"><b>' + text + '</b></div>';
        shopInfo += '<div style="font-size:11px"><b>' + postcode + '</b></div></div>';
        if (map != null) {
            map.addOverlay(marker);
        }
        //marker.openInfoWindowHtml(shopInfo);
        currentLocation = new GLatLng(lat, lng);
        GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(shopInfo); });
    }
}

