// JavaScript Document

// declarations
var content;
var scrollUp;
var scrollDown;
var scroller;
var sY = 36;
var sTop = 36;
var sMax = 227;
var sOffset = 0;
var cMax;
var scrolling = false;
var scrollable = false;
var direction = 1;
var moveAmount = 0.1;
var cStart = 0;
var cY = 0; // position
var cTop = 0; // clipping
var cBot = 295; // clipping
var t;
var context = false;

function setVars() {
	// prevent inner frame from loading without outer frame 
	if(window.parent == window) {
		window.location.href = 'index.html?redirect=' + window.location.pathname;
	}
	// objects needing event handlers
	content = document.getElementById('contentArea');
	scrollUp = document.getElementById('scrollUp');
	scrollDown = document.getElementById('scrollDown');
	scroller = document.getElementById('scroller');
	content.style.top = '0px';
	content.style.clip = 'rect(0px 277px 295px 0px)';
	scroller.style.top = '36px';
	
	// other scrolly vars
	cMax = content.offsetHeight;
	
	// assign event handlers
	scrollUp.onmousedown = function() { scrollable = true; direction = 1; startScroll(); }
	scrollDown.onmousedown = function() { scrollable = true; direction = -1; startScroll(); }
	scrollUp.onmouseup = function() { scrollable = false; moveAmount = 0.1; }
	scrollDown.onmouseup = function() { scrollable = false; moveAmount = 0.1; }
	scroller.onmousedown = function(e) { scrolling = true; sOffset = (getMouseY(e) - sY); }
	// must be document level
	document.onmousemove = doScroller;
	document.onmouseup = function() { scrolling = false; }
}

function startScroll() {
	// call moveContent in .1 seconds
	t = window.setTimeout('moveContent();',100);
	return false;
}

function moveContent() {
	if(scrollable) {
		// move content in desired direction
		var y = moveAmount*direction;
		cY = cY + y;
		// don't allow scroll off screen
		if(cY < (cMax*-1)+cBot) { cY = (cMax*-1)+cBot; scrollable = false; }
		if(cY > 0) { cY = 0; scrollable = false; }
		content.style.top = Math.round(cY) + 'px';
		// change clipping of content
		var top = cTop + cY;
		var bot = cBot - cY;
		content.style.clip = 'rect(' + top + 'px,277px,' + bot + 'px,0)';
		// reset moveAmount (the longer mouse is down, the faster the move)
		if(moveAmount < 8) {
			// slowly ramps up move ammount
			moveAmount = moveAmount*1.1;
		} else {
			moveAmount = 8;
		}
		// move scrollbar
		var ratio = (cY*-1)/(cMax-cBot);
		sY = ((sMax-sTop)*ratio)+sTop;
		scroller.style.top = sY + 'px';
		// call moveContent in .01 seconds
		t = window.setTimeout('moveContent();',10)
	} else {
		window.clearTimeout(t);
	}
}

function psuedoLink(linkName) {
	var obj = document.getElementById(linkName);
	if(typeof obj != 'undefined') {
		cY = obj.offsetTop*-1;
		scrollable = true;
		moveContent();
		scrollable = false;
	}
	return false;
}

function getMouseY(e) {
	// detect mouse position
	// var posX = 0;
	var posY = 0;
	if (!e) {
		var e = window.event;
	}
	if (e.pageX || e.pageY) {
		// posX = e.pageX;
		posY = e.pageY;
	} else if (e.clientX || e.clientY) {
		// posX = e.clientX + document.body.scrollLeft;
		posY = e.clientY + document.body.scrollTop;
	}
	return posY;
}

function doScroller(e) {
	if(scrolling) {
		// postion scroller
		var posY = getMouseY(e);
		sY = posY - sOffset;
		if(sY < sTop) { sY = sTop; }
		if(sY > sMax) { sY = sMax; }
		scroller.style.top = sY + 'px';
		// postion content
		var ratio = (sY-sTop)/(sMax-sTop);
		cY = ratio*((cMax*-1)+cBot);
		content.style.top = Math.round(cY) + 'px';
		var top = cTop + cY;
		var bot = cBot - cY;
		content.style.clip = 'rect(' + top + 'px,277px,' + bot + 'px,0)';
	}
}
