﻿// SEKO Locations Google Map

$(document).ready(function() {

    var map;

    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(10, 10), 2);

        var options = { minZoom: 1, maxZoom: 7 };
        custommap = GMapBox('world-light', 'W. Light', options);
        map.addMapType(custommap);
        map.setMapType(custommap);

        var customUI = map.getDefaultUI();
        customUI.controls.scalecontrol = false;
        customUI.maptypes.normal = false;
        customUI.maptypes.satellite = false;
        customUI.maptypes.hybrid = false;
        customUI.maptypes.physical = false;
        map.setUI(customUI);

        var customIcon = new GIcon(G_DEFAULT_ICON);
        customIcon.image = "/images/map_marker_tight.png";
        customIcon.iconSize = new GSize(10, 10);
        customIcon.iconAnchor = new GPoint(4, 8);
        customIcon.shadow = '';

        GDownloadUrl("/map/locations.ashx", function(data) {
            var xml = GXml.parse(data);
            var markers = xml.documentElement.getElementsByTagName("marker");
            for (var i = 0; i < markers.length; i++) {
                createMarker(markers[i]);
            }
        });

        function createMarker(location) {
            var latlng = new GLatLng(parseFloat(location.getAttribute("lat")), parseFloat(location.getAttribute("lng")));
            var marker = new GMarker(latlng, { icon: customIcon });
            map.addOverlay(marker);
            GEvent.addListener(marker, "mouseover", function() {
            var template = "<div class='map_bubble'><div class='bubble_txt'><h4>{1}</h4><p class='timezone'>{2} / {3}<p class='address'><span>Address:</span><br />{4}</p>{8}<p class='numbers'>T: {5}<br />F: {6}</p><p class='contact'><span>Contact:</span><br />{7}</p></div><img src='{0}'></div>";
                var html = String.format(template, location.getAttribute("image"), location.getAttribute("name"), location.getAttribute("airport"), location.getAttribute("timezone"), location.getAttribute("address"), location.getAttribute("telephone"), location.getAttribute("fax"), location.getAttribute("contact"), location.getAttribute("link"));
                marker.openInfoWindowHtml(htmlDecode(html));
            });
        }
    }

    function moveMapTo(continent) {
        switch (continent) {
            case 'europe':
                map.setCenter(new GLatLng(51.890053935216926, 21.09375), 4);
                break;
            case 'namerica':
                map.setCenter(new GLatLng(49.837982453084834, -101.07421875), 3);
                break;
            case 'samerica':
                map.setCenter(new GLatLng(-25.641526373065755, -55.72265625), 3);
                break;
            case 'africa':
                map.setCenter(new GLatLng(3.6888551431470478, 26.3671875), 3);
                break;
            case 'asia':
                map.setCenter(new GLatLng(36.4566360115962, 88.76953125), 3);
                break;
            case 'australia':
                map.setCenter(new GLatLng(-26.11598592533351, 146.07421875), 3);
                break;
            default:
                break;
        }
    }

    String.format = function() {
        var s = arguments[0];
        for (var i = 0; i < arguments.length - 1; i++) {
            var reg = new RegExp("\\{" + i + "\\}", "gm");
            s = s.replace(reg, arguments[i + 1]);
        }
        return s;
    }

    function htmlDecode(value) {
        return value.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>');
    }

    $('a#jump').click(function() {
        $('#continents').slideToggle('normal', function() {
            $('#continents a#jump').toggleClass('open');
        });
        return false;
    });

    $('#continents').mouseleave(function() {
        $('#continents').slideUp();
        $('#continents a#jump').removeClass('open');
    });

    $('#continents li a').click(function() {
        var continent = $(this).attr('id');
        moveMapTo(continent);
        return false;
    });

    $('#map_canvas > div:nth-child(3)').addClass('mapbox');

});
