var tabTimeout = 0;//100;
var tabTimeoutInterval = 100;
var animationTimeout = 600;
var carouselLoaded = false;

var moveActive = false;
var isRandomStart = false;

$(document).ready(function ()
{
	LoadCarousel();
});

function LoadCarousel()
{
	var tabCount = $(".jstTabBar .jstTBsection").length;
	var tabBarWidth = $(".jstTabBar").width();
	var tabTitleWidth = $(".jstTabBar .jstTBtitle").width();

	$(".jstTabBar .jstTBsection").each(function()
	{
		$(this).contents(".jstTBcontent").width(tabBarWidth - (tabCount * tabTitleWidth));

		// find the position of the current element
		var currentIndex = $(this).parent().children().index(this);

		//var titleWidth = $(this).contents(".jstTBtitle").width();
		var titleWidth = $(this).contents(".jstTBtitle").width();
		var contentWidth = $(this).contents(".jstTBcontent").width();

		if (currentIndex > 0)
			$(this).animate( { left: '+=' + (contentWidth + (currentIndex * titleWidth)) }, 0 );

		$(this).find(".jstTBtitle a").click(function()
		{
			var sectionElement = $(this).parent().parent();
			Click(sectionElement);

			if (typeof slide != "undefined")
			{
				clearInterval(slide);
				return false;
			}
		});
	});

	carouselLoaded = true;
}

function Click(sectionElement)
{
	var currentLeftPos = sectionElement.position().left;
	var currentIndex = sectionElement.parent().children().index(sectionElement);
	var tabBarContentsWidth = sectionElement.contents(".jstTBcontent").width();

	if (!moveActive)
	{
		moveActive = true;
		window.setTimeout("disableMoveActive()", 5000); 

		if (!sectionElement.hasClass("jstTBactive"))
		{
			// check to see if previous sections have been opened
			var elementList = sectionElement.parent().children(":lt(" + currentIndex + "):not(.jstTBactive)");
			var timeout = tabTimeout;
			        
			elementList.each(function() {
				var prevElement = $(this);
				setTimeout(function() { OpenSection(prevElement, tabBarContentsWidth) }, timeout);
				timeout += tabTimeoutInterval;
			});

			ClearLastClicked();
			sectionElement.addClass("jstLastClicked");
			setTimeout(function() { OpenSection(sectionElement, tabBarContentsWidth) }, timeout);
		}
		else
		{
			// close any active right hand sections
			var elementList = sectionElement.parent().children(".jstTBactive:gt(" + currentIndex + ")");
			var timeout = tabTimeoutInterval * elementList.length;
			        
			elementList.each(function() {
				var nextElement = $(this);
				setTimeout(function() { CloseSection(nextElement, tabBarContentsWidth) }, timeout);
				timeout -= tabTimeoutInterval;

				ClearLastClicked();
				sectionElement.addClass("jstLastClicked");
			});
		}
	}
	return false;
}

function disableMoveActive()
{
	moveActive = false;
}

function SlideToNext()
{
	if (carouselLoaded)
	{
		var sectionElement = $(".jstTabBar .jstLastClicked");
		var currentIndex = sectionElement.parent().children().index(sectionElement);
		var count = sectionElement.parent().children().size();

		if (isRandomStart)
		{
			var randomIndex = Math.floor(Math.random() * (count));
			currentIndex = randomIndex;
			isRandomStart = false;
		}

		if (currentIndex < (count - 1))
		{
			newSectionElement = sectionElement.parent().children(":eq(" + (currentIndex + 1) + ")");
			Click(newSectionElement);
		}
		else
		{
			newSectionElement = sectionElement.parent().children(":eq(0)");
			Click(newSectionElement);
		}
	}
}

function ClearLastClicked()
{
	$(".jstTabBar .jstLastClicked").removeClass("jstLastClicked");
}

function OpenSection(section, tabBarContentsWidth)
{
	section.addClass("jstTBactive");
	section.animate( { left: '-=' + tabBarContentsWidth }, animationTimeout, disableMoveActive );
}

function CloseSection(section, tabBarContentsWidth)
{
	section.removeClass("jstTBactive");
	section.animate( { left: '+=' + tabBarContentsWidth }, animationTimeout, disableMoveActive );
}

