﻿var mMap = null;
var MedicoverMap = function (aId, aLatitude, aLongtitude, aZoom)
{
    // mapa
    this.map = new GMap2($(aId));
    this.point = new GLatLng(aLatitude, aLongtitude);
    this.map.setCenter(this.point, aZoom);
    this.map.setUIToDefault();
    this.map.disableDoubleClickZoom();
    this.map.disableContinuousZoom();
    this.map.disableScrollWheelZoom();
    // typ strony
    this.isViewYN = $(mapCid + "hlkCancel") != null;
    if(!this.isViewYN)
        this.map.addControl(new MedMapControl());
    // ikona
    this.getIcon = function(img) {
        var icon = new GIcon();
        icon.image = gmImageFolder + "Google/" + img + ".png";
        if(img == "CenterB"){
            icon.iconSize = new GSize(32, 32);
            icon.iconAnchor = new GPoint(16, 16);
            icon.infoWindowAnchor = new GPoint(16,2);
        }
        else{
            icon.iconSize = new GSize(16,16);
            icon.iconAnchor = new GPoint(8,8);
            icon.infoWindowAnchor = new GPoint(8, 2);
        }
        return icon;
    }
    // markery
    this.markers = new Array();
    this.marker = function(aId, aOverlay, aPoint, aInfo, aIcon, aType, aAddress, aPhone) {
        this.Id = aId;
        this.Overlay = aOverlay;
        this.Point = aPoint;
        this.Info = aInfo;
        this.Info = aInfo;
        this.Address = aAddress;
        this.Phone = aPhone;
        this.Icon = aIcon;
        this.Type = aType;
    }
    this.addMarker = function(aId, aLat, aLng, aInfo, aIcon, aType, aAddress, aPhone){ // dodawanie nośnika
        this.markers[this.markers.length] = new this.marker(aId, null, new GLatLng(aLat, aLng), aInfo, aIcon, aType, aAddress, aPhone);
        this.markerAddOverlay(aId);
    }
    this.getMarker = function(aId){
        for(var i = 0; i < this.markers.length; i++)
            if(this.markers[i].Id == aId)
                return this.markers[i];
    }
    this.markerAddOverlay = function(aId){
        var m = this.getMarker(aId);
        var marker = new GMarker(m.Point, {icon: this.getIcon(m.Icon), draggable: false, title: m.Info, zIndexProcess: this.orderOfCreation});
        if(m.Type == 'C'){
            GEvent.addListener(marker, "click", function() { mMap.getCity(m.Id); });
        }
        if(m.Type == 'L'){
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(mMap.getMarkerInfo(m.Id));
              });
        }
        this.map.addOverlay(marker);
        m.Overlay = marker;
    }
    this.orderOfCreation = function(marker,b) {
            return 1;
    }

    this.removeMarker = function (aId) {
        var m = this.getMarker(aId);
	    m.Overlay.remove();
	    this.markers = this.arrayRemoveElement(this.markers, aId);
    }
    this.removeMarkers = function(){
        for(var i = 0; i < this.markers.length; i++)
            this.markers[i].Overlay.remove();
        this.markers = new Array();
    }
    this.arrayRemoveElement = function(aArr, aId){
        var a = new Array(); 
        for(var i = 0; i < aArr.length ; i++){
            var arrVal = aArr[i];
            if(arrVal.Id != aId)
                a.push(arrVal);
        }
        return a;
    }
    this.getMarkerInfo = function(aId){
        var m = this.getMarker(aId);
        var viewMore = this.isViewYN ? '' : '<br /><a href="javascript:mMap.more(' + aId + ')">' + label4 + '</a>';
        return '<div style="height:8px;"></div>' + m.Info + '<br />' + m.Address + (m.Phone.length > 0 ? '<br />' + label3 + ': ' + m.Phone : '')
                + viewMore
                + "<div id=\"route" + aId + "\"><a href=\"http://www.google.com/maps?source=uds&daddr=" 
                + encodeURI(m.Address) + "&saddr=\" target=\"_blank\">" + label1 + "</a></div>";
    }
    this.more = function(aId){
        document.location.href = moreUrl + aId;
    }
    // miasta
    this.getCity = function(aId){
        var m = this.getMarker(aId);
        this.map.setCenter(m.Point, 11);
        $(mapCid + "tbxMapType").value = "C";
        this.removeMarkers();
        this.changeMap();
    }
    this.changeMap = function(){
        var c = new Array();
        for(var i = 0; i < mMap.markers.length; i++)
            c[c.length] = mMap.markers[i].Id;
        $(mapCid + "tbxMarkers").value =  c.join(";");
        $(mapCid + "tbxZoom").value = mMap.map.getZoom();
        $(mapCid + "tbxBounds").value = mMap.map.getBounds().toString();
        var d = new Date();
        $(mapCid + "tbxDate").value = d + "'" + d.getMilliseconds();
        mMap.firePostBack('tbxDate');
    }
    this.firePostBack = function (o){
        setTimeout('__doPostBack(\'' + mapCid.replace("__", "_").replace("_", "$") + o + '\',\'\')', 1);
    }
    this.setViewMode = function(){
        this.addMoveend();
        this.changeMap();
    }
    this.addMoveend = function(){
        if(this.listener == null)
            this.listener = GEvent.addListener(this.map, "moveend", mMap.changeMap);
    }
    this.showCountry = function(){
        $(mapCid + "tbxZoom").value = "refreshMap";
        var d = new Date();
        $(mapCid + "tbxDate").value = d + "'" + d.getMilliseconds();
        mMap.firePostBack('tbxDate');
    }
}
function MedMapControl() {
}
MedMapControl.prototype = new GControl();
MedMapControl.prototype.initialize = function(map) {
    var container = document.createElement("div");
    var findControl = document.createElement("div");
    findControl.className = "mapButton";
    container.appendChild(findControl);
    findControl.appendChild(document.createTextNode(label5));
    GEvent.addDomListener(findControl, "click", function() {
        mMap.showCountry();
    });
    map.getContainer().appendChild(container);
    return container;
}
MedMapControl.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(75, 7));
}