/*
 * Ajax callback proxy for javascript objects
 */
var callbackArray = new Array();

function callbackProxy(response)
{
	if (!response.value) return;
	
	var result = response.value.split(',');
	response.value = result[2];
	callbackArray[result[0]][result[1]](response);

	callbackArray[result[0]][result[1]] = null;
	callbackArray[result[0]] = null;
}


/*
 * PAC
 */
function pac(){}

pac.prototype.EVENT_DELETE = 0;

pac.prototype.addChild = function(newChild)
{
	if (!this.children) this.children = new Array();
	
	newChild.parent = this;
	this.children[this.children.length] = newChild;
}

pac.prototype.removeChild = function(theChild)
{
	if (!this.children || this.children.length == 0) return;

	for (var i = 0; i < this.children.length; i++)
	{
		if (this.children[i].id == theChild.id)
		{
			this.children[i].parent = null;
			this.children.splice(i, 1);
			return;
		}
	}
}

pac.prototype.onNotify = function(sender, eventType)
{
}


/*
 * PAC - View
 */
function view()
{
}

view.prototype.show = function(elem)
{
	if (!elem) return;
	elem.style.visibility = '';
	elem.style.display = '';
}

view.prototype.hide = function(elem)
{
	if (!elem) return;
	elem.style.visibility = 'hidden';
	elem.style.display = 'none';
}

view.prototype.setAttribute = function(view, id, key, value, domTree)
{
	var elem = this.getElementById(id, domTree);

	if (elem)
	{
		elem.view = view;		// Closure
		elem[key] = value;
		return true;
	}
	
	return false;
}

view.prototype.getElementById = function(id, domTree)
{
	if (!domTree) domTree = this.dom;
	if (!domTree) return false;
	
	for (var i = 0; i < domTree.childNodes.length; i++)
	{
		var curChild = domTree.childNodes[i];
		if (curChild.id == id)
		{
			return curChild;
		}
		
		var childResult = this.getElementById(id, curChild);
		if (childResult)
		{
			return childResult;
		}
	}
	
	return false;
}

view.prototype.destroy = function(domTree)
{
	// Remove closures
	if (!domTree) return;
	this.destroyDomTree(domTree);
}

view.prototype.destroyDomTree = function(domTree)
{
	for (var i = domTree.childNodes.length - 1; i >= 0 ; i--)
	{
		var curChild = domTree.childNodes[i];
		this.destroyDomTree(curChild);
		
		if (curChild.view)
		{
			// Remove controller closure
			curChild.view = null;
		}
		
		domTree.removeChild(curChild);
		curChild = null;
	}
}


/*
 * PAC - Controller
 */
function controller()
{
	this.parent = null;
	this.children = null;
}


/*
 * Library functions
 */
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };


function EventLib(obj)
{
	// addEvent and removeEvent
	if (obj.attachEvent && obj.detachEvent)
	{
		this.addEvent = function(obj, type, fn)
		{
			obj['e' + type + fn] = fn;
			obj[type + fn] = function() { obj['e' + type + fn](window.event); }
			obj.attachEvent('on' + type, obj[type + fn]);
		}

		this.removeEvent = function(obj, type, fn)
		{
			obj.detachEvent('on' + type, obj[type + fn]);
			obj[type + fn] = null;
		}
	}
	else if (obj.addEventListener && obj.removeEventListener)
	{
		this.removeEvent = function(obj, type, fn)
		{
			obj.addEventListener(type, fn, false);
		}

		this.addEvent = function(obj, type, fn)
		{
			obj.removeEventListener(type, fn, false);
		}
	}
}

