/**
Class for storing and organizing map markers.

Application should only have one instantiation in scope
*/
var MapMarkers = Class.create();
MapMarkers.prototype = {
    m_hashMarkers : null,
    //m_arrMarkers : [],
    m_onMarkersChanged : null,
    m_layerDiv : null,
    m_collectionId : null,
    
    initialize : function(t_layerDiv)
    {
        this.m_hashMarkers = new Hash();
        this.m_layerDiv = t_layerDiv; //Used for glmap
        this.m_onMarkersChanged = this.addEventType("onmarkerschanged");
        this.m_onMarkersDrawn = this.addEventType("ondraw");
    },
        
    _supressEvents : false,
    
    /**
    * Function that should be called before adding multiple markers. Avoids triggering update events for each new object.
    * Finish by calling end()
    */
    _currentCollectionId : null,
    begin : function(t_collectionId, t_upperLevel)
    {
        if( !t_collectionId || t_collectionId == "" )
        {
            //Collection id required 
            throw {message : "Invalid or undefined argument 'collectionId'."};
        }
        
        try
        {
            this._currentCollectionId = t_collectionId;
            
            if( !this.m_hashMarkers[t_collectionId] )
            {
                var str = "this.m_hashMarkers.merge($H({" + t_collectionId + " : {upperLevel:99, collection: []}}))";
                eval(str);
            }
        }
        catch(e){alert("MapMarker.begin exception: " + e.message);}
        
        this._supressEvents = true;
    },
    
    /**
    * Function called after begin() and multiple add-calls to trigger update events.
    */
    end : function()
    {
        this._supressEvents = false;
        this.m_onMarkersChanged.fire(this._currentCollectionId, this.m_hashMarkers[this._currentCollectionId].collection.length);
        this._currentCollectionId = null;
    },
    
    add : function(
        t_spotId,
        t_icon, 
        t_northing, 
        t_easting, 
        t_title, //optional
        t_description, //optional
        t_callback,
        t_lowerbound, //optional
        t_upperbound, //optional ignored.
        t_iconXOffset, //optional
        t_iconYOffset,
        t_type,
        t_url1,
        t_url2,
        t_url3,
        t_visible ) //optional
    {     
        if( this._currentCollectionId == null )
        {
            //Collection id required 
            wedebug.log("map", "MapMarkers.add error: Init addition of points by first calling function 'begin'.", "error");
            throw {message : "Function begin not called to initiate collection."};
        }
        var t_marker = this.m_hashMarkers[this._currentCollectionId];
        
        t_marker.collection.push({
            id : t_spotId,
            icon : t_icon,
            northing : t_northing,
            easting : t_easting,
            title : t_title,
            description : t_description,
            callback : t_callback,
            lowerbound : t_lowerbound,
            upperbound : t_marker.upperLevel,
            iconXOffset : t_iconXOffset,
            iconYOffset : t_iconYOffset,
            type : t_type,
            url1 : t_url1,
            url2 : t_url2,
            url3 : t_url3,
            visible : t_visible
        });
      
//        this.m_arrMarkers.push({
//            icon : icon,
//            northing : northing,
//            easting : easting,
//            title : title,
//            description : description,
//            lowerbound : lowerbound,
//            upperbound : upperbound,
//            iconXOffset : iconXOffset,
//            iconYOffset : iconYOffset
//        });
        
        if(!this._supressEvents)
        {
            this.m_onMarkersChanged.fire(this.m_hashMarkers[this._currentCollectionId].collection.length);
        }
    },
    
//    set : function(
//        icon, 
//        northing, 
//        easting, 
//        title, //optional
//        description, //optional
//        lowerbound, //optional
//        upperbound, //optional
//        iconXOffset, //optional
//        iconYOffset)
//    {
//        this.m_arrMarkers.clear();
//        this.add(icon,northing,easting,title,description,lowerbound,upperbound,iconXOffset,iconYOffset);
//    },
    
    clear : function(t_collectionId, t_deleteFunctionDelegates)
    {
        if( t_deleteFunctionDelegates && t_collectionId )
        {
            if( this.m_hashMarkers[t_collectionId] )
            {
                var t_arrIds = new Array();
                var t_idfunc = function(t_marker)
                {
                    t_arrIds.push(t_marker.id);
                };     
                this.m_hashMarkers[t_collectionId].collection.each(t_idfunc);
                
                var i, t_length = t_deleteFunctionDelegates.length;
                for( i=0; i<t_length; i++ )
                {
                    t_deleteFunctionDelegates[i](t_collectionId, t_arrIds);
                }
                //this.m_arrMarkers.clear();
                this.m_onMarkersChanged.fire(this.m_collectionId, this.m_hashMarkers[t_collectionId].collection.length);
                
                this.m_collectionId = null;
                
                var str = "this.m_hashMarkers.merge($H({" + t_collectionId + " : {collection:[], upperLevel: " + this.m_hashMarkers[t_collectionId].upperLevel + "}}))";
                eval(str);                
            }                        
        }    
        else
        {
            wedebug.log("map", "MapMarkers.clear error. Arguments deleteFunctionDelegates and collectionId are required.", "error");
        }
    },
    
    draw : function(t_collectionId, t_funcs)
    {
        if( t_collectionId && t_funcs )
        {
            if( this.m_hashMarkers[t_collectionId] != null &&
                this.m_hashMarkers[t_collectionId].collection.length > 0 )
            {
                var t_layer = this.m_hashMarkers[t_collectionId];
                var t_markers = t_layer.collection;
                    
                try
                {
                    //alert([t_collectionId, this.m_hashMarkers, this.m_hashMarkers[t_collectionId]]);
                    var i, t_length = (t_markers!=null) ? t_markers.length : 0;
                    for(i=0; i<t_length; i++)
                    {
                        var curr = t_markers[i];
                                                                
                        var j, l2 = t_funcs.length;
                        for( j=0; j<l2; j++ )
                        {
                            var func = t_funcs[j];
                            
                            if( typeof func == "function" )
                            {
                                func(
                                    t_collectionId,
                                    t_markers[i].id,
                                    this.m_layerDiv,
                                    curr.icon, 
                                    curr.northing,
                                    curr.easting,
                                    curr.title, 
                                    curr.description, 
                                    curr.callback,                                   
                                    curr.lowerbound,
                                    t_layer.upperLevel,
                                    curr.iconXOffset,
                                    curr.iconYOffset,
                                    curr.type,
                                    curr.url1,
                                    curr.url2,
                                    curr.url3,
                                    curr.visible    
                                );
                            }
                            else
                            {
                                throw {message: "Invalid argument func. Must be function pointer"};
                            }
                        }
                    }
                    this.m_layerDiv.style.display = "block";
                }
                catch(e)
                {
                    throw {message: "Argument function threw exception upon invokation. '" + e.message + "'"};
                }
                this.m_onMarkersDrawn.fire(this.m_collectionId, t_markers.length);
            }
        }
        else
        {
            wedebug.log("map", "MapMarkers.draw error. Arguments collectionId and func are required.", "error");
        }
    }
};
Object.extend(MapMarkers.prototype, new AbstractEventTrigger()); //Extend with methods to make object an eventtrigger

