//http://www.artviper.net/mooHorizonSlider/mooSlider.php

var avScroll = new Class({

    options: {
		imgWidth: 0,
		trayWidth: 0, //how many images in the tray
		scrollRange: 0,
		leftHandle: null,
		rightHandle: null,
		container: null,
		leftClicks: 0,
		rightClicks: 0,
		speed: 0		
	},
	
	initialize: function(options){
	this.setOptions(options)
	
		this.imgWidth = options['imgWidth'];
		this.trayWidth = options['trayWidth'];
		this.scrollRange = options['scrollRange'];
		this.leftHandle = options['leftHandle'];
		this.rightHandle = options['rightHandle'];	
		this.container = $(options['container']);
		
		if($(options['ff'])){
			this.ff = $(options['ff']);
			$(this.ff).addEvent('click',this.fastForward.bindWithEvent(this));
		}
		if($(options['rw'])){
			this.rw = $(options['rw']);
			$(this.rw).addEvent('click',this.speedReverse.bindWithEvent(this));
		}
		
		this.leftClicks = 0;
		this.rightClicks = 0;
		this.speed = options['speed'];		
		$(this.leftHandle).addEvent('click',this.leftClick.bindWithEvent(this));
		$(this.rightHandle).addEvent('click',this.rightClick.bindWithEvent(this));
	},
	leftClick: function(){
		
				if(this.leftClicks > 0){
					var start= this.leftClicks * this.imgWidth;
					var end = ( this.leftClicks - 1 ) * this.imgWidth;
					$(this.container).effect('right',{ duration: this.speed, wait:true, transition:Fx.Transitions.Back.easeIn }).start(start,end);
					this.leftClicks--;
					$('rightScroller').setStyle('cursor', 'pointer');
					$('rightFF').setStyle('cursor', 'pointer');
				}
				if(this.leftClicks < 1){
					$('leftClicker').setStyle('cursor', 'default');
					$('leftFF').setStyle('cursor', 'default');
				}
	},
		
	rightClick: function(){
				if(this.leftClicks < this.scrollRange - this.trayWidth){
					var start= this.leftClicks * this.imgWidth;
					var end = (this.leftClicks * this.imgWidth) + this.imgWidth;
					$(this.container).effect('right',{ duration: this.speed, wait:true, transition: Fx.Transitions.Back.easeIn }).start(start,end);
					this.leftClicks++;
					$('leftClicker').setStyle('cursor', 'pointer');
					$('leftFF').setStyle('cursor', 'pointer');
				}
				if(this.leftClicks > (this.scrollRange - (this.trayWidth+1))){
					$('rightScroller').setStyle('cursor', 'default');
					$('rightFF').setStyle('cursor', 'default');
				}
	},
	
	fastForward: function(){
				if(this.leftClicks < this.scrollRange - this.trayWidth){
					// scroll this.trayWidth images to the right, calculate it on base of the clicks and the image width
					var start= (this.leftClicks) * this.imgWidth;			
					var end = (( this.leftClicks + this.trayWidth ) * this.imgWidth);
					$(this.container).effect('right',{ duration: this.speed, wait:true, transition:Fx.Transitions.Back.easeIn }).start(start,end);
					this.leftClicks = this.leftClicks + this.trayWidth;
					$('leftClicker').setStyle('cursor', 'pointer');
					$('leftFF').setStyle('cursor', 'pointer');
				}
				if(this.leftClicks > this.scrollRange - (this.trayWidth*2)) {
					$('rightScroller').setStyle('cursor', 'default');
					$('rightFF').setStyle('cursor', 'default');
				}
				
	},
	
	speedReverse: function(){
				if(this.leftClicks > 0){
					var start= (this.leftClicks *  this.imgWidth );
					var end = ((this.leftClicks  - this.trayWidth ) * this.imgWidth);
					$(this.container).effect('right',{ duration: this.speed, wait:true, transition: Fx.Transitions.Back.easeIn }).start(start,end);
					this.leftClicks-= this.trayWidth;
					$('rightScroller').setStyle('cursor', 'pointer');
					$('rightFF').setStyle('cursor', 'pointer');
				}
				if(this.leftClicks < this.trayWidth) {
					$('leftClicker').setStyle('cursor', 'default');
					$('leftFF').setStyle('cursor', 'default');
				}				
	}
				
});
avScroll.implement(new Options, new Events);

