$(document).ready( function(e) {

	$('div#carousel ul').css( 'display', 'block' );
	$('.controls').css( 'display', 'block' );
	$('div#carousel-cases ul').css( 'display', 'block' );
	$('div#carousel-cases-text ul').css( 'display', 'block' );
	$('div#carousel').jcarousel( { horizontal: true, scroll: 1, initCallback: mycarousel_initCallback, itemLoadCallback: trigger } );
	$('div#carousel-cases').jcarousel( { horizontal: true, scroll: 1 } );
	$('div#carousel-cases-text').jcarousel( { horizontal: true, scroll: 1, initCallback: mycarousel_cases_initCallback, buttonNextHTML: null, buttonPrevHTML: null } );

	// Add carousel callback for external controls
	function mycarousel_initCallback(carousel) {
		jQuery('.external-controls a').live('click', function() {
			carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
			var selected_element = $('div.external-controls').find('.selected');
			$(selected_element).removeClass('selected');
			jQuery(this).parent().addClass('selected');
			return false;
		});
	};

	// This function is run each time a new element of the carousel is shown
	// (either if the navigation arrows has been pressed or the external controls
	// tabs has been pressed).
	//
	// It will just change the color of the external controls tabs.
	function trigger(carousel, state)
	{
		var selected_index = carousel.first;
		var selected_element = $('div.external-controls').find('.selected');
		$(selected_element).removeClass('selected');
		var new_item = $('div.external-controls').find('.element-' + selected_index);
		$(new_item).addClass('selected');
	}

	// Add carousel cases callback for external buttons
	function mycarousel_cases_initCallback(carousel2) {
		jQuery('#carousel-cases .jcarousel-next').live('click', function() {
			carousel2.next();
			return false;
		});

		jQuery('#carousel-cases .jcarousel-prev').live('click', function() {
			carousel2.prev();
			return false;
		});
	};

	// Home fading carousel
	function contentRotate(feature) 
	{
		if (doAnimate)
		{  
		  	feature.fadeOut("fast", function (feature)
			{
				return function() 
				{
			  		$(".feature_content div").hide();
				
					/* HIGHLIGHT RELEVANT CONTROL */
					if ($(this).attr("id") == "feature_one")
					{
						$(".feature_nav .on").removeClass("on");
						$(".home_one").addClass("on");
					}
					else if ($(this).attr("id") == "feature_two") 
					{
						$(".feature_nav .on").removeClass("on");
						$(".home_two").addClass("on");
					}
					else if ($(this).attr("id") == "feature_three")
					{
						$(".feature_nav .on").removeClass("on");
						$(".home_three").addClass("on");
					}
			
				  	/* FADE IN NEXT ITEM OR GO BACK TO FIRST */
				  	feature.fadeIn("fast", function ()
					{
						if ($(this).attr("id") == "feature_three")
						{
					  		setTimeout(function ()
							{
								contentRotate($(".feature_content div:first"));
					  		}, 4000);
						}
						else
						{
					  		setTimeout(function ()
							{
								contentRotate($(feature.next()));
					  		}, 4000);
						}
				  	});
				};
		  	}(feature));
		}
	}

	/* HOME FEATURES */
	$("ul#nav a").click(function(e) {

		$("ul#nav .on").removeClass("on");
		$(this).addClass("on");

		var selected_parent_index = $(this).parent().index();

		$(".feature_content").children().hide();		

		var selected_index = $(this).parent().index();
		selected_index = selected_index;
		selected_index = selected_index+1;

		var selected_element_content = $(".feature_content div:nth-child("+selected_index+")");

		$(selected_element_content).fadeIn();
		$(selected_element_content).css("visibility","visible");
		$(selected_element_content).css("display","block");

		doAnimate = false;

		e.preventDefault();
	});

	var doAnimate = true;

	//contentRotate($(".feature_content div:first"));

});

