/* jquery tip: to add an effect on all browsers BUT IE:
if(!$('.ie').length){
  //do something
}
*/

/* Header slides */
jQuery(function($){
  if($('body.homebody').length){
    $timefade = 500;
    $timeinterval = 6000;
    $currentHeaderSlidesPos = 0;
  
    $loadHeaderSlides = function(){
      if(!$('body.talltop').length){
        $('body').addClass('talltop');
      }
    
      $('#headerslidesWrap').load('/fr/inc_headerslides.spy', function(){
        $('#headerslidesWrap').show();
        //$('#headerslides div.item:first').addClass('active');
      
        $currentHeaderSlidesItem = $('#headerslides' + $currentHeaderSlidesPos); //currently displayed img div
        $currentHeaderSlidesItem.addClass('active');
      
        $('#headerslidesWrap').fadeTo($timefade,1, function(){
          timer = setInterval("$switchHeaderSlides()", $timeinterval);
        });
      });
    }
  
    $switchHeaderSlides = function(){
      $currentSlide = $('#headerslides div.active');
    
      if (!$currentSlide.length){
        $currentSlide = $('#headerslides div.item:last');
      }
        
      $nextSlide = $currentSlide.next().length ? $currentSlide.next() : $('#headerslides div.item:first');
      $currentSlide.addClass('lastActive');
    
      $nextSlide.css({opacity: 0}).addClass('active').animate({opacity: 1}, $timefade, function(){
        $currentSlide.removeClass('active lastActive');
      });
    
      $currentSlide.animate({opacity: 0}, $timefade, function(){
        $currentSlide.css({opacity: 0});
      });
    }
    
    if(!$('.ie').length){
      $(window).focus(function(){
        timer = setInterval("$switchHeaderSlides()", $timeinterval);
      });
      $(window).blur(function(){
        clearInterval(timer);
      });
    }
  
    $loadHeaderSlides();
  }
});

/* Home promos switch */
jQuery(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(250,0, function(){
          $currentBigpic.show();
        });
    
        $activeBigpic.hide();
        $activeBigpic.fadeTo(250,1, function(){
          $activeBigpic.removeClass('itemOff');
          $currentPos = $activePos;
          $currentBigpic = $('#homepromos' + $currentPos);
        });
      }
  
      $allowPromoSwitch = 1;
    }
  }

  timerPromos = setInterval("$switchSlides($currentPos+1)", 5000);

  $('#homepromosThumbs li a').click(function(){
    if($allowPromoSwitch){
      $allowPromoSwitch = 0;
      clearInterval(timerPromos);
    
      $activeThumb = $(this).parent().attr('id'); //clicked thumbnail
      $activePos = parseInt($activeThumb.substring($activeThumb.length-1));
  
      $switchSlides($activePos);
  
      $allowPromoSwitch = 1;
    }
  
    return false;
  });

  $('#homepromos').hover(function(){
    clearInterval(timerPromos);
  }, function(){
    timerPromos = setInterval("$switchSlides($currentPos+1)", 5000);
  });
});
