// (C) 2007 Panic, Inc. http://www.panic.com/coda/

window.onload = initTabs;

var currentSection = "model-panel";
var urlAnchor = self.document.location.hash.substring(1);
if(urlAnchor) currentSection = urlAnchor;
var tabTag = "-tab";
var paneTag = "-panel";

function initTabs() {
	sectionTab = currentSection.split("-")[0] + tabTag;
    currentTab = document.getElementById(sectionTab);
	if(currentTab) currentTab.className = "active";
	
	modelAbv = urlAnchor.split("-")[0];
	kayakTab = document.getElementById("tab_kayakbg");
	if(kayakTab && urlAnchor) kayakTab.className = "kayaktab_" + modelAbv;
	underTab = document.getElementById("tab_models");
		if(modelAbv == 'qst' || modelAbv == 'over' || modelAbv == 'day' || modelAbv == 'comp') modelNum = 'nc17';
		else if(modelAbv == 'esc' || modelAbv == 'exc') modelNum = 'nc15';
		else if(modelAbv == 'expd') modelNum = 'nc19';
		else if(modelAbv == 'car') modelNum = 'nc22';
		else modelNum = 'model';
	if(underTab && urlAnchor) underTab.className = "undertab_" + modelNum;
}

function ScrollSection(link, scrollArea, offset) {

	if (currentSection == link) return;
	lastSection = currentSection;
	currentSection = link;
	
	modelAbv = currentSection.split("-")[0];
	if(modelAbv == 'qst' || modelAbv == 'over' || modelAbv == 'day' || modelAbv == 'comp') modelNum = 'nc17';
		else if(modelAbv == 'esc' || modelAbv == 'exc') modelNum = 'nc15';
		else if(modelAbv == 'expd') modelNum = 'nc19';
		else if(modelAbv == 'car') modelNum = 'nc22';
		else modelNum = 'model';
	kayakTab = document.getElementById("tab_kayakbg");
		if(kayakTab) kayakTab.className = "kayaktab_" + modelAbv;
	underTab = document.getElementById("tab_models");
		if(underTab) underTab.className = "undertab_" + modelNum;
		
	sectionTab = modelAbv + tabTag;
    document.getElementById(sectionTab).className = "active";
		
    if (lastSection) {
	    lastTab = lastSection.split("-")[0] + tabTag;
	    document.getElementById(lastTab).className = "inactive";
	}
    
	theScroll = document.getElementById(scrollArea);
	position = findElementPos(document.getElementById(link));

	if (offset != "") {
		offsetPos = findElementPos(document.getElementById(offset));
		position[0] = position[0] - offsetPos[0];
	}

	scrollStart(theScroll, theScroll.scrollLeft, position[0], "horiz");
	// return false;
}

function ScrollArrow(direction, toolbar, scrollArea, offset) {

	toolbarElem = document.getElementById(toolbar);
	toolbarNames = new Array();
    
	if (toolbarElem.hasChildNodes())
	{
		var children = toolbarElem.childNodes;
		for (var i = 0; i < children.length; i++) 
		{
			if (toolbarElem.childNodes[i].tagName == "LI") {
				toolbarNames.push(toolbarElem.childNodes[i].id.split("-")[0]);
			}
		}
	}

	for (var i = 0; i < toolbarNames.length; i++) {
		if (toolbarNames[i] == currentSection.split("-")[0]) {
			if (direction == "left") {
				if (i - 1 < 0) {
					gotoTab = toolbarNames[toolbarNames.length - 1];
				} else {
					gotoTab = toolbarNames[i - 1];
				}
			} else {
				if ((i + 1) > (toolbarNames.length - 1)) {
					gotoTab = toolbarNames[0];
				} else {
					gotoTab = toolbarNames[i + 1];
				}
			}
		}
	}
	
	ScrollSection(gotoTab+paneTag, scrollArea, offset);
}

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++;
	}
}

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;
}