﻿var mMap = null;

var MedicoverMap = function (aId, aLatitude, aLongtitude, aZoom)
{

    var latlng = new google.maps.LatLng(aLatitude, aLongtitude); 
    var myOptions = { 
      zoom: aZoom, 
      center: latlng, 
      scrollwheel:false,
      disableDoubleClickZoom:true,
      navigationControl: true,
      mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById(aId), 
        myOptions);   

    // typ strony
    this.isViewYN = $(mapCid + "hlkCancel") != null;
    if(!this.isViewYN){
        var showWholeCountryControlDiv = document.createElement('DIV');
        var showWholeCountryControl = new MedMapControl(showWholeCountryControlDiv, map);
        showWholeCountryControlDiv.index = 1;
        map.controls[google.maps.ControlPosition.TOP_RIGHT].push(showWholeCountryControlDiv);
    }
    // ikona
    this.getIcon = function(img) {    
        if(img == "CenterB")
        {
            var icon = new google.maps.MarkerImage(gmImageFolder + "Google/" + img + ".png",      
              new google.maps.Size(32, 32),     
              new google.maps.Point(0, 0),
              new google.maps.Point(16,16));
        }    
        else
        {
            var icon = new google.maps.MarkerImage(gmImageFolder + "Google/" + img + ".png",      
              new google.maps.Size(16, 16),  
              new google.maps.Point(0, 0),
              new google.maps.Point(8,8));
        }       
            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.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 google.maps.LatLng(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 google.maps.Marker({
            map: map,
            position: m.Point, 
            draggable: false, 
            title: m.Info, 
            zIndex: m.Icon == "CenterB" ? 2 : 1, 
            icon: this.getIcon(m.Icon)
        });
        
        if(m.Type == 'C')
        {
            google.maps.event.addListener(marker, 'click', function() { mMap.getCity(m.Id); });
        }        
        if(m.Type == 'L')
        {
            google.maps.event.addListener(marker, 'click', function() { if(m.Id != 0){ mMap.showInfoWindow(m.Id, marker); } });
        }        
        m.Overlay = marker;
    }
                  
    this.showInfoWindow = function(aId, marker)
    {
        var infowindow = new google.maps.InfoWindow();
        var text = mMap.getMarkerInfo(aId);
        infowindow.setOptions({
            content: text
        });
        infowindow.open(map,marker);
    }
    this.getMarkerInfo = function(aId){
        var m = this.getMarker(aId);
        if(typeof(m.Phone) != 'undefined')
            var phone = label3 + ': ' + m.Phone;
        else
            var phone = '';
        if(typeof(m.Address) != 'undefined')
            var address = m.Address;
        else
            var address = '';
        if(typeof(m.Info) != 'undefined')
            var info = m.Info;
        else
            var info = '';
        var viewMore = '<br /><a href="javascript:mMap.more(' + aId + ')">' + label4 + '</a>';
            
        return '<div style="height:8px;"></div>' + info + '<br />' + address + '<br />' + phone 
                + viewMore
                + "<div id=\"route" + aId + "\"><a href=\"http://www.google.com/maps?source=uds&daddr=" 
                + encodeURI(address) + "&saddr=\" target=\"_blank\">" + label1 + "</a></div>";               
    }
    this.more = function(aId){
        document.location.href = moreUrl + aId;
    }

    this.removeMarker = function (aId) {   	    
	    var m = this.getMarker(aId);
	    m.Overlay.setMap(null);
	    this.markers = this.arrayRemoveElement(this.markers, aId);
    }
    
    this.removeMarkers = function(){
        for (var i = 0; i < this.markers.length; i++){ 
         this.markers[i].Overlay.setMap(null);
        } 
        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;
    }    
    
    // miasta
    this.getCity = function(aId){
        var m = this.getMarker(aId);                     
        map.setCenter(m.Point);
        map.setZoom(11);           
        document.getElementById(mapCid + "tbxMapType").value = "L";      
        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;            
        document.getElementById(mapCid + "tbxMarkers").value =  c.join(";");    
        document.getElementById(mapCid + "tbxZoom").value = map.getZoom();        
        document.getElementById(mapCid + "tbxBounds").value = map.getBounds().toString();
        var d = new Date();
        document.getElementById(mapCid + "tbxDate").value = d + "'" + d.getMilliseconds();             
        mMap.firePostBack('tbxDate');
    }
    this.firePostBack = function (o){     
        setTimeout('__doPostBack(\'' + mapCid.replace("__", "_").replace("_", "$") + o + '\',\'\')', 1);
    }
    
    this.getSearchedLocalization = function(aLatitude, aLongtitude, aZoom){
        var latlng = new google.maps.LatLng(aLatitude, aLongtitude);
        map.setCenter(latlng);
        map.setZoom(aZoom);   
    }
    this.setViewMode = function(){
        this.addMoveend();
        this.changeMap();
    }

    this.addMoveend = function(){            
        if(this.listener == null)
            this.listener = google.maps.event.addListener(map, 'dragend', mMap.changeMap);
    }
    this.showCountry = function(){
        $(mapCid + "tbxZoom").value = "refreshMap";
        var d = new Date();
        $(mapCid + "tbxDate").value = d + "'" + d.getMilliseconds();
        mMap.firePostBack('tbxDate');
    }
    this.refreshMapOneFire = function(){
        this.addMoveend();
        this.listener1 = google.maps.event.addListener(map, 'tilesloaded', mMap.refreshMapOne);
    }
    this.refreshMapOne = function(){
        google.maps.event.removeListener(mMap.listener1);
        mMap.changeMap();
    }
}

function MedMapControl(controlDiv, map) {  
      controlDiv.style.padding = '5px';
      var controlText = document.createElement('DIV');
      controlText.className = "mapButton";
      controlText.innerHTML = label5;
      controlDiv.appendChild(controlText);

      google.maps.event.addDomListener(controlDiv, 'click', function() {
        mMap.showCountry();
      });
}
