/*
	scroller.js version: 1.0.0 date: August 2007
	contains all scrolling functionality
*/

/* Global variable -- contains all Scroller references */
var references = [];

function Scroller(wrapper, id, controller) {
	var wrapperObj					= document.getElementById(wrapper);
	if (!wrapperObj) return;
	
	this.minOffsetLeft			= wrapperObj.clientWidth - wrapperObj.scrollWidth;
	this.minOffsetTop				= wrapperObj.clientHeight - wrapperObj.scrollHeight;
	this.referenceAsString	= 'references["' + id + '"].';
	this.scrollId						= 0;
	this.scrollObj					= document.getElementById(id);
	
	/* reserved */
	this.scrollerHeight			= wrapperObj.clientHeight;
	this.scrollerWidth			= wrapperObj.clientWidth;
	this.pixelsToMove				= 12;
	this.controlObj					= document.getElementById(controller);
	this.controlTop					= -1;
	this.controlOffset 			= -1;
	this.controlYTop				= -1;
	this.controlExactBarH		= -1;
	this.controlMove				= false;
	if((this.controlObj != null)) {
		this.controlTop = this.controlObj.currentStyle ? parseInt(this.controlObj.currentStyle.top) :
			parseInt(document.defaultView.getComputedStyle(this.controlObj, null).getPropertyValue('top', null));
		this.controlOffset = this.controlTop;
		if((wrapperObj.scrollHeight > wrapperObj.clientHeight)) {
			//this.controlObj.style.backgroundColor = '#673E40';
			//this.controlExactBarH = ((wrapperObj.clientHeight * wrapperObj.clientHeight) /
			//	wrapperObj.scrollHeight);
			//this.controlObj.style.height = Math.floor(this.controlExactBarH) + 'px';
		}
	}
	
	/* opera hack */
	if(window.opera) this.scrollObj.style.height = this.scrollObj.scrollHeight + 'px';
	
	// keep a reference of self
	references[id]					= this;
}

/* Class static variables */
//Scroller.prototype.pixelsToMove = 8;

/* Class member function */
Scroller.prototype.shiftBy = function (dx, dy) {
	this.scrollObj.style.left	= this.scrollObj.offsetLeft	+ dx + 'px';
	this.scrollObj.style.top	= this.scrollObj.offsetTop	+ dy + 'px';
	
	if(dx < 0 && this.scrollObj.offsetLeft <= this.minOffsetLeft) {
		this.scrollObj.style.left = this.minOffsetLeft + 'px';
		this.stopScrolling();
	}
	if(dy < 0 && this.scrollObj.offsetTop <= this.minOffsetTop) {
		this.scrollObj.style.top = this.minOffsetTop + 'px';
		this.stopScrolling();
	}
	if(dx > 0 && this.scrollObj.offsetLeft >= 0) {
		this.scrollObj.style.left = '0px';
		this.stopScrolling();
	}
	if(dy > 0 && this.scrollObj.offsetTop >= 0) {
		this.scrollObj.style.top = '0px';
		this.stopScrolling();
	}
}

Scroller.prototype.shiftBarBy = function (event) {
	if((this.controlObj != null) && (this.controlMove) && (this.controlYTop != -1)) {
		var ny = this.controlTop + event.clientY - this.controlYTop;
		var oy = this.controlObj.currentStyle ? parseInt(this.controlObj.currentStyle.top) :
				parseInt(document.defaultView.getComputedStyle(this.controlObj, null).getPropertyValue('top', null));
		if(ny != oy) {
			var sbh = this.controlObj.parentNode.clientHeight;
			var shiftOK = true;
			if(ny < this.controlOffset) {ny = this.controlOffset; shiftOK = false;}
			if(ny > sbh - this.controlOffset - this.controlObj.clientHeight) {
				ny = sbh - this.controlOffset - this.controlObj.clientHeight;
				shiftOK = false;
			}
			this.controlObj.style.top		= ny + 'px';
			
			if(shiftOK) {
				var dx = 0;
				//alert('ny: ' + ny + '\noy: ' + oy + '\nsbh: ' + sbh);
				var dy = Math.floor(1 * ((ny - oy) * (this.scrollerHeight) * (sbh)) /
					(this.controlExactBarH * (this.controlExactBarH - sbh)));
				//var dy = Math.floor(1 * ((ny - oy) * (this.scrollerHeight) * (this.controlExactBarH)) /
				//	(sbh * (sbh - this.controlExactBarH)));
				//if(dy > 0) dy -=2;
				//if(dy < 0) dy +=3;
				
				//alert(ny);
				//setTimeout(this.referenceAsString + 'shiftBy(' + dx + ',' + dy + ')', 25);
			}
		}
	}
}

Scroller.prototype.toggleScrollingBar = function (event) {
	if((this.controlObj != null) && (!this.controlMove)) {
		this.controlMove = true;
		this.controlYTop = event.clientY;
	} else {
			if((this.controlObj != null) && (this.controlMove)) {
			this.controlMove = false;
			this.controlYTop = -1;
			this.controlTop = this.controlObj.currentStyle ? parseInt(this.controlObj.currentStyle.top) :
				parseInt(document.defaultView.getComputedStyle(this.controlObj, null).getPropertyValue('top', null));
		}
	}
}

Scroller.prototype.startScrolling = function (dir) {
	var dx = this.pixelsToMove * (dir == 'left' ? -1 : dir == 'right' ? 1 : 0);
	var dy = this.pixelsToMove * (dir == 'up' ? -1 : dir == 'down' ? 1 : 0);
	
	this.scrollId = setInterval(this.referenceAsString + 'shiftBy(' + dx + ',' + dy + ')', 25);
}

Scroller.prototype.stopScrolling = function () {
	if(this.scrollId != 0) {
		clearInterval(this.scrollId);
		this.scrollId = 0;
	}
}