/**
* @fileoverview
* 
* WindowManager is a singleton object which is stored in every page, each time you
* instatiate a draggableContainer the manager will be invoked if not already instatiated
* and the container will be added to it's collection. This way variables global to all
* containers can be stored and accessed by other containers
*/

/**
* WindowManager (singleton)
* An object for storing global container variables
*/
function WindowManager()
{
	this.objDocument = frames['contentFrame'].document;
	
	// hashtable (well object but lets pretend) of available features
	this.arrWindowFeatures = {
		'titlebar': '',
		'drag' : 'DragDecorator',
		'menubar': 'MenubarDecorator'
	};
	
	// check that I do not have an instance of myself already
	if(this.objDocument.containerManager) // return instance
	{
		return this.objDocument.containerManager;
	}
	else // set up instance
	{
		// which pane is active in dragging terms
		this.activePane = null;
		
		// this is the actual function to call on mousemove
		this.moveActivityType = null;
		// this is the actual function to call on mouseup
		this.upActivityType = null;
		// storage for any parameters
		this.activityParam = null;
		
		this.objContextMenu = null;
		
		// hash containing all panes
		this.arrContainers = new Object();
		
		//
		this.iMinimiseWidth = 150;		
		
		this.objDocument.containerManager = this;
		
		document.onmousemove = function _onmousemove()
		{
			try
			{
				me = this.objDocument.containerManager;
				if(me.moveActivityType)
				{
					eval("me.activePane." + me.moveActivityType);
				}
			}
			catch(objError)
			{
			}
		}
		
		document.onmouseup = function _onmouseup()
		{
			try
			{
				me = this.objDocument.containerManager;
				if(me.upActivityType)
				{
					eval("me.activePane." + me.upActivityType);
				}
			}
			catch(objError)
			{
			}
		}
		
		document.onmousedown = function _onclick()
		{
			this.objDocument.containerManager.doOnClick();
		}
	}
}


/**
* Creates a pane, based to some extent on javascript window.open(), but supports less features
* and has no URL as does not load in HTML content. yet.
* 
* @param {String} strWindowRef The reference for the window, if you pass in a reference that already exists
* @param {String} strTitle The title to display
* @param {String} strFeatures What features are required on the window, comma seperated list please
*
* @see WindowManager#getWindowFeatureList
* @see WindowManager#addWindowFeature
*/
WindowManager.prototype.openWindow = function _openWindow(strWindowRef, strTitle, strFeatures)
{
	var objNewWindow = null;
	var arrFeatures = new Array();
	if(strFeatures)
	{
		var arrFeatures = strFeatures.split(',');
	}
	// We cannot guarentee the presense of the SimplePane class so fail gracefully if unavailable
	try
	{
		objNewWindow = new SimplePane(strWindowRef, strTitle);
		this.arrContainers[strWindowRef] = objNewWindow;
	}
	catch ( objError )
	{
		// However we do assume presence of DEBUG! 
		DEBUG.lert("Cannot instatiate object. \nEnsure SimplePane.js is included.", 5, objError);
	}
	for(i=0; i < arrFeatures.length; i++)
	{
		// class to wrap with
		strDecorator = this.arrWindowFeatures[arrFeatures[i]];
		// try the wrap
		try
		{
			if(strDecorator.length > 1)
			{
				eval("objNewWindow = new "+strDecorator+"(objNewWindow)");
			}
		}
		catch(objError)
		{
			alert("failure to decorate with: "+strDecorator);
		}
		
	}
	return objNewWindow;
}

/**
* removes the window from array
*/
WindowManager.prototype.removeWindow = function _removeWindow(strWindowRef)
{
	try
	{
		arrContainers[strWindowRef] = null;
	}
	catch(objError)
	{
		
	}
}

/**
* Adds a feature to the window class by
*/
WindowManager.prototype.addWindowFeature = function _addWindowFeature(strFeatureName, strDecorator)
{
}

/**
* All windows minimise to an area at bottom of screen, figure out the next available slot
*/
WindowManager.prototype.getNextMinimiseArea = function getNextMinimiseArea()
{
	for(i = 0; i < this.aContainers.length; i++)
	{
		if(this.aContainers[i].bIsMinimised)
		{
			alert("got a mini");
			// add to a taken slots array
		}
	}
	// find height
	winHeight = (window.innerHeight) ? window.innerHeight : document.body.clientHeight;
	// loop through available slots until you can find one
	alert("minimise not yet available");
	return null;
}

/**
* Attempts to get a window that has already been created #
* @param {String} strWindowRef The reference passed and returned when the window was created
*/
WindowManager.prototype.getWindow = function _getWindow(strWindowRef)
{
	try
	{
		if(this.arrContainers[strWindowRef])
		{
			return this.arrContainers[strWindowRef];
		}
	}
	catch(objError)
	{
		DEBUG.lert("Error while looking for an existing window in WindowManager.prototype.getWindow", 5, objError);
	}
	return null;
}

/**
* Gets or creates the modal layer which is a alpha blended layer sitting on top of the page
* preventing interaction with page objects.
*/
WindowManager.prototype.getModalLayer = function _getModalLayer()
{
	try
	{
		if( this.objDocument.getElementById('modalLayer') )
		{
			return this.objDocument.getElementById('modalLayer');
		}
		else
		{
			// create a layer to make window modal and act as alpha blend
			modalLayer = this.objDocument.createElement('div');
			modalLayer.id = "modalLayer";
			modalLayer.className = "modalLayer";
			this.objDocument.getElementsByTagName('body')[0].appendChild(modalLayer);
			return this.objDocument.getElementById('modalLayer');
		}
	}
	catch(objError)
	{
		alert(objError.description);
		//DEBUG.lert("Unable to retrieve modal layer", 5, objError);
	}
}

/**
* An onClick handler function
*/
WindowManager.prototype.doOnClick = function _doOnClick()
{
	// close the contextMenu
	if(this.objContextMenu)
	{
		this.objContextMenu.hide();
	}
}
