Slider = function(){
  var path;
  var page = 1;
  var timeout = 5000;
  var mouseOver = false;
  var nextAutoSlideAt;
  var moved_pages_without_interaction = 0;
  var max_auto_sliding_pages = 5;

  function next(){
    moved_pages_without_interaction = 0;
    move(1);
    trackClick('navigation-click')
  }

  function prev(){
    moved_pages_without_interaction = 0;
    if(page==1)return;
    move(-1);
    trackClick('navigation-click')
  }

  function stop(){
    nextAutoSlideAt = NaN;
    indicateStop(true);
  }

  function indicateStop(stopped){
    if(stopped)$('slider_stop').addClassName('inactive');
    else $('slider_stop').removeClassName('inactive')
  }

  function toggleStop(){
    if(stopped()){
      setAutoSlide();//resume
    } else {
      stop();
    }
  }

  function stopped(){
    return isNaN(nextAutoSlideAt);
  }

  function move(direction){
    moved_pages_without_interaction ++;
    page += direction;
    setAutoSlide(9999999);//make the button active again, and do not slide again while waiting
    new Effect.Move($('recom_slider_content_inner'), {x: -1 * direction * 200, y:0 ,duration: 0.2});
    new Effect.Fade($('recom_slider_content_inner'), {duration: 0.2});
    new Ajax.Updater('recom_slider_content', path+'_content?page='+page,{onSuccess:function(){
      if(!stopped())setAutoSlide();//do not resume if user clicked on stop in the meantime
      setTimeout(observeEvents,200);
      noSpinner = false;
    }});
  }

  function trySlide(){
    if(mouseOver || stopped())return;
    if((new Date).getTime() < nextAutoSlideAt)return;
    noSpinner = true;
    move(1);
  }

  function setAutoSlide(time){
    nextAutoSlideAt = (new Date).getTime() + (time || timeout);
    indicateStop(false);
    if(moved_pages_without_interaction >= max_auto_sliding_pages)stop();
  }

  function trackClick(name){
    Counter.incrementForToday(path, name)
  }

  function observeEvents(){
    var content = $('recom_slider_content_inner');
    Event.observe(content, 'mouseover', function(){
      if(!stopped())indicateStop(true);
      mouseOver = true;
      moved_pages_without_interaction = 0;
    });

    Event.observe(content, 'mouseout', function(){
      if(!stopped())setAutoSlide(timeout / 2);//do not slide imidiatly after mouseout
      mouseOver = false;
    });

    Event.observe(content, 'click', function(){
      stop();
      trackClick('clicks');
    });
  }

  function start(id, load_path){
    path = load_path;
    new Ajax.Updater(id, path, {evalScripts:true, onSuccess:function(){
      setTimeout(function(){
        observeEvents();
        setAutoSlide();
        setInterval(trySlide,500);
      }, 100);
    }});
  }

  return {start:start, next:next, prev:prev, toggleStop: toggleStop}
}();