﻿var defaultLat = 41.8;
var defaultLng = 12.5;
var maxZoomLevel = 16;

google.maps.Map.prototype.markers = new Array();

google.maps.Map.prototype.getMarkers = function () {
    return this.markers
};

google.maps.Map.prototype.clearMarkers = function () {
    for (var i = 0; i < this.markers.length; i++) {
        this.markers[i].setMap(null);
    }
    this.markers = new Array();
};

google.maps.Marker.prototype._setMap = google.maps.Marker.prototype.setMap;

google.maps.Marker.prototype.setMap = function (map) {
    if (map) {
        map.markers[map.markers.length] = this;
    }
    this._setMap(map);
}

google.maps.Map.prototype.centerMap = function (point) {
    this.panTo(point);
}

google.maps.Map.prototype.centerMap = function (latitude, longitude) {
    var latlng = new google.maps.LatLng(latitude, longitude);
    this.panTo(latlng);
}

google.maps.Map.prototype.centerMapAndZoom = function (latitude, longitude, zoomLevel) {
    var latlng = new google.maps.LatLng(latitude, longitude);
    this.panTo(latlng);
    this.setZoom(zoomLevel);
}

google.maps.Map.prototype.addMarker = function (latitude, longitude, title, icon) {
    var myLatlng = new google.maps.LatLng(latitude, longitude);
    var marker = new google.maps.Marker({
        position: myLatlng,
        map: this,
        title: title,
        icon: icon
    });

    this.markers[this.markers.length] = marker;

    return marker;
}

function addInfoWindow(gMap, marker, info) {
    var infowindow = new google.maps.InfoWindow({
        content: info
    });
   
    google.maps.event.addListener(marker, 'click', function () {
        infowindow.open(gMap, marker);
    });
}

google.maps.Map.prototype.addLabeledMarker = function (latitude, longitude, title, icon) {
    var myLatlng = new google.maps.LatLng(latitude, longitude);

    var marker = new MarkerWithLabel({
        position: myLatlng,
        map: this,
        draggable: false,
        icon: icon,
        labelContent: title,
        labelAnchor: new google.maps.Point(5, 24),
        labelClass: "labels", // the CSS class for the label
        labelInBackground: false
    });
}

function CreateMap(mapPlaceHolderId
                , zoomLevel
                , enableScrollWheelZoom) {

    var latlng = new google.maps.LatLng(defaultLat, defaultLng);
    var myOptions = {
        zoom: zoomLevel,
        center: latlng,
        scrollwheel: enableScrollWheelZoom,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        navigationControl: true,
        navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL },
        mapTypeControl: true,
        mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU }
    };

    var map = new google.maps.Map(document.getElementById(mapPlaceHolderId), myOptions);

    return map;
}

function GetQueryStringValue(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

String.prototype.format = function () {
    var txt = this,
        i = arguments.length;

    while (i--) {
        txt = txt.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
    }
    return txt;
};
