var rotate;
$(function() {
   $('.hero').each( function(i) {
      var a = $('.hero').length;
      for (var n = 0; n < a; ++n) {
         if (n == i) {
            $(this).find('.hero-nav').append('<div class="active">&nbsp;</div>');
         } else {
            $(this).find('.hero-nav').append('<div>&nbsp;</div>');
         }
      }
   });
   $('div.hero:gt(0)').fadeOut(1);
   $('div.hero-nav div:not(.active)').click( function() {
      clearInterval(rotate);
      if ($(this).parents('div.hero').is(':animated'))
         return false;
      $('div.hero:visible').css('z-index', 1);
      $('div.hero:visible').fadeOut(1000);
      $('div.hero').eq($(this).parent().children().index(this)).fadeIn(800);
      $('div.hero').eq($(this).parent().children().index(this)).css('z-index', 2);
      rotate = setInterval('rotateHero()', 15000);
   });
   $('#search input').click( function() { $(this).css('background-image', 'none') } );
   $('#search input').val('');
   $('.expandcollapse').click( function() {
      var div = $(this).siblings('div:last');
      var img = $(this).parent().children('img.expandcollapse');
      if (div.is(':animated'))
         return;
      if (div.is(':visible')) {
         div.slideUp(250, function() { img.attr('src', '/assets/images/button-plus.png'); });
      } else {
         div.slideDown(250, function() { img.attr('src', '/assets/images/button-minus.png'); });
      }
   });
   $('.crossfade').css('position', 'relative');
   $('.crossfade').children('a').css({position: 'absolute', top:0, left:0}).each(function(i) {
      $(this).css('z-index', i);
   });
   $('.crossfade').each(function(i) {
      $(this).width($(this).children('a').width());
      $(this).height($(this).children('a').height());
   });
   $('.crossfade a:last-child').css('opacity', 0);
   $('.crossfade').hover(function() {
      $(this).children('a:last-child').stop();
      $(this).children('a:last-child').animate({opacity: 1}, 500);
   }, function() {
      $(this).children('a:last-child').stop();
      $(this).children('a:last-child').animate({opacity: 0}, 500);
   });
   $('.product-image-thumb').css('cursor', 'pointer');
   $('.product-image-thumb img').click( function() {
      $('#product-image-main img').attr('src', $(this).attr('src'));
   });
   if ($('div.hero').length > 0)
      rotate = setInterval('rotateHero()', 15000);
   $('a').each(function() {
      if($(this).attr('href').indexOf('.pdf') >= 0 || $(this).attr('href').indexOf('http://') >= 0)
         $(this).attr('target', '_blank');
   });
});

$('#leftnav-body').ready(function() {
   var h = $('#leftnav-body').height();
   if ($('#content-main').height() < h - 3) {
      $('#content-main').css('min-height', h - 3);
   } else {
      $('#leftnav-body').height($('#content-main').height() + 3);
   }
});

function rotateHero() {
   if ($('div.hero:animated').length > 0)
      return false;
   var currHero = $('div.hero').index($('div.hero:visible'));
   var nextHero = (currHero + 1) % $('.hero').length;
   $('div.hero:visible').css('z-index', 1);
   $('div.hero:visible').fadeOut(1000);
   $('div.hero').eq(nextHero).fadeIn(800);
   $('div.hero').eq(nextHero).css('z-index', 2);
}
