
/* Home promos switch */
$(document).ready(function(){
  $allowPromoSwitch = 1;
  
  $currentPos = 0;
  $currentBigpic = $('#homepromos' + $currentPos);
    
  $('#homepromosThumbs').show();
    
  $switchSlides = function(pos){
    if($currentPos != pos){
      if(pos >= $('#homepromos .homepromosItem').length)
        pos = 0;
      
      $activePos = pos;
      $activeThumb = $('#homepromosThumbs' + $activePos);
      $activeBigpic = $('#homepromos' + $activePos);
        
      $activeThumb.addClass('current');
      $('#homepromosThumbs li').not($activeThumb).removeClass('current');
      
      if($currentPos != $activePos){
        $currentBigpic.fadeTo(1000,0, function(){
          $currentBigpic.hide();
        });
        
        $activeBigpic.show();
        $activeBigpic.fadeTo(1000,1, function(){
          $activeBigpic.removeClass('itemOff');
          $currentPos = $activePos;
          $currentBigpic = $('#homepromos' + $currentPos);
        });
      }
      
      $allowPromoSwitch = 1;
    }
  }
    
  timer = setInterval("$switchSlides($currentPos+1)", 5000);
  
  $('#homepromosThumbs li a').click(function(){
    if($allowPromoSwitch){
      $allowPromoSwitch = 0;
      clearInterval(timer);
        
      $activeThumb = $(this).parent().attr('id'); //clicked thumbnail
      $activePos = parseInt($activeThumb.substring($activeThumb.length-1));
      
      $switchSlides($activePos);
      
      $allowPromoSwitch = 1;
    }
      
    return false;
  });
  
  $('#homepromos').hover(function(){
    clearInterval(timer);
  }, function(){
    timer = setInterval("$switchSlides($currentPos+1)", 5000);
  });
});

