$(document).ready(function()
{

$totSlides 	= 3;
$curSlide 	= 1;
$in_height	= 50;
$in_width	= 40;
$pad		= 7;
$yPos		= 0;
$effect		= 500;

$banHgt		= $("#banner").height();
$out_width = $(".bub").css("width");


	setInterval(NextSlide, 8000);
	init();
	
	function init()
	{
		$(".bg").hide();
		$("#bg-"+$curSlide).show();
		$totHgt = GetTotalHeight();	
		
		$yPos = ($banHgt/2) - ($totHgt/2);
		
		for( $k=0; $k<$totSlides; $k++ )
		{
			$("#bub-"+($k+1)).css("top", $yPos-15);
			$yPos += $("#bub-"+($k+1)).height() + $pad;
		}
	}
	
	function NextSlide()
	{
		$txt  = "#bub-"+$curSlide+" .intxt";
		$wrap = "#bub-"+$curSlide; 
		
		$($wrap).animate({width: $in_width, height: $in_height}, $effect, "swing", function()
		{
	
			$totHgt = $pad*($totSlides-1) + $totSlides*$in_height;
			$yPos 	= ($banHgt/2) - ($totHgt/2);
			
			for( $k=0; $k<$totSlides; $k++ )
			{
				$("#bub-"+($k+1)).animate({top: $yPos}, $effect, "swing");
				$yPos += $("#bub-"+($k+1)).height() + $pad;
			}
			
			
	
		});
		$($txt).fadeOut("fast");
		
		UpdateCurrentSlide();
		
		//updating the vars
		$txt  = "#bub-"+$curSlide+" .intxt";
		$wrap = "#bub-"+$curSlide;
			
		$($wrap).animate({width: $out_width }, $effect, "swing", function()
		{
			$totHgt 	= GetTotalHeight();
			$yPos 		= ($banHgt/2) - ($totHgt/2);
		
			for( $k=0; $k<$totSlides; $k++ )
			{
				if( $k+1 == $curSlide )
				{
					$i = $("#bub-"+($k+1)+" .intxt").height();
					$("#bub-"+($k+1)).animate({height: $i, top: $yPos}, $effect, "swing", function()
					{
						$($txt).fadeIn("slow");
					});
					$yPos += $i + $pad;
				}
				else
				{
					$("#bub-"+($k+1)).animate({top: $yPos}, $effect, "swing" );
					$yPos += $("#bub-"+($k+1)).height() + $pad;
				}
			}
	
		});
		
			
	}
	
	
	function GetTotalHeight()
	{
		$i = $pad*($totSlides-1);
		for( $k=0; $k<$totSlides; $k++ )
		{
			if( $k+1 == $curSlide )
			{
				$i += $("#bub-"+($k+1)+" .intxt").height();
			}
			else
			{
				$i += $in_height;
				$("#bub-"+($k+1)).css("height", $in_height+"px");
				$("#bub-"+($k+1)).css("width", $in_width+"px");
				$("#bub-"+($k+1)+" .intxt").hide();
			}
			
		}
		return $i;
	}
	
	function UpdateCurrentSlide()
	{
		if( $curSlide == $totSlides )
		{
			$curSlide = 1;
			
			//$("#bg-1").show();
			$(".bg").hide();
			$("#bg-1").show();
		}
		else
		{
			$curSlide++;
			//$("#bg-"+$curSlide).fadeIn("slow");
			$("#bg-"+$curSlide).show();
		}
	}
});
