/**
 * @author francissem@gmail.com
 * @license Apache
 * @version 0.4
*/

Scrollable = Class.create();
Object.extend(Scrollable.prototype, {
	  initialize: function(element,options) {
		this.element = $(element);
		this.position=0;
		this.rightSlidePE=null;
		this.leftSlidePE=null;
		this.options = options;
		
		this.container = this.element.down(".container");
		
	  },
	  startRight: function(porps){
		this.notify("startRight");
		this.stopLeft();
		this.rightSlidePE=new PeriodicalExecuter(function(pe) {
			this.element.scrollLeft+=4;
			//if(porps.onSlide) porps.onSlide(this);
			//onSlide 
			
			if(this.element.offsetWidth<this.container.offsetWidth){
				if(this.container.offsetWidth-this.element.offsetWidth<=this.element.scrollLeft){
					this.notify("endScrollingRight");
					
				}
				if(this.element.scrollLeft>0){
					this.notify("scrollingRight");
					
				}
			}
		}.bind(this), 0.02);
	  },
	  stopRight: function(){
		this.notify("stopRight");
		if(this.rightSlidePE!=null) this.rightSlidePE.stop();
	  },
	  startLeft: function(props){
		this.notify("startLeft");
		this.stopRight();
		this.leftSlidePE=new PeriodicalExecuter(function(pe) {
			this.element.scrollLeft-=4;
			
			if(this.element.offsetWidth<this.container.offsetWidth){
				if(this.container.offsetWidth-this.element.offsetWidth>this.element.scrollLeft){
					this.notify("scrollingLeft");
					
				}
				if(this.element.scrollLeft<=0){
					this.notify("endScrollingLeft");
					
				}
			}
		}.bind(this), 0.02);
	  },
	  stopLeft: function(){
		this.notify("stopLeft");
		if(this.leftSlidePE!=null) this.leftSlidePE.stop();
	  }
  }
 );
//this.notify("afterAdded");
Object.Event.extend(Scrollable);
