/* ---- YUI variables ---- */
var $D = YAHOO.util.Dom;
var $E = YAHOO.util.Event;
var $A = YAHOO.util.Easing;
var $ = $D.get; // get element by ID

var contracter = (function() {

	/* ---- Configurables ---- */
	var slideTime = 0.4; // duration in seconds of the change state, 0 will be instantanious

	function toggle(ev) {
		// set the element in question to be the unorderd list item inside the list item with the class
		var containingListItem = this;
		var ul = containingListItem.getElementsByTagName('ul')[0];
		var div = ul.parentNode;
		var region = $D.getRegion(ul);
		var ulHeight = region.bottom - region.top;
		
		if($D.hasClass(this,'contractedList')) {
			// is currently contracted
			if(slideTime != 0){
				var anim = new YAHOO.util.Anim(div, { height: { to: ulHeight } }, slideTime, $A.easeBoth);
				anim.animate();
				
				// let the css do the styles for contracting
				anim.onComplete.subscribe(function(){
					$D.removeClass(containingListItem, 'contractedList');
					div.style.height = 'auto';
				})
			} else {
				// let the css do the styles for contracting
				$D.removeClass(containingListItem, 'contractedList');

			}
		} else {
			// is not contracted already
			
			// if its not an instantanious switch animate the height of the element
			if(slideTime != 0){
				var anim = new YAHOO.util.Anim(div, { height: { to: 0 } }, slideTime, $A.easeBoth);
				anim.animate();
				
				// let the css do the styles for contracting
				anim.onComplete.subscribe(function(){
					$D.addClass(containingListItem, 'contractedList');
				})
			} else {
				// let the css do the styles for contracting
				$D.addClass(containingListItem, 'contractedList');
			}
		}
	}

	function init() {
		// first find any contractable elements 
		var contracters = $D.getElementsByClassName('contractList','li');
		
		// contract them 
		$D.addClass(contracters, 'contractedList');
		
		// add listeners
		$E.on(contracters,'click',toggle);
	}
	
	return {
		toggle: toggle,
		init: init
	}
})();


$E.onDOMReady(contracter.init); 
