﻿/// <reference path="GMAPJSHelper_Release.js" />

var DMap = {
    _map: null,
    _latitude: 0.0,
    _longitude: 0.0,
    _zoom: 1,
    _marker: null,
    _bounds: null,

    get_latitude: function() {
        return DMap._latitude;
    },

    set_latitude: function(value) {
        DMap._latitude = value;
    },

    get_longitude: function() {
        return DMap._longitude;
    },

    set_longitude: function(value) {
        DMap._longitude = value;
    },

    get_zoom: function() {
        return DMap._zoom;
    },

    set_zoom: function(value) {
        DMap._zoom = value;
    },


    init: function(map) {
        DMap._map = new GMap2(map);
        DMap._map.setMapType(G_NORMAL_MAP);
        //DMap._map.enableScrollWheelZoom();
        //DMap._map.addControl(new GLargeMapControl3D());
        DMap._map.enableDoubleClickZoom();
        DMap._map.enableContinuousZoom();
        DMap._bounds = new GLatLngBounds();
        //var point = new GLatLng(DMap.get_latitude(), DMap.get_longitude());

        //DMap._marker = new GMarker(point);
        //DMap._map.addOverlay(DMap._marker);
        //DMap._map.setCenter(point, DMap._zoom);
        //$("#latitude").val(Math.round(point.x * 100000) / 100000);
        //$("#longitude").val(Math.round(point.y * 100000) / 100000);
        //$("#zoom").val(DMap._zoom);

        //        GEvent.addListener(DMap._map, 'dblclick', function(overlay, point) {
        //            DMap.move_marker(point);
        //        });

        //        GEvent.addListener(DMap._map, 'zoomend', function(oldzoom, newzoom) {
        //            $("#zoom").val(newzoom);
        //        });
        $("#mapViewSatelliteButton").live("click", function() {
            DMap._map.setMapType(G_SATELLITE_MAP);
        });
        $("#mapViewHybridButton").live("click", function() {
            DMap._map.setMapType(G_HYBRID_MAP);
        });
        $("#mapViewRegularButton").live("click", function() {
            DMap._map.setMapType(G_NORMAL_MAP);
        });
        $("#mapZoomInButton").live("click", function() {
            DMap._map.zoomIn();
        });
        $("#mapZoomOutButton").live("click", function() {
            DMap._map.zoomOut();
        });
        $("#expandMapButton").click(function() {
            $("#contractedMapHeader").hide();
            $("#map-contracted").hide();
            $("#expandedMapHeader").show();
            $("#expandedMapHeader .map_header").append($("#mapTypeControls").remove());
            $("#map-expanded").append($("#map").remove().css("width", "774px").css("height", "530px"));
            $("#map-expanded").show();
            $("#mapControlsExpanded").append($("#mapZoomControls").remove());
            DMap._map.checkResize();
        });
        $("#contractMapButton").click(function() {
            $("#expandedMapHeader").hide();
            $("#map-expanded").hide();
            $("#contractedMapHeader .map_header").append($("#mapTypeControls").remove());
            $("#contractedMapHeader").show();
            $("#map-contracted").append($("#map").remove().css("width", "300px").css("height", "220px"));
            $("#map-contracted").show();
            $("#mapControlsContracted").append($("#mapZoomControls").remove());
            DMap._map.checkResize();
        });
    },

    loadMarkers: function(url, infoUrl, iconUrl) {
        $("#map").hide();
        $("#ts-Map-container").show();
        // Load map markers. This could be moved to be triggered by some other event.
        $.getJSON(url, function(data) {
            var i = 0;
            $.each(data.Data, function(key, item) {
                var marker;
                // Create this marker and add it to the map.
                var point = new GLatLng(item.lat, item.lng);

                var icon = new GIcon();
                icon.image = iconUrl;
                icon.iconSize = new GSize(24, 23);
                icon.iconAnchor = new GPoint(16, 16);
                icon.infoWindowAnchor = new GPoint(25, 7);
                ++i;
                opts = {
                    "icon": icon,
                    "clickable": true,
                    "labelText": i,
                    "labelOffset": new GSize(-15, -15),
                    "title": item.title,
                    "labelClass": "mapMarker_company"
                };
                var marker = new LabeledMarker(point, opts);

                //marker = new GMarker(point, { title: i });
                DMap._map.addOverlay(marker);
                DMap._bounds.extend(point);
                DMap.zoom_fit();
                // Add click event to the marker.
                GEvent.addListener(marker, 'click', function() {
                    // Load this node and open in an info window.
                    // Note: this should preferably be cached on the client side.
                    $.getJSON(infoUrl + item.id, function(data) {
                        marker.openInfoWindowHtml(data.Data);
                    });
                });
            });
            $("#map").show();
            $("#ts-Map-container").hide();
        });
    },
    loadAllMarkers: function(css, infoUrl, iconUrl, data) {
        $("#map").hide();
        $("#ts-Map-container").show();
        var i = 0;
        if (data) {
            $.each(data, function(key, item) {
                var marker;
                // Create this marker and add it to the map.
                var point = new GLatLng(item.lat, item.lng);

                var icon = new GIcon();
                icon.image = iconUrl;
                icon.iconSize = new GSize(24, 23);
                icon.iconAnchor = new GPoint(16, 16);
                icon.infoWindowAnchor = new GPoint(25, 7);
                ++i;
                opts = {
                    "icon": icon,
                    "clickable": true,
                    "labelText": i,
                    "labelOffset": new GSize(-15, -15),
                    "title": item.title,
                    "labelClass": css
                };
                var marker = new LabeledMarker(point, opts);

                //marker = new GMarker(point, { title: i });
                DMap._map.addOverlay(marker);
                DMap._bounds.extend(point);
                DMap.zoom_fit();
                // Add click event to the marker.
                GEvent.addListener(marker, 'click', function() {
                    // Load this node and open in an info window.
                    // Note: this should preferably be cached on the client side.
                    $.getJSON(infoUrl + item.id, function(data) {
                        marker.openInfoWindowHtml(data.Data);
                    });
                });
            });
        }
        else {
            this.load_item(iconUrl, "");
        }
        $("#map").show();
        $("#ts-Map-container").hide();

    },
    move_marker: function(point) {
        DMap._map.removeOverlay(DMap._marker);
        DMap._marker = new GMarker(point);
        DMap._map.addOverlay(DMap._marker);
        $("#latitude").val(Math.round(point.y * 100000) / 100000);
        $("#longitude").val(Math.round(point.x * 100000) / 100000);
        //DMap._map.setCenter(point, DMap._zoom);
    },

    locate: function(lt, lg, z) {
        DMap._latitude = lt;
        DMap._longitude = lg;
        DMap._zoom = z;
        var point = new GLatLng(DMap._latitude, DMap._longitude);
        DMap.move_marker(point);
        DMap._map.setCenter(point, DMap._zoom);
    },

    zoom_fit: function() {
        var newzoom = DMap._map.getBoundsZoomLevel(DMap._bounds);
        var newcenter = DMap._bounds.getCenter();
        DMap._map.setCenter(newcenter, newzoom);
    },

    load_item: function(iconUrl, title) {
        var point = new GLatLng(DMap._latitude, DMap._longitude);

        var icon = new GIcon();
        icon.image = iconUrl;
        icon.iconSize = new GSize(24, 23);
        icon.iconAnchor = new GPoint(16, 16);
        icon.infoWindowAnchor = new GPoint(25, 7);
        opts = {
            "icon": icon,
            "clickable": true,
            "labelText": "",
            "labelOffset": new GSize(-15, -15),
            "title": title,
            "labelClass": ""
        };
        var marker = new LabeledMarker(point, opts);

        //marker = new GMarker(point, { title: i });
        DMap._map.addOverlay(marker);
        DMap._map.setCenter(point, DMap._zoom);
    }
};
