//  Expander protoclass extension   (c) Daniel Robinson 2009

function stopPropagation(e) 
{ 
    e = e||event;/* get IE event ( not passed ) */ 
    e.stopPropagation? e.stopPropagation() : e.cancelBubble = true; 
} 

function Expander(element) {
	var e = proto(element);
	if (!e) {return;}
	e.currentNode = null;
	e.exclusive = e.getAttr('exclusive', 'yes') == 'yes';
	e.allowNone = e.getAttr('allownone', 'yes') == 'yes';
	e.items = new Array();
	e.expandCount = 0;
	e.srcExpand = e.getAttr('srcexpand', '');
	e.srcCollapse = e.getAttr('srccollapse', '');
	e.setupNode = function(node) {
		proto(node);
		node.registerEvent('onexpand');
		node.expander = this;
		node.titleNode = null;
		node.contentNode = document.createElement('div');
		node.contentNode.className = 'itemcontent';

//		for (var c = 0; c < node.childNodes.length; c++) {
		while (node.childNodes.length > 0) {
			var childNode = node.firstChild;
			if (childNode.nodeType == 1) {
				if (!node.titleNode) {
					node.titleNode = childNode;
					if (!childNode.className) {
						childNode.className = 'itemtitle';
					}
					if (!node.titleNode.style.cursor) {
						node.titleNode.style.cursor = 'pointer';
					}
				} else {
					node.contentNode.appendChild(childNode);
				}
			}
			node.removeChild(node.firstChild);
		}
		
		proto(node.titleNode);
		proto(node.contentNode);
		node.contentNode.hideMe();
		node.titleNode.contentNode = node.contentNode;
		node.titleNode.indicator = document.createElement('img');
		if (node.expander.srcCollapse != '') {
			node.titleNode.indicator.className = 'indicator';
			node.titleNode.indicator.src = node.expander.srcCollapse;
			node.titleNode.appendChild(node.titleNode.indicator);
		}
		node.contentNode.titleNode = node.titleNode;
		node.titleNode.item = node;
		node.contentNode.item = node;
		node.expanded = false;
		node.titleNode.onclick = function(event) {
			stopPropagation(event);
			this.item.toggle();
			return false;
		}
		node.setIndicator = function() {
			this.titleNode.indicator.src = (this.expanded) ? this.expander.srcExpand : this.expander.srcCollapse;
		}
		
		node.hideContent = function() {
			if (!this.contentNode) {return;}
			if (this.expander.allowNone || (!this.expander.allowNone && this.expander.expandCount > 1)) {
				this.contentNode.hideMe();
				this.expanded = false;
				this.setIndicator();
				this.expander.expandCount--;
			}
		}
		node.showContent = function() {
			if (!this.contentNode) {return;}
			this.contentNode.showMe();
			this.expanded = true;
			this.setIndicator();
			this.expander.expandCount++;
			if (this.expander.exclusive && this.expander.currentNode != null) {
				this.expander.currentNode.hideContent();
			}
			this.expander.currentNode = this;
			this.fireEvent('onexpand');			
		}
		node.toggle = function() {
			if (!this.contentNode) {return}
			if (this.expanded) {
				this.hideContent();
			} else {
				this.showContent();
			}
		}
		node.appendChild(node.titleNode);
		node.appendChild(node.contentNode);
	}
	e.autoAdd = function() {
		for (var c = 0; c < this.childNodes.length; c++) {
			var node = this.childNodes[c];
			if (node.nodeType == 1) {
				this.items.push(node);
				this.setupNode(node);
			}
		}
	}
	e.afterCreation = function() {
		this.autoAdd();
		for (var c = 0; c < this.items.length; c++) {
			var node = this.items[c];
			if (node.getAttr('expand', 'no') == 'yes') {
				node.showContent();
			}
		}
		if (!this.allowNone && this.items.length > 0 && this.expandCount == 0) {
			this.items[0].showContent();
		}
	}
	return e;
}

registerClass('expander', Expander);