var timer = new Array(null, null, null);
var theScroll = new Array();
var content = new Array();

var currentIndex = 0;
var timerInterval = 15;
var timerDelay = 800;

var arraypos = 0;
var no_end = 0;

var autoDelay1 = 60*1000;
var autoDelay2 = 60*1000;

var autoTimer = null;

var toolbarNames = new Array('item_1','item_2','item_3','item_4','item_5','item_6','item_7');

function add_content(text){
	
	if(arraypos < content.length){
	}
	else{
		content.push(text);
		arraypos++;
	}
	
}

function write_content(a1, a2, a3, a4, a5){

	if(a1) timerInterval = a1;
	if(a2) timerDelay = a2;

	no_end = ( a3 ? 1 : 0 );

	if(a4) autoDelay1 = a4*1000; // Length of time before first image rotates to the second
	if(a5) autoDelay2 = a5*1000; // Length of time between subsequent items

	var k = 0;

	var str = '';


	 str += '<TABLE ALIGN="center" BORDER="0" CELLSPACING=0 CELLPADDING=0 WIDTH="186">';
	 str += '';
	 str += '<tr><td>';

	 str += '<DIV id="frame" style="overflow: hidden; margin: 0 auto; width: 216px; position: relative; display:inline;">';
	 str += '<TABLE ALIGN="left" BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH="216">';

	for (var i = 1; i < 2; i++) {

		var space = ( i == 1 ? '203' : '203' );

		 str += '<TR ALIGN="left" VALIGN="top" style="height:'+space+'px;"><TD>';
		 str += '<DIV id="scroller-'+i+'" style="width: 216px; margin: 0 auto; overflow: hidden;">';
		 str += '<DIV id="content-'+i+'" style="height: 275px; width: 5250px;">';

		for (var j = 1; j < 9; j++) {
			 str += '<DIV id="item_'+j+'-'+i+'" style="width: 216px; margin: 0px; float: left; height: 215px;">';
			 str += content[k++];
			 str += '</DIV>';
		}

		 str += '</DIV>';
		 str += '</DIV>';
		 str += '</TD></TR>';

	}

	 str += '</TABLE>';
	 str += '</DIV>';
	 str += '</td></tr>';

	 str += '';
	 str += '<tr><td><div style="width:1px; position: absolute; left: 8px; top: 230px; display:inline;">'; // This refers to the block of navigation boxes
	 str += '<TABLE ALIGN="center" BORDER="0" CELLSPACING=0 CELLPADDING=0 HEIGHT="13" WIDTH="170" style="postion: relative; margin-top: 5px; margin-left: 20px;"><TR ALIGN="middle" VALIGN="middle"><TD><a href="javascript:ScrollArrow(\'left\',\'scroller\',\'item_1\',1);" ><img src="../images/leftarrow.png" style="border:0" alt="Scroll Left" /></a></TD><TD>';
	//  str += '<TABLE ID="nav-1" onClick="javascript:NavClick(0);" onMouseOver="javascript:document.body.style.cursor=\'hand\'" onmouseout="javascript:document.body.style.cursor=\'default\'" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="13" WIDTH="13" style="border:solid 1px #999999; background-color: #F3F3F3"><TR><TD></TD></TR></TABLE></TD><TD>';
	//  str += '<TABLE ID="nav-2" onClick="javascript:NavClick(1);" onMouseOver="javascript:document.body.style.cursor=\'hand\'" onmouseout="javascript:document.body.style.cursor=\'default\'" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="13" WIDTH="13" style="border:solid 1px #999999; background-color: #F3F3F3"><TR><TD></TD></TR></TABLE></TD><TD>';
	//  str += '<TABLE ID="nav-3" onClick="javascript:NavClick(2);" onMouseOver="javascript:document.body.style.cursor=\'hand\'" onmouseout="javascript:document.body.style.cursor=\'default\'" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="13" WIDTH="13" style="border:solid 1px #999999; background-color: #F3F3F3"><TR><TD></TD></TR></TABLE></TD><TD>';
	//  str += '<TABLE ID="nav-4" onClick="javascript:NavClick(3);" onMouseOver="javascript:document.body.style.cursor=\'hand\'" onmouseout="javascript:document.body.style.cursor=\'default\'" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="13" WIDTH="13" style="border:solid 1px #999999; background-color: #F3F3F3"><TR><TD></TD></TR></TABLE></TD><TD>';
	//  str += '<TABLE ID="nav-5" onClick="javascript:NavClick(4);" onMouseOver="javascript:document.body.style.cursor=\'hand\'" onmouseout="javascript:document.body.style.cursor=\'default\'" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="13" WIDTH="13" style="border:solid 1px #999999; background-color: #F3F3F3"><TR><TD></TD></TR></TABLE></TD><TD>';
	//  str += '<TABLE ID="nav-6" onClick="javascript:NavClick(5);" onMouseOver="javascript:document.body.style.cursor=\'hand\'" onmouseout="javascript:document.body.style.cursor=\'default\'" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="13" WIDTH="13" style="border:solid 1px #999999; background-color: #F3F3F3"><TR><TD></TD></TR></TABLE></TD><TD>';
	//  str += '<TABLE ID="nav-7" onClick="javascript:NavClick(6);" onMouseOver="javascript:document.body.style.cursor=\'hand\'" onmouseout="javascript:document.body.style.cursor=\'default\'" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="13" WIDTH="13" style="border:solid 1px #999999; background-color: #F3F3F3"><TR><TD></TD></TR></TABLE></TD><TD>';
	//  str += '<TABLE ID="nav-8" onClick="javascript:NavClick(7);" onMouseOver="javascript:document.body.style.cursor=\'hand\'" onmouseout="javascript:document.body.style.cursor=\'default\'" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="13" WIDTH="13" style="border:solid 1px #999999; background-color: #F3F3F3"><TR><TD></TD></TR></TABLE></TD><TD>';
	 	str += '<TABLE ID="nav-1" onMouseOver="javascript:NavClick(0);document.body.style.cursor=\'hand\'" onmouseout="javascript:document.body.style.cursor=\'default\'" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="13" WIDTH="13" style="border:solid 1px #999999; background-color: #F3F3F3"><TR><TD></TD></TR></TABLE></TD><TD>';
	 	  str += '<TABLE ID="nav-2" onMouseOver="javascript:NavClick(1);document.body.style.cursor=\'hand\'" onmouseout="javascript:document.body.style.cursor=\'default\'" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="13" WIDTH="13" style="border:solid 1px #999999; background-color: #F3F3F3"><TR><TD></TD></TR></TABLE></TD><TD>';
	 	  str += '<TABLE ID="nav-3" onMouseOver="javascript:NavClick(2);document.body.style.cursor=\'hand\'" onmouseout="javascript:document.body.style.cursor=\'default\'" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="13" WIDTH="13" style="border:solid 1px #999999; background-color: #F3F3F3"><TR><TD></TD></TR></TABLE></TD><TD>';
	 	  str += '<TABLE ID="nav-4" onMouseOver="javascript:NavClick(3);document.body.style.cursor=\'hand\'" onmouseout="javascript:document.body.style.cursor=\'default\'" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="13" WIDTH="13" style="border:solid 1px #999999; background-color: #F3F3F3"><TR><TD></TD></TR></TABLE></TD><TD>';
	 	  str += '<TABLE ID="nav-5" onMouseOver="javascript:NavClick(4);document.body.style.cursor=\'hand\'" onmouseout="javascript:document.body.style.cursor=\'default\'" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="13" WIDTH="13" style="border:solid 1px #999999; background-color: #F3F3F3"><TR><TD></TD></TR></TABLE></TD><TD>';
	 	  str += '<TABLE ID="nav-6" onMouseOver="javascript:NavClick(5);document.body.style.cursor=\'hand\'" onmouseout="javascript:document.body.style.cursor=\'default\'" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="13" WIDTH="13" style="border:solid 1px #999999; background-color: #F3F3F3"><TR><TD></TD></TR></TABLE></TD><TD>';
	 	  str += '<TABLE ID="nav-7" onMouseOver="javascript:NavClick(6);document.body.style.cursor=\'hand\'" onmouseout="javascript:document.body.style.cursor=\'default\'" ALIGN="center" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="13" WIDTH="13" style="border:solid 1px #999999; background-color: #F3F3F3"><TR><TD></TD></TR></TABLE></TD><TD>';
	 	  	 
	 
	 str += '<TABLE><TR><TD></TD></TR></TABLE></TD><TD><a href="javascript:ScrollArrow(\'right\',\'scroller\',\'item_1\',1);" ><img src="../images/scroll_right.png" style="border:0"  alt="Scroll Right" /></a></TD></TR></TABLE>';
	 str += '</div></td></tr>';
	 str += '';
	 str += '</table>';

	document.write(str);

	autoTimer = setTimeout('AutoScrollIt();', autoDelay1);

}

