/**
 * @author mike
 */

  /**
   * slide some layers
   * 
   */
  var LayerSlider = Class.create();
  LayerSlider.prototype = {
  	initialize: function (config) {
      if(!config) {
        config = {};
      }
			this.selectedLayer    = config.selectedLayer || 1;
      this.layerWidth       = config.layerWidth || 845;
    	this.layerID          = config.layerID || 'SlideLayer';
      this.visibleLayers    = config.visibleLayers || 1;
      this.duration         = config.duration || 0.7;
      this.noButtons        = config.noButtons || false;
      this.isBusy           = false;
      this.onChange         = config.onChange || function(){};
      this.initLayers();
      this.initSlider();
      this.initButtons();
      if(this.selectedLayer > this.visibleLayers) {
        var d = Math.floor(this.selectedLayer/this.visibleLayers);
        d = d*this.visibleLayers;
        if(d+this.visibleLayers > this.layers.length) {
          d = d-(d+this.visibleLayers-this.layers.length);
        }
        this.selectedLayer = 1;
        this.slideLeft(d);
      } else {
        this.selectedLayer = 1;
        this.initButtons();
      }
	  },
    
    initLayers: function() {
      this.layers = $(this.layerID).getElementsByClassName('layer');
      for(var i=0; i<this.layers.length; i++) {
        this.layers[i].setStyle({left: i*this.layerWidth+'px'});
        this.layers[i].show();
      }
		},
    
		initSlider: function() {
      this.slider = $(this.layerID).getElementsByClassName('slider');
      this.slider = this.slider[0];
      this.slider.setStyle({
        width: this.layers.length*this.layerWidth+'px'
      });
		},
		
		initButtons: function() {
      if(this.noButtons) return false;
      var left         = $(this.layerID).getElementsByClassName('slideLeft');
      this.buttonLeft  = $(left[0]);
      var right        = $(this.layerID).getElementsByClassName('slideRight');
      this.buttonRight = $(right[0]);
      this.hideButtons();
		},
    
    slideTo: function(to) {
      var d = to-this.selectedLayer;
      if(d < 0) {
        this.slideRight(-1*d);
      } else if(d > 0) {
        this.slideLeft(d);
      }
    },
		
		slideLeft: function(d) {
      if(this.isBusy) {
        return;
      }
      this.isBusy = true;
      if(!d) {
        if(this.selectedLayer-1+this.visibleLayers+this.visibleLayers <= this.layers.length) {
          d = this.visibleLayers;
        } else {
          d = this.layers.length-(this.selectedLayer+this.visibleLayers-1);
        }
      }
      if(this.selectedLayer+d > this.layers.length) {
        return;
      }
      this.move(-(d*this.layerWidth));
      this.selectedLayer = this.selectedLayer+d;
      this.hideButtons();
      this.ready.delay(0.5, this);
		},
		
		slideRight: function(d) {
      if(this.isBusy) {
        return;
      }
      this.isBusy = true;
      if(!d) {
        if(this.selectedLayer-this.visibleLayers >= 1) {
          d = this.visibleLayers;
        } else {
          d = this.selectedLayer-1;
        }
      }
      if(this.layers.length <= 1 || this.selectedLayer <= 1) {
        return false;
			}
      this.selectedLayer = this.selectedLayer-d;
			this.move(d*this.layerWidth);
      this.hideButtons();
      this.ready.delay(0.5, this);
		},
    
		hideButtons: function(){
      //console.log('HIDE:'+this.layers.length+":"+this.selectedLayer+":"+this.visibleLayers);
      if(this.noButtons) return false;
      // hide both
      if(this.layers.length <= this.visibleLayers) {
        this.buttonLeft.setStyle({visibility: 'hidden'});
        this.buttonRight.setStyle({visibility: 'hidden'});
      // hide right
      } else if(this.selectedLayer <= 1) {
				this.buttonLeft.setStyle({visibility: 'visible'});
				this.buttonRight.setStyle({visibility: 'hidden'});
      // hide left
			} else if(this.selectedLayer+this.visibleLayers-1 >= this.layers.length) {
				this.buttonLeft.setStyle({visibility: 'hidden'});
				this.buttonRight.setStyle({visibility: 'visible'});
      // hide none
			} else {
				this.buttonLeft.setStyle({visibility: 'visible'});
				this.buttonRight.setStyle({visibility: 'visible'});
			}
		},
    
    move: function(v) {
      new Effect.Move(this.slider, { x: v, y: 0, mode: 'relative', duration: this.duration, afterFinish: this.onChange});
    },
    
    ready: function (obj) {
      obj.isBusy = false;
    }
    
  };
  