/* ------------ DHTML dropdown navi - special case for JACOBS 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 jacobsNavi_init() {
		
		// clicking the webcafé logo button reloads the page but skips the zoom sequence in the flash movie
		//if (location.href.indexOf('PagecRef=2385') > -1) {
			if ($$('ul.dnavi_left li.dnavi_item_id_2316 a')[0]) {
				var flashUrl = $$('ul.dnavi_left li.dnavi_item_id_2316 a')[0].href += "&pageId=home";
				$$('ul.dnavi_left li.dnavi_item_id_2316 a')[0].setAttribute('href', flashUrl);
			}
		//}
		

		if (location.href.indexOf('dynNav=pDB') > -1) {
			
			var productCategory;

			if (location.href.indexOf('cid=649') > -1) {
				//productCategory = "Filterkaffee";
				productCategory = "PagecRef=2402";
			}
			else if (location.href.indexOf('cid=650') > -1) {
				//productCategory = "Kaffeepads";
				productCategory = "PagecRef=2403";				
			}
			else if (location.href.indexOf('cid=651') > -1) {
				//productCategory = "Ganze Bohne";
				productCategory = "PagecRef=2405";				
			}
			else if (location.href.indexOf('cid=652') > -1) {
				//productCategory = "TASSIMO";
				productCategory = "PagecRef=2404";				
			}
			else if (location.href.indexOf('cid=653') > -1) {
				//productCategory = "L\&ouml\;skaffee";
				productCategory = "PagecRef=2406";				
			}
			else if (location.href.indexOf('cid=654') > -1) {
				//productCategory = "Spezialit\&auml\;ten";
				productCategory = "PagecRef=2407";				
			}

			var allDivs = document.getElementsByTagName('div');
			var categoryLinklist;

			for (var i=0; i < allDivs.length; i++) {
				if (allDivs[i].className.indexOf('categoryLinklist') > -1) {
					categoryLinklist = allDivs[i];
				}
			}

			if (categoryLinklist) {
				var categoryLinklistLinks = categoryLinklist.getElementsByTagName('a');
				var activeCategoryLink;
				var activeCategoryLinkClass = "";

				for (var i=0; i < categoryLinklistLinks.length; i++) {

					/*if (categoryLinklistLinks[i].innerHTML == productCategory) {
						activeCategoryLink = categoryLinklistLinks[i];
					}*/

					if (categoryLinklistLinks[i].href.indexOf(productCategory) > -1) {
						activeCategoryLink = categoryLinklistLinks[i];
					}

					if (activeCategoryLink) {
						activeCategoryLinkClass = activeCategoryLink.className;
						activeCategoryLinkClass += ' activeCategoryLink';
						activeCategoryLink.className = activeCategoryLinkClass;
					}
				}
			}
		}


		//create outer container
		var newContainer = new Element('div', {id: 'outerContainer'});	
		var innerContainer = $('stdcontent');
		if(!innerContainer) {
			//maybe we are on a PromotionalRegistration template
			innerContainer = $('promoReg');
		}
		innerContainer.wrap(newContainer);


		//removes Backgroundimage on page 3493 (Vielfaltspromo)
		//function is called by Flashfile 
		/*function removeClass(a) {
			$$('body')[0].removeClassName(a);
		}*/


    	// 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 jacobsNavi_dropDownItem(item);
		});

		
	}
	

	
	var jacobsNavi_dropDown_openItem = [];
	
	jacobsNavi_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();
				jacobsNavi_dropDown_openItem.unshift(thisItem.down('.dnavi_item_body'));
			}
			
			if (jacobsNavi_dropDown_openItem) {
				jacobsNavi_dropDown_openItem.each(function(openItem) {
					if (!thisItem.descendantOf(openItem) && thisItem.down('.dnavi_item_body') != openItem) {
						openItem.hide();
						jacobsNavi_dropDown_openItem = jacobsNavi_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', jacobsNavi_init);
	document.observe("dom:loaded", jacobsNavi_init);	
}


