
var default_pos = 0;
var carousel_proto = null;

function animateSHIT(id, ti)
{
	var cxc = id.position(), tit = id.text();
	$('#navigationcursor').stop().animate(
	{
		left: parseInt( cxc.left + parseInt(parseInt(tit.length * 8 )/ 2) ),
	}, ti, function() {
  	});	
}

function getDefLeft()
{
	var cxc = $("#navigation li a.active").position(), tit = $("#navigation li a.active").text();
	default_pos = parseInt( cxc.left + parseInt(parseInt(tit.length * 8 )/ 2) );
}

function reanimateSHIT(id){
	$('#navigationcursor').stop().animate({
		left: parseInt( id ),
	  }, 500, function() {
		// Animation complete.
	  });	
}

function mycarousel_initCallback(carousel) 
{
	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() 
	{
		carousel.stopAuto();
	}, function() 
	{
		carousel.startAuto();
	});
	
	carousel_proto = carousel;
}

function resetCarousel() 
{
    carousel_proto.reset();
}

function reloadCarousel() 
{
    carousel_proto.reload();
}

function initCarousel()
{
	// featured slideshow
	var carousel = $('#carousel');
	if(carousel.length > 0)
	{
		//jCarousel Plugin
	    carousel.jcarousel({
			horizontal: true,
			scroll: 1,
			auto: 0,
			wrap: 'last',
			initCallback: mycarousel_initCallback,
			itemFallbackDimension: 270
			/*reloadCallback: function(carousel, state) 
			{
                //if (state != 'init') return;
                getProperties(function(properties) 
				{
                    $.each(properties, function(i, property) 
					{
                        carousel.add(i + 1, getPropertyHtml(property));
                    });
               		carousel.size(properties.length);
                });
            }*/
	   	});
	
		//Front page Carousel - Initial Setup
	   	$('div#slideshow-carousel a img').css({'opacity': '0.5'});
	   	$('div#slideshow-carousel a img:first').css({'opacity': '1.0'});
	   	$('div#slideshow-carousel li a:first').append('<span class="arrow"></span>');
	  
	  	//Combine jCarousel with Image Display
	    $('div#slideshow-carousel li a').hover(
	       	function () {
	        		
	       		if (!$(this).has('span').length) {
	        		$('div#slideshow-carousel li a img').stop(true, true).css({'opacity': '0.5'});
	   	    		$(this).stop(true, true).children('img').css({'opacity': '1.0'});
	       		}		
	       	},
	       	function () {
	        		
	       		$('div#slideshow-carousel li a img').stop(true, true).css({'opacity': '0.5'});
	       		$('div#slideshow-carousel li a').each(function () {
	
	       			if ($(this).has('span').length) $(this).children('img').css({'opacity': '1.0'});
	
	       		});
	        		
	       	}
		);
	}
}

$(document).ready(function()
{
	// navigation animation
	var active_item = $("#navigation li a.active");
	if(active_item.length > 0)
	{
		animateSHIT(active_item, 0);
		getDefLeft();
	}
	else
	{
		var first_item = $("#navigation ul li").first();
		first_item.addClass("active");
		
		active_item = $("#navigation ul li a").first();
		active_item.addClass("active");
		
		animateSHIT(active_item, 0);
		getDefLeft();
	}
	
	$("#navigation li a").hover(
	  function () {
		animateSHIT($(this), 700);
	  }, 
	  function () {
		reanimateSHIT(default_pos);
	  }
	);
	
	initCarousel();
	
	// filter buttons for featured slideshow
	$("#futureslideshowbutton ul li").click(function() 
	{
	    var filter = $('a', this).attr('rel');
	    $('#slideshow-carousel li').each(function() 
		{
	        if(!$(this).hasClass(filter)) 
			{
	            $(this).fadeOut('normal');
	        } 
			else 
			{
	            $(this).fadeIn('normal');
			}
		});
			
		reloadCarousel();
			
	    return false;
	});
	
	$('#slideshow-carousel li').click(function() 
	{
		$('#portfolio-description').fadeOut('normal');
		
		$.post(BASE_URL + 'featured_slideshow/get_content', 
		{ 
			portfolio_id : $(this).attr('id') 
		}, 
		function(portfolio_content)
		{
			$('#portfolio-description').html( portfolio_content );
			$('#portfolio-description').fadeIn('normal');
		});
		
		return false;
	});
	
});

