/* Simple Slider for Schaller und Partner Homepage           */
/* the initialiser should be called on the container element */
(function($){
  $.fn.simpleSlider = function ( options ){
    var settings = {
      duration:1500,
      delay:4000,
      initialDelay:3000,
      itemWidth:990,
      itemsHeight:500,
      easing:'swing'
    }
    
    if(options){
      $.extend( settings, options);
    }
    
    // grab some important containers
    var $self = this; // the stage object
    var wrapper = $(document.createElement('div'));
    var stageWidth = $self.width();
    var count = $self.find('li').length; // (int)
    if(count < 2) return this; // bail if there's nothing to slide
    
    var maxScroll = count*stageWidth;
    var timeout;
    var autoScroll = true;
    var animating = false;
    
    // reset scrollLeft
    $self.scrollLeft(0);
    
    // duplicate the first item and put it at the end
    var firstitem = $self.find('li:first').clone();
    $self.find('ul').append(firstitem);
    
    // setup the wrapper
    wrapper.css({position:'relative',width:'100%',height:'100%'});
    
    // setup the arrow buttons
    $self.wrap(wrapper);
    $self.parent().append("<div id='arrowNext' class='slider_prev_next' width='18' height='28' /><div id='arrowPrev' class='slider_prev_next' width='18' height='28' />");
    
    var onMouseEnter = function(){
      $self.unbind('mouseenter');
      $self.parent().bind('mouseleave',onMouseLeave);
      // console.log('mouse enter');
      clearTimeout(timeout);
      autoScroll = false;
      $self.parent().find('.slider_prev_next').show();
      $('#arrowNext').click(slideLeft);
      $('#arrowPrev').click(slideRight);
      
    }
    
    var onMouseLeave = function(){
      $self.parent().unbind('mouseleave');
      $self.bind('mouseenter',onMouseEnter);
      
      // kill the arrows
      $('#arrowNext').unbind('click').hide();
      $('#arrowPrev').unbind('click').hide();
      
      autoScroll = true;
      timeout = setTimeout(slideLeft,settings.delay);
    }
    
    var slideLeft = function(){

      // console.log('slideLeft',$self.scrollLeft(), maxScroll);
      
      // wait till finished before going again.
      if(animating) return;
      animating = true;
      
      // reset scroll if already at end
      if($self.scrollLeft() >= maxScroll){
        $self.scrollLeft(0);
      }
      
      // do the animation
      $self.animate(
        {scrollLeft:$self.scrollLeft()+stageWidth},
        settings.duration,
        settings.easing,
        slideComplete
      );
    };
      
    var slideRight = function(){
      
      // console.log('slideRight',$self.scrollLeft(), maxScroll);
      
      // wait till finished before going again.
      if(animating) return;
      animating = true;
      
      // reset scroll if already at end
      if($self.scrollLeft() <= 0){
        $self.scrollLeft(maxScroll);
      }
      
      // do the animation
      $self.animate(
        {scrollLeft:$self.scrollLeft()-stageWidth},
        settings.duration,
        settings.easing,
        slideComplete
      );
    };
    
    var slideComplete = function(){
      animating = false;
      if(autoScroll){
        // console.log('restart autoscroll');
        timeout = setTimeout(slideLeft,settings.delay);
      } else {
        // console.log('autoScroll off');
      }
    };
    
    
    // Setup mouse interaction
    $self.bind('mouseenter',onMouseEnter);
    
    
    // start the first animation
    timeout = setTimeout(slideLeft,settings.initialDelay);
    
    return this;
  }
})(jQuery);

