// JavaScript Document
function MagicHoverScroller(targetObj , prev , next){
	this.onLeftMost = null;
	this.onRightMost = null;
	this.onMidMost = null;
	this.iconScrollSpeed = 0;
	this.iconScrollMultiplier = 8;
	// How quickly setinterval fires
	this.iconUpdateSpeed = 20;
	// Careful about putting the speed adjuster below 1... there's an odd rounding issue
	this.speedAdjuster = 1;
	// The speed we're aiming for
	this.targetSpeed = 1;
	// The minimum the speed will get to (negative too)
	this.slowSpeed = 0;
	// the highest the speed will get to (negative too)
	this.fastSpeed = 14;

	var scrollInt;
	var productsMC = targetObj;
	var prevButton = prev;
	var nextButton = next;
	var onMidCalledLast = false;
	
	
	this.init = function(){
		var host = this;
		

		// Event Handlers
		if(prevButton){
			
			prevButton.addEvent('mousedown' , function(){host.startScrolling();host.makeSpeedFast(false);});
			prevButton.addEvent('mouseup' , function(){host.makeSpeedSlow(false);});
			prevButton.addEvent('mouseout' , function(){host.makeSpeedSlow(false);});																   
		}
		
		if(nextButton){
			nextButton.addEvent('mousedown' , function(){host.startScrolling();host.makeSpeedFast(true);});
			nextButton.addEvent('mouseup' , function(){host.makeSpeedSlow(true);});
			nextButton.addEvent('mouseout' , function(){host.makeSpeedSlow(true);});
		}
		
		this.doHandlers();
		
	}
	
	
	this.stopScrolling = function(){
		this.iconScrollSpeed = 0;
		window.clearInterval(scrollInt);
	}
	
	this.scrollIcons = function(){
		var sX = productsMC.scrollLeft;
		
		if(this.iconScrollSpeed > this.targetSpeed){ this.iconScrollSpeed = Math.max(this.targetSpeed , this.iconScrollSpeed - this.speedAdjuster); }
		if(this.iconScrollSpeed < this.targetSpeed){ this.iconScrollSpeed = Math.min(this.targetSpeed , this.iconScrollSpeed + this.speedAdjuster); }
		
		productsMC.scrollLeft += this.iconScrollSpeed; 
		
		if(this.iconScrollSpeed == 0){
			this.stopScrolling();
		}
		
		if(sX !== productsMC.scrollLeft){
			this.doHandlers();
		}
			
	}
	
	this.doHandlers = function(){
		if(!onMidCalledLast){
			this.callOnMidMost(this);
		}
			
			
		if(productsMC.scrollLeft == 0){		
			this.callOnLeftMost();
		}
			
		if(productsMC.scrollLeft == productsMC.scrollWidth - productsMC.clientWidth){
			this.callOnRightMost();
		}
	}
	
	this.callOnMidMost = function(){
		onMidCalledLast = true;
		
		if(this.onMidMost){
			this.onMidMost(this);
		}
	}
	
	this.callOnLeftMost = function(){
		this.iconScrollSpeed = 0;
		onMidCalledLast = false;
		
		if(this.onLeftMost){
			this.onLeftMost(this);
		}
	}
	
	this.callOnRightMost = function(){
		
		onMidCalledLast = false;
		
		if(this.onRightMost){
			this.onRightMost(this);
		}
	}
	
		
		
		
		
	this.makeSpeedFast = function(dir){
		if(dir){
			this.targetSpeed = this.fastSpeed;
			return;
		}
		this.targetSpeed = -this.fastSpeed;
	}
	
	
	
	this.makeSpeedSlow = function(dir){
		if(dir){
			this.targetSpeed = this.slowSpeed;
			return;
		}
		this.targetSpeed = -this.slowSpeed;
	}
	
	
	
	this.startScrolling = function(){
		this.stopScrolling();
		var host = this;
		scrollInt = window.setInterval(function(){host.scrollIcons();} , this.iconUpdateSpeed);
	}
}