var Dom = YAHOO.util.Dom;
var Event = YAHOO.util.Event;
var $ = function(id) {
      return document.getElementById(id);
} 

//++++++++++++++++++++++++++++++++++++
// YUI ACCORDION
// 1/22/2008 - Edwart Visser
//
// accordion
//
// REQUIRES: yahoo-dom-event.js, animation-min.js
//
// TODO: build hover script for highlighting header in IE
// TODO: attach behaviour based on rel attribute
//++++++++++++++++++++++++++++++++++++

YAHOO.namespace("lutsr");

YAHOO.lutsr.accordion = {
	properties : {
		animation : true,
		animationDuration : 5,
		multipleOpen : false,
		initDone : false
	},

	init : function(animation,animationDuration,multipleOpen) {
		if(this.initDone)
			return;
			
		this.initDone = true;
		
		if(animation) {
			this.animation = animation;
		}
		if(animationDuration) {
			this.animationDuration = animationDuration;
		}
		if(multipleOpen) {
			this.multipleOpen = multipleOpen;
		}

		var accordionObjects = Dom.getElementsByClassName("accordion");
		if(accordionObjects.length > 0) {
			for(var i=0; i<accordionObjects.length; i++) {
				var headers = Dom.getElementsByClassName("accordionHeader", "", accordionObjects[i]);
				var bodies = Dom.getElementsByClassName("accordionBody", "", accordionObjects[i]);

				for(var body=0;  body<bodies.length;  body++) {
					if(Dom.hasClass(bodies[body],"open"))
						this.showExpanded(bodies[body]);
				}

				this.attachEvents(accordionObjects[i], headers, bodies, i);
			}
		}
	},

	attachEvents : function(accordion, headers, bodies, nr) {
		for(var i=0; i<headers.length; i++) {
			var headerProperties = {
				accordionRef : accordion,
				headerRef : headers[i],
				bodyRef : bodies[i],
				nr : i,
				jsObj : this
			}
			Event.addListener(headers[i],"click",this.clickHeader,headerProperties);
		}
	},

	clickHeader : function(e,headerProperties) {
		var header = headerProperties.headerRef;
		var body = headerProperties.bodyRef;

		/*
		if(Dom.hasClass(body,"open")) {
			this.collapse(header, body);
		} else {
			this.expand(header, body);
		}
		*/
		if(Dom.hasClass(header,"open")) {
			headerProperties.jsObj.toggleItem(header, body);
		} else {
			if(headerProperties.jsObj.properties.multipleOpen) {
				headerProperties.jsObj.toggleItem(header, body);
			} else {
				var headers = Dom.getElementsByClassName("accordionHeader", "", headerProperties.accordionRef);
				var bodies = Dom.getElementsByClassName("accordionBody", "", headerProperties.accordionRef);
				for(var i=0; i<bodies.length; i++) {
					if(Dom.hasClass(bodies[i],"open")) {
						headerProperties.jsObj.toggleItem(headers[i], bodies[i]);
					}
				}
				headerProperties.jsObj.toggleItem(header, body);
			}
		}
	},
	showCollapsed : function(header) {
		Dom.removeClass(header,"open");
		Dom.getPreviousSibling(header).innerHTML = Dom.getPreviousSibling(header).innerHTML.replace(/Hide /i, "Show ");
		Dom.getPreviousSibling(header).innerHTML = Dom.getPreviousSibling(header).innerHTML.replace(/buttonoff/i, "button");
	},
	showExpanded : function(header) {
		if(!Dom.hasClass(header,"open"))
			Dom.addClass(header,"open");
		Dom.getPreviousSibling(header).innerHTML = Dom.getPreviousSibling(header).innerHTML.replace(/Show /i, "Hide ");
		Dom.getPreviousSibling(header).innerHTML = Dom.getPreviousSibling(header).innerHTML.replace(/button/i, "buttonOff");
	},
	collapse : function(header, body) {
		Dom.removeClass(header,"selected");
		if(!this.properties.animation) {
			this.showCollapsed(header, body);
		} else {
			this.initAnimation(body,"close");
		}
	},
	expand : function(header, body) {
		Dom.addClass(header,"selected");
		if(!this.properties.animation) {
			this.showExpanded(body);
		} else {
			this.initAnimation(body,"open");
		}
	},
	
	initAnimation : function(header,dir) {
		var onComplete;
		
		if(dir == "open") {
			Dom.setStyle(header,"visibility","hidden");
			Dom.setStyle(header,"height","auto");
			Dom.addClass(header,"open");
			var attributes = {
				height : {
					from : 0,
					to : header.offsetHeight
				}
			}
			Dom.setStyle(header,"height",0);
			Dom.setStyle(header,"visibility","visible");
			
			animationEnd = function(e, args, sender) {
				sender.showExpanded(header);
				// leave it here
			}
		} else if ("close") {
			var attributes = {
				height : {
					to : 0
				}
			}			
			animationEnd = function(e, args, sender) {
				sender.showCollapsed(header);
			}
		}

		var animation = new YAHOO.util.Anim(header,attributes);
		animation.duration = this.properties.animationDuration;
		animation.useSeconds = false;
		animation.onComplete.subscribe(animationEnd, this);
		// animation.method = YAHOO.util.Easing.easeIn;
		animation.animate();
	},

	toggleItem : function(header, body) {
		if(Dom.hasClass(body,"open")) {
			this.collapse(header, body);
		} else {
			this.expand(header, body);
		}
	}
}

initPage = function() {
	YAHOO.lutsr.accordion.init();
}

Event.on(window,"load",initPage);