﻿// 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;
var markerList = null;
function load(lat, lng) {
    try {
        if (GBrowserIsCompatible() && lat != "0" && lng != "0") {

            map = new GMap2(document.getElementById("map"));

            var point = new GLatLng(lat, lng);
            //alert([point,lat,lng]);
            map.setCenter(point, 16);
            map.enableScrollWheelZoom();

            var zoom = new GSmallMapControl();
            map.addControl(zoom);

            var mapControl = new GMapTypeControl();
            map.addControl(mapControl);

            markerList = null;
            //map.setZoom(7);
        }
    }
    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, postcode, 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();
        if (type == 23 || type == 24) {
            iconPTSmall.image = _siteRoot + "/JS/Images/ProfileBaloonSmall.png";
        }
        else if (type == 27) {
            iconPTSmall.image = _siteRoot + "/JS/Images/ABCBaloonSmall.png";
        }
        else if (type == 28) {
            iconPTSmall.image = _siteRoot + "/JS/Images/123BaloonSmall.png";
        }
        else if (type == 29) {
            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";
        }

        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);

        if (markerList == null)
            markerList = new Array();
        markerList.push(marker);    
           
        
        var shopInfo = '<div><div style="font-size:14px;"><b>' + shopName + '</b></div>';
        shopInfo += '<div style="font-size:11px">' + shopAddress + '</div>';
        shopInfo += '<div style="font-size:11px"><b>' + postcode + '</b></div></div>';
        if (selected == false) {
            shopInfo += '<div style="font-size:11px"><a href="' + shopLink + '">Details</a></div></div>';
        }
        if (map != null) {
            map.addOverlay(marker);
        }
        marker.shopInfo = shopInfo;
        marker.shopid = getShopIdFromLink(shopLink);

        GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(shopInfo); });
    }
}

function getShopIdFromLink(s) {
    var lowerSiteRoot = _siteRoot.toLowerCase();
    var link = s.toLowerCase();
    link = link.replace(lowerSiteRoot, "");
    link = link.replace("default.aspx?shopdetails=true&shopid=", "");
    link = link.replace("/", "");
    link = link.replace("/", "");
    return link;
}

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); });
    }
}

