/** accordion.js unmodified from webucator.com r1528 */
(function(){
	
	/**
	* I am the Accordion Constructor
	* @param container <HTML Element|String> The HTML element or element ID for the right accordion container
	* @param 
	* @param options <Object> (optional) If present, it will be used to set the options for the current class.
	* @return void
	*/
	var Accordion = function(accordion, podClass, podHeaderClass, podContentClass, options){
		//setup configuration options
		this.setOptions(options);
		
		//store instances of accordion container element and pod classes
		this.container = $(accordion);
		this.podClass = podClass;
		this.podHeaderClass = podHeaderClass;
		this.podContentClass = podContentClass;
		
		//initialize
		this.init();
	};
	
	Accordion.prototype = {
		/**
		* I initialize everything after the Constructor is called
		* @return void
		*/
		init: function(){
			this.pods = YAHOO.util.Dom.getElementsByClassName(this.podClass, 'div', this.container);
			for (var i=0; i<this.pods.length; i++){
				var pod = this.pods[i];
				var podId = YAHOO.util.Dom.generateId(pod);
				
				var contentNodes = YAHOO.util.Dom.getElementsByClassName(this.podContentClass, 'div', pod);
				if (contentNodes.length > 1){
					window.alert('Error initializing accordion.  There can only be one content element per pod.');
					return;
				}else if (contentNodes.length == 1){
					var contentNode = contentNodes[0];
				}else{
					window.alert('Error initializing accordion.  Could not identify content node within pod.');
					return
				}
				
				var headerNodes = YAHOO.util.Dom.getElementsByClassName(this.podHeaderClass, 'div', pod);
				if (headerNodes.length > 1){
					window.alert('Error initializing accordion.  There can only be one header element per pod.');
					return;
				}else if (headerNodes.length == 1){
					var headerNode = headerNodes[0];
				}else{
					window.alert('Error initializing accordion.  Could not identify header node within pod.');
					return
				}
				
				YAHOO.util.Event.on(headerNode, 'mouseover', function(e, params){
					YAHOO.util.Event.stopPropagation(e);
					YAHOO.util.Dom.addClass(params.id, 'hover')
				}, {id:podId});
				
				YAHOO.util.Event.on(headerNode, 'mouseout', function(e, params){
					YAHOO.util.Event.stopPropagation(e);
					YAHOO.util.Dom.removeClass(params.id, 'hover')
				}, {id:podId});
				
				YAHOO.util.Event.on(headerNode, 'click', this.show, {id: podId, content: contentNode}, this);
			}
		},
		
		/**
		* I set the Class configuration options object
		* @param options <Object> (optional) If present, it will be used to set the options for the current class.
		* @return void
		*/
		setOptions: function(options){
			this.options = {
				duration: 0.2,
				easing: YAHOO.util.Easing.easeIn
			};
			YAHOO.lang.augmentObject(this.options, options || {}, true);
		},
		
		/**
		* I hide all pod contents
		* @param options <Object> (optional) If present, it will be used to set the options for the current class.
		* @return void
		*/
		hide: function(){
			var contentNodes = YAHOO.util.Dom.getElementsByClassName(this.podContentClass, 'div', this.container);
			for (var i=0; i<contentNodes.length;i++){
				var contentNode = contentNodes[i];
				var podContainer = contentNode.parentNode;
				var blind = false;
				//only blind pods who are active
				if (YAHOO.util.Dom.hasClass(podContainer, 'active')){
					new YAHOO.widget.Effects.BlindUp(contentNode, {seconds:this.options.duration, ease: this.options.easing});
					YAHOO.util.Dom.removeClass(podContainer, 'active');
				}
			}
		},
	
		/**
		* I hide any active pods and show the pod
		* @param e <Event> The event handler from the click on the header node
		* @param params <Object> An object containing two members: id (the pod ID) and content(the content node)
		* @return void
		*/
		show: function(e, params){
			var content = params.content;
			var id = params.id;
			
			if (YAHOO.util.Dom.hasClass(id, 'active')){
				//blind up
				var effect = new YAHOO.widget.Effects.BlindUp(content, {delay:true, seconds:this.options.duration});
				effect.onEffectComplete.subscribe(function(){
					YAHOO.util.Dom.removeClass(id, 'active');
				});
				effect.animate();
			}else{
				//blind down
				this.hide();
				content.style.display = 'block';
				var effect = new YAHOO.widget.Effects.BlindDown(content, {delay:true, seconds:this.options.duration});
				effect.animate();
				YAHOO.util.Dom.addClass(id, 'active');
			}
		}
	};
	YAHOO.com.webucator.Accordion = Accordion;
})();
