/*
---
description: This provides a simple Drop Down menu with infinit levels

license: MIT-style

authors:
- Arian Stolwijk

requires:
  core/1.2.4: [Class.Extras,Element.Style,Element.Event]

provides: [MooDropMenu,Element.MooDropMenu]

...
*/

var MooDropMenu = new Class({
	Implements: [Options,Events],
	options: {
		onOpen: function(el){
			// open the menu
			el.set('opacity',1);
		},
		onClose: function(el){
			// close the menu
			el.set('opacity',0);
		},
		onInitialize: function(el){
			// set menu to hide
			el.set('opacity',0);
		},
		mouseoutDelay: 200,
		mouseoverDelay: 0
	},
	
	initialize: function(menu, options, level){
		this.setOptions(options);
		
		if ($type(level) == 'number') {
			this.menu = document.id(menu); //attach menu to object
			this.fireEvent('initialize',menu);
			
			// hook up menu's parent with event to trigger menu
			this.menu.pel.addEvents({
				
				'mouseover': function(){
					// Set the DropDownOpen status to true			
					this.menu.pel.mel.store('DropDownOpen',true);
					
					// Clear the timer of the delay
					$clear(this.timer);
					// Fire the event to open the menu
					this.timer = (function(){
						this.fireEvent('open',this.menu.pel.mel);
					}).delay(this.options.mouseoverDelay,this);		
					
				}.bind(this),
				
				'mouseout': function(){
					// Set the DropDownOpen status to false
					this.menu.pel.mel.store('DropDownOpen',false);
					
					// Clear the timer of the delay
					$clear(this.timer);
					// Build a delay before the onClose event get fired
					this.timer = (function(){
						if(!this.menu.pel.mel.retrieve('DropDownOpen')){
							this.fireEvent('close',this.menu.pel.mel);
						}
					}).delay(this.options.mouseoutDelay,this);		
					
				}.bind(this)				
			});
		}
		else {
			level = 0;
			this.menu = document.id(menu);
		}
		
		// grab all of the menus children - LI's in this case		
		// loop through children
		this.menu.getChildren('li').each(function(item, index){
			var list = item.getFirst('ul'); // Should be an A tag
			// if there is a sub menu UL
			if ($type(list) == 'element') {
				item.mel = list; // pel = parent element
				list.pel = item; // mel = menu element
				new MooDropMenu(list, options, level + 1); // hook up the subMenu
			}
		});			
	},
	
	toElement: function(){
		return this.menu
	}
	
});

/* So you can do like this $('nav').MooDropMenu(); or even $('nav').MooDropMenu().setStyle('border',1); */
Element.implement({
	MooDropMenu: function (options){
		this.store('MooDropMenu',new MooDropMenu(this,options));
		return this;
	}
});

// initialisierung dropdown menü
window.addEvent('domready',function(){
	$('zukMainNav').MooDropMenu({
		onOpen: function(el){
			el.fade('in')
		},
		onClose: function(el){
			el.fade('out');
		},
		onInitialize: function(el){
			el.fade('hide').set('tween',{duration:200});
		}
	});
	
	
	// function von p.d.
	// auslesen URL Parameter zum direkten öffnen eines accordions
	var checkCatOpen = window.location.search.split('&');
	var accOpen = -1;
	for (i=1;i<checkCatOpen.length;i++){
		if(checkCatOpen[i].indexOf('showCat')!=-1){
			accOpen = parseInt(checkCatOpen[i].split('=')[1]);
		}
	}
	// implement accordion function
	var myAccordion = new Fx.Accordion('h3.toggler', 'div.element', {
		opacity: false,
		display: -1,
		show:accOpen,
		alwaysHide: true,
		onActive: function(toggler, element){
			toggler.addClass('open');
		},
		onBackground: function(toggler, element){
			toggler.removeClass('open');
		}
	});
	
	
});

function emptyField(eItem,eValue,eMode){
	if(eItem == undefined || eValue == undefined || eMode == undefined) return;
	if(eMode){
		if(eItem.value == eValue){
			eItem.value = "";
		}
	}else{
		if(eItem.value == ""){
			eItem.value = eValue;
		}
	}
}

// initialisierung shadowbox
Shadowbox.init({overlayOpacity: 0.8, flashVars:{skin:'/flash/skinag.swf', controlbar:'over', autostart:true, stretching:'uniform', frontcolor:'000000', backcolor:'FFFFFF', lightcolor:'97A30D', volume:10}});

// Typoersetzung mittels cufon
Cufon.replace('#zukMainMenu p.level1',{hover:true, textShadow:'0 1px 2px #333'});
Cufon.replace('#zukSite h1');
Cufon.replace('#zukSite #zukTeaserHl h2');
Cufon.replace('#zukMetaMenu p',{hover:true});

