/*
---
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){
			el.set('opacity',1);
		},
		onClose: function(el){
			el.set('opacity',0);
		},
		onInitialize: function(el){
			el.set('opacity',0);
		},
		mouseoutDelay: 100,
		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;
	}
});


// Klassendefinition Slider und Ticker
var Ticker = new Class({
	setOptions: function(options) {
		this.options = Object.extend({
			speed: options.speed,
			delay: options.delay,
			direction: options.direction,
			autostart: options.autostart,
			onComplete: Class.empty,
			onStart: Class.empty
		}, options || {});
	},
	initialize: function(el,options){
		this.setOptions(options);
		this.el = $(el);
		this.items = this.el.getElements('li');
		var w = 0;
		var h = 0;
		this.morphing = false;
		if(this.options.direction.toLowerCase()=='horizontal') {
			h = this.el.getSize().y;
			this.items.each(function(li,index) {
				w += li.getSize().x;
			});
		} else {
			w = this.el.getSize().x;
			this.items.each(function(li,index) {
				h += li.getSize().y;
			});
		}
		this.el.setStyles({
			position: 'absolute',
			top: 0,
			left: 0,
			width: w,
			height: h
		});
		this.fx = new Fx.Morph(this.el,{duration:this.options.speed,transition:Fx.Transitions.Cubic.easeInOut,onComplete:function() {
				this.morphing = false;
				if (!this.revert) {
					var i = (this.current==0)?this.items.length:this.current;
					this.items[i-1].injectInside(this.el);
					this.el.setStyles({
						left:0,
						top:0
					});
				}
				this.revert=false;
			}.bind(this),
			onStart:function() {
				this.morphing = true;
			}.bind(this)
		});
		this.current = 0;
		this.revert = false;
		if (this.options.autostart){
		this.next();
		}
		},

	pause: function() {
	    $clear(mytimer);
	    mytimer = null;
	},
	resume: function() {
	    if (mytimer == null) {
	    this.next();
	    }
	},
	next: function(single, revert) {
		if (this.morphing) return;
		if (revert != undefined) {
			this.revert = revert;
		}
		if (this.revert){
			this.current--;
			if (this.current < 0) this.current = this.items.length-1;
			this.pos = this.items[this.current];
			this.items[this.current].injectTop(this.el);
			this.el.setStyles({
				left:-this.pos.offsetWidth,
				top:0
			});

			this.fx.start({top: 0, left: 0});
		} else {
			this.current++;
			if (this.current >= this.items.length) this.current = 0;
			this.pos = this.items[this.current];
			this.fx.start({top: -this.pos.offsetTop,left: -this.pos.offsetLeft});
		}
		if (!single) mytimer = this.next.bind(this).delay(this.options.delay+this.options.speed);
	}
});

var mytimer = null;

// Slider wechseln
function changeSlider(theSlide){
	var elements = document.getElementById('zukSlideMenu').getElementsByTagName("li");
	var elementsUl = document.getElementById('zukSlideHolder').getElementsByTagName("ul");
	for(var i = 0;i < elements.length-1;i++){
		var el = elements[i].id;
		var elUl = elementsUl[i].id;
		$(el).set('morph', {duration: 400, transition: Fx.Transitions.Cubic.easeIn });
				if(el == theSlide){
			$(el).morph('.slidecurr');
			$(elUl).fade('in');
		} else {
			$(el).morph('.slidereg');
			$(elUl).fade('out');
		}
	}
}


// Videos in Shadowbox anzeigen und Viewcounter erhöhen
function showVideo(title, id, video, width, height, counterelement, useroptions) {
	var countRequest = new Request({ link: 'chain' });

	if (counterelement) {
		countRequest.onSuccess = function(responseText, responseXML) {
			var hits = responseXML.getElementsByTagName('hits')[0].firstChild.nodeValue;
			counterelement.innerHTML = hits;
		};
	}

	countRequest.send({ url: '/modules/communication/hitcounter.cfc?method=increaseCounter&instanceid=' + id});

	var options = {
		player: 'flv',
		content: video + "-SBFIX.flv",
		width: width,
		height: height,
		title: title
	};


	if (useroptions)
		options = $merge(options, useroptions);

	Shadowbox.open(options);
}

// initialisierung shadowbox
Shadowbox.init({overlayOpacity: 0.8, flashVars:{skin:'/flash/skinag.swf', controlbar:'over', autostart:true, stretching:'uniform', frontcolor:'000000', backcolor:'FFFFFF', lightcolor:'CDAE00', volume:10}});

// Funktion um den Darstellungsmodus eines Divs mit Hilfe der Id des Divs zu wechseln
function switchDisplayMode(cDivId,cDisplayMode) {
	var cElement = document.getElementById(cDivId);
	var cElementStyle = cElement.style.display;
	if (cDisplayMode != '') {
		cElement.style.display = cDisplayMode;
	}
	else
	{
		if (cElementStyle == '' || cElementStyle == 'block' || cElementStyle == 'inline') {
			cElement.style.display = 'none';
		}
		else {
			cElement.style.display = 'block';
		}
	}
}

// Funktion um innerhalb eines Divs einen Ladebalken anzuzeigen
function showLoadingAnimation(cDivId) {
	var cElement = document.getElementById(cDivId);
	cElement.innerHTML = '<div align="center"><img src="/img/loadingAnimation.gif"></div>';
}

// Typoersetzung mittels cufon
Cufon.replace('##zukMetaMenu p',{hover:true});
Cufon.replace('##zukMainMenu p.level1',{hover:true});
Cufon.replace('##zukTitleBar h1');
Cufon.replace('.box h1');
