//horizontal Scroller (HS)
// uses functions.js

var hsFirtPanelID = 'panel_1';
var currentIndex = 1; // The default loaded section on the page
var prefixStr = 'panel_';

// Scroll the page manually to the position of element "link", passed to us.

function ScrollSection(indexLink, scrollArea)
{
	// Store the last section, and update the current section
	if (currentIndex == indexLink)
		return;
	
	//change navigation button source
	GetElement('hs_selector_' + currentIndex.toString()).src = '/images/pic_selector_off.png';
	
	currentIndex = indexLink;
	
	// Get the element we want to scroll, get the position of the element to scroll to
	theScroll = document.getElementById(scrollArea);
	position = findElementPos(document.getElementById(prefixStr + indexLink.toString()));

	// Get the position of the offset div -- the div at the far left.
	// This is the amount we compensate for when scrolling
	if (hsFirtPanelID != "") {
		offsetPos = findElementPos(document.getElementById(hsFirtPanelID));
		position[0] = position[0] - offsetPos[0];
	}

	scrollStart(theScroll, theScroll.scrollLeft, position[0], "horiz");
	
	//change navigation button source
	GetElement('hs_selector_' + indexLink.toString()).src = '/images/pic_selector_on.png';
	
	ScrollAnimationReset();
}

// Scroll the page using the arrows
function ScrollArrow(direction, scrollArea, panelCount) 
{
	var nextIndex = currentIndex;
	
	if (direction == "left")
	{
		if (currentIndex==1)
			nextIndex = panelCount;
		else
			nextIndex -= 1;
		
	} 
	else 
	{
		if (currentIndex==panelCount)
			nextIndex = 1;
		else
			nextIndex += 1;
	}
	
	// Go to new section
	ScrollSection(nextIndex, scrollArea);
}

//
// 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};
var hsPlayerID;
var hsPanelCount;

function ScrollAnimationStart(panelCount)
{
	hsPanelCount = panelCount;
	hsPlayerID = setInterval("ScrollArrow('right', 'scroller', "+hsPanelCount+")", 5000);
}

function ScrollAnimationStop()
{
	clearInterval(hsPlayerID);
}

function ScrollAnimationReset()
{
	ScrollAnimationStop();
	ScrollAnimationStart(hsPanelCount);
}


function scrollStart(elem, start, end, 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++;
	}
}


//
// MOVE: Animate the move of an element.
//
// Move is also synchronous. One at a time, please.
//

var moveanim = {time:0, beginX:0, changeX:0.0, beginY:0, changeY:0, duration:0.0, element:null, timer:null};

function moveStart(elem, startX, endX, startY, endY, duration)
{
	if (moveanim.timer != null) {
		clearInterval(moveanim.timer);
		moveanim.timer = null;
	}
	moveanim.time = 0;
	moveanim.beginX = startX;
	moveanim.changeX = endX - startX;
	moveanim.beginY = startY;
	moveanim.changeY = endY - startY;
	moveanim.duration = duration;
	moveanim.element = elem;

	moveanim.timer = setInterval("moveAnimDo();", 15);
}

function moveAnimDo()
{
	if (moveanim.time > moveanim.duration) {
		clearInterval(moveanim.timer);
		moveanim.timer = null;
	}
	else {
		moveX = cubicOut(moveanim.time, moveanim.beginX, moveanim.changeX, moveanim.duration);
		moveY = cubicOut(moveanim.time, moveanim.beginY, moveanim.changeY, moveanim.duration);
		moveanim.element.style.left = moveX + "px";
		moveanim.element.style.top = moveY + "px";
		moveanim.time++;
	}
}

function sineInOut(t, b, c, d)
{
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}


// Utility: Get Shift Key Status
// IE events don't seem to get passed through the function, so grab it from the window.

function getShift(evt) 
{
	var shift = false;
	if (! evt && window.event) {
		shift = window.event.shiftKey;
	} else if (evt) {
		shift = evt.shiftKey;
		if (shift) evt.stopPropagation(); // Prevents Firefox from doing shifty things
	}
	return shift;
}

// 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);
}


