/************************************************
*   scrollerMoo v.0                             *
*   Http: WwW.developer.ps/moo/scrollermoo      *
*   Dirar Abu Kteish dirar@zanstudio.com        *
*   The code was inspired from jd.gallery.js    *
*    and jd.gallery.css. WebSite:               *
*    smoothgallery.jondesign.net                *
/***********************************************/

var scrollerMoo = new Class({
    initialize: function(element, options) {
    this.setOptions({
      steps: 3,
      wrapperClass: 'div.scrollerMooWrapper',
      scrollerItemSelector: 'div.scrollerMooItem'
      }, options);
      this.currentPos = 0;
      this.scrollerItems = null;
      this.scrollerElement = element;
      this.wrapperDiv = element.getElement(this.options.wrapperClass);

      this.scrollRes = new Fx.Scroll(this.wrapperDiv, {});

      this.scrollPeriodicalRight = function(){
        this.scrollRes.scrollTo(this.wrapperDiv.scrollLeft+160, 0);
      }.bind(this);

      this.scrollPeriodicalLeft = function(){
        this.scrollRes.scrollTo(this.wrapperDiv.scrollLeft-160, 0);
      }.bind(this);

      this.initScrollerMoo();

      this.timer = null;

  },
  initScrollerMoo: function() {
    var ele = this.scrollerElement;
    var options = this.options;
    this.scrollerItems = this.wrapperDiv.getElements(options.scrollerItemSelector);
    if (this.scrollerItems.length > 1)
    {
      new Element('a').addClass('left').addEvents({
        'mouseover': function(){this.scrollToItem(-1);}.bind(this),
        'mouseout' : function(){$clear(this.timer); this.scrollRes.stop()}.bind(this)

       }).injectBefore(this.wrapperDiv);

      new Element('a').addClass('right').addEvents(
        {
          'mouseover': function(){this.scrollToItem(1);}.bind(this),
          'mouseout' : function(){this.scrollRes.stop();$clear(this.timer);}.bind(this)
        }).injectAfter(this.wrapperDiv);
    }
  },
  scrollToItem: function(dir) {
        /*var move = dir * this.options.steps;
        var index = (this.scrollerItems[this.currentPos+move]) ? this.currentPos+move : ((this.currentPos+move) > 0) ? this.scrollerItems.length-1 : 0;
        if(((index + 1) >= this.scrollerItems.length) && (index % this.options.steps) != 0){return;}*/
        if(dir>0)
          this.timer = this.scrollPeriodicalRight.periodical(300);
        else
          this.timer = this.scrollPeriodicalLeft.periodical(300);
  }
});
scrollerMoo.implement(new Options);
