﻿    function MapLabel (__infoHTML, __locationName, marker){
        this._infoHTML      = __infoHTML;
        this._locationName  = __locationName;
        this._marker = marker;
    }        
        
    MapLabel.prototype = new GOverlay();
    MapLabel.prototype.initialize = function (_Map){
            var labelOpacity = 95;
	        var opacity = labelOpacity / 100;
	        var div = document.createElement("div");
	        div.style.position   = "absolute";
	        div.style.visibility    = "hidden";
	        div.style.margin     = "0";
	        div.style.MozOpacity = opacity;
	        div.style.filter     = "alpha(opacity=" + labelOpacity + ")";
	        div.style.opacity    = opacity;
	        div.innerHTML = "<div class='profileMapFloat'>" + this._locationName + "</div>";//this._infoHTML;
	        
	        _Map.getContainer().appendChild(div);
	        this.div_ = div;
	        this._Map = _Map;
	        delete div;
    }
    
    MapLabel.prototype.remove = function (){
        this.div_.parentNode.removeChild(this.div_);
        delete this.div_;
        this.div_ = null;
    }
    MapLabel.prototype.copy = function (){}
    MapLabel.prototype.redraw = function (forced){
        if (forced) this.hide();
        var LatLng = this._Map.fromContainerPixelToLatLng(new GPoint(0,0), true);
        var DivPixel = this._Map.fromLatLngToDivPixel(LatLng);
        var pointDivPixel = this._Map.fromLatLngToDivPixel(this._marker.getPoint());
        var c = new GPoint(pointDivPixel.x-DivPixel.x, pointDivPixel.y-DivPixel.y);
        this.div_.style.display = "block";
        var tipLeft = c.x + this._marker.getIcon().iconAnchor.x;
        this.div_.style.left = tipLeft + "px";
        var tipTop = c.y - 2*this._marker.getIcon().iconAnchor.y + 10;
        this.div_.style.top = tipTop + "px";
        delete c;
    }
    MapLabel.prototype.show = function (){
        this.redraw();
        this.div_.style.visibility = "visible";
    }
    MapLabel.prototype.hide = function (){
        this.div_.style.visibility = "hidden";
    }

