/* 
   Author: Tom Cox
   Author URI: www.d-coda.com | tom@d-coda.com
*/

jQuery(function($) // set $ to JQuery within function to avoid prototype conflicts!
{
	$show = "#imageshow",
	$slideTotal = $($show+" li").length-1,
	$left = $($show+" .left"),
	$right = $($show+" .right"),
	$titleSpeed = 300,
	$descriptionSpeed = 600,
	$fadeSpeed = 500,
	$movement = -500;
	
	var timer,
	    pause = 8000;
	
	index = function()
	{
		return $($show+" ul").find('li.active').index();
	}
	
	showRight = function()
	{
		
		$($show+" li.active .title").animate({left:$movement},$titleSpeed);
		$($show+" li.active .description").animate({left:$movement},$descriptionSpeed,function()
		{	
			if(index() < $slideTotal)
			{

				$($show+" li.active").next("li").addClass("next");
				$($show+" li.next").fadeIn($fadeSpeed);
				$($show+" li.active").fadeOut($fadeSpeed,function()
				{										   
					$(this).removeClass("active");
					$($show+" li.next").addClass("active");
					$($show+" li.next .title").animate({left:0},$titleSpeed);
					$($show+" li.next .description").animate({left:0},$descriptionSpeed);
					$($show+" li.next").removeClass("next");
		
				});
				
			}
			else
			{				
				$($show+" li").first().addClass("next");
				$($show+" li.active").fadeOut($fadeSpeed);	
				$($show+" li.next").fadeIn($fadeSpeed,function()
				{
					$($show+" li.next .title").animate({left:0},$titleSpeed);
					$($show+" li.next .description").animate({left:0},$descriptionSpeed);

					$($show+" li.active").removeClass("active");	
					$($show+" li.next").addClass("active");
					$($show+" li.next").removeClass("next");
					
				});
				
			}
		
		});

	}

	showLeft = function()
	{
			$($show+" li.active .title").animate({left:$movement},$titleSpeed);
			$($show+" li.active .description").animate({left:$movement},$descriptionSpeed,function()
			{	
				if(index() > 0)
				{
	
					$($show+" li.active").prev("li").addClass("next");
					$($show+" li.next").fadeIn($fadeSpeed);
					$($show+" li.active").fadeOut($fadeSpeed,function()
					{										   
						$(this).removeClass("active");
						$($show+" li.next").addClass("active");
						$($show+" li.next .title").animate({left:0},$titleSpeed);
						$($show+" li.next .description").animate({left:0},$descriptionSpeed);
						$($show+" li.next").removeClass("next");
					});
				}
				else
				{				
					$($show+" li").last().addClass("next");
					$($show+" li.active").fadeOut($fadeSpeed);	
					$($show+" li.next").fadeIn($fadeSpeed,function()
					{
						$($show+" li.next .title").animate({left:0},$titleSpeed);
						$($show+" li.next .description").animate({left:0},$descriptionSpeed);
	
						$($show+" li.active").removeClass("active");	
						$($show+" li.next").addClass("active");
						$($show+" li.next").removeClass("next");
					});
				}
			
			});
	}
		
	$($show+" .right").click(function()
	{
			clearInterval(timer);
			showRight();
			timer = setInterval("showRight()",pause);
	});
	
	$($show+" .left").click(function()
	{
			clearInterval(timer);
			showLeft();
			timer = setInterval("showRight()",pause);
	});
		
	$($show+" li.active .title").animate({left:0},$titleSpeed);
	$($show+" li.active .description").animate({left:0},$descriptionSpeed);
	
	timer = setInterval("showRight()",pause);

});
