$(document).ready(function()
{
	$('#featured-work #category-list li a[class!="active"]').addClass('not-active');

    $('#featured-work ul#showreel-carousel').jcarousel(
    {
    	initCallback: callBack,
    	wrap: 'last'
    });
});

function callBack(carousel)
{	

	$('#featured-work #category-list li a.active').live('click',function(e)
    {
    	e.preventDefault();
    	
		carousel.scroll(jQuery.jcarousel.intval(0));
		
		$(this).removeClass('active');
		$('#featured-work #category-list li a').addClass('not-active');
		
		$('#featured-work #category-list li a.active').removeClass('active');
		$('#featured-work #category-list li a.active').addClass('not-active');
		
		$('#showreel-carousel li:hidden').show('normal', function()
		{
			$('#featured-work-title h1 span.post-count').text('(' + $('#showreel-carousel li').length + ') Projects');
		});
		
		return false;
		
    });

	$('#featured-work #category-list li a.not-active').live('click',function(e)
    {
    	e.preventDefault();
    	
    	carousel.scroll(jQuery.jcarousel.intval(0));
    	
    	$('#featured-work #category-list li a.active').removeClass('active').addClass('not-active');
    	$(this).removeClass('not-active');
		$(this).addClass('active');
		
		var catID = $(this).parent().attr('class').replace('cat-item','').replace(' ', '').replace('cat-item-', '');
				
		$('#showreel-carousel li[data-category!="'+ catID +'"]:visible').hide('normal', function()
		{
			var append = "Projects";
			
			if($('#showreel-carousel li:visible').length == 1) append = "Project";

			if($('#showreel-carousel li:visible').length <= 5)
			{
				// disables the next button when there are less than 5 visible items.
				$('#featured-work .jcarousel-next').off('click').addClass('jcarousel-next-disabled');
			}
			else
			{
				// re enable the next button
				$('#featured-work .jcarousel-next').on('click').removeClass('jcarousel-next-disabled');
			}
			
			
			$('#featured-work-title h1 span.post-count').text('(' + $('#showreel-carousel li:visible').length + ') ' + append);
		});
		
		$('#showreel-carousel li[data-category="'+ catID +'"]:hidden').show('normal');
		
		return false;
    });
}
