var tabLabel = "-tab";
var paneLabel = "-pane";

function initSlider(inSlideArea, startPane, inOffset)
{
	currentPane = startPane;
	slideArea = inSlideArea;
	offset = inOffset;
	currentPaneIndex = 0;
	targetPane = startPane;
}

function startAutoSlide(sliderBarId)
{
	autoSlide = true;
	sliderBarElem = document.getElementById(sliderBarId);
	sliderPaneNames = new Array();
    
	if(sliderBarElem.hasChildNodes())
	{
		var children = sliderBarElem.childNodes;
		for(var i=0; i<children.length; i++) 
		{
			if(sliderBarElem.childNodes[i].tagName == "LI")
				sliderPaneNames.push(sliderBarElem.childNodes[i].id.split("-")[0] + "-pane");
		}
	}
	autoSlideTimer = setInterval("nextSlide();", 5000);
}

function nextSlide()
{
	for(var i=0; i<sliderPaneNames.length; i++)
	{
		if(sliderPaneNames[i] == currentPane)
		{
			currentPaneIndex = i;
			break;
		}
	}
	if(currentPaneIndex >= (sliderPaneNames.length - 1))
		targetPane = sliderPaneNames[0];
	else if(currentPaneIndex < 0)
		targetPane = sliderPaneNames[0];
	else
	{
		targetPane = sliderPaneNames[currentPaneIndex+1];
	}
	slideToPane(targetPane, false);
}

function slideToPane(pane, userClicked)
{
	if(autoSlide && userClicked)
	{
		autoSlide = false;
		clearInterval(autoSlideTimer);
	}
	if (currentPane == pane)
		return;

	lastPane = currentPane;
	currentPane = pane;
    tab = currentPane.split("-")[0] + tabLabel;
    document.getElementById(tab).className = "active";
	
    if (lastPane)
	{
	    lastTab = lastPane.split("-")[0] + tabLabel;
	    document.getElementById(lastTab).className = "inactive";
	}
	
	slideAreaElem = document.getElementById(slideArea);
	position = findElementPos(document.getElementById(pane));
	
	if (offset != "")
	{
		offsetPos = findElementPos(document.getElementById(offset));
		position[0] = position[0] - offsetPos[0];
	}
	
	slideStart(slideAreaElem, slideAreaElem.scrollLeft, position[0]);
}

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

function slideStart(elem, start, end)
{
	if (slideanim.timer != null)
	{
		clearInterval(slideanim.timer);
		slideanim.timer = null;
	}
	slideanim.time = 0;
	slideanim.begin = start;
	slideanim.change = end - start;
	slideanim.duration = 25;
	slideanim.element = elem;
	
	slideanim.timer = setInterval("slideAnim();", 15);
}

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

function findElementPos(elem)
{
	var posX = elem.offsetLeft;
	var posY = elem.offsetTop;

	return Array(posX, posY);
}

function sineInOut(t, b, c, d)
{
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}
