/* default dropdown behaviour of DHTML navigation
 * (auto-initializes itself on window load when included)
 * to make it look like a real dropdown menu it still needs appropriate CSS!
 *
 * requires: "Prototype" JS framework
 */

// global "enter" and "leave" checks for elements without the usual event-bubbling over/out-repetition problem
	function containsDOM (container, containee) 
	{
		var isParent = false;
		do 
		{
			if ((isParent = container == containee))
			break;
			try {containee = containee.parentNode;}
			catch (e) {containee = null} 
		}
 		while (containee != null);
 		return isParent;
	}
	
	function checkMouseEnter (element, evt) 
	{
 		if (element.contains && evt.fromElement) {
 			return !element.contains(evt.fromElement);
 		}
 		else if (evt.relatedTarget) {
 			return !containsDOM(element, evt.relatedTarget);
 		}
	}

	function checkMouseLeave (element, evt) 
	{
 		if (element.contains && evt.toElement) {
 			return !element.contains(evt.toElement);
 		}
 		else if (evt.relatedTarget) {
 			return !containsDOM(element, evt.relatedTarget);
 		}
	}

var prototype_included = (typeof Prototype != 'undefined');
if (prototype_included) {

	function dnavi_dropDown_init() {
		// loop through item heads to define events and setup proper open/closed state
		var items = $$('.dnavi_wrapper.dnavi_dropDown .dnavi_item_head');
		items = items.reverse();
		if (items) items.each(function(item) {
			new dnavi_dropDownItem(item);
		});
	}
	
	var dnavi_dropDown_openItem = [];
	
	dnavi_dropDownItem = Class.create({
		initialize: function(ele) {
			this.ele = ele;
			
			Event.observe(this.ele.up(0), 'mouseout', this.outEle.bindAsEventListener(this));
			Event.observe(this.ele.up(0), 'mouseover', this.over.bindAsEventListener(this));
			
			// hide elem's body
			var itemBody = this.ele.up(0).down('.dnavi_item_body')
			if (itemBody) 
			{
				itemBody.hide();
				itemBody.removeClassName('dnavi_item_open');
				itemBody.removeClassName('dnavi_item_collapsed');
				
				var itemLevel;
				if (itemBody.up(0).className)
					itemBody.up(0).className.split(' ').each(function(v){
						if (v.match(/^dnavi_item_level\d+$/))
							itemLevel = v.replace('dnavi_item_level', '');
				});
				if (itemLevel)
					itemBody.setStyle({zIndex: 50 + parseInt(itemLevel)});
			}
		},
		
		over: function(evt) {
			if (!checkMouseEnter(this.ele.up(0), evt)) return;
			
			this.ele.addClassName('dnavi_item_over');
			
			var thisItem = this.ele.up(0);
			if (thisItem.down('.dnavi_item_body')) {
				thisItem.down('.dnavi_item_body').show();
				dnavi_dropDown_openItem.unshift(thisItem.down('.dnavi_item_body'));
			}
			
			if (dnavi_dropDown_openItem) {
				dnavi_dropDown_openItem.each(function(openItem) {
					if (!thisItem.descendantOf(openItem) && thisItem.down('.dnavi_item_body') != openItem) {
						openItem.hide();
						dnavi_dropDown_openItem = dnavi_dropDown_openItem.without(openItem);
					}
				});
			}
			
		},
		
		outEle: function(evt) 
		{
			if (!checkMouseLeave(this.ele.up(0), evt)) return;
			
			this.ele.removeClassName('dnavi_item_over');
			
			if (this.ele.up(0).down('.dnavi_item_body'))
				this.ele.up(0).down('.dnavi_item_body').hide();
		}
	});
	
	Event.observe(window, 'load', dnavi_dropDown_init);

}
