/***
* ModTree - Module-Based PHP Framework
* (c) Sebastian Bojanowski ( sbojanowski (at) gmail (dot) com )
* Expandable Items Script
***/
var Expand  = {
	init : function() {
		this.groups = new Utils.List();
	},
	
	getObject : function(destObject) {
		if (typeof destObject == 'object')
			return destObject;
		else
			return document.getElementById(destObject);
	},
	
	getGroup : function(groupName) {
		if ((group = this.groups.getItemByName(groupName)) == null)
			group = this.groups.addItem(new Expand.ExpandGroup(groupName));	
		return group;
	},
	
	expand : function(groupName,itemName,destObject) {
		var object = null;
		var group = this.getGroup(groupName)
		var item = group.getItem(itemName),activeElement = null;
		if (destObject)
			 object = this.getObject(destObject);
		if (activeElement = item.getActiveElement()) {
			if (group.onHide != null)
				group.onHide(activeElement);
			this.getObject(activeElement).style.display = 'none';
		}
		if (object) {
			object.style.display = 'block';	
			if (group.onExpand != null)
				group.onExpand(object.id);
			item.setActiveElement(object.id);
		} else
			item.setActiveElement(null);
	},
	
	setOnHideAction: function(groupName,funcHandle) {
		this.getGroup(groupName).setOnHideAction(funcHandle);
	},
	
	setOnExpandAction: function(groupName,funcHandle) {
		this.getGroup(groupName).setOnExpandAction(funcHandle);
	}
}

// Expand Item Class
Expand.ExpandItem = function(name) {
	this.itemName = name;
	this.activeElement = null;
}

Expand.ExpandItem.prototype = {
	setActiveElement : function(element) {
		this.activeElement = element;
	},
	
	getActiveElement : function() {
		return this.activeElement;
	},
	
	getName : function() {
		return this.itemName;
	}
}

// Expand Group Class
Expand.ExpandGroup = function(name) {
	this.groupName = name;
	this.items = new Utils.List();
	this.onExpand = null;
	this.onHide = null;
}

Expand.ExpandGroup.prototype = {
	getItem : function(itemName) {
	    var item;
		if ((item = this.items.getItemByName(itemName)) == null)
			item = this.items.addItem(new Expand.ExpandItem(itemName));	
		return item;
	},
		
	setOnHideAction: function(funcHandle) {
		this.onHide = funcHandle;
	},
	
	setOnExpandAction: function(funcHandle) {
		this.onExpand = funcHandle;
	},
	
	getName: function() {	
		return this.groupName; 
	}
}

Expand.init();
