Object.extend(GLMap.prototype, {
    m_hotspotIndex : 0,
    m_arrHotspots : [], //Array containing hot spot objects with id
    
    showHotspotDialog : function(t_id, t_delay)
    {
        var t_findFunc = function(t_value) {
            if( t_id == t_value.id )
            {
                t_internalId = t_value.internalId; 
                return true;                   
            }
            return false;
        }
        var t_hotspot = this.m_arrHotspots.find(t_findFunc); //Keep all except the one to be removed..
        
        if(t_hotspot != null && typeof t_hotspot.callback != "undefined" && t_hotspot.callback != null)
        {
            this._currSpotId = t_hotspot.id;
            var t_element = $(t_id);
            if(t_element != null )
            {
                if( typeof t_delay == "undefined" || t_delay == null )
                {
                    this._fireHotspotCallback(t_element.style.left, t_element.style.top, ++this._runningId);                
                }
                else
                {
                    setTimeout(
                        this._fireHotspotCallback.bind(this, t_element.style.left, t_element.style.top, ++this._runningId),
                        t_delay);
                }
            }
        }  
    },
    
    addHotspot : function(
        t_listingname, 
        t_id,
        t_control,
        t_icon,
        t_northing,
        t_easting,
        t_title,
        t_description,
        t_callback,
        t_lowerbound, //optional
        t_upperbound, //optional
        t_iconXOffset, //optional
        t_iconYOffset,
        t_type,
        t_url1,
        t_url2,
        t_url3,
        t_visible ) //optional
    {    
        wedebug.log("gl", "-> addHotspot: " + [t_listingname, t_id, t_icon, t_northing, t_easting, t_title, t_description, t_callback, t_lowerbound, t_upperbound, t_iconXOffset, t_iconYOffset]);   
        
        var t_marker = t_icon ? t_icon : this.DEFAULT_MAP_MARKER;
        
        //var t_id = "glPin_" + this.m_hotspotIndex++;
        //map.addHotspot(t_control, t_id, t_icon, t_northing, t_easting, this._cb_hotSpotMouseOver.bind(this), this._cb_hotSpotMouseOut.bind(this), t_lowerbound, t_upperbound, t_iconXOffset, t_iconYOffset,t_type);
        map.addHotspot($('annotationlayer'), t_id, t_icon, t_northing, t_easting, this._cb_hotSpotMouseOver.bind(this), this._cb_hotSpotMouseOut.bind(this), t_lowerbound, t_upperbound, t_iconXOffset, t_iconYOffset,t_type);
        
        this.m_arrHotspots.push({
            control : $('annotationlayer'),
            id: t_id,
            callback: t_callback,
            lat : t_northing,
            lon : t_easting,
            title : t_title,
            description : t_description,
            type : t_type,
            url1 : t_url1,
            url2 : t_url2,
            url3 : t_url3,
            visible : t_visible
        });
    },

    clearHotspots : function(t_listingname, t_ids)
    {
        wedebug.log("gl", "-> clearHotspots: " + t_ids);
        if( this.m_arrHotspots != null )
        {
            var i, t_length = t_ids.length;
            for( i=0; i<t_length; i++ )
            {
                map.removeHotspot(t_ids[i]);
                
                var selectfunc = function(value) {
                    return t_ids[i] != value.id;
                };
                
                this.m_arrHotspots = this.m_arrHotspots.select(selectfunc);
                
            }
            this._currSpotId = null;
            //map.clearHotspots();
            this.m_hotspotIndex = this.m_arrHotspots.length;
            //this.m_arrHotspots.clear();
        }
    },
    
    hideHotspots : function()
    {
        map.hideHotspots();
    },
    
    showHotspots : function()
    {
        map.showHotspots();
    },

    _currSpotId : null,
    _runningId : 0,
    /**
    Callback for ajaxmap hotspot mouseover
    */
    _cb_hotSpotMouseOver : function(t_spotId, t_imgX, t_imgY)
    {
        wedebug.log("gl", "-> _hotSpotMouseOver: " + [t_spotId, t_imgX, t_imgY]);       
        if (typeof t_spotId != "undefined")
        {
    //            e.eventName
    //            e.elementID
            this._currSpotId = t_spotId;
            setTimeout(
                (function()
                {
                    this._fireHotspotCallback(t_imgX, t_imgY, ++this._runningId);
                }).bind(this), 500);
        }
    },

    /**
    Callback for ajaxmap hotspot mouseout
    */
    _cb_hotSpotMouseOut : function(t_spotId)
    {
        if (typeof t_spotId != "undefined")
        {
    //            e.eventName
    //            e.elementID
            if( t_spotId == this._currSpotId )
            {
                this._currSpotId = null;
            }
        }
    },

    _fireHotspotCallback : function(t_imgX, t_imgY, t_runningId)
    {
        wedebug.log("gl", "-> _fireHotspotCallback: " + [this._currSpotId, t_imgX, t_imgY]);
        
        if( this._currSpotId != null && t_runningId == this._runningId)
        {
            var t_spot = this._findHotspot(this._currSpotId);
            
            if( t_spot != null )
            {
                t_spot.callback(t_spot.title, t_spot.description, t_spot.lat, t_spot.lon, t_imgX, t_imgY,t_spot.type,t_spot.url1,t_spot.url2,t_spot.url3,t_spot.visible);
            }
        }
    },

    _findHotspot : function(t_id)
    {
        if( typeof this.m_arrHotspots != "undefined" && this.m_arrHotspots != null )
        {
            var i, t_length = this.m_arrHotspots.length;
            for( i=0; i<t_length; i++ )
            {
                if(this.m_arrHotspots[i].id == t_id)
                {
                    return this.m_arrHotspots[i];
                }
            }
        }
        return null;
    }
});


