window.onload=init;

var d=document;
var lContainer; 			// object reference for the UL

var currentTop=0;			// the current Y position of the UL
var zInterval = null;		// animation thread interval
var direction;			// direction we're scrolling the UL. 0==up, 1==down
var startTop=0;			// the starting top of the UL

var scrollRate=6;			// initial rate of scrolling
var scrollTick=0;			// keeps track of long we've scrolled so we can slow it down accordingly
var listHeight=154;			// the current height of the UL

var MAX_SCROLL_TICK=4;		// the maximum value of scrollTick before it's reset
var LI_PADDING=2;		// the LI's padding value. used to compensate for overall dimensions
var LI_HEIGHT=10;		// the height of the LI
var MIN_LIST_HEIGHT=154;		// contracted height of the list
var REBOUND = -2;		// the value of scrollRate when we stop scrolling
var SPEED_TRANSITION=20;	// when this value + the MAX or MIN list height is reached, we set scrollRate to its slower rate

function init() {
	if(!d.getElementById)return; // bail out if this is an older browser

	up=d.getElementById("upArrow");
	down=d.getElementById("downArrow");

	//get number of items in list, if over 8 display arrows
	p = d.getElementById("subList");
	lItems = p.getElementsByTagName('li').length;

	if(lItems > 8){
	// apply onclick behaviors to the up arrow, down arrow and expansion control elements
	// remove absolute uri and replace with correct path to where images reside
	
		up.style.backgroundImage = 'url(http://www.adlit.org/images/medianav/arrowup.gif)';
		down.style.backgroundImage = "url(http://www.adlit.org/images/medianav/arrowdwn.gif)";
	
		down.onclick=function(){scrollObjects(0);}
		up.onclick=function(){scrollObjects(1);}
		lContainer = d.getElementById("subList");
	
		d.getElementById("viewArea").style.height=MIN_LIST_HEIGHT+"px";
	}
}

function scrollObjects(dir) {

	if(zInterval)return; // already scrolling.
	if((!dir && currentTop<=-240) || (dir && currentTop==0))return; // dont scroll up if we're at the top or down if at the bottom of the list
	direction=dir;
	zInterval=setInterval("animate()",20);
}

function animate() {
	// increment or decrement currentTop based on direction
	if(!direction) {
		currentTop-=scrollRate;
	} else {
		currentTop+=scrollRate;
	}
	scrollTick++;	
	if(scrollTick>=MAX_SCROLL_TICK) {
		scrollRate--; // slow the scroll rate down for a little style
		scrollTick=0;
	}

	lContainer.style.top=currentTop+"px";

	if(scrollRate<=REBOUND) {
		// scroll is finished. clear the interval and reset vars for the next scroll
		clearInterval(zInterval);
		zInterval=null;
		startTop=currentTop;
		scrollTick=0;
		scrollRate=6;
	}
}
