//
// Handles sideways scrolling of bike panel
// Muchos cudos to Panic Inc for scroll code.
//
// 
// Additions made to remove the need for tabs
//

// Scroll: Setup Scrolling Stuff

var currentSection = "image_option_1"; // The default loaded section on the page
//var tabTag = "-tab";
//var paneTag = "-pane";

var currentIndex = 0;
var paneIDs = new Array();

var loop = "false";

var isFirstMove = "false";

function registerPaneID(paneID)
{
	paneIDs.push(paneID);
	//alert("added: " + paneID + " to " + paneIDs);
}


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

function ScrollSection(link, container, scrollArea, direction, offset)
{
	//alert("here");

	theScroll = document.getElementById(scrollArea);
	position = findElementPos(document.getElementById(link));
	
	//alert("theScroll: " + theScroll + " position: " + position + " scrollArea: " + scrollArea + " scrollArea: " + container);
	//alert("t " + document.getElementById(offset));
	// Get the position of the offset div -- the div at the far left.
	// This is the amount we compensate for when scrolling
	
	if (offset != "") {
		//alert("here: " + document.getElementById(offset));
		offsetPos = findElementPos(document.getElementById(offset));
		//alert(position[0] + " " + offsetPos[0]);
		position[0] = position[0] - offsetPos[0];
		//alert("ya: " + position[0]);
	}
	
	//alert("mmkay: " + scrollArea);
	//alert("moving to position: " + position[0] + " containerWidth: " + getElementWidth(container) + " scrollAreaWidth: " + getElementWidth(scrollArea));
	
	if(position[0] + getElementWidth(scrollArea) > getElementWidth(container))
	{
		//alert("fail: " + direction);
		if(direction == "left")
		{
			ScrollArrow(direction, container, scrollArea, offset);
		}
		else
		{
			if(loop != "false")
			{
				GoToPaneID(0, container, scrollArea, offset);
			}
			else
			{
				GoToPaneID(currentIndex - 1, container, scrollArea, offset);
			}
		}
	}
	else
	{
		if(isFirstMove == "true")
		{
			isFirstMove = "false";
			scrollJump(theScroll, theScroll.scrollLeft, position[0], "horiz");	
		}
		else
		{
			
			scrollStart(theScroll, theScroll.scrollLeft, position[0], "horiz");	
		}
	}
	// return false;
}

function GoToPaneID(targetID, container, scrollArea, offset)
{
	//alert("ReturnToScrollStart: " + container + " " + scrollArea + " " + offset);
	currentIndex = targetID;
	
	gotoPane = paneIDs[currentIndex];
	
	//alert("ReturnToScrollStart: " + currentIndex + " " + gotoPane);
	
	ScrollSection(gotoPane, container, scrollArea, "", offset);
}

// Scroll the page using the arrows

function ScrollArrow(direction, container, scrollArea, offset)
{
	//alert("currentIndex: " + currentIndex + " direction " + direction + " " + );
	
	if (direction == "left") {
		if (currentIndex - 1 < 0) {
			if(loop != "false")
			{
				currentIndex = paneIDs.length - 1;
			}
			else
			{
				currentIndex = currentIndex;
			}
		} else {
			currentIndex = currentIndex - 1;
		}
	} else {
		if ((currentIndex + 1) > (paneIDs.length - 1)) {
			if(loop != "false")
			{
				currentIndex = 0;
			}
			else
			{
				currentIndex = currentIndex;
			}
		} else {
			currentIndex = currentIndex + 1;
		}
	}

	// Go to the section name!
	
	gotoPane = paneIDs[currentIndex];
	
	//alert("going to: " + paneIDs[currentIndex] + " (" + currentIndex + ")");
	
	ScrollSection(gotoPane, container, scrollArea, direction, 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 scrollJump(elem, start, end, direction)
{
	if (direction == "horiz")
	{
		elem.scrollLeft = end;
	}
	else
	{		
		elem.scrollTop = end;
	}
}

function scrollStart(elem, start, end, direction)
{
	//console.log("scrollStart from "+start+" to "+end+" in direction "+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 == "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 getElementWidth(Elem) {
	//alert(BrowserDetect.browser + " " + BrowserDetect.version);
	/*if (BrowserDetect.browser == "Netscape") {
		var elem = getObjNN4(document, Elem);
		return elem.clip.width;
	} else {*/
		if(document.getElementById) {
			var elem = document.getElementById(Elem);
		} else if (document.all){
			var elem = document.all[Elem];
		}
		if (BrowserDetect.browser == "Opera" && BrowserDetect.version == 5) {
			xPos = elem.style.pixelWidth;
		} else {
			xPos = elem.offsetWidth;
		}
		return xPos;
	/*}*/
}

//console.log("Initialized");
