/**********************************************************/
/*  digiKwick, inspired by mootools Kwick				  */ 
/*	Based on jKwicks by www.gmarwaha.com				  */
/*	Author: Simon Jentsch								  */
/*	Copyright DIGIPETS 2009								  */
/*	Version 0.7		 									  */
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/




$(document).ready(function() {

	$.fn.digiKwick = function(o) {
		    o = $.extend({
		        min: 0,             	// Minimum size of each element. All other elements other than the currently hovered element get this size
		        max: 0,             	// Maximum size of the element. The element that is currently hovered gets this size
		        speed: 150,         	// Speed with which the animation should be applied while moving the focus for the element
		        easing: "swing",   		// The easing effect to be applied to the animation that moves the element's focus 
				selectClass: "Select"	// Defines the Class for the active Element (The Element, on that Page we are on), change to "noClass" or any other unused class if de active Element shouldn't be opened by default
		    }, o || {});
			
			
			var object = $(this), objectChild = $(this).children("li"), objectChildSelect = $(this).children("li."+o.selectClass+""), $old = null, $new = null, first = true, running = false, noop = function () {};
			objectChild.children("ul").not("li."+o.selectClass+" ul").fadeOut(0);
			objectChild.children(".menuShadowLeft").not("li."+o.selectClass+" .menuShadowLeft").animate({opacity: 0}, 0);
			objectChild.children(".menuShadowRight").not("li."+o.selectClass+" .menuShadowRight").animate({opacity: 0}, 0);

								
				$(this).children("li").mousemove(function hoverMoveFunc(){ 
															
					if (running == false) {
						running = true;
						$new = $(this);
						if (first == true) {
							
							//Reset selected element if it's not hovered itself
							if ($new.hasClass("Select")) { } else {
								objectChildSelect.stop().animate({width: o.min}, o.speed);
								objectChildSelect.children("ul").fadeOut(o.speed);
								objectChildSelect.children(".menuShadowLeft").stop().animate({opacity: 0}, o.speed);
								objectChildSelect.children(".menuShadowRight").stop().animate({opacity: 0}, o.speed);
							}
														
							//Animate hovered element (only if it's not the selected element)
							if ($new.hasClass("Select")) { $old = $new; running = false; } else {
								$new.children("ul").fadeIn(o.speed);
								$new.children(".menuShadowLeft").stop().animate({opacity: 0.1}, o.speed);
								$new.children(".menuShadowRight").stop().animate({opacity: 0.1}, o.speed);
								$new.animate({width: o.max}, o.speed, o.easing, function() {                
									$old = $new; first = running = false;
						        });	
							}
						} else {
							objectChild.children("ul").not($(this).children("ul")).fadeOut(o.speed);
							var newWidth = $new.width(), oldWidth = $old.width();
			                object.children('li').stop();
							$new.children("ul").fadeIn(o.speed);
							
							$old.children(".menuShadowLeft").stop().animate({opacity: 0}, o.speed);
							$new.children(".menuShadowLeft").stop().animate({opacity: 0.1}, o.speed);
							
							$old.children(".menuShadowRight").stop().animate({opacity: 0}, o.speed);
							$new.children(".menuShadowRight").stop().animate({opacity: 0.1}, o.speed);
							
							$new.animate( { "width": o.max }, { duration: o.speed, easing: o.easing,
			                    step: function(n) {
			                        $old.width(oldWidth - (n-newWidth));
			                    },
			                    complete: function() {
			                        $old = $new; running = false;
			                    }
			                });
							//$old.animate( { "width": o.min }, { duration: o.speed, easing: o.easing});
						}
					}
				});
								
				object.hover(noop, function(i){
					
					//Bring all elements to the standard oositon
					objectChild.stop().not("li."+o.selectClass+"").animate({width: o.min}, o.speed);
					objectChild.children("ul").not("li."+o.selectClass+" ul").fadeOut(o.speed);
					objectChild.children(".menuShadowLeft").not("li."+o.selectClass+" .menuShadowLeft").stop().animate({opacity: 0}, o.speed);
					objectChild.children(".menuShadowRight").not("li."+o.selectClass+" .menuShadowRight").stop().animate({opacity: 0}, o.speed);
					
					//Bring selected element to the standard positon
					objectChildSelect.stop().animate({width: o.max}, o.speed);
					objectChildSelect.children("ul").fadeIn(o.speed);
					objectChildSelect.children(".menuShadowLeft").stop().animate({opacity: 0.1}, o.speed);
					objectChildSelect.children(".menuShadowRight").stop().animate({opacity: 0.1}, o.speed);
										
					first = true;
					running = false;
				});
		}
		
	//Behebt IE6 Hintergrund Flackern
	if (jQuery.browser.msie && jQuery.browser.version < 7) {
		document.execCommand('BackgroundImageCache', false, true);
	}
	
	$('#STYLE_hauptnavi').digiKwick({max: 300, min: 75, speed: 300, selectClass: "noClass"});
	
	
});