﻿function MapOverlayManager()
{
	//#region 'private' variables
	
	var _overlayDictionary = new Object();
	var _itemCount = 0;
	
	//#endregion 'private' variables
	
	//#region public methods
	
	// Add a MapOverlay instance to the collection
	this.addMapOverlay =
		function(mapOverlay)
		{
			// Use the mapOverlay's ID as the Key in the Dictionary object
			_overlayDictionary[mapOverlay.ID] = mapOverlay;
			_itemCount++;
			
			mapOverlay.onVisibleChange = this.onItemVisibleChange;
			this.onAdd(mapOverlay);
		};
	
	// Creates a MapOverlay instance from the provided ID and URL and adds it
	// to the collection
	this.add =
		function(id, url, iconURL)
		{
			var mapOverlay = new MapOverlay();
			mapOverlay.ID = id;
			mapOverlay.URL =url;
			mapOverlay.IconURL = iconURL;
			mapOverlay.GoogleOverlayReference = new GGeoXml(url);
					
			this.addMapOverlay(mapOverlay);
		};
	
	// Returns a MapOverlay based on its ID	
	this.getMapOverlay =
		function(id)
		{
			return _overlayDictionary[id];
		}
		
	// Returns the MapOverlay Collection
	this.getMapOverlays =
		function()
		{
			return _overlayDictionary;
		}

	// Toggle the visiblity of overlays		
	this.toggleVisible =
		function(id, visible)
		{
			var mapOverlay = this.getMapOverlay(id);
			mapOverlay.setVisible(visible);
		}
		
	//#endregion public methods
	
	//#region public property accessor methods

	// Gets the total number of MapOverlays	
	this.getCount =
		function()
		{
			return _itemCount;
		};
	
	
	//#endregion public property accessor methods
	
	//#region public 'events'
	
	this.onAdd = 
		function(mapOverlay)
		{
			// intentionally empty
		};
		
	this.onItemVisibleChange =
		function(mapOverlay)
		{
			// intentionally empty
		}
	
	//#endregion public 'events'
}

// MapOverlay 'Class/Struct'
function MapOverlay()
{
	// 'reference' used as reference for inner functions
	var reference = this;
	
	this.URL = "";
	this.ID = -1;
	this.IconURL = "";
	this.GoogleOverlayReference = null;
	this.IsAddedToGoogleMap = false;
	this.Visible = true;
	
	this.setVisible =
		function(isVisible)
		{
			reference.Visible = isVisible;
			
			// 'Fire' event
			this.onVisibleChange(reference);
		};
	
	//#region public 'events'
		
	this.onVisibleChange =
		function(mapOverlay)
		{
			// intentionally empty
		};
		
	//#endregion public 'events'
}
