
/*
	**Create new NestedAccordion : new NestedAccordion (nestedListID, nestedLevel, defaultItem)
		+ nestedListID : ID of the list to add Accordion Effects.
		+ nestedLevel : number of nested level in nested list.
		+ defaultItem : index of the item to be shown at loading 
			- default value is FALSE = no item is shown.
*/
//NestedAccordion
NestedAccordion = function (id, nestedLevel, defaultItem) {
	this.accordion = new Tree(id, nestedLevel);
	//Private Var
	var togglerName = id.toUpperCase() + "_Node";
	var elementName = id.toUpperCase() + "_Element";
	
	//Behavior
	for (var i = 0 ; i < this.accordion.LEVEL.length ; i++) {
		for (var j = 0 ; j < this.accordion.LEVEL[i].length ; j++) {
			//ChildGroup is UL
			if (this.accordion.LEVEL[i][j].getElementsByTagName("ul")[0] != null && this.accordion.LEVEL[i][j].getElementsByTagName("ul")[0].parentNode.nodeName.toLowerCase() == "li" && this.accordion.LEVEL[i][j].getElementsByTagName("a") != null) {
// SHAY: This method is problematic - it searches recursively for the first
// a and div elements. If the a contains a div, it is taken...
 				var el = $(this.accordion.LEVEL[i][j]);
				el.getFirst("a").className += " " + togglerName + i.toString();
				el.getFirst("ul").className += " " + elementName + i.toString();
//				this.accordion.LEVEL[i][j].getElementsByTagName("a")[0].className += " " + togglerName + i.toString();
//				this.accordion.LEVEL[i][j].getElementsByTagName("ul")[0].className += " " + elementName + i.toString();
			}
			//ChildGroup is DIV
			if (this.accordion.LEVEL[i][j].getElementsByTagName("div")[0] != null && this.accordion.LEVEL[i][j].getElementsByTagName("a") != null) {
// SHAY: This method is problematic - it searches recursively for the first
// a and div elements. If the a contains a div, it is taken...
 				var el = $(this.accordion.LEVEL[i][j]);
				el.getFirst("a").className += " " + togglerName + i.toString();
				el.getFirst("div").className += " " + elementName + i.toString();
//				this.accordion.LEVEL[i][j].getElementsByTagName("a")[0].className += " " + togglerName + i.toString();
//				this.accordion.LEVEL[i][j].getElementsByTagName("div")[0].className += " " + elementName + i.toString();
			}
		}
	}
	addingEffects(togglerName, elementName, defaultItem, nestedLevel);
}

function addingEffects (togglerName, elementName, defaultItem, nestedLevel) {
	if(window.ie6) var heightValue='100%';
	else var heightValue='';
	
	if (typeof(defaultItem) == "undefined") {
		defaultItem = false;
	}

	for (var i = 0 ; i < nestedLevel ; i++) {
		new Accordion($$('.' + togglerName + i.toString()), $$('.' + elementName + i.toString()), {
			duration: 400,																			
			display: defaultItem,
			opacity: false,
			alwaysHide: true,
			onActive: function(toggler, element) {
				toggler.getParent("li").addClass("Active"); // Refer to NOTE* for the explain of this line
			},
			onComplete: function() {
				var element = $(this.elements[this.previous]); //Object which has just been opened
				var lastelement = $(this.elements[this.lastopen]);
				if(element && element.offsetHeight > 0) {
					element.setStyle('height', heightValue); // Remove CSS height property
				}
				if (lastelement && lastelement.offsetHeight == 0) { // Refer to NOTE* for the explain of this line
					lastelement.getParent("li").removeClass("Active");
				}
			}
		});
	}
}

//*NOTE : because this menu has 1px seperating between LI, when we set height = 0 and margin-top = 1px to the child-UL , it appears to have one more 1px margin-top to the parent-LI and cause 2px, so to resolve this problem, we need to set CSS-margin=0 to child-UL and then set margin-top = 1px when open it and set margin-top = 0px when close it.
