Effect.ScrollVertical = Class.create();

Object.extend(Object.extend(Effect.ScrollVertical.prototype, Effect.Base.prototype),
{
   initialize: function(element)
   {
       if(typeof element == "string")
       {      
           this.element = $(element);
           if(!this.element)
           {
               throw(Effect._elementDoesNotExistError);
           }
       }
      
       var options = Object.extend({
           from: this.element.scrollTop || 0,
           to:   this.element.scrollHeight
       }, arguments[1] || {});
      
       options.to = options.to == this.element.scrollHeight ? options.to : options.from + options.to;
      
       this.start(options);
   },
  
   update: function(position)
   {
       this.element.scrollTop = position;
   }
});

Effect.ScrollHorizontal = Class.create();

Object.extend(Object.extend(Effect.ScrollHorizontal.prototype, Effect.Base.prototype),
{
   initialize: function(element)
   {
       if(typeof element == "string")
       {      
           this.element = $(element);
           if(!this.element)
           {
               throw(Effect._elementDoesNotExistError);
           }
       }
      
       var options = Object.extend({
           from: this.element.scrollLeft || 0,
           to:   this.element.scrollWidth
       }, arguments[1] || {});
      
       this.start(options);
   },
  
   update: function(position)
   {
       this.element.scrollLeft = position;
   }
});

var horizontalScrollBox = Class.create({
	
	initialize: function(holder,options){
		this.options = Object.extend({
			scrollAreaSelector : 'scrollArea',
			scrollContentSelector : 'scrollContent',
			previousLinkSelector : 'prev',
			previousLinkOff : '',
			nextLinkSelector : 'next',
			nextLinkOff : ''
		}, options || {});
		
		this.holder = $(holder);
		this.area = $$('#' + holder + ' .' + this.options.scrollAreaSelector)[0];
		this.content = $$('#' + holder + ' .' + this.options.scrollContentSelector)[0];
		this.prevLink = $$('#' + holder + ' .' + this.options.previousLinkSelector)[0];
		this.nextLink = $$('#' + holder + ' .' + this.options.nextLinkSelector)[0];
		this.children = this.content.immediateDescendants();
		this.width = this.area.getWidth();
		this.maxWidth = this.children.length * this.children[0].getWidth();
		this.curPos=0;
		this.moveNum=0;
		
		if (this.children[1]){
			this.maxWidth = this.children.length * this.children[1].positionedOffset().left;
		}
		
		// add the functions to the links
		Event.observe(this.prevLink,'click',this.onClick.bind(this),false);
		Event.observe(this.nextLink,'click',this.onClick.bind(this),false);
		
		this.onInit();
	},
	
	onInit: function(){
				
	},
	
	onClick: function(e){
		
		// Set animation state
		var animate=false;
		var clickFrom = e.currentTarget;
		
		if (!e.currentTarget){
			// this is for IE
			clickFrom = e.srcElement;
		}		
				
		if(clickFrom == this.prevLink){
		 	// move left	
			// If direction is left only change current position if more than 0
			if (this.curPos>0){
				this.moveNum=this.width;
				this.curPos-=this.width;
				animate=true;
			}
		}else{
			// move right
			// If direction is right only change next position if less than maxWidth - width
			if (this.curPos< (this.maxWidth-this.width)){
				this.moveNum=-this.width;	
				this.curPos+=this.width;	
				animate=true;	
			}
		}
		
		if (this.curPos<=0){
			this.prevLink.addClassName(this.options.previousLinkOff);
		}else{
			this.prevLink.removeClassName(this.options.previousLinkOff);			
		}
		
		if (this.curPos>= (this.maxWidth-this.width)){
			this.nextLink.addClassName(this.options.nextLinkOff);
		}else{
			this.nextLink.removeClassName(this.options.nextLinkOff);			
		}
		
		//animate
		if (animate){
			new Effect.ScrollHorizontal(this.area.identify(), {to:this.curPos,duration:.5});
		}	
		
		// remove focus
		clickFrom.blur();
		return false;
		
	}
});
