/** slider.js 
 *  ©copyright 2002 Garrett Smith
 *  http://dhtmlkitchen.com/
 * http://dhtmlkitchen.com/dhtml/slider/
 */

var ua= navigator.userAgent;
var OPERA = (ua.indexOf("Opera") > 0);
var OMNI = (ua.indexOf("Omni") > 0);
var IE5,IE6, NS6;
var px;

if(!OPERA && !OMNI){
	IE6 = (ua.indexOf("MSIE 6") > 0);
	IE5 = (ua.indexOf("MSIE 5") > 0  || IE6);
	NS6 = (ua.indexOf("Gecko") > 0);
	px = "px";
}

// Opera needs a number for style values, not a string.
else px = 0;

window.sliders = new Array();

/* Constructs a VSlider. */

function VSlider(id) {

	var el = document.getElementById(id);
	this.el = el;
	this.id = el.id;
	this.origTop = this.getTop();
	
	if(el.style.top == "")
		el.style.top = (typeof this.origTop == "number") ? 
			this.origTop + px: (el.offsetTop-1) + px;
	this.isIdle = true;
	window.sliders[this.id] = this;
	this.animString = "sliders."+this.id+".updatePosition();";
	this.slideTimer = setInterval(this.animString, 54);
}

VSlider.prototype = {

/**
 *  moves the element .25 of the distance between its
 *  (origTop + scrollTop) its current position.
 */

	updatePosition : function() {
	
		viewTop = getScrollTop();
		if(!viewTop) viewTop = 0;
			
		var elTop = this.getTop();
		var idlePos = viewTop + this.origTop;
		
		// to ensure slide end, add .25 and round.
		// this makes .25 -> .5 and rounds to 1.
		var dy = Math.round((Math.abs( idlePos - elTop ) /4 )+.25);
		if(idlePos < elTop)
			dy=-dy;
		this.moveTo(elTop, dy);
		this.prepareUpdate(idlePos, viewTop);
	},
	
	moveTo : function(curr, i) {
		this.el.style.top = (curr + i) + px;
	},
	
	/**
	 * Check the position. 
	 * If this is in idlePos,
	 *     if this is not idle, stop sliding.
	 * Else if this is not in idlePos and this is idle, 
	 * start sliding.
	 */
	prepareUpdate : function(idlePos, viewTop) {
	
		var elTop = this.getTop();
		
		if(idlePos == elTop) { // are we in in idlePos?
		
			if(!this.isIdle) { // if not idle, stop sliding.
				clearInterval(this.slideTimer);
				this.onSlideEnd();
				this.slideTimer = setInterval(this.animString, 400);
				this.isIdle = true;
			}
		}
			//  if we got here, we not in idlePos.
			//  have we started sliding yet?
		else if(this.isIdle) {
		
			 // clear the slow timer and set a faster one.
			clearInterval(this.slideTimer);
			this.onSlideStart();
			
			// if our element's girth is completely outside 
			// the viewport's perimeter, move the element to 
			// just outside the element's perimeter.
			if(elTop + this.el.offsetHeight < viewTop + this.origTop)
				this.el.style.top= (viewTop - this.el.offsetHeight) + px;
			else {
				viewBottom = viewTop + getViewportHeight();
				if(elTop > viewBottom)
					this.el.style.top = viewBottom+px;
			}
			this.slideTimer = setInterval(this.animString, 5);
			this.isIdle = false;
		}
	},
	
	onSlideStart : function(){},
	onSlideEnd : function(){},
	
	getTop : function() {
		if(typeof this.el.offsetTop == "number") return this.el.offsetTop;
		return this.el.style.pixelTop;
	}
};

function getScrollTop() {
	if(NS6 || OPERA) return window.pageYOffset;
	if(IE6) return Math.max(document.documentElement.scrollTop, document.body.scrollTop);
	return document.body.scrollTop;
}

function getViewportHeight() {
	if(NS6 || OPERA) return window.innerHeight -16; 
	if(IE6) return Math.max(document.documentElement.clientHeight, document.body.clientHeight);
	return document.body.clientHeight - 2;
}
