var imageSwapTimer = 0;


$( document ).ready( function()
{
	imageSwapTimer = setInterval( "Slideshow.nextImage()", 5000 );
	
	$( "body#feedbackHelper form .inputField #firstName" ).focus();
	$( "body#supportHelper form .inputField #firstName" ).focus();


	// Bind to the slide controller link elements
	$( "#slideController li a" ).click( function()
	{
		// The user clicked on a link.
		if( !( $( this ).parent( "li" ).hasClass( "current" )))
		{
			if( imageSwapTimer != 0 )
				clearTimeout( imageSwapTimer );
			
			Slideshow.transitionToImage( $( this ));
		}
		
		// Prevent the link from executing.
		return false;
	});
	
	
	$( "body#supportHelper #sidebar h3#supportRequest" ).click( function()
	{
		$( "#sidebar h3.current" ).removeClass( "current" );
		$( "#issues div.slideElement.current" ).fadeOut( 200 );
		$( "#issues div.slideElement.current" ).removeClass( 200 );
		
		$( this ).addClass( "current" );
		$( "#issues #reportIssue" ).fadeIn( 200 );
		$( "#issues #reportIssue" ).addClass( "current" );
		
		$( "body#supportHelper form .inputField #firstName" ).focus();
	});
	
	
	// Bind to the support sidebar link elements
	$( "body#supportHelper #sidebar ul.internalLinks a" ).click( function()
	{
		// Hide the currently selected slideElement
		$( "#issues div.slideElement.current" ).fadeOut( 200 );
		$( "#issues div.slideElement.current" ).removeClass( "current" );
		
		// Extract the case number from the link.
		var rawHref = $( this ).attr( "href" );
		var newElement = rawHref.substring( 1, rawHref.length );
		
		// Show the new slideElement
		$( "#issues div.slideElement#" + newElement ).fadeIn( 200 );
		$( "#issues div.slideElement#" + newElement ).addClass( "current" );
		
		// Update the header elements
		$( "#sidebar h3.current" ).removeClass( "current" );
		
	
		return false;
	});
	
	
	// Bind to the feedback form submission button
	$( ".submitForm input.sendButton" ).click( function()
	{
		var success = 1;
		
		// Check that all the input fields are filled in.
		$( "form .inputField .dataInput" ).each( function()
		{
			// There was no value in the input field.
			if( $( this ).val() == "" )
			{
				$( this ).parent( ".inputField" ).addClass( "error" );
				
				// Set the focus to the first field that failed to pass.
				if( success == 1 )
					$( this ).focus();
					
				success = 0;
			}
			else
			{
				if( $( this ).parent( ".inputField" ).hasClass( "error" ))
					$( this ).parent( ".inputField" ).removeClass( "error" );
					
				// If the field is an email addresss... validate it.
				if( $( this ).attr( "id" ) == "emailAddress" )
				{
					var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
					var emailAddress = $( this ).val();

					if( reg.test( emailAddress ) == false )
					{
						$( this ).parent( ".inputField" ).addClass( "error" );
						
						// Set the focus to the first field that failed to pass.
						if( success == 1 )
							$( this ).focus();
							
						success = 0;
   					}
				}
			}
		});
		
		
		if( success == 1 )
			return true;

		alert( "Please make sure all the fields are filled out and that your email address is correct." );	
		return false;
	});
});




var Slideshow =
{
	nextImage : function()
	{
		var nextElement = $( "#slideController li.current + li" );
		
		if( nextElement.text() == "" )
			nextElement = $( "#slideController li:first-child" );
		
		this.transitionToImage( nextElement.children( "a" ));
	},


	transitionToImage : function( current )
	{
		var previous = current.parents( "ul" ).find( "li.current" );
		var previousIndex = previous.text();
		var currentIndex = current.text();

		// Remove the current class
		previous.removeClass( "current" );

		// Add the current class to the selected link.
		current.parent( "li" ).addClass( "current" );
		
		// Show the appropriate image.
		$( "#slider li:nth-child(" + previousIndex + ")" ).fadeOut( 400 );
		$( "#slider li:nth-child(" + currentIndex + ")" ).fadeIn( 400 );
	}
};
	
	
