var inTransition = false;
var stripPage = 0;
var pageWidth = 1; //Items per page
var imageWidth = 0;
var marginWidth = 0;
var maxImages = 0;
var lastPage = 0;

var currentAutoLoopImage = -1;
var autoLoopImageCount = 0;
var elementTimer;


$(document).ready(function(){
	initSlide();
});

function initSlide()
{	
	if ($("#secondNav").length > 0) {
		$("#secondNav li:nth-child(1) a").addClass("active");
		//Hide all the images bar the first
		//$("#contentWrapper .section").not(".section:nth-child(1)").hide();
		maxImages = $("#secondNav a").click(function(){slideImage(this);},function(){hideImage(this);}).length;
		imageWidth = $("#contentWrapper .section").width();
		lastPage = Math.ceil(maxImages/pageWidth);
		
		$("#contentWrapper").css("width", imageWidth*lastPage);
		$("#contentWrapper").css("overflow","hidden");
		$("#contentWrapper").css("position","relative");
		
		$(".section").css("width", imageWidth);
		$(".section").css("position","relative");
		
		$(".scrollLeft").click(function(){scrollStrip(-1);});
		$(".scrollRight").click(function(){scrollStrip(1);});
		
		updateArrows();
		
		update();
		
		//Check if this is the current page
		var items = $(".imageColumn a");
		var currentPageUrl = document.URL;
		currentPageUrl = currentPageUrl.split("/");
		currentPageUrl = currentPageUrl[currentPageUrl.length-1];
		for (var i=0; i<items.length; i++)
		{
			if (items[i].href.indexOf(currentPageUrl) >= 0 && currentPageUrl != "")
			{
				$(items[i]).addClass("on");
			}
		}
		
		$("#wrapper").css("visibility", "visible");
	}
}

function initFade(sectionNumber)
{	
	//Hide all the images bar the first
	$("#head li").not("li:nth-child(" + sectionNumber + ")").hide();
	maxImages = $("#firstNav a").hover(function(){fadeImage(this);},function(){hideImage(sectionNumber);}).length;
	update();
}

function scrollStrip(dir)
{
	//stripPage = stripPage + dir;
	//updateArrows();
	
	var distanceToMove = stripPage * ((pageWidth * (imageWidth+marginWidth)));
	$("#contentWrapper").animate({ 
        left: -distanceToMove
      }, 1000, "easeOutCirc" );
}

function updateArrows()
{
	if (stripPage >= lastPage-1)
	{
		stripPage = lastPage-1;
		$(".scrollRight").fadeTo(300,0);
	}
	else
	{
		$(".scrollRight").fadeTo(300,1);
	}
	
	if (stripPage <= 0)
	{
		stripPage = 0;
		$(".scrollLeft").fadeTo(300,0);
	}
	else
	{
		$(".scrollLeft").fadeTo(300,1);
	}	
}

function slideImage(what)
{
	inTransition = true
	$("#secondNav a").removeClass("active");
	$(what).addClass("active");
	var imageLis = $("#contentWrapper .section");
		
	var controllerLis = $("#secondNav a");
	for (var i=0; i<imageLis.length; i++)
	{
		if (controllerLis[i] == what)
		{
			stripPage = i;
			scrollStrip();
		}
	}
}

function fadeImage(what)
{
	inTransition = true;
	//Set the correct tab as active
	//$("#firstNav a").removeClass("active");
	//$(what).addClass("active");
	var imageLis = $("#head li");
		
	var controllerLis = $("#firstNav a");
	for (var i=0; i<imageLis.length; i++)
	{
		if (controllerLis[i] == what)
		{
			$(imageLis).fadeTo(1, 0);
			$(imageLis).hide();
			
			$(imageLis[i]).show();
			$(imageLis[i]).fadeTo(500, 1, function(){update();});
		}
	}
}

function hideImage(sectionNumber)
{
	if (!inTransition)
	{
		inTransition = true;
		var imageLis = $("#head li");
		$(imageLis).fadeTo(1, 0);
		$(imageLis).hide();
		$(imageLis[sectionNumber-1]).show();
		$(imageLis[sectionNumber-1]).fadeTo(500, 1, function(){update();});
	}
}

function update()
{
	inTransition = false;
}

function inSection(sectionNumber)
{	
	$("#firstNav li:nth-child(" + sectionNumber + ") a").addClass("active");
	initFade(sectionNumber);
}

function inSectionNoFade(sectionNumber)
{	
	$("#firstNav li:nth-child(" + sectionNumber + ") a").addClass("active");
}

function fadeImageByNumber(what)
{
	inTransition = true;
	var imageLis = $("#head li");
	$(imageLis).fadeTo(1, 0);
	$(imageLis).hide();
	
	$(imageLis[what]).show();
	$(imageLis[what]).fadeTo(500, 1, function(){update();});
}

function initAutoLoop()
{
	var imageLis = $("#head li");
	$(imageLis).fadeTo(1, 0);
	autoLoopImageCount = imageLis.length;
	clearTimeout(elementTimer);
	autoLoop(10000);
}

 function autoLoop(nextChange)
 {	
	var imageLis = $("#head li");
	
	$(imageLis[currentAutoLoopImage]).fadeTo(500, 0);
	currentAutoLoopImage++;
	if (currentAutoLoopImage == autoLoopImageCount)
	{
		currentAutoLoopImage = 0;
	}
	$(imageLis[currentAutoLoopImage]).show();
	$(imageLis[currentAutoLoopImage]).fadeTo(500, 1);
	
	elementTimer = setTimeout("autoLoop(" + nextChange + ")",nextChange);
 }