var Dom = YAHOO.util.Dom;
var Event = YAHOO.util.Event;
var TimeoutID = null;
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 : false,
		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 < 1) {
		  var accordionObjects = Dom.getElementsByClassName("accordion-gray");
		}		  

		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");
				}
				this.attachEvents(headers,i,accordionObjects[i]);
			}
		}
	},

	attachEvents : function(headers,nr, accordObj) {
		for(var i=0; i<headers.length; i++) {
			var headerProperties = {
				objRef : headers[i],
				nr : i,
				jsObj : this,
				imgID: null
			}
			
			// go through dt anchor tags
			var anchors = headers[i].getElementsByTagName("a");
			for(var j=0; j<anchors.length; j++) {
			  /* make sure image is only swapped out on menu out */
			  var images = anchors[j].getElementsByTagName("img");
			  headerProperties.imgID = images[0].id;
			  Event.addListener(anchors[j],"mouseover",this.onMouseOverMenu,headerProperties);
			  Event.addListener(anchors[j],"mouseout",this.onMouseOutMenu,headerProperties);
			}
			var bodies = headers[i].parentNode.getElementsByTagName("dd");
			for(var i=0; i<bodies.length; i++) {
		    this.expand(bodies[i]);
		    /*var bodyProperties = {
				  objRef : bodies[i],
				  nr : i,
				  jsObj : this,
				  imgID : headerProperties.imgID
			  }
			  // go through dd anchor tags
			  var anchorsBody = bodies[i].getElementsByTagName("a");
			  for(var j=0; j<anchorsBody.length; j++) {
			    Event.addListener(anchorsBody[j],"mouseover",this.onMouseOverSubMenu,bodyProperties);
			    Event.addListener(anchorsBody[j],"mouseout",this.onMouseOutSubMenu,bodyProperties);
			    Event.addListener(anchorsBody[j].parentNode,"mouseover",this.onMouseOverSubMenu,bodyProperties);
			    Event.addListener(anchorsBody[j].parentNode,"mouseout",this.onMouseOutSubMenu,bodyProperties);
			  }*/
		  }
		}
	},

	onMouseOverMenu : function(e, objMenu) {
		imgOn(objMenu.imgID);
		/*clearTimeout(TimeoutID);
		//var headers = objMenu.objRef.parentNode.getElementsByTagName("dd");
		var ddObj = Dom.getNextSibling(objMenu.objRef);
		
		if(objMenu.jsObj.properties.multipleOpen) {
			objMenu.jsObj.openSubMenu(ddObj);
		} else {
		  var accordionObjects = Dom.getElementsByClassName("accordion-gray");
			for(var i=0; i<accordionObjects.length; i++) {
			  var headers = accordionObjects[i].getElementsByTagName("dd")
			  for(var j=0; j<headers.length; j++) {
				  if(Dom.hasClass(headers[j],"open")) {
					  objMenu.jsObj.removeSubMenu(headers[j],accordionObjects[i].getElementsByTagName("a")[0].getElementsByTagName("img")[0].id);
				  }
				}
			}
			objMenu.jsObj.openSubMenu(ddObj);
		}*/
	},
	
	onMouseOutMenu : function(e, objMenu) {
	  imgOff(objMenu.imgID);
		/*var ddObj = Dom.getNextSibling(objMenu.objRef); //get dd node

		if(Dom.hasClass(ddObj,"open")) {
		  clearTimeout(TimeoutID);
		  TimeoutID = setTimeout("YAHOO.lutsr.accordion.removeSubMenu('"+ddObj.id+"','"+objMenu.imgID+"')", 300);
		}*/
	},
	
	onMouseOverSubMenu : function(e,objSubMenu) {
	  clearTimeout(TimeoutID);
		var headers = objSubMenu.objRef.parentNode.getElementsByTagName("dd");
		objSubMenu.jsObj.openSubMenu(objSubMenu.objRef);
	},
	
	onMouseOutSubMenu : function(e,objSubMenu) {
		if(Dom.hasClass(objSubMenu.objRef,"open")) {
			clearTimeout(TimeoutID);
			TimeoutID = setTimeout("YAHOO.lutsr.accordion.removeSubMenu('"+objSubMenu.objRef.id+"','"+objSubMenu.imgID+"')", 300);
		}
	},
	
	removeSubMenu : function(objID, imgID){
	  YAHOO.lutsr.accordion.collapse(objID);
	  imgOff(imgID);
	},
	
	openSubMenu : function(objDDMenu){
	  if(!Dom.hasClass(objDDMenu,"open")) {
	    this.expand(objDDMenu);
	  }
	},
	
	collapse : function(objID) {
	  var header = Dom.get(objID);
	  if(Dom.hasClass(Dom.getPreviousSibling(header),"selected"))
		  Dom.removeClass(Dom.getPreviousSibling(header),"selected");
		if(Dom.hasClass(header,"open")){
		  if(!this.properties.animation) {
			  Dom.removeClass(header,"open");
		  } else {
			  this.initAnimation(header,"close");
		  }
		}
	},
	expand : function(objID) {
	  var header = Dom.get(objID);
		Dom.addClass(Dom.getPreviousSibling(header),"selected");
		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");
			var attributes = {
				height : {
					from : 0,
					to : header.offsetHeight
				}
			}
			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);
