Gallery = Class.create(Abstract, {
	initialize: function (scroller, slide, controls, options) {
          this.scroller	= $(scroller);
          this.slide	= $(slide);
          this.controls	= controls;
          this.lock     = false;

          this.options    = Object.extend({
            duration:           0.4,
            frequency:          3,
            visibleItems:       3,
            step:               this.scroller.getWidth(),
            controlClassName:   'gallery-control',
            disabledClassName:  'disabled',
            selectedClassName:  'selected'//,
          //  wheel:              true
          }, options || {});
          
          this.options.lock = this.options.duration * 2;
          this.current_page = 1;
          this.total_count  = Math.ceil(this.slide.childElements().length / this.options.visibleItems);

          if (this.controls) {
              this.controls.invoke('observe', 'click', this.click.bind(this));
          }
          
          //if (this.options.wheel) {            
          //    this.scroller.observe('mousewheel', this.wheel.bindAsEventListener(this)).observe('DOMMouseScroll', this.wheel.bindAsEventListener(this));;
         // }

	},

	click: function (event) {
	  var element = event.findElement('a');

	  if (!element.hasClassName(this.options.disabledClassName)) {
	    if (element.hasClassName(this.options.controlClassName)) {
		eval("this." + element.rel + "()");
            }
          }

	  //this.deactivateControls();

	  event.stop();
        },

	prev: function () {
          if (!this.lock) {
            this.move('prev');
          }
	},

	next: function () {
          if (!this.lock) {
            this.move('next');
          }
	},

        move: function(action) {
          this.lockSlide(this.options.lock);
          if (action == 'next') {
            x_step = -this.options.step;
            this.current_page++;
          } else if (action == 'prev') {
            x_step = this.options.step;
            this.current_page--;
          }
	  new Effect.MoveBy( this.slide, 0, x_step, {duration: this.options.duration, transition: Effect.Transitions.sinoidal, afterFinish: this.checkArrows()} );
        },

	deactivateControls: function () {
	  this.controls.invoke('addClassName', this.options.disabledClassName);
        },

	activateControls: function () {
	  this.controls.invoke('removeClassName', this.options.disabledClassName);
        },

        checkArrows: function() {
          thisObj = this;
          this.controls.each(function(control){
            if (control.rel == 'next') {
              if (thisObj.current_page == thisObj.total_count) {
                control.addClassName(thisObj.options.disabledClassName);
              } else {
                control.removeClassName(thisObj.options.disabledClassName);
              }
            } else if (control.rel == 'prev') {
              if (thisObj.current_page == 1) {
                control.addClassName(thisObj.options.disabledClassName);
              } else {
                control.removeClassName(thisObj.options.disabledClassName);
              }
            }
          })
        },

      lockSlide: function( intDur ) {
        var lockDur = intDur * 1000; // convert seconds to ms
        this.lock = true;
        var thisObj = this;
        setTimeout(function () { thisObj.lock = false }, lockDur);
      }
});
