﻿var UnitasMapApp = function() { };

UnitasMapApp.prototype = {
    initialize: function(target) {
        this.gmap = new GMap2(document.getElementById(target));
        this.gmap.setCenter(new GLatLng(0, 0), 2);
        this.gmap.enableScrollWheelZoom();

        this.gmap.addControl(new GSmallMapControl());
        this.gmap.setMapType(G_HYBRID_MAP);

        this.waypoints = [];
    },

    changeMap: function(json) {
        this.waypoints = json;
        this.redrawMap(true);
    },

    redrawMap: function(resetBounds) {
        this.gmap.clearOverlays();

        var bounds = new GLatLngBounds();
        var gmap = this.gmap;

        var polyLatLngs = [];
        $.each(this.waypoints, function(i, wp) {
            //            if (wp.showinmap) {

            var latlng = new GLatLng(wp.latitude, wp.longitude);

            var icon = new GIcon(G_DEFAULT_ICON);
            icon.image = '/labeledicon.ashx?imgtext=' + wp.day;
            icon.iconSize = new GSize(32, 32);
            icon.iconAnchor = new GPoint(17, 33);

            wp.marker = new GMarker(latlng, icon);
            bounds.extend(latlng);

            GEvent.addListener(wp.marker, "mouseover", function() {
                var text = "<b>" + wp.title + "</b><br/>" + wp.description;
                if (wp.image != "") {
                    text += '<br /><img src="' + wp.image + '" alt="' + wp.title + '"/>';
                }
                wp.marker.openInfoWindowHtml(text);
            });


            gmap.addOverlay(wp.marker);
            polyLatLngs.push(latlng);
            //            }
        });


        if (resetBounds) {
            this.gmap.setCenter(bounds.getCenter(), this.gmap.getBoundsZoomLevel(bounds));
        }

        this.gmap.addOverlay(new GPolyline(polyLatLngs, null, null, null, { geodesic: true }));
    }
}

