/*
Programmer: Darryl Ballard
Date created: 2009-04-06
Last updated: 2010-05-13
Version: 2.0.1

Requires:
	Prototype 1.6
	
Version History:
	2.0.1, 2010-05-13: Changed how the anonymous function is created/called.  Changed to slightly more efficient for loops.
	
	2.0.0, 2009-05-15: Got it working.  Went with a completely anonymous function object.
	
	2.0.0 alpha, 2009-05-14: Trying a different object-oriented JS technique.
	
	1.0.0, 2009-05-14: Corrected and removed the alert if Prototype is missing.  It works fine, it's no longer alpha.
	
	1.0.0 alpha, 2009-04-06: Built.
*/

if (typeof Prototype != "undefined") {
	document.observe("dom:loaded", function() {
		var id_to_activate = "caught";
		var class_for_hover = "hover";
		var hide_parent_if_zero_visible_children = true;
		
		var handle_click = function() {
			this.hide();
			
			var visibleSiblings = 0;
			var siblings = this.parentNode.childElements();
			var i, il;
			for (i = 0, il = siblings.length; i < il; i++) {
				if (siblings[i].visible()) {
					visibleSiblings++;
				}
			}
			
			if (hide_parent_if_zero_visible_children && !visibleSiblings) {
				$(this.parentNode).hide();
			}
		};
		
		var handle_mouseover = function() {
			this.addClassName(class_for_hover);
		};
	
		var handle_mouseout = function() {
			this.removeClassName(class_for_hover);
		};
		
		// Init
		var objCaught = $(id_to_activate);
		if (objCaught) {
			// Give hover and click behavior to each child element
			var childElems = objCaught.childElements();
			var i, il;
			var elem;
			for (i = 0, il = childElems.length; i < il; i++) {
				elem = childElems[i];
				elem.observe("click", handle_click);
				elem.observe("mouseover", handle_mouseover);
				elem.observe("mouseout", handle_mouseout);
			}
		}
		
	});
}

