//requires jb_DHTMLLib.js

jb_scroller.prototype.scrollNorth = function() { this.startScroll(90) }
jb_scroller.prototype.scrollSouth = function() { this.startScroll(270) }
jb_scroller.prototype.scrollWest = function() { this.startScroll(180) }
jb_scroller.prototype.scrollEast = function() { this.startScroll(0) }

jb_scroller.prototype.getY = function() {
	return this.scrollTop;
}
jb_scroller.prototype.startScroll = function(deg, speed) {
	if (!this.loaded) {return false;}
	if (this.aniTimer) window.clearTimeout(this.aniTimer);
	this.overrideScrollAngle(deg);
	this.speed = speed ? speed : this.origSpeed;
	this.lastTime = (new Date()).getTime() - this.y.minRes;
	this.aniTimer = window.setTimeout(this.gRef + ".scroll()", this.y.minRes)
}


jb_scroller.prototype.endScroll = function() {
	if (!this.loaded) {return false;}
	this.aniTimer = window.clearTimeout(this.aniTimer);
	this.speed = this.origSpeed;
}


jb_scroller.prototype.overrideScrollAngle = function(deg) {
	deg = deg % 360;
	if (deg % 90 == 0) {
	var cos = deg == 0 ? 1 : deg == 180 ? -1 : 0;
	var sin = deg == 90 ? -1 : deg == 270 ? 1 : 0;
	} else {
	var angle = deg * Math.PI / 180;
	var cos = Math.cos(angle);
	var sin = Math.sin(angle);
	sin = -sin;
	}
	this.fx = cos / (Math.abs(cos) + Math.abs(sin));
	this.fy = sin / (Math.abs(cos) + Math.abs(sin));
	this.stopH = deg == 90 || deg == 270 ? this.scrollLeft : deg < 90 || deg > 270 ? this.scrollW : 0;
	this.stopV = deg == 0 || deg == 180 ? this.scrollTop : deg < 180 ? 0 : this.scrollH;
}

jb_scroller.prototype.overrideScrollSpeed = function(speed) {
	this.speed = speed;
}


jb_scroller.prototype.scrollTo = function(stopH, stopV, aniLen) {
	if (stopH != this.scrollLeft || stopV != this.scrollTop) {
		if (this.aniTimer) { window.clearTimeout(this.aniTimer); }
		this.lastTime = (new Date()).getTime();
		var dx = Math.abs(stopH - this.scrollLeft);
		var dy = Math.abs(stopV - this.scrollTop);
		var d = Math.sqrt(Math.pow(dx,2) + Math.pow(dy,2));
		this.fx = (stopH - this.scrollLeft) / (dx + dy);
		this.fy = (stopV - this.scrollTop) / (dx + dy);
		this.stopH = stopH;
		this.stopV = stopV;
		this.speed = d / aniLen * 1000;
		window.setTimeout(this.gRef + ".scroll()", this.y.minRes);
	}
}


jb_scroller.prototype.smoothScrollTo = function(stopH, stopV, aniLen) {
	if (this.loaded) {
	if (this.intv) { window.clearInterval(this.intv); }
	this.stopH = Math.max(Math.min(stopH, this.scrollW),0);
	this.stopV = Math.max(Math.min(stopV, this.scrollH),0);
	this.numSteps = this.y.fps * aniLen;
	this.startX = this.scrollLeft;
	this.startY = this.scrollTop;
	this.stepX = (stopH - this.scrollLeft) / this.numSteps;
	this.stepY = (stopV - this.scrollTop) / this.numSteps;
	this.aniLen = aniLen;
	this.intv = window.setInterval(this.gRef + ".smoothScroll()", 1000 / this.y.fps);
	}
}

jb_scroller.prototype.smoothScroll = function() {
	var dx = Math.abs(this.stopH - this.scrollLeft);
	var dy = Math.abs(this.stopV - this.scrollTop);
	if (dx > 1 || dy > 1) {
		//deceleration
/*		if (this.stepX != 0) {
			this.stepX *= Math.pow(Math.abs(this.stepX), -.035/(this.aniLen+1));
		}
		if (this.stepY != 0) {
			this.stepY *= Math.pow(Math.abs(this.stepY), -.035/(this.aniLen+1));
		}
*/
		var nx = Math.ceil(this.scrollLeft + this.stepX);
		var ny = Math.ceil(this.scrollTop + this.stepY);
		if ((this.startX < this.stopH && nx >= this.stopH)
		|| (this.startX > this.stopH && nx <= this.stopH)
		|| (this.startY < this.stopV && ny >= this.stopV)
		|| (this.startY > this.stopV && ny <= this.stopV)
		) {
			this.jumpTo(this.stopH, this.stopV);
			window.clearInterval(this.intv);
		} else {
			this.jumpTo(nx, ny);
		}
	} else {
		this.jumpTo(this.stopH, this.stopV);
		window.clearInterval(this.intv);
	}
}

