
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//  OverlayCache
//  Contains all current map overlays, and methods for mamaging them
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function OverlayObjectBase(Type, Id, Obj)
{
    this.Type   = Type;
    this.Id     = Id;
    this.Obj    = Obj;
}

function OverlayCacheBase()
{
    this.OCollection = new Array();
    
    OverlayCacheBase.prototype.Add = function(type, id, obj)
    {
        if (this.GetByTypeId(type, id) == null)
        {
            var OverlayObject = new OverlayObjectBase(type, id, obj);
            this.OCollection.push(OverlayObject);
        }
    };
    
    OverlayCacheBase.prototype.Remove = function(obj)
    {
        var TempOCollection = new Array();
        for (var x=0;x<this.OCollection.length;x++)
        {
            if (this.OCollection[x].Obj != obj)
            {
                TempOCollection.push(this.OCollection[x]);
            }
        }
        this.OCollection = TempOCollection;
    };
    
    OverlayCacheBase.prototype.RemoveCacheType = function(Type)
    {
        var TempOCollection = new Array();
        for (var x=0;x<this.OCollection.length;x++)
        {
            if (this.OCollection[x].Type != Type)
            {
                TempOCollection.push(this.OCollection[x]);
            }
        }
        this.OCollection = TempOCollection;
    };
    
    OverlayCacheBase.prototype.RemoveCacheTypes = function(arrTypes)
    {
        for(var x=0;x<arrTypes.length;x++)
        {
            this.RemoveCacheType(arrTypes[x]);
        }
    };
    
    OverlayCacheBase.prototype.GetType = function(Type)
    {
        var TempOCollection = new Array();
        for (var x=0;x<this.OCollection.length;x++)
        {
            if (this.OCollection[x].Type == Type)
            {
                TempOCollection.push(this.OCollection[x].Obj);
            }
        }
        return TempOCollection;
    };
    
    OverlayCacheBase.prototype.GetByTypeId = function(Type, Id)
    {
        for (var x=0;x<this.OCollection.length;x++)
        {
            if (this.OCollection[x].Type == Type)
            {
                if (this.OCollection[x].Id == Id)
                {
                    return this.OCollection[x].Obj;
                }
            }
        }
        return null;
    };
    
    OverlayCacheBase.prototype.RemoveByTypeId = function(Type, Id)
    {
        var TempOCollection = new Array();
        for (var x=0;x<this.OCollection.length;x++)
        {
            if (this.OCollection[x].Type == Type)
            {
                if (this.OCollection[x].Id != Id)
                {
                    TempOCollection.push(this.OCollection[x]);
                }
            }
            else
            {
                TempOCollection.push(this.OCollection[x]);
            }
        }
        this.OCollection = TempOCollection;
    };
    
    OverlayCacheBase.prototype.RemoveOverlayByTypeId = function(MapObj, Type, Id)
    {
        var TempOCollection = new Array();
        for (var x=0;x<this.OCollection.length;x++)
        {
            if (this.OCollection[x].Type == Type)
            {
                if (this.OCollection[x].Id == Id)
                {
                    MapObj.removeOverlay(this.OCollection[x].Obj);
                    this.RemoveByTypeId(Type, Id);
                }
            }
        }
    };
    
    OverlayCacheBase.prototype.RenderOverlayTypes = function(MapObj, arrType)
    {
        for (var x=0;x<arrType.length;x++)
        {
            for (var y=0;y<this.OCollection.length;y++)
            {
                if (this.OCollection[y].Type == arrType[x])
                {
//                    if (this.OCollection[y].Type == "Listing")
//                    {
//                        try
//                        {
//                            Kluz.AddMarker(this.OCollection[y].Obj, MapPointCollection[parseFloat(this.OCollection[y].Obj.getTitle())]);
//                        }
//                        catch(ex)
//                        {
//                            //alert("ocache add type:" + this.OCollection[y].Type + "\n\n" + ex.message);
//                        }
//                    }
//                    else 
                    if (this.OCollection[y].Type != "GLatLng")
                    {
                        try
                        {
                            MapObj.addOverlay(this.OCollection[y].Obj);
                        }
                        catch(ex)
                        {
                            //alert("ocache add type:" + this.OCollection[y].Type + ":\n\n" + ex.message);
                        }
                    }
                    
                }
            }
        }
    };
    
    OverlayCacheBase.prototype.RenderOverlayType = function(MapObj, Type)
    {
        for (var y=0;y<this.OCollection.length;y++)
        {
            if (this.OCollection[y].Type == Type)
            {
                MapObj.addOverlay(this.OCollection[y].Obj);
            }
        }
    };
    
    OverlayCacheBase.prototype.RemoveOverlayType = function(MapObj, Type)
    {
        
        for (var y=0;y<this.OCollection.length;y++)
        {
            if (this.OCollection[y].Type == Type)
            {
                try
                {
                    MapObj.removeOverlay(this.OCollection[y].Obj);
                }
                catch(ex)
                {
                    //alert("ocache: remove type:" + this.OCollection[y].Type + "\n\n" + ex.message);
                }
            }
        }
    };
    
    OverlayCacheBase.prototype.RemoveOverlayTypes = function(arrTypes)
    {
        for(var x=0;x<arrTypes.length;x++)
        {
            this.RemoveOverlayType(arrTypes[x]);
        }
    };
    
    OverlayCacheBase.prototype.GetMapBoundsByTypes = function(arrType)
    {
        var MapBounds = new GLatLngBounds();
        for (x=0;x<arrType.length;x++)
        {
            MapBounds = this.ExtendMapBoundsByType(arrType[x], MapBounds);
        }
        return MapBounds;
    };
    
    OverlayCacheBase.prototype.GetMapBoundsByType = function(Type)
    {
        var MapBounds = new GLatLngBounds();
        if (Type == "Neighborhood" || Type == "PrimeNeighborhood")
        {
            arrNeighborhoods = this.GetType(Type);
            if (arrNeighborhoods.length > 0)
            {
                for(var x=0;x<arrNeighborhoods.length; x++)
                {
                    var NBounds = arrNeighborhoods[x].getBounds();
                    MapBounds.extend(NBounds.getSouthWest());
                    MapBounds.extend(NBounds.getNorthEast());
                }
                return MapBounds;
            }
        }
        else if (Type == "Listing")
        {
            var arrElements = this.GetType(Type);
            for(var x=0;x<arrElements.length;x++)
            {
                MapBounds.extend(arrElements[x].getLatLng());
            }
            return MapBounds;
        }
        
    };
    
    OverlayCacheBase.prototype.ExtendMapBoundsByType = function(Type, MapBounds)
    {
        var arrElements = this.GetType(Type);
        for(var x=0;x<arrElements.length;x++)
        {
            MapBounds.extend(arrElements[x].getLatLng());
        }
        return MapBounds;
    };
}

var OverlayCache = new OverlayCacheBase();
