/**
* SimplePane defines a basic container which has a title and a containing body div and can be closed
* @param {String} sContainerId the id of the container, which will be assigned to a div in page
*/
function SimplePane(strContainerId, strTitle)
{
	try
	{
		// get the containerManager
		this.objWindowManager = new WindowManager();
	}
	catch(err)
	{
		DEBUG.lert("problem getting manager", 6);
		return null;
	}
	objWindow = this.objWindowManager.getWindow(strContainerId);

	if(objWindow == null)
	{
		// switches
		this.booIsModal = false;
		
		// internal variables
		this.intCoordX = null;
		this.intCoordY = null;
		this.intWidth = null;
		this.intHeight = null;
		
		// set a document object that works - in IE it seems as though the
		// document object of a frameset wont create DOM Elements properly?
		this.objDocument = frames['contentFrame'].document;
		
		
		this.strId = strContainerId;
		this.strTitle = strTitle;
		
		this.hiddenElements = new Array(); // this is to store any elements we hide
		this.arrContainedItems = new Array();
		this.arrCloseActions = new Array();
		
		// create the pane
		this.objPane = this.objDocument.createElement('div');
		this.objPane.id = strContainerId;
		this.objPane.className = 'pane';
		// don't show straight away!
		this.objPane.style.display = 'none';
		
		// add the pane to the document
		this.objDocument.getElementsByTagName('body')[0].appendChild(this.objPane);
		
		// create the main layout of HTML
		this._createDOMElements();
		
		// set the title
		this.setTitle(strTitle);
		
		this.objModalLayer = this.objWindowManager.getModalLayer();
		
		// attach this object to the actual div
		this.objPane.objSimplePane = this;
		objWindow = this;
	}
	return objWindow;
}


/**
* Add an item to the pane, by default this will go into the main container
* however, you may pass in a string reference to an added object
*
* @param {Object} objItem a HTML DOM element to add to container
* @param {String} strName the name of the item, for accessing it later
* @param {String} strObject Name of an already added element
*
* @return The item that has been added
* @type Object
*
* @member SimplePane
*/
SimplePane.prototype.addItem = function _addItem(objItem, strName, strParent)
{
	var objParent = null;
	// get the parent object
	if(strParent)
	{
		objParent = this.getItem(strParent);
	}
	else
	{
		objParent = this.objPane;
	}
	if(objParent)
	{
		// set the name to be an ID and a className
		objItem.id = strName;
		objItem.className = strName;
	
		// add a key for this item
		this.arrContainedItems[strName] = objItem;
	
		// append to parent object
		objParent.appendChild(objItem);
	}
	return objItem;
}

/**
* Get an item from the container
* @param {Object} objItem a HTML DOM element to add to container
* @member SimplePane
*/
SimplePane.prototype.getItem = function _getItem(strName)
{
	try
	{
		// Get the ID and then return a new reference to the object
		var strObjectID = this.arrContainedItems[strName];
		strObjectID = strObjectID.id;
		var objItem = this.objDocument.getElementById(strObjectID);
		return objItem;
	}
	catch(objError)
	{
		return null;
	}
}

/**
* Remove an item from the container
* @param {Object} objItem a HTML DOM element to add to container
* @member SimplePane
*/
SimplePane.prototype.removeItem = function _removeItem(strName)
{
	// add a key for this item
	this.arrContainedItems[strName] = null;
	this.objContainer.appendChild(objItem);
	return objItem;
}

/**
* Remove actual nodes from the container and clear the containedItems array
* @member SimplePane
*/
SimplePane.prototype.clearContents = function _clearContents()
{
	for( ;this.container.childNodes.length != 0; )
	{
		this.container.firstChild.removeNode(true);
	}
}

/**
* Make the pane visible, if bIsModal make the modalLayer visible also
* @member SimplePane
*/
SimplePane.prototype.show = function _show()
{
	if(this.bIsModal)
	{
		this.objModalLayer.style.display = 'block';
	}
	this.hideElements();
	this.objPane.style.display = 'block';
};

/**
* Make the pane hidden, if bIsModal make the modalLayer hidden also
* @member SimplePane
*/
SimplePane.prototype.hide = function _hide()
{
	if(this.bIsModal)
	{
		this.objModalLayer.style.display = 'none';
	}
	this.showElements();
	this.objPane.style.display = 'none';
};

/**
* Remove the pane from the document completely and from the window manager
* @member SimplePane
*/
SimplePane.prototype.destroy = function _destroy()
{
	if(this.bIsModal)
	{
		this.modalLayer.style.display = 'none';
	}
	this.pane.removeNode(true);
	this.objWindowManager.removeWindow(this.strId);
	
};

/**
*
*/
SimplePane.prototype.hideElements = function _hideElements()
{
	// hide select lists
	var arrForms = this.objDocument.forms;
	
	for ( var idxFrm = 0; idxFrm < arrForms.length; idxFrm++)
	{
		var arrElements = arrForms[idxFrm].elements;
		for ( var idxElm = 0; idxElm < arrElements.length; idxElm++)
		{
			// some course have the correct answer stored in type for wordmatch!!
			if ( !isNaN(arrElements[idxElm].type) || arrElements[idxElm].type == 'select-one' || arrElements[idxElm].type == 'select-multiple' )
			{
				// add to array of things we deleted
				this.hiddenElements[this.hiddenElements.length] = arrElements[idxElm];
				arrElements[idxElm].style.visibility = 'hidden';
			}
		}
	}
	// hide flash!
	var arrFlash = this.objDocument.getElementsByTagName('object');
	for ( var idxFla = 0; idxFla < arrFlash.length; idxFla++)
	{
		// add to array of things we deleted
		this.hiddenElements[this.hiddenElements.length] = arrFlash[idxFla];
		arrFlash[idxFla].style.visibility = 'hidden';
	}
}

/**
*
*/
SimplePane.prototype.showElements = function _showElements()
{
	// loop through the array of things we hid and make them display again
	for (var objHiddenElement = this.hiddenElements.pop(); objHiddenElement!= null; objHiddenElement = this.hiddenElements.pop())
	{
		objHiddenElement.style.visibility = 'visible';
	}
}

/**
* Sets the title, and actually changes the title in the HTML
* @param {String} strTitle the title to display
* @member SimplePane
*/
SimplePane.prototype.setTitle = function _setTitle(strTitle)
{
	objTitlebar = this.getItem('title');
	// first remove the title we have
	/*if(objTitlebar.childNodes.length > 0)
	{
	}*/
	objTitlebar.appendChild(this.objDocument.createTextNode(strTitle));
}

/**
* Sets the title, and actually changes the title in the HTML
*/
SimplePane.prototype._createDOMElements = function __createDOMElements()
{	
	// Add a div for the title bar
	this.addItem(this.objDocument.createElement('div'), 'title_bar');
	
	// Add the actual heading where the title will go
	this.addItem(this.objDocument.createElement('h1'), 'title', 'title_bar');

	// Add a div for close button
	var objCloseButton = this.addItem(this.objDocument.createElement('div'), 'close_button', 'title_bar' );
	objCloseButton.onclick = function _closeClicked()
	{
		// get the object reference
		this.parentNode.parentNode.objSimplePane.hide();
	};
	
	// Add the container
	this.addItem(this.objDocument.createElement('div'), 'container');
	
	// Add the status bar
	this.addItem(this.objDocument.createElement('div'), 'status_bar');
	
}
