
var avScroll = new Class({
    options: {
		imgWidth: 0,
		scrollRange: 0,
		totalItems: 0,
		leftHandle: null,
		rightHandle: null,
		container: null,
		leftClicks: 0,
		rightClicks: 0,
		speed: 500
	},
	
	initialize: function(options){
	this.setOptions(options)
		//alert("initialize");	
		this.imgWidth = options['imgWidth'];
		this.scrollRange = options['scrollRange'];
		this.leftHandle = options['leftHandle'];
		this.rightHandle = options['rightHandle'];	
		this.container = $(options['container']);
		this.totalItems = options['totalItems'];
		
		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(){
		//alert("left Click");
				if(this.leftClicks > 0){
					var start = (this.leftClicks * this.imgWidth) + 170;
					var end = ((this.leftClicks - this.scrollRange ) * this.imgWidth) + 170;
					if ((this.leftClicks / this.scrollRange) == 1) end -= 170; // if this is the first LEFT click we need to add an offset for the arrow
					
					//alert(start);
					//alert(end);	

					$(this.container).effect('right',{ duration: this.speed, wait:true, transition:Fx.Transitions.linear }).start(start,end);
					this.leftClicks-=this.scrollRange;
					
					/*Change Arrows*/
					document.getElementById('rightClicker').src = '../../left_right_click/thumbs/arrowPage2.jpg';	
					document.getElementById('aRight').style.cursor = "pointer";
					if(this.leftClicks <= 0){
						document.getElementById('leftClicker').src = '../../left_right_click/thumbs/arrowPage-grayed.JPG';
						document.getElementById('aLeft').style.cursor = "default";
					}
				}
				
				
	},
		
	rightClick: function(){
		//alert("right click");
				if (this.leftClicks <= this.totalItems - this.scrollRange) {
					var start = (this.leftClicks * this.imgWidth);
					if(this.leftClicks > 0) start += 170; // if this is NOT the first right click we need to add an offset for the arrow
					var end = ((this.leftClicks + this.scrollRange) * this.imgWidth) + 170;
					if(this.leftClicks == 5){
						end+=170;
					}
					//alert(this.leftClicks + " - " + start + "-" + end);	
					
					$(this.container).effect('right',{ duration: this.speed, wait:true, transition: Fx.Transitions.linear }).start(start,end);
					this.leftClicks+=this.scrollRange;
					
					/*Change Arrows*/
					document.getElementById('leftClicker').src = '../../left_right_click/thumbs/arrowPage.jpg';		
					document.getElementById('aLeft').style.cursor = "pointer";
					if (this.leftClicks > this.totalItems - this.scrollRange) {
						document.getElementById('rightClicker').src = '../../left_right_click/thumbs/arrowPage2-grayed.JPG';
						document.getElementById('aRight').style.cursor = "default";
					}					
				}				
	},
	
	ubicate: function(imgPosition){
		var end = Math.floor(imgPosition / 7);
		var mod = imgPosition % 7;
		if (end > 0){
			//alert(end);
			if (mod == 0)
				end--;
				
			$(this.container).effect('right',{ duration: this.speed, wait:true, transition: Fx.Transitions.linear }).start(end*670,end*670);
			this.leftClicks+=this.scrollRange*end;
		}
	},
	
	fastForward: function(){
				// scroll 4 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 + 4 ) * this.imgWidth);
				//$(this.container).effect('right',{ duration: this.speed, wait:true, transition:Fx.Transitions.Back.easeIn }).start(start,end);
				//this.leftClicks = this.leftClicks +  4;
				
				
	},
	
	speedReverse: function(){
				//var start= (this.leftClicks *  this.imgWidth );
				//var end = ((this.leftClicks  - 4 ) * this.imgWidth);
				//$(this.container).effect('right',{ duration: this.speed, wait:true, transition: Fx.Transitions.Back.easeIn }).start(start,end);
				//this.leftClicks-= 4;
				
	}
				
});
avScroll.implement(new Options, new Events);

