/* RLCCC -------------------------------------------------------- */
var activeTab = false, ignoreTab = true, slideshowTimeout;
var reephamTimeout, reephamVisible = true, reephamShowFor = 8000, reephamFadeSpeed = 2000;
var slideDelay = reephamShowFor+reephamFadeSpeed, slideSpeed = 1500, slideTimeout = 5000, slideResume = 30000;
var currCat = 1;

$(function()
{
	if ($('#projectfeature').is('div'))
	{	
		// initialise
		reephamTimeout = setTimeout('hideReepham(-1)', reephamShowFor);
		$('#reepham').click(function()
		{
			$('#projecttabs .n1 a').click();
		});
		
		$('#projectcat').cycle
		({
			fx:			'scrollLeft',
			delay:		slideDelay,
			speed:		slideSpeed,
			timeout:	slideTimeout,
			before:		updateTab
		});
		
		$('#projecttabs a').click(function()
		{ 
			var index = $(this).parent('li').index();
			
			//get rid of reepham
			if (reephamVisible)
			{
				clearTimeout(reephamTimeout);
				$('#reepham').fadeOut(reephamFadeSpeed);
			}
			
			//set the correct slide			
			$('#projectcat').cycle(index);
			ignoreTab = true;
			setStaticTab(index - 1);
			
			// stop the show, set the resume timer running
			$('#projectcat').cycle('pause');
			slideshowTimeout = setTimeout("resumeCycle()", slideResume);
			return false; 
		}); 
		
		// when a project is clicked, use the current category if applicable
		$('#prs a').click(function()
		{
			if ($(this).hasClass('active'))
			{
				top.location.href = r + 'projects/' + cats[currCat] + '/' + $(this).attr('name');
				return false;
			}
			else
			{
				return true;
			}
		});
	}
	
	// project side nav
	if ($('#projects').is('div'))
	{
		// hide inactive projects
		$('#projects li:not(#cproj) ul').hide();
		
		// accordian for others
		$('#projects :not(.sub) a').click(function()
		{
			var clicky = $(this).parent('li').attr('class');
			$('#projects .' + clicky + ' .sub').slideDown('normal',function()
			{
				$('#projects > ul > li').each(function()
				{
					var me = $(this).attr('class');
					if (me != clicky)
					{
						$('#projects .' + me + ' .sub').slideUp();
					}
				});
			});
			$(this).blur();
			return false;
		});
	}
	
	// external links
	$('a[rel=external]').attr('target','_blank');
});

function hideReepham(index)
{
	clearTimeout(reephamTimeout);
	$('#reepham').fadeOut(reephamFadeSpeed);
	reephamVisible = false;
	setNewTab(index);
}

// resumes the slideshow after a click has paused it
function resumeCycle()
{
	$('#projectcat').cycle('resume');
	slideshowTimeout = false;
	ignoreTab = false;
}

// animates the old tab out and then calls the new tab function
function updateTab(curr, next, opts)
{
    var index = opts.currSlide;
    
    // ignore first callback, seems to first do it on init and not on first change
    if ( ! ignoreTab)
    {
		if (activeTab)
		{
			$('#projecttabs .' + activeTab).animate({ top: '-10px' }, 500, function()
			{
				if ( ! ignoreTab)
				{
					setNewTab(index);
				}
			});
		}
		else
		{
			setNewTab(index);
		}
	}
	else
	{
		ignoreTab = false;
	}
}

// animates the new tab into place and updates border colour
function setNewTab(index)
{
	var tab = index + 2;
	if (tab <= 1 || tab >= 10) tab = 1;

	activeTab = 'n' + tab;
	
	$('#projecttabs').css('borderColor', $('#projecttabs .' + activeTab).css('backgroundColor') );
	$('#projecttabs .' + activeTab).animate({ top: '0px' }, 500);
	setProjects(index);
}

// for tabs that are clicked on
function setStaticTab(index)
{
	$('#projecttabs li').each(function(i)
	{
		$(this).animate({top: '-10px'}, 500, function()
		{
			if (i == 8)
			{
				setNewTab(index);
			}
		});
	});
}

// highlight relevant projects
function setProjects(index)
{
	var tab = index + 2;
	if (tab <= 1 || tab >= 10) tab = 1;

	currCat = tab;

	$('#prs a').removeClass();
	$('#prs .p' + tab + ' a').addClass('active p' + tab + 'a');
}