Type.registerNamespace('SeekPlay');
SeekPlay.Maps = function (_mapID, _lat, _long, _zoom, _display, _domain){
    this._MapID              = $get(_mapID);            
    this._Latitude           = _lat;
    this._Longitude          = _long;
    this._ZoomLevel          = _zoom;
    this._DisplayList        = _display;
    this._Domain             = _domain;
    this._IconPath           = this._Domain + '/static/images/icons/map/';
    this._geocoder           = null;
    this._gmarkers           = [];
    this._markerIndex        = 0;
    this._iconSet            = [];
    this._baseIcon           = null;
    this._startIcon          = null;
}
SeekPlay.Maps.prototype = {
    loadMap:function (control1, control2, geoCoder, _ctrlPrefix){
         if (GBrowserIsCompatible()) {
            this._Map = new GMap2(this._MapID);
            this._Map.setCenter(new GLatLng(this._Latitude,this._Longitude), this._ZoomLevel);
            if(control1)
                this._Map.addControl(new GLargeMapControl());
            else
                this._Map.addControl(new GSmallMapControl());
            if(control2)
                this._Map.addControl(new GMapTypeControl());
            if(geoCoder)
                 this._geocoder = new GClientGeocoder();
            Sys.UI.DomEvent.addHandler(window, "unload", Function.createDelegate(this, this.unloadMapControl));
            
            this._baseIcon = new GIcon();
            this._baseIcon.shadow =  this._IconPath + "shadow.png";
            this._baseIcon.iconSize = new GSize(41,39);
            this._baseIcon.shadowSize = new GSize(36,33);
            this._baseIcon.iconAnchor = new GPoint(13,30);
            this._baseIcon.infoWindowAnchor = new GPoint(23, 5);
            this._baseIcon.infoShadowAnchor = new GPoint(15, 32);   
           
            this._startIcon = new GIcon(this._baseIcon);
            this._startIcon.image = this._IconPath + "starting-icon.png";
        }
    },
    unloadMapControl:function(test){
        this._Map           = null;
        GUnload();  
    },
    GeoCode:function(){
        var _value = $('address').value;
        var _city = $('city').value;
        this._geocoder.getLocations(_value + " " + _city, Function.createDelegate(this, this.addAddressToMap));
    },
    GetIconType:function (_type){
       if (null == this._iconSet[_type]){
            this._iconSet[_type] = new GIcon(this._baseIcon);
            this._iconSet[_type].image = this._IconPath + "icon-" + _type + ".png";
       }
       return this._iconSet[_type];
    },
    AddMarker:function(_lat, _long, _id, _info, _type){
        var marker;
        if(_id >= 0){
           marker = new GMarker(new GLatLng(_lat, _long), this.GetIconType(_type));
        }
        else{
           marker = new GMarker(new GLatLng(_lat, _long), this._startIcon);
        }
        marker._id = _id;
        marker._info = _info;
        marker.showLabel = this.MarkerShowLabel;
        marker.hideLabel = this.MarkerHideLabel;
        GEvent.addListener(marker, 'mouseover', function (){marker.showLabel()});
        GEvent.addListener(marker, 'mouseout', function (){marker.hideLabel()});
        
//        GEvent.addListener(marker, 'mouseover', function(){
//           marker.openInfoWindowHtml(this._info);
//	    });
	    this._Map.addOverlay(marker);
	    this._gmarkers[this._markerIndex ++] = marker;
    },
    DisplayMarkerWindow:function(id){
        for (var i = 0; i < this._gmarkers.length; i++) {
            if(this._gmarkers[i]._id == id){
                this._Map.setCenter(this._gmarkers[i].point, this._ZoomLevel);
                this._gmarkers[i].openInfoWindowHtml(this._gmarkers[i]._info);
            }
        }
    },
    SetCenter:function (_lat, _long){
         this._Map.setCenter(new GLatLng(_lat, _long));
    },
    GeoCodeOnly:function(address, func){
        if(this._geocoder == null)
            this._geocoder = new GClientGeocoder();
        this._geocoder.getLocations(address, func);
    },
    GetDirections:function (toDirection, fromDirection){
        alert("open browser to directions");
    },
    getCenter:function(){
        alert(this._Map.getCenter());
    },
    RemoveAllMarkers:function(){
         for (var i = 0; i < this._gmarkers.length; i++) {
            this._Map.removeOverlay(this._gmarkers[i]);
            this._gmarkers[i]= null;
         }
         this._gmarkers    = [];
		 this._markerIndex = 0;
    },
    FindCenterAndZoom:function () {
	    var defaultZoom = 14;
	    var newZoom = 14;
	    if (this._gmarkers.length <= 1){
		    return defaultZoom;
	    }

	    var bounds = new GLatLngBounds(new GLatLng(90.0,180.0), new GLatLng(-90.0,-180.0));
	    for (var i = 0; i < this._gmarkers.length; i++) {
	        if (this._gmarkers[i].getPoint().lat() != 0 && this._gmarkers[i].getPoint().lng() != 0)
	        {
	            bounds.extend(this._gmarkers[i].getPoint());
	        }
	    }
    
        var newZoom = this._Map.getBoundsZoomLevel(bounds);
	    if (newZoom <= 0 || newZoom >= defaultZoom) {
		    newZoom = defaultZoom;
	    }
        this._ZoomLevel = newZoom;
	    this._Map.setZoom(this._ZoomLevel);
	    this._Map.setCenter(bounds.getCenter());
    },
    MarkerShowLabel:function (){
        if (typeof this.mapLabel == "undefined") {
            this.mapLabel = new MapLabel(this._info, this._info, this);
            __map._Map.addOverlay(this.mapLabel);
        }
        this.mapLabel.show();
    },
    MarkerHideLabel:function (){
        if (typeof this.mapLabel != "undefined") this.mapLabel.hide();
    },  
    MapShowLabel:function(id){
        if(typeof id != "undefined"){
            var xyPoint = this._gmarkers[id].getLatLng();
            this.SetCenter(xyPoint.y, xyPoint.x);
            this._gmarkers[id].showLabel();
        }
    },
    MapHideLabel:function(id){
            if(typeof id != "undefined"){
                this._gmarkers[id].hideLabel();
            }
    }
}
