/* Mark Wise : Expando.js : v1.03 : 12/4/08 12:11 PM */

/*	v1.02
 *	Allows nesting of "dl" elements. Pointer correctly targets the root level "dt" and "dd" elements. Whereas v1.01
 *	would also target nested "dt" and "dd" elements making it impossible to nest "dl" elements inside "dd" elements.
 *
*/

/*	v1.03
 *	The "expando-closed" class can be hard coded on the "dd" elements instead of javascript generating them. This will
 *	prevent the elements from snapping closed on page load.
 *
*/	


Expando = function(){
	this.ele = arguments[0];
	this.fnc = arguments[1];
	this.initialize();

}
Expando.prototype = {
	initialize: function(){
		var delegate = this;
		var arr = [];
			
		
		//IF ELEMENT ID IS PASSED IN INSTEAD OF THE ELEMEMT, SET "this.ele" EQUAL TO THE ELEMENT
		if(typeof this.ele == "string"){
			this.ele = document.getElementById(this.ele);
	
		}
			
		
		//INITIALIZE ARR
		var ele = this.ele.childNodes;
		for(var i=0; i<ele.length; i++){
			if(ele[i].nodeName.toLowerCase() == "dt" || ele[i].nodeName.toLowerCase() == "dd"){
				arr.push(ele[i]);
			
			}
				
		}
		for(var i=0; i<arr.length; i++){
									
			//INITIALIZE DD ELEMENTS
			if(arr[i].nodeName.toLowerCase() == "dd"){
				if(arr[i].className.indexOf("expando_open") == -1 && arr[i].className.indexOf("expando_closed") == -1){
					if(arr[i].className.length > 1){
						arr[i].className = arr[i].className + " expando_closed";
									
					}else{
						arr[i].className = "expando_closed";
								
					}
													
				}
				
			}
			
			
			//INITIALIZE DT ELEMENTS
			if(arr[i].nodeName.toLowerCase() == "dt"){
				if(arr[i + 1].className.indexOf("expando_open") != -1){
					if(arr[i].className.length > 1){
						arr[i].className = arr[i].className + " expando_open";
									
					}else{
						arr[i].className = "expando_open";
								
					}
												
				}else{
					if(arr[i].className.length > 1){
						arr[i].className = arr[i].className + " expando_closed";
									
					}else{
						arr[i].className = "expando_closed";
								
					}
														
				}
				arr[i].index = i;
				arr[i].state = false;
				arr[i].onclick = function(){
					if(arr[this.index + 1].className.indexOf("expando_open") == -1){
						this.state = true;
						this.className = this.className.replace("expando_closed", "expando_open");
						arr[this.index + 1].className = arr[this.index + 1].className.replace("expando_closed", "expando_open");
																					
					}else{
						this.state = false;
						this.className = this.className.replace("expando_open", "expando_closed");
						arr[this.index + 1].className = arr[this.index + 1].className.replace("expando_open", "expando_closed");
																
					}
					delegate.fnc(this);
																	
				}
				
			}	
																			
		}
			
	}

}