function NavClick(itemIndex) {

	if(currentIndex != itemIndex){
		
		
		var navBoxCur = 'nav-' + (currentIndex+1);
		document.getElementById(navBoxCur).style.backgroundColor = '#E1E1E1';  
		document.getElementById(navBoxCur).style.border = 'solid 1px #999999';
    
		
		if(!theScroll[0]) 
		  theScroll[0] = document.getElementById('scroller-1');
		var pos = findElementPos(document.getElementById('item_'+(itemIndex+1)+'-1'));	
		var offsetPos = findElementPos(document.getElementById('item_1-1'));
		pos[0] = pos[0] - offsetPos[0];
;
		clearTimeout(autoTimer);
		scrollStart(pos[0], "horiz", 0, currentIndex, itemIndex);
		currentIndex = itemIndex;
		autoTimer = setTimeout('AutoScrollIt();', autoDelay1);
		
	}
}



function AutoScrollIt(){
	//alert('auto');

	ScrollArrow('right','scroller','item_1', 0);

	autoTimer = setTimeout('AutoScrollIt();', autoDelay2);

}

function ScrollSection(nextIndex, scrollArea, offset, bool)
{

	// Store the last section, and update the current section

	if (currentIndex == nextIndex) {
		return;
	}
	lastIndex = currentIndex;
	currentIndex = nextIndex;



	//CTF - Only one scroll area
	theScroll[0] = document.getElementById(scrollArea+'-1');
	position = findElementPos(document.getElementById('item_'+(currentIndex+1)+'-1'));

	// Get the position of the offset div -- the div at the far left.
	// This is the amount we compensate for when scrolling

	//CTF - only one offset
	if (offset != "") {
		
		offsetPos = findElementPos(document.getElementById(offset+'-1'));
		position[0] = position[0] - offsetPos[0];
	}
	
	
	timer[0] = setTimeout('scrollStart('+position[0]+', "horiz", 0, '+lastIndex+', '+currentIndex+');', 0);

}

