/*
	Sliding Pane
	used to scroll a div under a "window" that exposes only a portion of the content.
	can scroll either horizontally or vertically.

	-- jb
	
	1.0 - 10/05
	1.1 - 5/9/06 (added horizontal scrolling, documentation)
	
	------------ SETUP:

	requires 3 divs:

		<wrap>				<< width & height to reflect the "window" size
			<container>		<< must have class style "position:relative; overflow:hidden" -- nothing else
				<content>	<< must have INLINE style "position:absolute; top:0px; left:0px"
					 . . .	<< (full contents go here)
				</content>
			</container>
		</wrap>


	this script must appear ABOVE the buttons:

		var sliderObj;
		function initSlider() {
			sliderObj = new SlidingPane('CONTENTID');		// see <content> above
			sliderObj.init();
			sliderObj.prepPager(220,20,152);				// pageSize, #frames, window size (width or height)
		}
	
	and then call "initSlider()" at window load-time.
	
	paging buttons should have an action like this:
		sliderObj.pageRight()

*/

	function easeOutQuad(t, b, c, d) { return -c *(t/=d)*(t-2) + b; }

// constructor
function SlidingPane(eltID) { this.elt = document.getElementById(eltID); }

// for convenience
var SLP = SlidingPane.prototype;

// keep track of the ONE slider currently moving
var CurSlider;

// call after onload!
SLP.init = function() { this.contentheight = this.elt.offsetHeight;
						this.contentwidth = this.elt.offsetWidth; }

// the listener hears about every move. it gets the LEFT or TOP position at each interval,
// along with the min/max edges. for example:
//		function myListener( curPos, leftEdge, rightEdge )  {}
//
SLP.listenIn = function(fnc) { this.listenerFunc = fnc; }

//--------------- PRIVATE BELOW HERE --------
				
SLP.getTop = function()		{ return this.elt ? parseInt(this.elt.style.top) : 0; }
SLP.setTop = function(t)	{ if (this.elt) this.elt.style.top = parseInt(t)+"px"; }
SLP.getLeft = function()	{ return this.elt ? parseInt(this.elt.style.left) : 0; }
SLP.setLeft = function(t)	{ if (this.elt) this.elt.style.left = parseInt(t)+"px"; }

	// need a procedural fnc for the timer
	function SlidingPaneVert( obj, curFrame, startPos, travelDistance, steps) {
		obj.glideVert( curFrame, startPos, travelDistance, steps );
	}
	function SlidingPaneHoriz( obj, curFrame, startPos, travelDistance, steps) {
		obj.glideHoriz( curFrame, startPos, travelDistance, steps );
	}
	
SLP.glideVert = function( curFrame, startPos, travelDistance, steps) {
	var curTop = Math.round(easeOutQuad( curFrame, startPos, travelDistance, steps));
	this.setTop( curTop);
	if ( this.listenerFunc) (this.listenerFunc)(curTop,this.topEdge,this.bottomEdge);
	if ( ++curFrame < steps) {
		var fnc = 'SlidingPaneVert(CurSlider,' + curFrame + ',' + startPos + ',' + travelDistance + ',' + steps + ');';
		activeTimer = setTimeout(fnc,20);
	}
}
SLP.glideHoriz = function( curFrame, startPos, travelDistance, steps) {
	var curLeft = Math.round(easeOutQuad( curFrame, startPos, travelDistance, steps));
	this.setLeft( curLeft);
	if ( this.listenerFunc) (this.listenerFunc)(curLeft,this.leftEdge,this.rightEdge);
	if ( ++curFrame < steps) {
		var fnc = 'SlidingPaneHoriz(CurSlider,' + curFrame + ',' + startPos + ',' + travelDistance + ',' + steps + ');';
		activeTimer = setTimeout(fnc,20);
	}
}

// sliding paging functions
SLP.prepPager = function(pageSize, pageAnimFrames, parentSize) {
	this.pageSize = pageSize;
	this.pageAnimFrames = pageAnimFrames;
	this.parentSize = parentSize ? parentSize : 0;
	this.leftEdge = this.topEdge = 0;
	this.rightEdge = -(this.contentwidth - this.parentSize);
	this.bottomEdge = -(this.contentheight - this.parentSize);
}
SLP.pageUp = function() {
	var targetDistance = this.pageSize, curTop = this.getTop();
	if ( curTop + targetDistance >= 0) targetDistance = -curTop;
	if ( targetDistance > 0) {
		CurSlider = this;
		this.glideVert(0, curTop, targetDistance, this.pageAnimFrames);
	}
}
SLP.pageDown = function() {
	var targetDistance = -this.pageSize, curTop = this.getTop();
	if ( -(curTop + targetDistance) >= this.contentheight) targetDistance = curTop + this.contentheight;
	if ( targetDistance < 0) {
		CurSlider = this;
		this.glideVert(0, curTop, targetDistance, this.pageAnimFrames);
	}
}
SLP.pageLeft = function() {
	var targetDistance = this.pageSize, curLeft = this.getLeft();
	if ( (curLeft + targetDistance) > 0)
		targetDistance = -curLeft;
	if ( targetDistance > 0) {
		CurSlider = this;
		this.glideHoriz(0, curLeft, targetDistance, this.pageAnimFrames);
	}
}
SLP.pageRight = function() {
	var targetDistance = -this.pageSize, curLeft = this.getLeft();
	if ( (curLeft + this.contentwidth + targetDistance) < this.parentSize)
		targetDistance = this.parentSize - (curLeft + this.contentwidth);
	if ( targetDistance < 0) {
		CurSlider = this;
		this.glideHoriz(0, curLeft, targetDistance, this.pageAnimFrames);
	}
}