jb_scroller.prototype.jumpTo = function(nx, ny) {
	nx = Math.min(Math.max(nx, 0), this.scrollW);
	ny = Math.min(Math.max(ny, 0), this.scrollH);
	this.scrollLeft = nx;
	this.scrollTop = ny;
	if (this.y.ns4) {
		this.content.moveTo(-nx, -ny);
	} else {
		this.content.style.left = -nx + "px";
		this.content.style.top = -ny + "px";
	}
}


jb_scroller.minRes = 10
jb_scroller.fps = 16;
jb_scroller.ie = document.all ? 1 : 0
jb_scroller.ns4 = document.layers ? 1 : 0
jb_scroller.dom = document.getElementById ? 1 : 0
jb_scroller.mac = navigator.platform == "MacPPC"
jb_scroller.mo5 = document.getElementById && !document.all ? 1 : 0

jb_scroller.prototype.scroll = function() {
	this.aniTimer = window.setTimeout(this.gRef + ".scroll()", this.y.minRes);
	var nt = (new Date()).getTime();
	var d = Math.round((nt - this.lastTime) / 1000 * this.speed);
	var nx = d * this.fx + this.scrollLeft;
	var ny = d * this.fy + this.scrollTop;
	//changed these lines from ny >= this.scrollTop to ny > this.scrollTop, because it was broken in Firefox. This makes it less accurate, but I'll be damned if I can figure out what was wrong. Rounding error?
	var xOut = (nx > this.scrollLeft && nx >= this.stopH) || (nx < this.scrollLeft && nx <= this.stopH);
	var yOut = (ny > this.scrollTop && ny >= this.stopV) || (ny < this.scrollTop && ny <= this.stopV);
	if (nt - this.lastTime != 0 &&
	((this.fx == 0 && this.fy == 0) ||
	(this.fy == 0 && xOut) ||
	(this.fx == 0 && yOut) ||
	(this.fx != 0 && this.fy != 0 && xOut && yOut))) {
		this.jumpTo(this.stopH, this.stopV);
		this.endScroll();
	} else {
		this.jumpTo(nx, ny);
		this.lastTime = nt;
	}
}


function jb_scroller(id, width, height, speed, initLeft, initTop)
{//width and height are probably unnecessary now that it's being controlled by CSS, but I can't see a good way to rip them out.
	var y = this.y = jb_scroller;
	if (!initLeft) initLeft = 0;
	if (!initTop) initTop = 0;
	if (document.layers && !y.ns4) history.go(0);
	if (y.ie || y.ns4 || y.dom) {
		this.loaded = false;
		this.id = id;
		this.origSpeed = speed;
		this.aniTimer = false;
		this.op = "";
		this.lastTime = 0;
		this.clipH = height;
		this.clipW = width;
		this.scrollTop = initTop;
		this.scrollLeft = initLeft;
		this.gRef = "jb_scroller_"+id;
		eval(this.gRef+"=this");
		var d = document;
		
		
		
		//supermodified by jonathan: CSS is now in external file for greater control
	}
}


jb_scroller.prototype.load = function() {
	var d;
	d = document;
	this.container = getRawObject(this.id + "SCContainer");
	this.content = getRawObject(this.id + "SCContent");
	this.docH = Math.max(this.y.ns4 ? this.content.document.height : this.content.offsetHeight, this.clipH);
	
	this.docW = Math.max(this.y.ns4 ? this.content.document.width : this.content.offsetWidth, this.clipW);
	this.scrollH = this.docH - this.clipH;
	this.scrollW = this.docW - this.clipW;
	this.loaded = true;
	this.scrollLeft = Math.max(Math.min(this.scrollLeft, this.scrollW),0);
	this.scrollTop = Math.max(Math.min(this.scrollTop, this.scrollH),0);
	this.jumpTo(this.scrollLeft, this.scrollTop);
}