﻿
// google map api for http://chris-marine.24hr.se
// ABQIAAAAgwrd-yLHWnaJ92q8sB6lKBQVlLxY5yLu-QNnWOqF6OVX0TbKwBTHy_M9I4brDsYG7WHrPWeVhd75bQ

$(document).ready(function(){
    $(".thumbnails>span>img").each(function(){
        $(this).click(switch_img);
    });
});


function switch_img()
{
    $(".thumbnails>span").each(function(){
        $(this).attr('class', '');
    });

    if (typeof productdata != 'undefined')
    {
        $(".producttext").html(productdata[this.id].text);
        $(".topimage>img").attr( "src", productdata[this.id].image );
        $(this).parent().attr('class', 'chosen');
        var imgid = this.id;
        $(".topimage>img").each(function(){
            $(this).unbind('click');
            $(this).click(function(){
                window.open(basepath + 'ProductImageDisplay.aspx?image=' + productdata[imgid].fullimage, 'Imageviewer', 'height=' + productdata[imgid].fullimageheight + ',width=' + productdata[imgid].fullimagewidth + ',scrollbars=1,resizable=1');
            });
        });
    }
}


function createMarker(point, url, name, icon)
{
    //var marker = new GMarker(point);
    var marker = new GMarker(point, icon());
    GEvent.addListener(marker, "click", function() {
                var html = '<a href="' + url + '">' + name + '</a>';
                marker.openInfoWindowHtml(html);
            });
    return marker;
}

function getZoom(north, south, east, west, minlevel)
{
    if (typeof minlevel == 'undefined')
        minlevel = 5;
    var distance = 3958.75 * Math.acos(Math.sin(north/57.2958) * Math.sin(south/57.2958) + Math.cos(north/57.2958) * Math.cos(south/57.2958) * Math.cos(east/57.2958 - west/57.2958));
    if (distance < 0.2 && minlevel >= 16)
        return 16;
    if (distance < 0.5 && minlevel >= 15)
        return 15;
    if (distance < 1 && minlevel >= 14)
        return 14;
    if (distance < 2 && minlevel >= 13)
        return 13;
    if (distance < 3 && minlevel >= 12)
        return 12;
    if (distance < 7 && minlevel >= 11)
        return 11;
    if (distance < 15 && minlevel >= 10)
        return 10;
    if (distance < 25 && minlevel >= 9)
        return 9;
    if (distance < 120 && minlevel >= 8)
        return 8;
    if (distance < 240 && minlevel >= 7)
        return 7;
    if (distance < 450&& minlevel >= 6)
        return 6;
    if (distance < 1000 && minlevel >= 5)
        return 5;
    if (distance < 1600 && minlevel >= 4)
        return 4;
    if (distance < 2200 && minlevel >= 3)
        return 3;
    if (distance < 2600 && minlevel >= 2)
        return 2;
    if (distance < 3200 && minlevel >= 1)
        return 1;
    return 1;
}

function runMap(companies)
{
    if (GBrowserIsCompatible() && companies.length > 0) {
        var map = new GMap2(document.getElementById("map"));
        map.setUIToDefault();
        var north = 0;
        var south = 0;
        var west = 0;
        var east = 0;
        
        var blueIcon = new GIcon(G_DEFAULT_ICON);
        blueIcon.image = "/Files/images/blue-dot.png";
        blueIcon.shadow = '/Files/images/shadow2.png';
        blueIcon.iconSize = new GSize(32, 32);
        blueIcon.shadowSize = new GSize(48, 32);
        blueIcon.iconAnchor = new GPoint(15, 32);
        blueIcon.infoWindowAnchor = new GPoint(15, 1)
        
        var redIcon = new GIcon(G_DEFAULT_ICON);
        redIcon.image = "/Files/images/red-dot.png";
        redIcon.shadow = '/Files/images/shadow2.png';
        redIcon.iconSize = new GSize(32, 32);
        redIcon.shadowSize = new GSize(48, 32);
        redIcon.iconAnchor = new GPoint(15, 32);
        redIcon.infoWindowAnchor = new GPoint(15, 1);


        
        
        for (var i in companies)
        {
            if (north == 0 || companies[i].latitude > north)
                north = companies[i].latitude * 1.0;
            if (south == 0 || companies[i].latitude < south)
                south = companies[i].latitude * 1.0;
            if (west == 0 || companies[i].longitude < west)
                west = companies[i].longitude * 1.0;
            if (east == 0 || companies[i].longitude > east)
                east = companies[i].longitude * 1.0;
                
            var point = new GLatLng( companies[i].latitude, companies[i].longitude );
            var marker = createMarker(point, companies[i].url, companies[i].name, function() { return companies[i].representative == "True" ? { icon: blueIcon } : { icon: redIcon }; } );
           
            map.addOverlay(marker);
        }

        var latitude = south + (north-south)/2.0;
        var longitude = west + (east-west)/2.0;
        var zoom = map.getBoundsZoomLevel( new GLatLngBounds( new GLatLng(south, west), new GLatLng(north, east) ) );
        if (zoom < 1)
            zoom = 1;
        map.setCenter(new GLatLng(latitude, longitude), zoom )
    }
}