var rotateCount = 0;
var doScrollTimer;
var rotateLimit = 5;
var frequency = 4000; //in milliseconds
var displayWindows = 1;
var context = "";

var currentSection = ""; // The default loaded section on the page
var lastSection = "";

// Scroll the page manually to the position of element "link", passed to us.
function ScrollSection(link, scrollArea, offset)
{

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

	if (currentSection == link) {
		return;
	}
	lastSection = currentSection;
	
	currentSection = link;
	
	// Get the element we want to scroll, get the position of the element to scroll to
	
	theScroll = document.getElementById(scrollArea);
	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];
	}
	
	deselectPrize();
	scrollStart(theScroll, theScroll.scrollLeft, position[0], "scrollHoriz");

	// return false;
}

// Scroll the page using the arrows

function ScrollArrow(direction, toolbar, scrollArea, offset) {

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

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

	for (var i = 0; i < toolbarNames.length; i++) {
		if (toolbarNames[i] == currentSection) {
			if (direction == "left") {
				if (i - 1 < 0) {
					gotoTab = toolbarNames[toolbarNames.length - displayWindows];
				} else {
					gotoTab = toolbarNames[i - 1];
				}
			} else {
				if ((i + 1) > (toolbarNames.length - displayWindows)) {
					gotoTab = toolbarNames[0];
				} else {
					gotoTab = toolbarNames[i + 1];
				}
			}
		}
	}
	
	ScrollSection(gotoTab, scrollArea, offset);

}

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

function scrollStart(elem, start, end, direction)
{
	//alert("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 == "scrollHoriz") {
		scrollanim.timer = setInterval("scrollHorizAnim();", 15);
	}
	else if (direction == "scrollVert"){
		scrollanim.timer = setInterval("scrollVertAnim();", 15);
	}
	else if (direction == "resizeVert"){
		scrollanim.timer = setInterval("resizeVertAnim();", 15);
	}
}

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

function scrollVertAnim()
{
	if (scrollanim.time > scrollanim.duration) {
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
		selectPrize();
	}
	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;
		selectPrize();
	}
	else {
		move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
		scrollanim.element.scrollLeft = move;
		scrollanim.time++;
	}
}

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;
}

function selectPrize()
{
	var selectedObj = document.getElementById(currentSection);
	var selectedImage = document.createElement('div');
	selectedImage.id = "selectedImage";
	selectedObj.appendChild(selectedImage);
	prizeId = currentSection.split("_")[1];
	replaceAjax(context+'/microsite_ajax.jsp?prizeId='+prizeId,document.getElementById('content_main'),true);
}

function deselectPrize()
{
	var oldSelectedObj = document.getElementById(lastSection);
	var selImg = document.getElementById('selectedImage');
	if (selImg != null){
		oldSelectedObj.removeChild(selImg);
	}
}
