/*
 *	com.stevemeinel.AccordionMenu
 */

var com;
if (!com) {
	com = {};
} else if (typeof com !== "object") {
	throw new Error("com already exists and is not an object!");
}

if (!com.stevemeinel) {
	com.stevemeinel = {};
} else if (typeof com.stevemeinel !== "object") {
	throw new Error("com.stevemeinel already exists and is not an object!");
}

if (com.stevemeinel.AccordionMenu) {
	throw new Error("com.stevemeinel.AccordionMenu already exists!");
}

if (com.stevemeinel.AccordionMenuItem) {
	throw new Error("com.stevemeinel.AccordionMenuItem already exists!");
}

if (YAHOO) {
	if (YAHOO.util) {
		if (!YAHOO.util.Dom) {
			throw new Error("YAHOO.util.Dom does not exist!");
		}
		if (!YAHOO.util.Anim) {
			throw new Error("YAHOO.util.Anim does not exist!");
		}
	} else {
		throw new Error("YAHOO.util does not exist!");
	}
} else {
	throw new Error("YAHOO does not exist!");
}

com.stevemeinel.AccordionMenu = function (container_node, node_class) {
	this.container_node = document.getElementById(container_node);
	this.menu_items = (function (nc) {
		var items = [];
		var list = YAHOO.util.Dom.getElementsByClassName(nc, 'div');
		var length = list.length;
		var i = 0;
		for (i = 0; i < length; i++) {
			var child = "sub" + list[i].id;
			child = document.getElementById(child);
			if (child) {
				items[i] = new com.stevemeinel.AccordionMenuItem(list[i], child);
			} else {
				items[i] = new com.stevemeinel.AccordionMenuItem(list[i], null);
			}
		}
		return items;
	})(node_class);
	
	this.handle_click = function (this_obj) {
		return function (e) {
			var target = YAHOO.util.Event.getTarget(e);	
			while (target.id != this_obj.container_node.id) {
				var i = 0;
				var length = this_obj.menu_items.length;
				for (i = 0; i < length; i++) {
					if (YAHOO.util.Dom.hasClass(target, this_obj.menu_items[i].parent_node.className)) {
						if(target.id == this_obj.menu_items[i].parent_node.id) {
							this_obj.menu_items[i].open();
						} else {
							this_obj.menu_items[i].close();
						}
					}
				}
				target = target.parentNode;
			}
		};
	}(this);
	
	(function (this_obj) {
		YAHOO.util.Event.on(this_obj.container_node, "click", this_obj.handle_click);
	})(this);
};

com.stevemeinel.AccordionMenuItem = function (parent_node, child_node) {
	this.parent_node = parent_node;
	this.child_node = child_node;
	
	this.open = function () {
		if (!this.child_node) {
			this.open = function () {};
			return;
		}
		if (this.child_node.animating) {
			this.child_node.anim.stop(true);
			this.child_node.anim = null;
		}
		if (this.child_node.old_height) {
			this.child_node.anim = new YAHOO.util.Anim(this.child_node, {height: {to: this.child_node.old_height}}, 0.5, YAHOO.util.Easing.bounceOut); 
			this.child_node.anim.onStart.subscribe(function (el) { 
				return function () { 
					el.animating = true; 
				}; 
			}(this.child_node));
			this.child_node.anim.onComplete.subscribe(function (el) { 
				return function () { 
					el.animating = false;
					this.child_node.anim = null;
					this.child_node.old_height = null;
				}; 
			}(this.child_node));
			this.child_node.anim.animate();
		}
	};
	
	this.close = function () {
		if (!this.child_node) {
			this.close = function () {};
			return;
		}
		if (this.child_node.animating) {
			this.child_node.anim.stop(true);
			this.child_node.anim = null;
		}
		if (!this.child_node.old_height) {
			this.child_node.old_height = this.child_node.offsetHeight;
		}
		this.child_node.anim = new YAHOO.util.Anim(this.child_node, {height: {to: 0}}, 0.5, YAHOO.util.Easing.bounceOut); 
		this.child_node.anim.onStart.subscribe(function (el) { 
			return function () { 
				el.animating = true; 
			}; 
		}(this.child_node));
		this.child_node.anim.onComplete.subscribe(function (el) { 
			return function () { 
				el.animating = false; 
				this.child_node.anim = null;
			}; 
		}(this.child_node));
		this.child_node.anim.animate();
	};
	
	this.highlight = function (this_obj) {
		return function () {
			if (this_obj.parent_node.color_anim && this_obj.parent_node.color_anim.isAnimated()) {
				this_obj.parent_node.color_anim.stop();
				this_obj.parent_node.color_anim = null;
			}
			if (!this_obj.parent_node.old_color) {
				if (this_obj.parent_node.currentStyle) {
					this_obj.parent_node.old_color = this_obj.parent_node.currentStyle.backgroundColor;
				} else if (window.getComputedStyle) {
					this_obj.parent_node.old_color = window.getComputedStyle(this_obj.parent_node, null).backgroundColor;
				}
			}
			var color = this_obj.parent_node.getAttribute("highlight");
			this_obj.parent_node.color_anim = new YAHOO.util.ColorAnim(this_obj.parent_node, {backgroundColor: {to: color}}, 0.1);
			this_obj.parent_node.color_anim.animate();
		};
	}(this);
	
	this.unhighlight = function(this_obj) {
		return function () {
			if (this_obj.parent_node.color_anim && this_obj.parent_node.color_anim.isAnimated()) {
				this_obj.parent_node.color_anim.stop();
				this_obj.parent_node.color_anim = null;
			}
			this_obj.parent_node.color_anim = new YAHOO.util.ColorAnim(this_obj.parent_node, {backgroundColor: {to: this_obj.parent_node.old_color}}, 0.1);
			this_obj.parent_node.color_anim.animate();
		};
	}(this);

	
	(function (this_obj) {
		this_obj.parent_node.onmouseover = this_obj.highlight;
		this_obj.parent_node.onmouseout = this_obj.unhighlight;
	})(this);
};
