﻿var markers = [];
var htmlInfo = [];

function showMap() {
    document.getElementById('GoogleMapContainer').style.visibility = 'visible';
    document.getElementById('GoogleMapContainer').style.display = 'inline';
}

function hideMap() {
    document.getElementById('GoogleMapContainer').style.visibility = 'hidden';
    document.getElementById('GoogleMapContainer').style.display = 'none';
}


var BoundedMap =
{

    InitializeMap:
        function() {
            if (GBrowserIsCompatible()) {
                map = new GMap2(document.getElementById("map_canvas"));
                map.addControl(new GLargeMapControl());
                map.addControl(new GScaleControl());
                map.addControl(new GMapTypeControl());
                geocoder = new GClientGeocoder();
                markers = [];
                htmlInfo = [];
            }
        },

    CreateBoundedMap:
        function(maxLat, maxLng, minLat, minLng) {

            document.getElementById("GoogleMapContainer").style.display = 'inline';
            this.InitializeMap();
            var sw = new GLatLng(minLat, minLng);
            var ne = new GLatLng(maxLat, maxLng);
            var bounds = new GLatLngBounds(sw, ne);
            map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));

        },

    CreateBoundedMapWithMargin:
        function(maxLat, maxLng, minLat, minLng) {

            document.getElementById("GoogleMapContainer").style.display = 'inline';
            this.InitializeMap();
            var sw = new GLatLng(minLat, minLng);
            var ne = new GLatLng(maxLat, maxLng);
            var bounds = new GLatLngBounds(sw, ne);
            var zoomLvl = map.getBoundsZoomLevel(bounds);

            zoomLvl = zoomLvl - 1;
            map.setCenter(bounds.getCenter(), zoomLvl);


        },

    MakePointWithMarker:
    function(lat, lng, address) {
        var point = new GLatLng(lat, lng);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        //marker.openInfoWindowHtml(address);
        GEvent.addListener(marker, 'click', function() { marker.openInfoWindowHtml(address); });
        marker.hide();
    },
    AddMarker:
        function(lat, lng, details, itemNum, mapIcon) {
            var newMark = BoundedMap.CreateMarker(lat, lng, details, mapIcon);
            markers[itemNum] = newMark;
            htmlInfo[itemNum] = details;
        },

    CreateMarker:
        function(lat, lng, details, mapIcon) {
            var point = new GLatLng(lat, lng);
/*
            var custIcon = new GIcon(G_DEFAULT_ICON);
            custIcon.image = mapIcon;
            custIcon.iconSize = new GSize(36, 40);
            custIcon.iconAnchor = new GPoint(18, 37);
*/
            markerOptions = {
                //icon: custIcon 
            };

            var newMark = new GMarker(point, markerOptions);
            GEvent.addListener(newMark, "click", function() { newMark.openInfoWindowHtml(details) });

            return newMark;
        },

    SetPoints:
        function() {
            //This prototype is provided by the Mozilla foundation and
            //is distributed under the MIT license.
            //http://www.ibiblio.org/pub/Linux/LICENSES/mit.license

            if (!Array.prototype.forEach) {
                Array.prototype.forEach = function(fun /*, thisp*/) {
                    var len = this.length;
                    if (typeof fun != "function")
                        throw new TypeError();

                    var thisp = arguments[1];
                    for (var i = 0; i < len; i++) {
                        if (i in this)
                            fun.call(thisp, this[i], i, this);
                    }
                };
            }

            markers.forEach(BoundedMap.SetOnePoint);
        },

    SetOnePoint:
        function(x, idx) {
            map.addOverlay(markers[idx]);
        },

    ShowInfoLink:
        function(i) {
            GEvent.trigger(markers[i], "click");
        }
}