// Smooth Div Scroller
// Matt Cardoza, Seven2.net
//
// Uses scriptaculous transition to provide a smooth scrolling div 
// with option for timed auto-scroll up and down panel range
//
SmoothScrollInProgress= false;
function smoothScroller (layerName, totalPanels, incrX, incrY, autoScrollDelayMS) {

	this.CurrentPanel = 0;
	this.TotalPanels = totalPanels;
	this.LayerName = layerName;
	this.IncrementX = incrX;
	this.IncrementY = incrY;
	this.AutoScrollDelayMS = autoScrollDelayMS;
	this.AutoScrollDirection = 1;
	
	var myTimeout;
	setAutoInterval(this);

	this.MovePrev = function (c) {
		if((this.CurrentPanel > 0) && (!SmoothScrollInProgress))
		{
			SmoothScrollInProgress = true;
			new Effect.Move(this.LayerName, { x: this.IncrementX , y: this.IncrementY, afterFinish: this.FinishMove(-1), transition: Effect.Transitions.sinoidal });
		
			if (c!=null) {
				setAutoInterval(this);
			} else {
				clearTimeout(myTimeout);
			}
		}		
	}
	
	this.MoveNext = function (c) {
		if((this.CurrentPanel < this.TotalPanels) && (!SmoothScrollInProgress))
		{ 
			SmoothScrollInProgress = true;
			new Effect.Move(layerName, { x: -this.IncrementX, y: -this.IncrementY, afterFinish: this.FinishMove(1), transition: Effect.Transitions.sinoidal });
			if (c!=null) {
				setAutoInterval(this);
			} else {
				clearTimeout(myTimeout);
			}		
		}

	}
	this.FinishMove = function(i) {
		this.CurrentPanel = this.CurrentPanel + i;
		setTimeout("SmoothScrollInProgress=false",1000);
	}
	
	function setAutoInterval(obj) {
		if ((obj.AutoScrollDelayMS != null) && (this.TotalPanels >0)) {
			if (obj.CurrentPanel == obj.TotalPanels) {
				obj.AutoScrollDirection = -1;
			}
			
			if (obj.CurrentPanel == 0) {
				obj.AutoScrollDirection = 1;
			}
			
			if (obj.AutoScrollDirection == -1) {
				myTimeout = setTimeout(function () { obj.MovePrev(1) } , obj.AutoScrollDelayMS);
			} else {
				myTimeout = setTimeout(function () { obj.MoveNext(1) } , obj.AutoScrollDelayMS);
			}
		}	
	}
}

