// JCover © 2011 Stoffler Musik AG
$(document).ready(function() {
   //show paging, activate first link
   $(".jcover_paging").show();
   $(".jcover_paging a:first").addClass("active");
   //get image size, image reel size and number of images
   var imageWidth = $(".jcover_window").width();
   var imageSum = $(".jcover_imagereel img").size();
   var imageReelWidth = imageWidth * imageSum;
   //set selected image index
   var number = 0;
   //hide all headers and text, show currently selected
   $('.index_left_jcoverheader').hide().eq(number).show();
   $('.index_left_jcovertext').hide().eq(number).show();
   //adjust image reel to its new size
   $(".jcover_imagereel").css({'width' : imageReelWidth});
   //paging and slider function
   rotate = function() {
      // get number of times to slide (rel is set in html)
      var triggerID = $active.attr("rel") - 1;
      // set the distance, the image reel needs to slide
      var imageReelPosition = triggerID * imageWidth;
      //remove all active classes
      $(".jcover_paging a").removeClass('active');
      //add active class (the $active is
      //declared in rotateSwitch function)
      $active.addClass('active');
      //get selected image index
      number = triggerID;
      //hide all headers and text, show currently selected
      $('.index_left_jcoverheader').hide().eq(number).show();
      $('.index_left_jcovertext').hide().eq(number).show();
      //slider animation, slide left, duration 500ms
      $(".jcover_imagereel").animate({left:-imageReelPosition},500);
   };
   //rotation and timing event function
   rotateSwitch = function() {
      //set timer, repeat every 7000ms
      play = setInterval(function() {
         //move to the next paging
         $active = $('.jcover_paging a.active').next();
         //if paging reaches the end
         if ($active.length === 0) {
            //go back to first
            $active = $('.jcover_paging a:first');
         }
         //trigger the paging and slider function
         rotate();
      }, 7000);
   };
   //run function on launch
   rotateSwitch();
   //onhover event
   $(".jcover_imagereel a").hover(function() {
      //stop the rotation
      clearInterval(play);
   }, function() {
      //resume rotation timer
      rotateSwitch();
   });
   //onclick event
   $(".jcover_paging a").click(function() {
      //activate the clicked paging
      $active = $(this);
      //reset timer and stop rotation
      clearInterval(play);
      //trigger rotation immediately
      rotate();
      //resume rotation timer
      rotateSwitch();
      //prevent browser jump to link anchor
      return false;
   });
});
