/* ------------ DHTML dropdown navi - special case for Cafe HAG pages ------------ */

// 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);
 		}
		return true;
	}

	function checkMouseLeave (element, evt) 
	{
 		if (element.contains && evt.toElement) {
 			return !element.contains(evt.toElement);
 		}
 		else if (evt.relatedTarget) {
 			return !containsDOM(element, evt.relatedTarget);
 		}
		return true;
	}

//var library_included = (typeof Prototype != 'undefined' && typeof Scriptaculous != 'undefined');
var library_included = (typeof Prototype != 'undefined');
if (library_included) {

	function cafehagNavi_init() {
		// loop through item heads to define events and setup proper open/closed state
		var items = $$('.dnavi_left .dnavi_item_level1 .dnavi_item_open .dnavi_item_head');
		items = items.reverse();
		if (items) items.each(function(item) {
			new cafehagNavi_dropDownItem(item);
		});
		
		//temp buzh navi behaviour
		new buzhNavi_item($$('.dnavi_top')[0]);
		
		//$$('body')[0].observe('mousemove', checkIfMouseIsOverTopNavi);
		
		if ($$('div.level0.HIDE a')[0]) $$('div.level0.HIDE a')[0].observe('mouseover', showTopNavi);
		if ($$('div#hdrLinks li.hl_item1 a')[0]) $$('div#hdrLinks li.hl_item1 a')[0].observe('mouseover', showTopNavi);
		if ($$('div#hdrLinks li.hl_item2 a')[0]) $$('div#hdrLinks li.hl_item2 a')[0].observe('mouseover', showTopNavi);		
		if ($$('div#hdrLinks li.hl_item3 a')[0]) $$('div#hdrLinks li.hl_item3 a')[0].observe('mouseover', showTopNavi);
		if ($$('div#hdrLinks li.hl_item4 a')[0]) $$('div#hdrLinks li.hl_item4 a')[0].observe('mouseover', showTopNavi);	
		if ($$('div#hdrLoggedLinks li.hl_item1 a')[0]) $$('div#hdrLoggedLinks li.hl_item1 a')[0].observe('mouseover', showTopNavi);
		if ($$('div#hdrLoggedLinks li.hl_item2 a')[0]) $$('div#hdrLoggedLinks li.hl_item2 a')[0].observe('mouseover', showTopNavi);
		if ($$('div#hdrLoggedLinks li.hl_item3 a')[0]) $$('div#hdrLoggedLinks li.hl_item3 a')[0].observe('mouseover', showTopNavi);
		
		if ($$('div.level0.HIDE a')[0]) $$('div.level0.HIDE a')[0].observe('mouseout', hideTopNavi);		
		if ($$('div#hdrLinks li.hl_item1 a')[0]) $$('div#hdrLinks li.hl_item1 a')[0].observe('mouseout', hideTopNavi);
		if ($$('div#hdrLinks li.hl_item2 a')[0]) $$('div#hdrLinks li.hl_item2 a')[0].observe('mouseout', hideTopNavi);		
		//if ($$('div#hdrLinks li.hl_item3 a')[0]) $$('div#hdrLinks li.hl_item3 a')[0].observe('mouseout', hideTopNavi);
		//if ($$('div#hdrLinks li.hl_item4 a')[0]) $$('div#hdrLinks li.hl_item4 a')[0].observe('mouseout', hideTopNavi);	
		if ($$('div#hdrLoggedLinks li.hl_item1 a')[0]) $$('div#hdrLoggedLinks li.hl_item1 a')[0].observe('mouseout', hideTopNavi);
		if ($$('div#hdrLoggedLinks li.hl_item2 a')[0]) $$('div#hdrLoggedLinks li.hl_item2 a')[0].observe('mouseout', hideTopNavi);
		if ($$('div#hdrLoggedLinks li.hl_item3 a')[0]) $$('div#hdrLoggedLinks li.hl_item3 a')[0].observe('mouseout', hideTopNavi);
		
		
	}
	
	function showTopNavi() {
		$$('.dnavi_top')[0].addClassName('dnavi_buzhMainItem_over');		
	}
	function hideTopNavi() {
		$$('.dnavi_top')[0].removeClassName('dnavi_buzhMainItem_over');		
	}
	
	function checkIfMouseIsOverTopNavi(e) {
		if (Event.pointerY(e) <= 0) {		
			$$('.dnavi_top')[0].removeClassName('dnavi_buzhMainItem_over');
		}
		else if (Event.pointerY(e) <= 45) {
			$$('.dnavi_top')[0].addClassName('dnavi_buzhMainItem_over');
		}
		else if (Event.pointerY(e) > 70) {
			if ($('headerLoginbox').style.display == "none" && $('searchBox').style.display == "none") {
				$$('.dnavi_top')[0].removeClassName('dnavi_buzhMainItem_over');
			}
		}
	}	
	
	var cafehagNavi_dropDown_openItem = [];
	
	cafehagNavi_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();
				cafehagNavi_dropDown_openItem.unshift(thisItem.down('.dnavi_item_body'));
			}
			
			if (cafehagNavi_dropDown_openItem) {
				cafehagNavi_dropDown_openItem.each(function(openItem) {
					if (!thisItem.descendantOf(openItem) && thisItem.down('.dnavi_item_body') != openItem) {
						openItem.hide();
						cafehagNavi_dropDown_openItem = cafehagNavi_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();
		}
	});
	
	
	buzhNavi_item = Class.create({
		initialize: function(ele) {
			this.ele = ele;

			Event.observe(this.ele, 'mouseout', this.outEle.bindAsEventListener(this));
			Event.observe(this.ele, 'mouseover', this.over.bindAsEventListener(this));
		},
		
		over: function(evt) {
			if (!checkMouseEnter(this.ele, evt)) return;			
			
			this.ele.addClassName('dnavi_buzhMainItem_over');
			//new Effect.Morph(this.ele, {style: 'height:25px', duration: 0.5});
		},
		
		outEle: function(evt) {
			if (!checkMouseLeave(this.ele, evt)) return;			
			
			this.ele.removeClassName('dnavi_buzhMainItem_over');
			//new Effect.Morph(this.ele, {style: 'height:0px', duration: 0.5});
		}
	});
	
	
	Event.observe(window, 'load', cafehagNavi_init);

}

