/* ------------ DHTML dropdown navi - special case for BUZH 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 buzhNavi_init() {	
		//alert("bla");
		
	}

	function buzhNavi_init_new() {
		// loop through item heads to define events and setup proper open/closed state
		var items = $$('.dnavi_top .dnavi_item_level0 .dnavi_item_body');
		items = items.reverse();
		if (items) items.each(function(item) {
			new buzhNavi_dropDownItem(item);
		});
		
		//temp buzh navi behaviour
		//new buzhNavi_item($$('.dnavi_top')[0]);
		
		
		/*
		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#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 buzhNavi_dropDown_openItem = [];
	
	buzhNavi_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');
			this.ele.up(0).down(1).addClassName('active');
			
			/*var thisItem = this.ele.up(0);
			if (thisItem.down('.dnavi_item_body')) {
				thisItem.down('.dnavi_item_body').show();
				buzhNavi_dropDown_openItem.unshift(thisItem.down('.dnavi_item_body'));
			}
			
			if (buzhNavi_dropDown_openItem) {
				buzhNavi_dropDown_openItem.each(function(openItem) {
					if (!thisItem.descendantOf(openItem) && thisItem.down('.dnavi_item_body') != openItem) {
						openItem.hide();
						buzhNavi_dropDown_openItem = buzhNavi_dropDown_openItem.without(openItem);
					}
				});
			}*/
			
		},
		
		outEle: function(evt) {
			if (!checkMouseLeave(this.ele.up(0), evt)) return;
			this.ele.removeClassName('dnavi_item_over');
			this.ele.up(0).down(1).removeClassName('active');			
			
			//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');
		},
		
		outEle: function(evt) {
			if (!checkMouseLeave(this.ele, evt)) return;			
			this.ele.removeClassName('dnavi_buzhMainItem_over');
		}
	});*/
	
	
	Event.observe(window, 'load', buzhNavi_init_new);

}


