var currentmainText = "slide1_Page"; // The default loaded mainText on the page
var tabMarker = "_Tab";
var pageMarker = "_Page";

// Scroll the page manually to the position of element "link", passed to us.

function ScrollmainText(link, sliderArea, offset)
{

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

	if (currentmainText == link) {
		return;
	}
	lastmainText = currentmainText;
	currentmainText = link;
	
	// Change the mainText highlight.
	// Extract the root mainText name, and use that to change the background image to 'top', revealing the alt. state

    mainTextTab = currentmainText.split("_")[0] + tabMarker;
    document.getElementById(mainTextTab).className = "active";
    if (lastmainText) {
	    lastTab = lastmainText.split("_")[0] + tabMarker;
	    document.getElementById(lastTab).className = "inactive";
	}
    
	// Get the element we want to scroll, get the position of the element to scroll to
	
	theScroll = document.getElementById(sliderArea);
	position = findElementPos(document.getElementById(link));

	// Get the position of the offset div -- the div at the far left.
	// This is the amount we compensate for when scrolling
	
	if (offset != "") {
		offsetPos = findElementPos(document.getElementById(offset));
		position[0] = position[0] - offsetPos[0];
	}

	scrollStart(theScroll, theScroll.scrollLeft, position[0], "horiz");
	// return false;
}

// Scroll the page using the arrows

function ScrollArrow(direction, showcaseWork, sliderArea, offset) {

	showcaseWorkElem = document.getElementById(showcaseWork);
	showcaseWorkNames = new Array();
    
	// Find all the <li> elements in the showcaseWork, and extract their id's into an array.
    
	if (showcaseWorkElem.hasChildNodes())
	{
		var children = showcaseWorkElem.childNodes;
		for (var i = 0; i < children.length; i++) 
		{
			if (showcaseWorkElem.childNodes[i].tagName == "LI") {
				showcaseWorkNames.push(showcaseWorkElem.childNodes[i].id.split("_")[0]);
			}
		}
	}

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

	for (var i = 0; i < showcaseWorkNames.length; i++) {
		if (showcaseWorkNames[i] == currentmainText.split("_")[0]) {
			if (direction == "left") {
				if (i - 1 < 0) {
					gotoTab = showcaseWorkNames[showcaseWorkNames.length - 1];
				} else {
					gotoTab = showcaseWorkNames[i - 1];
				}
			} else {
				if ((i + 1) > (showcaseWorkNames.length - 1)) {
					gotoTab = showcaseWorkNames[0];
				} else {
					gotoTab = showcaseWorkNames[i + 1];
				}
			}
		}
	}
	
	// Go to the mainText name!
	
	ScrollmainText(gotoTab+pageMarker, sliderArea, offset);

}

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

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

function scrollStart(elem, start, end, direction)
{
	//console.log("scrollStart from "+start+" to "+end+" in direction "+direction);

	if (scrollanim.timer != null) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	scrollanim.time = 0;
	scrollanim.begin = start;
	scrollanim.change = end - start;
	scrollanim.duration = 25;
	scrollanim.element = elem;
	
	if (direction == "horiz") {
		scrollanim.timer = setInterval("scrollHorizAnim();", 15);
	}
	else {
		scrollanim.timer = setInterval("scrollVertAnim();", 15);
	}
}

function scrollVertAnim()
{
	if (scrollanim.time > scrollanim.duration) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	else {
		move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
		scrollanim.element.scrollTop = move; 
		scrollanim.time++;
	}
}

function scrollHorizAnim()
{
	if (scrollanim.time > scrollanim.duration) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	else {
		move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
		scrollanim.element.scrollLeft = move;
		scrollanim.time++;
	}
}
