// JavaScript Document
/**
* @author Stephane Roucheray
* @extends jquery
*/

jQuery.fn.carousel = function(previousButton, nextButton, options) {
   var sliderList = jQuery(this).children()[0];
   

   if (sliderList) {
       var increment = jQuery(sliderList).children().outerWidth("true"),
       elmnts = jQuery(sliderList).children(),
       numElmts = elmnts.length,
       sizeFirstElmnt = increment,
       shownInViewport = Math.round(jQuery(this).width() / sizeFirstElmnt),
       firstElementOnViewPort = 1,
       isAnimating = false;
       
       if(numElmts <= 1) { jQuery(previous).hide(); jQuery(next).hide(); } if(shownInViewport == Infinity) return false;
       
       if(options && options.show)
           shownInViewport = options.show;
   
       for (i = 0; i < shownInViewport; i++) {
           jQuery(sliderList).css('width',(numElmts+shownInViewport)*increment + increment + "px");
           jQuery(sliderList).append(jQuery(elmnts[i]).clone());
       }
           
       jQuery(previousButton).click(function(event){
           prev();
           return false;
       
       });
       jQuery(nextButton).click(function(event){
           next();
           return false;
       });
       
       function prev() {
           if (!isAnimating) {
               if (firstElementOnViewPort == 1) {
                   jQuery(sliderList).css('left', "-" + numElmts * sizeFirstElmnt + "px");
                   firstElementOnViewPort = numElmts;
               }
               else {
                   firstElementOnViewPort--;
               }
               animate("+=");
           }

       }
       
       function next() {
           if (!isAnimating) {
               if (firstElementOnViewPort > numElmts) {
                   firstElementOnViewPort = 2;
                   jQuery(sliderList).css('left', "0px");
               }
               else {
                   firstElementOnViewPort++;
               }
               animate("-=");
           }
           
       }
       
       function showIndex(i) {
           firstElementOnViewPort = i;
           i = ((i * sizeFirstElmnt) - sizeFirstElmnt);
           jQuery(sliderList).animate({
               left: "-" + i + "px"
           });
       }

       function animate(operator) {
           jQuery(sliderList).animate({
               left: operator + increment,
               y: 0,
               queue: true
           }, "swing", function(){isAnimating = false;});
           isAnimating = true;
       }
   }
   
   return {
       next : function () {
           next();
       },
       prev : function () {
           prev();
       },
       show : function (i) {
           showIndex(i);
       }
   }
};

//The auto-scrolling function
function slide(){
 $('#simpleNext').click();
}
//Launch the scroll every 2 seconds
var intervalId = window.setInterval(slide, 5000);

//On user click deactivate auto-scrolling
$('#simplePrevious, #simpleNext').click(
function(){
 window.clearInterval(intervalId);
}
);


