$(document).ready(function() {

    /* =============================================================================
   NAVIGATION
   ========================================================================== */
   if (!$.browser.msie) {
       $("#mainmenu").animate({
           backgroundPosition: '10px 5px'
       }, 200);

       $("#mainmenu").find(".current").delay("150").animate({
           marginLeft: "+=15px"
       } , 200, "easeInExpo");
   }

    /* =============================================================================
    HOME Slider
    ========================================================================== */
    if ($(".slider").length) {
        var $slider = $(".slider");
        var $slides = $slider.find("img");
        var slideCount = $slides.length;
        var slideWidth = $slides.width();
        $slider.width((slideCount + 2) * slideWidth);

        $slides.first().addClass("firstSlide");
        $slides.last().addClass("lastSlide");
        $('.firstSlide').clone().appendTo('.slider');
        $('.lastSlide').clone().prependTo('.slider');
        $slider.css({'left': -1 * slideWidth + 'px'}); //reset the slider so the first slide is still visible

        window.slideBg = function() {
            var newSliderPosition = parseInt($slider.css("left")) - (parseInt(slideWidth));
            $slider.animate({'left': newSliderPosition + 'px'}, 1750, 'easeInOutExpo', function() {
                if (Math.abs(newSliderPosition) === ((slideCount + 1) * slideWidth)) {
                    //if the slider is displaying the last slide, which is the clone of the first slide
                    $slider.css({ 'left': -slideWidth + 'px' }); //reset the slider back to the first slide
                }
            });

        };
        window.setTimeout(function() {  
            slideBg();
            setInterval(slideBg, 7500);  
        }, 3000); 
        
    }


    /* =============================================================================
    PRODUCTS & INGREDIENTS
    ========================================================================== */
	$("area").click(function(e){ e.preventDefault()});
	
	window.nutritionLayerIsHidden = true;

	$("#productsMap").find("area").mouseenter(function(){
		//if($(".nutritionLayer").filter(":hidden")){
		if(nutritionLayerIsHidden){
			$(".productLayer").stop(true, true).hide();
			
			 if (!$.browser.msie) {
			    $("#"+$(this).attr("class")).fadeIn();
			 } else {
			    $("#"+$(this).attr("class")).show(); 
			 }    
				
		}
	});
	
	$("#content").mouseleave(function(){
		//$(".nutritionLayer").hide();
		$(".productLayer").hide();
		$(".ingredientsLayer").hide();
		$("#spriteIngredients").hide();
		//nutritionLayerIsHidden = true;
	});
	
	//Initialisiere die Layertexte
	if($("#products").length){
			window.infoTextAchter = $("#achter-info").html();
			window.infoTextEiner = $('#nutritionKnoppers').find('header').html();
	}
		
	
	$(".productLayer").click(function(e){
		var that = this;
		e.preventDefault();
		$(".nutritionLayer").hide();
		$(".productLayer").hide();
		$($(this).attr("href")).show();	
		$($(this).attr("href")).find(".tabNavi a").removeClass("active").first().addClass("active");
		nutritionLayerIsHidden = false;	
		if( $(that).hasClass('achter') ){
			$('#nutritionKnoppers').find('header').html(infoTextAchter); 
		}else{
			$('#nutritionKnoppers').find('header').html(infoTextEiner);
		}
		
	});
	
	$(".closeNutritionLayer").click(function(e){
		e.preventDefault();
		$(".nutritionLayer").hide();	
		nutritionLayerIsHidden = true;
	});
	
	$("#ingredientsMap").find("area").mouseenter(function(){
	    $("#ingredientsLayer")
	     .hide()
	     .removeClass()
	     .addClass($(this).attr("alt"))
	     .show();
	    $("#spriteIngredients")
	     .hide()
	     .removeClass()
	     .addClass($(this).attr("data-arrowClass"))
	     .show();

	});
	
	
	if( $(".showNutritions").length > 0){
	    $("#nutritionKnoppers").show();
	    nutritionLayerIsHidden = false;	
	}
	

     /* =============================================================================
    NUTRITION INFOS
    ========================================================================== */
     //$(".tabContent").hide().first().show();
     var $tabNaviLinks = $(".tabNavi").find("a");
     $tabNaviLinks.click(function(e) {
         e.preventDefault();
         $tabNaviLinks.removeClass("active");
         $(this).addClass("active");
         $(this).parents(".nutritionsInfo").find(".tabContent").hide();
         $($(this).attr("href")).show();
     });


    /* =============================================================================
   TV-SPOTS
   ========================================================================== */
    $(".tvspotTeaser").click(function(e) {
        e.preventDefault();

        jwplayer("mediaplayer").setup({
            flashplayer: "/jwplayer/player.swf",
            file: $(this).attr("href"),
            width: 610,
            height: 325,
            image: $(this).attr("data-preview"),
            stretching: "exactfit"
        });
        
        if ( !$.browser.msie ) {
            $("#playerLayer").fadeIn();
            $("#mainmenu").fadeTo('slow', 0.2);
        }else {
            $("#playerLayer").show();
        }
        jwplayer("mediaplayer").play();
    });

    $("#closePlayerLayer").click(function(e) {
        e.preventDefault();
        if ( !$.browser.msie ) {
            $("#mainmenu").fadeTo('slow', 1);
            $("#playerLayer").fadeOut();
        }else{
            $("#playerLayer").hide();
        }    
        jwplayer("mediaplayer").stop();
    });



    /* =============================================================================
   SOUNDBOARD
   ========================================================================== */
    var my_jPlayer = $("#jplayer");

    my_jPlayer.jPlayer({
        /*
    		ready: function () {
    			//$(".track").click();
    		},
    		timeupdate: function(event) {
    		//	my_extraPlayInfo.text(parseInt(event.jPlayer.status.currentPercentAbsolute, 10) + "%");
    		},
    		play: function(event) {
    		//	my_playState.text(opt_text_playing);
    		},
    		pause: function(event) {
    			    //my_playState.text(opt_text_selected);
    		},
    		ended: function(event) {
    			//my_playState.text(opt_text_selected);
    		},
    		*/
        swfPath: "/javascripts/libs",
        cssSelectorAncestor: "#jp_container",
        supplied: "mp3",
        wmode: "window"
    });

    $("#jp_container .track").click(function(e) {
        e.preventDefault();
        my_jPlayer.jPlayer("setMedia", {
            mp3: $(this).attr("href")
        });
        my_jPlayer.jPlayer("play");
        return false;
    });
 
     /* =============================================================================
    JSCROLLPANE
    ========================================================================== */   
   
   if($('.scroll-pane').length){
       $('.scroll-pane').jScrollPane({
           verticalDragMinHeight: 70,
           verticalDragMaxHeight: 70
       });
   } 

});


function setWebtrendsOptoutCookie(cookieDomain){
	//WTLOPTOUT
	 $.cookie('WTLOPTOUT', 'yes', {
          expires : 3650,
          domain: cookieDomain,
          path : '/'
        });
	alert("Der Opt-Out-Cookie wurde für "+cookieDomain+" gesetzt!");
}

