/*
 * *******************************************************
 * This object enables content within an HTML document to 
 * be re-arranged so that specified elements in the 
 * content can be expanded/collapsed.
 * 
 * author  : Greg Wilton (Queensland Treasury)
 * version : 2.1  2003-06-16
 *
 * *******************************************************
 */

/*
 * constructor
 */
var ExpandableContent = function (oddColor, evenColor, minusImg, plusImg) {
	this.oddColor = oddColor;
	this.evenColor = evenColor;
	this.minusImg = minusImg;
	this.plusImg = plusImg;
}

/*
 * re-arranges the content within the "className" parameter into expandable menus
 */
ExpandableContent.prototype.setup = function (expandableContentObj, className, node, childNode, openFirstNode) {
	if (document.getElementsByTagName && document.createElement) {
		var sections = document.getElementsByTagName(node);
		for (var i = 0; i < sections.length; i++) {
			if (sections[i].className == className) {
				var items = sections[i].getElementsByTagName(childNode);
				for (var j = 0; j < items.length; j++) {
					// set the background colors
					items[j].style.backgroundColor = (j % 2 == 0) ? this.evenColor : this.oddColor;
					
					// store the id value to be used later
					var id = "section_" + i + "_" + j;
					
					// set the id of the section to be expanded / colapsed
					var sibling = items[j];
					while (sibling != null && sibling.tagName != node.toUpperCase()) {
						sibling = this.getNextSibling(sibling);
					}
					sibling.id = id;
					
					// hide the content on startup - unless the "openFirstNode" 
					// parameter is true in which case first node is left open
					if (j != 0 || (j == 0 && openFirstNode != true)) {
						sibling.style.display = "none";
					}
					
					// get the contents of the element passed in the childNode parameter
					var text = items[j].innerHTML;
					
					// make the href attribute
					var href = "javascript:" + expandableContentObj + ".changeView(\"" + id + "\");";
					var a = document.createElement("a");
					a.setAttribute("href", href);
					var a2 = a.cloneNode(false);
					
					// create an anchor tag for the childNode that action the javascript onClick
					var content = items[j].innerHTML;
					items[j].innerHTML = "";
					a.innerHTML = content;
					items[j].appendChild(a);
					
					// create an anchor tag for the +/- that action the javascript onClick
					a2.className = "xc";
					a2.appendChild(this.getAnchorContent(sibling));
					
					// add + / - navigation to the content
					items[j].insertBefore(a2, items[j].firstChild);
				}
			}
		}
	}
}

/*
 * toggles the + / - on the navigation anchor
 */
ExpandableContent.prototype.getAnchorContent = function (obj) {
	var img = document.createElement("img");
	img.setAttribute("border", 0);
	
	var src = (obj.style.display == "none") ? this.plusImg : this.minusImg;
	var alt = (obj.style.display == "none") ? "[+]" : "[-]";
	img.setAttribute("src", src);
	img.setAttribute("alt", alt);
	
	return img;
}

/*
 * toggles and objects display on and off
 */ 
ExpandableContent.prototype.setDisplay = function (obj) {
	// set the display style
	obj.style.display = (obj.style.display == "none") ? "block" : "none"

	// the the a tag that hold the + / -
	var a = this.getPreviousSibling(obj).firstChild;
	
	// delete all children of anchor tag
	var children = a.childNodes;
	for (var k = 0; k < children.length; k++) {
		a.removeChild(children[k]);
	}
	
	// toggle the + / - images
	a.appendChild(this.getAnchorContent(obj));
}

/*
 * allows access to the setDisplay through an id reference
 */
ExpandableContent.prototype.changeView = function (objName) {
	this.setDisplay(document.getElementById(objName));
}

/*
 * element.nextSibling only for tags
 */
ExpandableContent.prototype.getNextSibling = function (obj0) {
	var obj = obj0.nextSibling;
	while (obj != null && obj.tagName == "") obj = obj.nextSibling;
	return obj;
}

/*
 * element.previousSibling only for tags
 */
ExpandableContent.prototype.getPreviousSibling = function (obj0) {
	var obj = obj0.previousSibling ;
	while (obj != null && obj.tagName == "") obj = obj.previousSibling ;
	return obj;
}
