// JavaScript Document

var clipTop = 0;
var clipWidth = 470;
var clipBottom = 411;
var topper = 3;
var lyrheight = 0;
var anchorOverflow = 0; 
var time,amount,theTime,theHeight,DHTML;


// Function to calculate the actual height of the layer we're scrolling.
function init() {

	if(!document.getElementById && !document.all && !document.layers)
		return;
	
	var x = new getObj("scrollcopy");

	if (document.getElementById || document.all) {
		lyrheight = x.obj.offsetHeight;
		x.style.clip = 'rect('+clipTop+'px,'+clipWidth+'px,'+clipBottom+'px,0)';
	}
	
	if(lyrheight > (clipBottom - clipTop)) {
		scrolldiv = new getObj("scrollarrows");
		scrolldiv.style.display = "block"; 
		backdiv = new getObj("backtoTop");
		backdiv.style.display = "block";
		
	}

}

// Dummy wrapper function for realscroll...probably not really necessary, but it doesn't hurt anything. 
function scrollayer(layername,amt,tim) {

	init(); 
	
	thelayer = new getObj(layername);
	if (!thelayer) return;
	amount = amt;
	theTime = tim;
	realscroll();
}

// The function actually responsible for the scrolling. This is where the magic happens, folks. 
function realscroll() {

	clipTop += amount;
	clipBottom += amount;
	topper -= amount;
	
	// Did we scroll past the ends?! OH NOES!!!1
	if (clipTop < 0 || clipBottom > lyrheight && !anchorOverflow) {
		clipTop -= amount;
		clipBottom -= amount;
		topper += amount;
		return;
	}
	
	if(clipBottom > lyrheight && anchorOverflow && amount > 0) {
		clipTop -= amount;
		clipBottom -= amount; 
		topper += amount;
		return; 
	}
	
	// anchorOverflow should be set back to false once we're no longer 
	// scrolled past the ends. 
	if(clipTop > 0 && clipBottom < lyrheight && anchorOverflow) 
		anchorOverflow = 0; 
	
	// Actually change the layer now so that it appears to scroll. 
	if (document.getElementById || document.all) {
		clipstring = 'rect('+clipTop+'px,'+clipWidth+'px,'+clipBottom+'px,0)';
		thelayer.style.clip = clipstring;
		thelayer.style.top = topper + 'px';
	}

	time = setTimeout('realscroll()',theTime);
}

// Called with an "onmouseup" event in scroll.html so that when the scroll buttons are released, the scrolling stops. Magic!
function stopscroll() {
	if (time) clearTimeout(time);
}

// Lets you jump to the beginning or the end of the layer. 
// TODO: Edit this to jump to anchors? 
function scrolljump(layername, direction) {

	init();

	thelayer = new getObj(layername);
	if (!thelayer) return;

	difference = clipBottom - clipTop;

	switch(direction) {
		case 1:				// Jump to the top
		    amount = clipBottom;
		    clipTop = 0; 
		    clipBottom = difference;
		    amount -= clipBottom;
		    topper += amount;
		    break;
		    
		case -1:			// Jump to the bottom 
		    amount = clipTop;
		    clipBottom = lyrheight;
		    clipTop = clipBottom - difference;
		    amount = clipTop - amount;
		    topper -= amount;
		    break;
	}

	if (document.getElementById || document.all) {
		clipstring = 'rect('+clipTop+'px,'+clipWidth+'px,'+clipBottom+'px,0)';
		thelayer.style.clip = clipstring;
		thelayer.style.top = topper + 'px';
	}

}

// Function which simulates the behavior of an anchor tag within the scroll layer. 
function jumpToAnchor(layername, anchorid) {

	init();
	
	var thelayer = new getObj(layername); 
	var anchor = new getObj(anchorid); 
	
	if(!anchor || !thelayer) 
		return;
		
	difference = clipBottom - clipTop; 
	anchorlocation = anchor.obj.offsetTop;
		
	amount = anchorlocation - clipTop; 
	clipTop = anchorlocation; 
	clipBottom = clipTop + difference;
	topper -= amount; 
		
	if(clipBottom > lyrheight)
		anchorOverflow = 1; 
	
	if(document.getElementById || document.all) {
		clipstring = 'rect(' + clipTop + 'px, ' + clipWidth + 'px, ' + clipBottom + 'px,0)'; 
		thelayer.style.clip = clipstring;
		thelayer.style.top = topper + 'px';
	}
}

function getObj(name) {
	if (document.getElementById) {
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	}
	else if (document.all) {
		this.obj = document.all[name];
		this.style = document.all[name].style;
	}
}


