$(document).ready(function() {

  $(".lastViewed.jcarousel-init").addClass("jcarousel-skin-lastViewed");

  /* 
   * JCarousels bei Tabswechsel deaktivieren / neu initialisieren 
   */

  // INIT der JCarousels

  // Carousel-Objekte merken
  var brandCarousel = null;
  var lastViewedCarousel = null;

  // Carousel-Inhalte merken
  var brandsList = $("#brandTeaser").children("li");
  var lastViewedList = $(".lastViewed.jcarousel-init").children("li");

  // init brandsCarousel
  function brandcarousel_initCallback(carousel)
  {
    brandCarousel = carousel;

    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
      carousel.startAuto(0);
    });
    carousel.buttonPrev.bind('click', function() {
      carousel.startAuto(0);
    });
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
      carousel.stopAuto();
    }, function() {
      carousel.startAuto();
    });
  };

  // init lastViewedCarousel
  function lastViewedCarousel_initCallback(carousel)
  {
    lastViewedCarousel = carousel;

    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
      carousel.startAuto(0);
    });
    carousel.buttonPrev.bind('click', function() {
      carousel.startAuto(0);
    });
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
      carousel.stopAuto();
    }, function() {
      carousel.startAuto();
    });
  };
  
  // brandsCarousel initial anzeigen (1. Tab ist standardmäßig geöffnet)
  jQuery('#brandTeaser').show();
  jQuery('#brandTeaser').jcarousel ({
    auto: 2,
    wrap: 'circular',
    initCallback: brandcarousel_initCallback
  });

  // Auf Tab-Clicks hören
  var firstClick3 = true; // beim ersten Mal im 4. Tab muss initialisiert werden, danach nur resetted
  
  $("#tabs li").click(function() {
    // alle Carousels ausschalten
    if (brandCarousel != null && !$("#content0").is(':visible')) {
      brandCarousel.stopAuto();
    };

    if ($("#content0").is(':visible')) {  
      // Tab 1 einblenden
      brandCarousel.startAuto();
    } else if ($("#content3").is(':visible')) {
      // Tab 4 einblenden
      if (firstClick3) {
        // initialize
        $(".lastViewed.jcarousel-init").jcarousel({
          auto: 0,
          scroll: 1,
          visible: 5,
          initCallback: lastViewedCarousel_initCallback
        });
        firstClick3 = false;
      }
    }
  });

  // jCarousel auf Kategorieseite anzeigen
  $('#lastViewedScroller').jcarousel({
      auto: 0,
      vertical: true,
      scroll: 1,
      visible: 2,
      itemFallbackDimension: 300
  });
  if($('#colorLayer').get() != '') { colorVariant(); };
});

$(document).ready(function() {
  $("#home #content3").find("ul.lastViewed").css("width", "800px");
});


$(document).ready(function(){
  $('.delivery').mouseenter(function(e){
    var offsetContainer = $("#detail .description").position().top;
    var offset = 135;
    $('.hint').css('top', (e.pageY - offsetContainer - offset) + 'px').show();
    }).mouseleave(function(e){
      $('.hint').hide();
  }); 
});


/*
$(document).ready(function(){
  if ($("#lastViewed").length == 1)
  {
    var border = 2;

    var contentHeight = $("#content").outerHeight(true);
    var subnavHeight = $("#subnav").outerHeight(true);
    var lVMinHeight = $("#lastViewed").outerHeight(false);
    var lVMarginPx = $("#lastViewed").css('marginTop');
    var lVMargin = parseInt(lVMarginPx.substring(0, lVMarginPx.length - 2), 10);

    // Vorhandene Boxen, innerer marginBottom #content, Borders
    var lVCalculatedHeight = contentHeight - subnavHeight - lVMargin - 15 - border * 2;

    var $next = $("#lastViewed").find(".jcarousel-next-vertical");
    var next = $next.outerHeight(true);
    var nextPos = lVCalculatedHeight - next * 2 - border;

    if(lVCalculatedHeight > lVMinHeight)
    {
      $("#lastViewed").css("height", lVCalculatedHeight);
      $next.css("top", nextPos);
    }
  }
});
*/

$(document).ready(function() {
  $("#commentsForm .stars .images").children("img").click(function() {
    var hiddenValue = $(this).attr("class").split(" ")[0].substr(5,1);
    $("#commentsForm input.starsValue").val(hiddenValue);
    
    $("#commentsForm .stars .images").children("img").each(function() {
      var current = $(this);
      var currentStar =  current.attr("class").split(" ")[0].substr(5,1);
      if (currentStar <= hiddenValue) {
        $("#commentsForm .images img.stars" + currentStar + ".starEmpty").hide();
        $("#commentsForm .images img.stars" + currentStar + ".starFull").show();
      } else {
        $("#commentsForm .images img.stars" + currentStar + ".starEmpty").show();
        $("#commentsForm .images img.stars" + currentStar + ".starFull").hide();                        
      }
    });
  });
});

