﻿// JScript File
function Scroller() {
	var fRatio;
	var starttime;
	var iScrollEnd;
	var iScrollStart;
	var iMaxScroll;
	var objDiv;
	var me = this;
	
	this.IsScrolling = false;
	
	function SetDivLeft(objDiv, iLeft) {
	    iLeft = Math.floor(iLeft);
	    objDiv.style.left = iLeft + "px";
	    if (objDiv.offsetLeft != iLeft) {
	        var d;
	        var n;
	        d = Math.abs(objDiv.offsetLeft) - Math.abs(iLeft);
	        n = iLeft + d;
	        objDiv.style.left = n + "px";
	    }
	}
	
	function ScrollThis() {				
		if (
				(
				iMaxScroll > 0 &&
				objDiv.offsetLeft < iScrollEnd
				) ||
				(
				iMaxScroll < 0 &&
				objDiv.offsetLeft > iScrollEnd
				)
			) {
			var iNewV;
			var curtime = new Date().getTime();
			var elapsedms = curtime - starttime;

			iNewV = iScrollStart + (elapsedms * fRatio);
			
			if (iMaxScroll > 0) {
				iNewV = (iNewV > iScrollEnd ) ? iScrollEnd : iNewV;
			}
			else if (iMaxScroll < 0) {
				iNewV = (iNewV < iScrollEnd ) ? iScrollEnd : iNewV;
			}

            SetDivLeft(objDiv, iNewV);
			setTimeout(ScrollThis, 1);
		}
		else {
			objDiv.style.left = iScrollEnd + "px";
			me.IsScrolling = false;
		}
	}

	this.StartScroll = function(objHTMLElement, inM, iMaximumScrollValue) {
		objDiv = objHTMLElement;
		iMaxScroll = iMaximumScrollValue;
		iScrollEnd = objDiv.offsetLeft + iMaxScroll;
		iScrollStart = objDiv.offsetLeft;
		fRatio = iMaxScroll / inM;
		starttime = new Date().getTime();
		this.IsScrolling = true;
		ScrollThis();
	}
}

