// JavaScript Document

var PanelManager = new PanelManagerObject();

function PanelManagerObject()
{
	var me = this;
	
	var panelObjects = Array();
	panelObjects.get = function(id)
	{
		for(var i = 0; i < this.length; ++i)
		{
			if(this[i].id == id)
			{
				return this[i];
			}
		}
		return null;
	};
	panelObjects.getByName = function(name)
	{
		for(var i = 0; i < this.length; ++i)
		{
			if(this[i].name == name)
			{
				return this[i];
			}
		}
		return null;
	};

	this.getPanelObject = function(id)
	{
		return panelObjects.get(id);
	};
	this.getPanelObjectByName = function(name)
	{
		return panelObjects.getByName(name);
	};
	
	this.registerPanelObject = function(panel)
	{
		panelObjects.push(panel);
	};
	this.unregisterPanelObject = function(panel)
	{
		panelObjects.remove(panelObjects.get(panel.id));
	};
	
	this.updatePanelObject = function(panel, delay)
	{
		return setTimeout('PanelManager.getPanelObject('+panel.id+').update()', delay ? delay : 0);
	};
	this.updatePanelObjectByName = function(name, delay)
	{
		return setTimeout('PanelManager.getPanelObjectByName("'+name+'").update()', delay ? delay : 0);
	};
}

function PanelObject()
{
	var me = this;
	
	this.ajax = null;
	this.id = getCurrentTimestamp();
	this.name = null;
	
	this.wrapper = document.createElement('div');
	
	this.statusDiv = document.createElement('div');
	this.clearStatus = function()
	{
		me.statusDiv.className = '';
		removeChildren(me.statusDiv);
	};
	this.setStatus = function(text)
	{
		removeChildren(me.statusDiv);
		me.statusDiv.className = 'calign';
		me.statusDiv.appendChild(document.createTextNode(text));
	};
	this.setNotificationStatus = function(text)
	{
		removeChildren(me.statusDiv);
		me.statusDiv.className = 'notification';
		me.statusDiv.appendChild(document.createTextNode(text));
	};
	this.setErrorStatus = function(text)
	{
		removeChildren(me.statusDiv);
		me.statusDiv.className = 'error';
		me.statusDiv.appendChild(document.createTextNode(text));
	};
	this.setInfoStatus = function(text)
	{
		removeChildren(me.statusDiv);
		me.statusDiv.className = 'info';
		me.statusDiv.appendChild(document.createTextNode(text));
	};
	this.wrapper.appendChild(this.statusDiv);
	
	this.contentDiv = document.createElement('div');
	this.wrapper.appendChild(this.contentDiv);
	
	this.update = function()
	{
		if(this.ajax)
		{
			this.onUpdateStart();
			this.ajax.update();
		}
	};
	this.onUpdateStart = function() {};
	this.onUpdateEnd = function() {};
	
	this.clear = function()
	{
		removeChildren(me.contentDiv);
	};
	
	this.appendTo = function(element)
	{
		element.appendChild(me.wrapper);
	};
	
	this.appendChild = function(element)
	{
		this.contentDiv.appendChild(element);
	};
	
	this.replaceElement = function(element, include, asContentDiv)
	{
		var parent = element.parentNode;
		parent.replaceChild(me.wrapper, element);
		if(include)
		{
			if(asContentDiv)
			{
				this.wrapper.replaceChild(element, this.contentDiv);
				this.contentDiv = element;
			}
			else
			{
				me.appendChild(element);
			}
		}
	};
}

function IncludePanelObject(action, passData, params)
{
	this.parentClass = PanelObject; // inherit
	this.parentClass();
	
	var me = this;
	
	this.ajax = new AjaxObject(params ? 'ajax.php?'+params : null);
	this.ajax.htmlResponse = true;
	this.ajax.callback = function(ajax)
	{
		if(!ajax.success)
		{
			me.setErrorStatus(ajax.message);
			return;
		}
		
		me.contentDiv.innerHTML = ajax.getVariable('responseText');
		runPostProcessing(me.contentDiv);
		me.clearStatus();
		me.onUpdateEnd();
	};
	this.ajax.passData = 'html=1&action='+(action ? action : 'include')+(passData ? '&'+passData : '');
}

function AjaxPanelObject(ajax)
{
	var me = this;
	
	if(!ajax.isType('AjaxObject'))
	{
		throw new Exception('No AjaxObject for Panel specified');
	}
	this.ajax = ajax;
}
AjaxPanelObject.prototype = new PanelObject();
