/* ******** */
/* SCROLLER */
/* ******** */
// ---------.---------.---------.---------.---------.---------.---------.
// SCROLLER
// FOR SCROLLING MOVEMENT
function scroller(pName) {
	/* ARRANGEMENT 
	   <DIV ID='pnl{pName}'>-- WITH OVERFLOW HIDDEN
	      <DIV ID='pnl{pName}_cont'>
			CONTENT TO BE SCROLLED
		</DIV>
	   </DIV>
	*/
	/* EVENT 
	   onFinishedScroll 	= RUNS ON FINISHED ANIMATED SCROLLING
	*/
	// VARIABLE NAME: scr{name}
	this.int		= null;			// INTERVAL HOLDER
	this.x		= 0;				// SCROLL X POSITION
	this.name 		= pName;			// NAME: IF THIS WILL BECOME A CLASS, THE REQUIRED NAME IS scr{name}
	this.jumpamount	= 200;			// JUMP SCROLL AMOUNT
	this.scrspeed	= 50;				// INTERVAL OF THE SCROLLING
	this.scramount	= 15;				// AMOUNT OF SCROLLING PER INTERVAL
	
	// SEMIPRIVATE
	this.___prevScrollLeft	= 0;			// PREVIOUS LEFT SCROLL OF THE OBJECT
	
	// PRIVATE
	//var ___scrDir	= 1;				// SCROLL DIRECTION. -1 IS LEFTWARD, 1 IS RIGHTWARD
	
	
	// FUNCTION HOLDER
	if(!window.FUNCHLD) window.FUNCHLD = new Array();
	var thisInd = window.FUNCHLD.length;
	FUNCHLD[thisInd] = this;
	
	
	// THE SCROLLER WILL CONTROL THE THUMBNAIL LIST
	// SCROLL TO LEFT
	this.scrollLeft = function() {
		this.doScrollTo(this.x-this.jumpamount);
	}
	// SCROLL TO RIGHT
	this.scrollRight = function() {
		this.doScrollTo(this.x+this.jumpamount);
	}
	
	// DO THE SCROLLING 
	this.doScrollTo = function(pWhere) {
		// SET INITIAL SCROLL
		this.x = parseInt(document.getElementById("pnl" + this.name).scrollLeft);
		
		// DETERMINE WHERE TO SCROLL
		var ___scrDir = 1;
		if(pWhere > this.x)
			___scrDir = 1;
		else	
			___scrDir = -1;
		
		// SET THE ANIMATION
		clearInterval(this.int);
		this.int = setInterval("FUNCHLD["+thisInd+"].___aniScrollTo(" + pWhere + ", " + ___scrDir + ");", this.scrspeed);
	}
	
	// DO THE SCROLLING ANIMATION (PRIVATE)
	this.___aniScrollTo = function (pWhere, pDir) {
		this.x += (this.scramount * pDir);
		var obj 	= document.getElementById("pnl" + this.name);
		var objCont = document.getElementById("pnl" + this.name + "_cont");
			
		var thisObj = this;	
		var stopThis = function() {
			clearInterval(thisObj.int);
			if(thisObj.onFinishedScroll)
				thisObj.onFinishedScroll();
			
		}
		// DETERMINE WHERE TO STOP
		// ON STOPPING: THE this.x WILL BE AT THE pWhere
		// FOR GOING TO RIGHT
		if(pDir>=1) {
			if(this.x >= pWhere) {
				stopThis();
				this.x = pWhere;
			}
		}
		// FOR GOING TO LEFT
		else {
			if(this.x <= pWhere) {
				stopThis();
				this.x = pWhere;
			}
		}
		
		// FINAL VALIDATION
		var t_maxscroll = objCont.offsetWidth - obj.offsetWidth;
		if(this.x < 0) {
			this.x = 0;
			stopThis();
		}
		else if(this.x > t_maxscroll) {
			this.x = t_maxscroll;
			stopThis();
		}
			
		// SET THE SCROLLING
		obj.scrollLeft = this.x;
		
		// IF THE SCROLLLEFT OF THE PREVIOUS 
		
		// SAVE THE CURRENT SCROLLLEFT
		this.___prevScrollLeft = obj.scrollLeft;		

	}
}

