var Dom = YAHOO.util.Dom;
var Event = YAHOO.util.Event;
var $ = function(id) {
      return document.getElementById(id);
}

//++++++++++++++++++++++++++++++++++++
// YUI ACCORDION
// 1/22/2008 - Edwart Visser
// 6/30/2008 - John C. Scott
//
// 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 : 10,
		multipleOpen : false
	},

	init : function(animation,animationDuration,multipleOpen) {
		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++) {
				if(accordionObjects[i].nodeName == "DL") {
					var headers = accordionObjects[i].getElementsByTagName("dt");
					var bodies = headers[i].parentNode.getElementsByTagName("dd");
				}

				// For compatability when javascript is disabled,
				// the default state for all accordion items will be
				// set to open in the view. In the following loop,
				// we'll close all items except the first, without animation.
				for(var i=1; i<bodies.length; i++) {
					Dom.removeClass(Dom.getPreviousSibling(bodies[i]),"selected");
					// Swap open image for closed
					this.swapImg(bodies[i]);
					Dom.removeClass(bodies[i],"open");
				}

				this.attachEvents(headers,i);
			}
		}
	},

	attachEvents : function(headers,nr) {
		for(var i=0; i<headers.length; i++) {
			var headerProperties = {
				objRef : headers[i],
				nr : i,
				jsObj : this
			}

			Event.addListener(headers[i],"click",this.clickHeader,headerProperties);
		}
	},

	clickHeader : function(e,headerProperties) {
		var parentObj = headerProperties.objRef.parentNode;
		var headers = parentObj.getElementsByTagName("dd");
		var header = headers[headerProperties.nr];

		if(Dom.hasClass(header,"open")) {
			headerProperties.jsObj.collapse(header);
		} else {
			if(headerProperties.jsObj.properties.multipleOpen) {
				headerProperties.jsObj.expand(header);
			} else {
				for(var i=0; i<headers.length; i++) {
					if(Dom.hasClass(headers[i],"open")) {
						headerProperties.jsObj.collapse(headers[i]);
					}
				}
				headerProperties.jsObj.expand(header);
			}
		}
	},

	swapImg : function(header) {
		var myImgs = Dom.getPreviousSibling(header).getElementsByTagName("img");
		var imgSrc = myImgs[0].src;
		var root = imgSrc.substring(0, imgSrc.lastIndexOf('/') + 1);
		var fileName = imgSrc.substring(imgSrc.lastIndexOf('/') + 1, imgSrc.length);
		var ext = fileName.substring(fileName.lastIndexOf('.'), fileName.length);
		var fileNameRoot = fileName.substring(0, fileName.lastIndexOf('-') + 1);
		var imgState = fileName.substring(fileName.lastIndexOf('-') + 1, fileName.lastIndexOf('.'));
		newState = ((imgState == "open") ? "closed" : "open");
		myImgs[0].src = root + fileNameRoot + newState + ext;
	},

	collapse : function(header) {
		Dom.removeClass(Dom.getPreviousSibling(header),"selected");
		// Swap open image for closed
		this.swapImg(header);
		if(!this.properties.animation) {
			Dom.removeClass(header,"open");
		} else {
			this.initAnimation(header,"close");
		}
	},
	expand : function(header) {
		Dom.addClass(Dom.getPreviousSibling(header),"selected");
		// Swap closed image for open
		this.swapImg(header);
		if(!this.properties.animation) {
			Dom.addClass(header,"open");
		} else {
			this.initAnimation(header,"open");
		}
	},

	initAnimation : function(header,dir) {
		if(dir == "open") {
			Dom.setStyle(header,"visibility","hidden");
			Dom.setStyle(header,"height","auto");
			Dom.addClass(header,"open");
			// minus border top and bottom (2px)
			var attributes = {
				height : {
					from : 0,
					to : header.offsetHeight - 2
				}
			}
			Dom.setStyle(header,"height",0);
			Dom.setStyle(header,"visibility","visible");

			var animation = new YAHOO.util.Anim(header,attributes);
			animationEnd = function() {
				// leave it here
			}
			animation.duration = this.properties.animationDuration;
			animation.useSeconds = false;
			animation.onComplete.subscribe(animationEnd);
			animation.animate();
		} else if ("close") {
			var attributes = {
				height : {
					to : 0
				}
			}
			animationEnd = function() {
				Dom.removeClass(header,"open");
			}
			var animation = new YAHOO.util.Anim(header,attributes);
			animation.duration = this.properties.animationDuration;
			animation.useSeconds = false;
			animation.onComplete.subscribe(animationEnd);
			animation.animate();
		}
	}
}

initPage = function() {
	YAHOO.lutsr.accordion.init();
}

//Event.on(window,"load",initPage);
YAHOO.util.Event.onDOMReady(initPage);