

function browserIsIE7(){
   return (/MSIE (7)/.test(navigator.userAgent) && navigator.platform == "Win32");
}


$(function() {
  
  
  if ($.browser.msie && $.browser.version.substr(0,1)<7) {
    
    $('#flash').css('top', '72px');
    
    $('#wrap').prepend('<div id="ie6suggestion" style="padding: 5px; background: #ffe5e6 url(/fileadmin/templates/img/ie6-warning.png) no-repeat 5px 5px; margin: 5px auto; width: 650px; height: 50px; padding-left: 60px; line-height: 15px;border: 1px solid red;">Sie benutzen einen veralteten Browser, weshalb nicht gewährleistet werden kann, dass die Webseite korrekt dargestellt wird. Wir empfehlen Ihnen daher ganz unverbindlich den Umstieg auf einen zeitgemäßen Browser, wie <a href="http://www.mozilla-europe.org/de/firefox/" target="_blank">Firefox</a>, <a href="http://www.apple.com/de/safari/" target="_blank">Safari</a>, <a href="http://www.google.com/chrome" target="_blank">Chrome</a> oder <a href="http://www.microsoft.com/germany/windows/internet-explorer/browse-with-confidence.aspx" target="_blank">Internet Explorer 8</a>.</div>');

  
  } 
	
  

  $.preloadCssImages();
  
  $('.drucktop .drucken').click(function() {
    window.print();
    return false;
  });
  



// Klick auf Produkt Info Button
$('.product_info_button').click(function(e) {
	var name = $.trim($(this).attr('class').replace('product_info_button', '')); // name = mm_pet_himbeere, mm_becher_schoko, ...
	var box = $('#products_contentbox');
	
											
	// Passenden Content einblenden
	$('.product_info:not(.product_info.'+name+')', box).fadeOut();
	$('.product_info.'+name).fadeIn();
	
	// nur runterscrollen, wenn die infobox noch nicht aufgeklappt ist
	if (!box.hasClass('tall')) { 
		$.scrollTo('.product_info.'+name, 1000, {'easing': 'easeInOutQuint'});
	}
	
	// Slidebox vergrößern, wenn sie es nicht schon ist
	showProductInfoBox();
	
	// Nutrition-Slide-Tabelle zurücksetzen
	resetNutritionTable();
	
	// Passendes Quiz einblenden.

	var quiz_name = quiz_product_relations[name];
	
	if (quiz_name != "") { // Kein Quiz: Quiz ausblenden
	  var quizze = $('#quiz li.quiz:not(.cloned)');
  	var quiz_to_show = -1;
  	for (var i = 0; i < quizze.length; i++) {
  		if ($(quizze[i]).hasClass('q_' + quiz_name)) {
  			quiz_to_show = i;
  		}
  	}
  	if (quiz_to_show != -1) {
  		$('#quiz .anythingSlider').anythingSlider(quiz_to_show+1);
  	}
	}
	

	
	
	
});		

// Produkt-Infobox wieder einklappen
$('.product_info_close_button').click(function() {
	hideProductInfoBoxes(true);
	$.scrollTo('#products_contentbox', 1000, {'easing': 'easeInOutQuint'});						
	resetNutritionTable();
});


// Nutrition-Tabelle sliden
$('.toggle_nutrition').click(function() {
	var first_table = $('.nutrition .nutrition_wrapper.first');
	if (first_table.css('left') == '0px') {
		
		first_table.animate({'left' : '-290px'}, 500, 'easeOutExpo');
		$('.nutrition .nutrition_wrapper.last').animate({'left' : '0px'}, 500, 'easeOutExpo');
		$(this).removeClass('next').addClass('previous').text('zurück');
		
	} else {
		first_table.animate({'left' : '0px'}, 500, 'easeOutExpo');
		$('.nutrition .nutrition_wrapper.last').animate({'left' : '290px'}, 500, 'easeOutExpo');
		$(this).removeClass('previous').addClass('next').text('weiter');
	}
});

		
		
			
  // Top Link
  $('.top').click(function() {
    $.scrollTo('#top', 500, {'easing': 'easeInOutQuint'});
    return false;
  });




  $('#search_button').toggle(function() {
    $('#search_box').fadeIn(200);
    $('#search_input').focus();
    $('#search_products').css('backgroundImage', 'url(/fileadmin/templates/img/header_top_search_products_bg_active_search.png)');
  }, 
  function() {
    $('#search_box').fadeOut(200);
    $('#search_products').css('backgroundImage', 'url(/fileadmin/templates/img/header_top_search_products_bg.png)');
  });

  $('#products_button').click(function(e) {
    $('#products_box').fadeIn(200);
    $('#search_box').fadeOut(200);
    $('#search_products').css('backgroundImage', 'url(/fileadmin/templates/img/header_top_search_products_bg.png)');
    e.stopPropagation();
  });

  $('*').click(function(e) {
    e.stopPropagation();
    if ($(this).attr('id') != 'products_button' && $(this).attr('id') != 'products_box') {					
      $('#products_box').fadeOut(200);

    }
  });

  // Extract the product names so we can search them
  var products = [];
  $('.products_column a').each(function() {
    var code = $(this).attr('id').split('_').slice(2).join("_");
    var text = $(this).text().toLowerCase().replace('&reg;', '');
    var href = $(this).attr('href');
    products.push([code, text, $(this).text(), href]);
  });

  $('#search_input').keyup(productfinder);
  $('#search_form').submit(function() {productfinder(); return false;});

  function productfinder() {
    var query = $('#search_input').val().toLowerCase();
    var found_products = [];
    if (query.length != 0) {
      for (i in products) {						
        if (products[i][0].search(query) != -1 || products[i][1].search(query) != -1) {
          found_products.push(i);
        }						
      }
    }
    var sr = $('#search_results');
    sr.empty();
    if (found_products.length > 0) {
      for (i in found_products) {

        var product = $('<a></a>')
        .attr({	'href': ''+products[found_products[i]][3],
        'id' : 'search_result_' + products[found_products[i]][0],
        'class' : 'search_result',
        'style' : 'background-image: url(/fileadmin/templates/img/products/pf_icon_'+products[found_products[i]][0]+'.png)'
      })
      .text(products[found_products[i]][2]);
      sr.append(product);
    }
  }
}


//// Main Menu /////
$('#mainmenu > li > a').hover(function() {
  if (!(($(this).attr('id') == 'mainmenu_1_getraenke' && $('#mainmenu_sub1').is(':visible'))
  || ($(this).attr('id') == 'mainmenu_1_joghurt' && $('#mainmenu_sub2').is(':visible'))
  || ($(this).attr('id') == 'mainmenu_1_desserts' && $('#mainmenu_sub3').is(':visible')))) {

    $('.mainmenu_sub').stop().css('top', '-30px').hide();
  }
});
$('#mainmenu_1_getraenke').hover(function() {
  $('#mainmenu_sub1').show().animate({top: '0px'}, {duration: 2000, easing: 'easeOutElastic'});					
  }, null);

  $('#mainmenu_1_joghurt').hover(function() {
    $('#mainmenu_sub2').show().animate({top: '0px'}, {duration: 2000, easing: 'easeOutElastic'});					
    }, null);

    $('#mainmenu_1_desserts').hover(function() {
      $('#mainmenu_sub3').show().animate({top: '0px'}, {duration: 2000, easing: 'easeOutElastic'});							
      }, null);



      // Content slider //
      var anythingSliderOptions = {
        easing: "easeOutExpo",        // Anything other than "linear" or "swing" requires the easing plugin
        autoPlay: false,                 // This turns off the entire FUNCTIONALY, not just if it starts running or not.
        delay: 3000,                    // How long between slide transitions in AutoPlay mode
        startStopped: false,            // If autoPlay is on, this can force it to start stopped
        animationTime: 600,             // How long the slide transition takes
        hashTags: true,                 // Should links change the hashtag in the URL?
        buildNavigation: true,          // If true, builds and list of anchor links to link to each slide
        pauseOnHover: true,             // If true, and autoPlay is enabled, the show will pause on hover
        startText: "Go",             // Start text
        stopText: "Stop",               // Stop text
        navigationFormatter: formatText,       // Details at the top of the file on this use (advanced use)
        afterSlideCallback: afterSlideCallback		// rob - Gets called after a slide occurs, if the slide has class="call_afterSlideCallback"
      };
      $('.contentbox .anythingSlider').anythingSlider(anythingSliderOptions);

      function afterSlideCallback(previousPage, currentPage) {					
        // Laufende Videos ausblenden und somit anhalten, wenn der Slide gewechselt wird
        $('.video_container', previousPage).hide();
        
        // Ausgeklappte Produktinfos einklappen
        var pcb = $(previousPage).parents('#products_contentbox');
        if (pcb.length != 0) {
          if (pcb.hasClass('tall')) {
            hideProductInfoBoxes();
            resetNutritionTable();
            if (previousPage != currentPage) { // Nur scrollen, wenn per Hand geslidet wurde.afterSlideCallback wird nämlich auch beim Laden der Seite aufgerufen
					    $.scrollTo('#products_contentbox', 1000, {'easing': 'easeInOutQuint'});
					  }
					}
        }
        
        // Ausgeklappte Quiz-Resultate einklappen
        if ($(previousPage).hasClass('quiz')) {
          var quiz_name = $.trim($(previousPage).attr('class').replace('quiz', '').replace('call_afterSlideCallback', '')).replace('q_', '');
          
          $('#quiz .quiz_result').hide();					
							
        	
					// Buttons zurücksetzen
					$('.answer', $(previousPage)).css('backgroundImage', 'url(/fileadmin/templates/img/quiz/'+quiz_name+'_back.png)');
					$('.answer_letter', $(previousPage)).css('color', '#e70101');
        }
        
        // Hat das Element eine dazugehörige Funktion im inline_html? Wenn ja, aufrufen
        var classes = $(previousPage).attr('class').split(' ');
        var function_name = '';
        for (c in classes) {
          if (classes[c].substring(0,9) == 'slidebox_') {
            function_name = classes[c].substring(9);
          }
        }
        if (function_name != '') {
          var fn = window['afterSlideCallback_'+function_name];
          if (typeof fn == 'function') {
            fn(previousPage);
          }
        }
        
        if ($(currentPage).hasParent('#products_contentbox')) {
          
          // Hat das current Element ein Quiz? Einblenden, ansonsten ausblenden          
          if($(currentPage).hasClass('hasQuiz')) {
            $('#quiz').show();
          } else {
            $('#quiz').hide();
          }
          
          // Hat es eine neue Flash Header UID? Wenn ja, setHeader(uid) aufrufen
          var classes = $(currentPage).attr('class');
          var header_uid = -1;
          if (classes.indexOf('flash_header_uid_') != -1) {
            var carr = classes.split(" ");
            for (i in carr) {
              if (carr[i].substring(0,17) == 'flash_header_uid_') {
                header_uid = carr[i].substring(17);
              }
            }
            
            var old_header_uid = -1;
            var old_classes = $(previousPage).attr('class');
            if (old_classes.indexOf('flash_header_uid_') != -1) {
              var carr = old_classes.split(" ");
              for (i in carr) {
                if (carr[i].substring(0,17) == 'flash_header_uid_') {
                  old_header_uid = carr[i].substring(17);
                }
              }
            }
            if (old_header_uid != header_uid && header_uid != -1) {
              setFlashHeader(header_uid);
            }
            
          }
        }
        
        
        
      }

      function formatText(index, panel) {
        return $('.slidebox_title_short', panel).length != 0 ? $('.slidebox_title_short', panel).html() : $('.slidebox_title', panel).html();				
      }
      



    });
    
    
    $.fn.hasParent = function(objs) {
    	// ensure that objs is a jQuery array
    	objs = $(objs); var found = false;
    	$(this[0]).parents().andSelf().each(function() {
    		if ($.inArray(this, objs) != -1) {
    			found = true;
    			return false; // stops the each...
    		}
    	});
    	return found;
    }

   // Zu Slidebox pgXXX sliden
    // function navTo(pageID){     
    function setContentTeaser(pageID){

      
      var boxen = $('#products_contentbox li:not(.cloned)');
      
      var box_nr = 0;
      for (i = 0; i < boxen.length; i++) {
        if ($(boxen[i]).hasClass(pageID)) {
          $('#products_contentbox .anythingSlider').anythingSlider(i+1);
          break;
        }
      }           
    }
    
    
    // Produkt Box ausklappen
    function showProductInfoBox() {
      var box = $('#products_contentbox');
      
      if (box.length > 0 && !box.hasClass('tall')) {
        // Höhe +670px
  			newHeight = $('.anythingSlider', box).height() + 670;
			
  			//$('.anythingSlider,.sliderWrap', box).animate({height: newHeight}, 1000).css('overflow', 'visible');
  			$('.anythingSlider', box).animate({height: newHeight}, 1000).css('overflow', 'visible');
  			$('.sliderWrap', box).animate({height: newHeight}, 1000).css('overflow', 'visible');
  			$('.anythingSlider .wrapper', box).animate({height: newHeight}, 1000);
  			$('.thumbNav', box).animate({'top': newHeight+10}, 1000);
  			$('.wrapper li .drucktop', box).animate({'top': newHeight-40}, 1000);		
  			
  			
  			//$('.anythingSlider,.sliderWrap,.anythingSlider .wrapper', box).css('height', newHeight);
  			//$('.thumbNav', box).css('top', newHeight+10);
  			//$('.wrapper li .drucktop', box).css('top',newHeight-40);				
			
  			box.addClass('tall');
		  }
    }
    

    
    // Produkt Box einklappen
    function hideProductInfoBoxes(animate) {    
      var box = $('#products_contentbox');    
      if (box.length > 0 && box.hasClass('tall')) {
        newHeight = $('.anythingSlider', box).height() - 670;
			  

			  
			  if (animate == true) {
			    $('.anythingSlider', box).animate({height: newHeight}, 1000).css('overflow', 'visible');
			    $('.sliderWrap', box).animate({height: newHeight}, 1000).css('overflow', 'visible');
    			$('.anythingSlider .wrapper', box).animate({height: newHeight}, 1000);
    			$('.thumbNav', box).animate({'top': newHeight+10}, 1000);
    			$('.wrapper li .drucktop', box).animate({'top': newHeight-40}, 1000);
    			$('.product_info', box).fadeOut();
    			
			  } else {
			    $('.anythingSlider,.sliderWrap,.anythingSlider .wrapper', box).height(newHeight);
  				$('.thumbNav', box).css('top', newHeight+10);
  				$('.wrapper li .drucktop', box).css('top', newHeight-40);						
  			  $('.product_info', box).hide();
			  }
			  

      
				box.removeClass('tall');
		  }
    }
    
    function resetNutritionTable() {
			// Nutrition-Infos in Ausgangsstellung bringen	
			$('.nutrition .nutrition_wrapper.first').css('left', '0px');
			$('.nutrition .nutrition_wrapper.last').css('left', '290px');
			$('.toggle_nutrition').removeClass('previous').addClass('next').text('weiter');
		}
		
	function navTo(pageID){
		var fv = swfobject.getFlashPlayerVersion();
		var obj = swfobject.getObjectById("flash");
		if(fv['major'] >= 10 && obj && typeof obj.setPage != "undefined") {    										    
			obj.setPage(pageID); 
			return false;
		} else {
			return true;
		}
	}

	// kann aus HTML-Content aufgerufen werden
	// > führt zum Slide des Headers auf Headerbild des Nodes mit Child uID=uID, es erfolgt anschlieﬂend kein Seitenaufruf
	function setFlashHeader(uID){
		var fv = swfobject.getFlashPlayerVersion();
		var obj = swfobject.getObjectById("flash");
		if(fv['major'] >= 10 && obj && typeof obj.setPage != "undefined") {    										    
			obj.setHeader(uID); 
			return false;
		} else {
			return true;
		}
	}
	
	// MW: fuer Produktgruppen
	function scrollToQuiz(quiz_name) {
		// Passendes Quiz einblenden.

  		var quiz_to_show = -1;
  		  var quizze = $('#quiz li.quiz:not(.cloned)');
  		for (var i = 0; i < quizze.length; i++) {
  			if ($(quizze[i]).hasClass('q_' + quiz_name)) {
  				quiz_to_show = i;
  			}
  		}
  		if (quiz_to_show != -1) {
  			$('#quiz .anythingSlider').anythingSlider(quiz_to_show+1);
  		}
	}
	
	

function openMiniPlayer(playerID,flvSrc) {
     $('#miniplayerwrapper_' + playerID).show();
     var c = document.getElementById('player_' + playerID);

     if (!c) {
      var d = document.createElement("div");
      d.setAttribute('id', 'player_' + playerID);
      document.getElementById("miniplayerwrapper_inner_" + playerID).appendChild(d);
     }

     var flashvars = {};
      flashvars.flvPath=flvSrc;
      flashvars.autoplay = "true";

     var params = {};
      params.menu = "false";
      params.scale = "noscale";
      params.wmode = "opaque";
     var attributes = {};
     swfobject.embedSWF("/fileadmin/videoplayer/Mueller_VideoPlayer_small.swf", "player_" + playerID, "532", "274", "10.0.0", "/fileadmin/templates/swf/expressInstall.swf", flashvars, params, attributes);
    }

    

    function closeMiniPlayer(playerID) {
     swfobject.removeSWF('player_' + playerID);
     $('#miniplayerwrapper_' + playerID).hide();
    }
    