// Scroll the page using the arrows
function ScrollArrow(direction, scrollArea, offset, clearAuto) {

	if(clearAuto) clearTimeout(autoTimer);

	var bool = 0;

	// Now iterate through our array of tab names, find matches, and determine where to go.
	for (var i = 0; i < toolbarNames.length; i++) {
		if (i == currentIndex) {
			if (direction == "left") {
				if (i - 1 < 0) {
					nextIndex = toolbarNames.length - 1;
					bool =1;
				} else {
					nextIndex = i - 1;
				}
			} else {
				if ((i + 1) > (toolbarNames.length - 1)) {
					nextIndex = 0;
					bool =1;
				} else {
					nextIndex = i + 1;
				}
			}
		}
	}
	
	// Go to the section name!
	ScrollSection(nextIndex, scrollArea, offset, bool);

	if(clearAuto) autoTimer = setTimeout('AutoScrollIt();', autoDelay1);

}



// Scroll the page using the arrows

function ScrollArrowSingle(direction, scrollArea, offset, clearAuto, level) {

	if(clearAuto) clearTimeout(autoTimer);

	var bool = 0;

	// Now iterate through our array of tab names, find matches, and determine where to go.

	for (var i = 0; i < toolbarNames.length; i++) {
		if (i == currentIndex) {
			if (direction == "left") {
				if (i - 1 < 0) {
					nextIndex = toolbarNames.length - 1;
					bool =1;
				} else {
					nextIndex = i - 1;
				}
			} else {
				if ((i + 1) > (toolbarNames.length - 1)) {
					nextIndex = 0;
					bool =1;
				} else {
					nextIndex = i + 1;
				}
			}
		}
	}
	
	// Go to the section name!
		ScrollLevel(nextIndex, scrollArea, offset, bool, level);

	if(clearAuto) autoTimer = setTimeout('AutoScrollIt();', autoDelay1);

}


//
// Animated Scroll Functions
// Scrolls are synchronous -- only one at a time.
//

var scrollanim = new Array();

scrollanim[0] = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};



function scrollStart(end, direction, index, startIndex, endIndex)
{
	//alert(end+' '+direction+' '+index+' '+ startIndex+' '+ endIndex);

    var navBox1 = 'nav-' + ((startIndex*1)+(index+1));
	document.getElementById(navBox1).style.backgroundColor = '#E1E1E1'; //defines VISITED little nav squares// 
    document.getElementById(navBox1).style.border = 'solid 1px #999999';

	elem = theScroll[index];
	start = theScroll[index].scrollLeft;



	if (scrollanim[index].timer != null) {
		clearInterval(scrollanim[index].timer);
		scrollanim[index].timer = null;
	}
	scrollanim[index].time = 0; //how long after the rotation before the nav square is illuminated// 
	scrollanim[index].begin = start;
	scrollanim[index].change = end - start;
	scrollanim[index].duration = 25; //the movement speed when rotating between images// 
	scrollanim[index].element = elem;
	
	scrollanim[index].timer = setInterval("scrollHorizAnim("+index+","+endIndex+");", timerInterval);
}


function scrollHorizAnim(index, navIndex)
{

	if (scrollanim[index].time > scrollanim[index].duration) {
		clearInterval(scrollanim[index].timer);
		scrollanim[index].timer = null;

		var navBox2 = 'nav-' + ((navIndex*1)+(index+1));
		document.getElementById(navBox2).style.backgroundColor = '#336699'; //defines CURRENT little nav squares//
//		document.getElementById(navBox2).style.borderWidth = '2px';
    	document.getElementById(navBox2).style.border = 'solid 1px #999999';
	}
	else {
		move = sineInOut(scrollanim[index].time, scrollanim[index].begin, scrollanim[index].change, scrollanim[index].duration);
		scrollanim[index].element.scrollLeft = move;
		scrollanim[index].time++;
	}
}

// Utility: Find the Y position of an element on a page. Return Y and X as an array

function findElementPos(elemFind)
{
	var elemX = 0
	var elemY = 0;
	do {
	elemX += elemFind.offsetLeft;
elemY += elemFind.offsetTop;
} while ( elemFind = elemFind.offsetParent )

return Array(elemX, elemY);
}


function sineInOut(t, b, c, d)
{
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}
