/*
 * Simple YUI-powered Accordion Menu Written by Juan I.Leon Aug 2008 v1.3
 */
YAHOO.namespace("JILS");
YAHOO.JILS.accordionMenu = function(settings, theListID) {
	// object vars
	this.theList;
	this.theLIs;
	this.theSettings = settings;
	this.selectedID = null;
	this.previouslySelectedID = null;
	this.loadedSections = [];
	var self = this; // used for scope correction in callbacks
	// fxns
	this.buildWidget = function() {
		// set defaults
		if (typeof this.theSettings.sectionCollapsedSize == 'undefined') {
			this.theSettings.sectionCollapsedSize = 1.25;
		}
		if (typeof this.theSettings.sectionExpandedSize == 'undefined') {
			this.theSettings.sectionExpandedSize = 20;
		}
		if (typeof this.theSettings.sectionSizeUnit == 'undefined') {
			this.theSettings.sectionSizeUnit = 'em';
		}
		if (typeof this.theSettings.multiSelect == 'undefined') {
			this.theSettings.multiSelect = false;
		}
		if (typeof this.theSettings.onlyCollapseOnTitleClick == 'undefined') {
			this.theSettings.onlyCollapseOnTitleClick = true;
		}
		if (typeof this.theSettings.sectionExpanded == 'undefined') {
			this.theSettings.sectionExpanded = null;
		}
		if (theListID != null) {
			this.theList = document.getElementById(theListID);
			this.theLIs = this.theList.getElementsByTagName("li");
		}
		var expanded = false;
		for (var i = 0; i < this.theLIs.length; i++) {
			// set height
			this.theLIs[i].style.height = this.theSettings.sectionCollapsedSize
					+ this.theSettings.sectionSizeUnit;

			if (YAHOO.util.Dom.hasClass(this.theLIs[i], "active") == true) {

				this.expandSection(this.theLIs[i]);
				expanded = true;
			}

			// add listeners
			YAHOO.util.Event.addListener(this.theLIs[i], "click",
					this.toggleSection, self);

		}
		if(expanded==false && this.theSettings.sectionExpanded){
			this.expandSection(this.theLIs[this.theSettings.sectionExpanded-1]);
		}
	};

	this.getSelectedID = function() {
		return this.selectedID;
	};

	this.toggleSection = function(e, obj) {
		// scope in *this fxn* for 'this' is the DOM element whose click event
		// was detected
		var theLI = this;
		var theTarget = YAHOO.util.Event.getTarget(e, 1);
		if (obj.theSettings.onlyCollapseOnTitleClick == true) {
			if (!YAHOO.util.Dom.hasClass(theTarget, "accordionTitleDiv")) {
				return;
			}
		}
		self.selectedID = this;
		if (obj.theSettings.multiSelect != false) {
			if (parseFloat(theLI.style.height, 10) != obj.theSettings.sectionCollapsedSize) {
				self.collapseSection(theLI);
			} else {
				self.expandSection(theLI);
			}
		} else { // only allow single section open at a time:
			// early bail:
			if (parseFloat(theLI.style.height, 10) != obj.theSettings.sectionCollapsedSize) {
				self.collapseSection(theLI);
				return;
			}
			// optimize this. likely no loop needed
			for (var i = 0; i < self.theLIs.length; i++) {
				if (parseFloat(self.theLIs[i].style.height, 10) != obj.theSettings.sectionCollapsedSize) {
					self.collapseSection(self.theLIs[i]);
				}
			}
			self.expandSection(theLI);
		}
		self.previouslySelectedID = this; // final step: set correct state
	};

	this.expandSection = function(theSelection) {
		var exAnim = new YAHOO.util.Anim(theSelection, {
					height : {
						from : this.theSettings.sectionCollapsedSize,
						to : this.theSettings.sectionExpandedSize,
						unit : this.theSettings.sectionSizeUnit
					}
				}, 0.25, YAHOO.util.Easing.easeIn);
		exAnim.animate();
	};

	this.collapseSection = function(theSelection) {
		var colAnim = new YAHOO.util.Anim(theSelection, {
					height : {
						from : this.theSettings.sectionExpandedSize,
						to : this.theSettings.sectionCollapsedSize,
						unit : this.theSettings.sectionSizeUnit
					}
				}, 0.25, YAHOO.util.Easing.easeIn);
		colAnim.animate();
	};

	// call "constructor"
	self.buildWidget();
}; // end widget

// helper:
YAHOO.JILS.populateDivWithURL = function(domID, theURL) {
	// debugger;
	var self = this;
	// set up callback object
	this.YUICallback = {
		success : function(o) {
			document.getElementById(o.argument).innerHTML = o.responseText
		},
		failure : function(o) {
			document.getElementById(o.argument).innerHTML = 'Request Failed:'
					+ o.statusText
		},
		argument : domID
	};
	this.buildWidget = function() {
		// async request
		var connectionObject = YAHOO.util.Connect.asyncRequest('GET', theURL,
				this.YUICallback);
	};
	self.buildWidget();
};

