Scroller = function( el, loader ) {
    this.init( el, loader );
};

Scroller.prototype = {

	// The point to reset too	
	reset_point : 0,

	// The scroller div
	scroller : null,

	// Current scroller position
	position : 0,

	// Movement --  This is "constant"
	movement : 5,
	
	// Time out between movements on mouse down --  This is "constant"
	delay : 5,
	
	// The timer object
	timer : null,
	
	// Movement direction. 0 for nothing, -1 for left, 1 for right
	direction : 0,	
	
	// Initialize the scroller stuff
	init : function( el, loader ) {
		// Get rid of the ' ... loading ... ' div
		var anim = new YAHOO.util.Anim( loader, { opacity: { to: 0 } }, 1, YAHOO.util.Easing.easeOut);
		anim.animate();
		var load_el = YAHOO.util.Dom.get( loader );
		load_el.style.display = "none";
		
		// bring in the scroll object:
		this.scroller = YAHOO.util.Dom.get( el );
		this.scroller.style.display = "block";
		anim = new YAHOO.util.Anim( el, { opacity: { to: 100 } }, 1, YAHOO.util.Easing.easeIn);
		anim.animate();
		this.scroller = YAHOO.util.Dom.get( el );
		this.reset_point = this.scroller.offsetLeft - this.scroller.parentNode.offsetLeft;
	},
	
	// Initialize the left moving
	scroll_left : function( ) {
		this.stop();
		this.direction = -1;
		this.scroll();
	},
	
	// Initialize the right moving
	scroll_right : function( ) {
		this.stop();
		this.direction = 1;
		this.scroll();
	},
	
	// Actually do the scrolling action
	scroll : function( ) {
		// no direction? bail!
		if( this.direction == 0 ) {
			timer = null;
			return;
		}
		
		var start = this.position;		
		
		// Calculate new position:
		this.position = ( this.position <= ( this.reset_point * 2 ) || this.position >= 0 )
						? (  this.reset_point + this.direction * this.movement )
						: ( this.position + this.direction * this.movement );
		
		// set the new position:
		var start = this.scroller.style.marginLeft;		
		this.scroller.style.marginLeft = this.position + "px";
		
		this.timer = setTimeout( "scroller.scroll()", this.delay ); 
	},
	
	// Kill whatever scrolling is happening
	stop : function( ) {
		this.timer = null;
		this.direction = 0;		
	}
}

var scroller;
function start_scroller() {
	scroller = new Scroller( "scroller", "scroll_loader" );
}

YAHOO.util.Event.onAvailable( "scroller", start_scroller );