$(document).ready( function()
{
	$('#pictures div').innerfade(
	{   
		speed: 3000,
		timeout: 8000,
		type: 'sequence'
	} );	
	
	if ($('#flashcontent').length > 0 )
	{
		var flashvars = {
			paramXMLPath: "/flash/param.xml",
			initialURL: escape(document.location)
		}
		var params = {
			base: ".",
			bgcolor: "#121212",
			allowfullscreen: "true",
			wmode: "transparent"
		}
		var attributes = {}
		swfobject.embedSWF("/flash/slideshowpro.swf", "flashcontent", "887", "441", "9.0.0", false, flashvars, params, attributes);
	}
	
	$('#col1').jScrollPane({showArrows:true});
	$('#col2').jScrollPane({showArrows:true});
	$('#col3').jScrollPane({showArrows:true});
	$('#col4').jScrollPane({showArrows:true});                				
	$('#col5').jScrollPane({showArrows:true});			
	
	var getConfirmationMessage = function()
	{
		var fast = 0;
		var hosp = 0;
		var registration = 0;
		
		if ( $('#donation_fast').val() == 'Donation Amount' )
		{
			fast = 0;
		}
		else
		{
			fast = parseInt( $('#donation_fast').val() );
		}
		if ( $('#donation_hospital').val() == 'Donation Amount' )
		{
			hosp = 0;
		}
		else
		{
			hosp = parseInt( $('#donation_hospital').val() );
		}

		var tickets = 0;
		
		var confirmMessage = "Payment Confirmation\n\n";
		
		if ($('.riderForm').length > 0) {
			registration = 100;
			confirmMessage += "\tRide Registration: $100\n";
		}		
		
		if (fast > 0) {
			confirmMessage += "\tFAST Donation: $" + fast + "\n";
		}
		if (hosp > 0) {
			confirmMessage += "\tHospital Donation: $" + hosp + "\n";
		}
		
		if ( parseInt( $('#tickets').val() ) > 0 )
		{
			tickets = ( parseInt( $('#tickets').val() ) * 30 );	
			confirmMessage += "\tDinner Tickets: $" + tickets + "\n";	
		}

		$("#amount").val( fast + hosp + tickets + registration );
		
		confirmMessage += "\t** Total: $" + $("#amount").val() + "\n";
		
		confirmMessage += "\nWould you like to continue with your payment?";
		return confirmMessage;
	}
	
	/* General donation code */
	var updateDonation = function()
	{
		var fast = $('#donation_fast').val();
		var hosp = $('#donation_hospital').val();
		
		if ( fast == 'Donation Amount' ) { fast = 0; }
		if ( hosp == 'Donation Amount' ) { hosp = 0; }
		
		var totalDonation = parseInt( fast ) + parseInt( hosp );
		
		$('#total-donation .pad').html( totalDonation.toFixed( 2 ) );
		
		return totalDonation;
	}
	
	$('#donation_fast').numeric();
	$('#donation_hospital').numeric();
	
	$('#donation_fast').keyup(  updateDonation );
	$('#donation_hospital').keyup(  updateDonation );
	
	$('#donation_fast').click( function() {
		if ( $(this).val() == 'Donation Amount' )
		{
			$(this).val( '' );
		}
	});
	$('#donation_hospital').click( function() {
		if ( $(this).val() == 'Donation Amount' )
		{
			$(this).val( '' );
		}
	});	
	
	/* Contact Form */
	$('#contact_form').submit( function()
	{
		if ( $('#contactEmail').val() == "" ||
			 $('#contactMessage').val() == "" )
		{
			alert( "Please ensure you have filled out all form fields correctly." );
			return false;
		}

		$.post( $('#contact_form').attr( 'action' ),
			$('#contact_form').serialize(),
		  	function( data )
			{
				// Close window if success
				if ( data.result == 1 )
				{
					disablePopup();
				}
			
				alert( data.message );
		  	},
			'json'
		);

		return false;
	} );	
	
	/* Donation Form */
	var donationFormSubmit = false;
	$('.donationForm').submit( function()
	{
		if ( donationFormSubmit == true )
		{
			return true;
		}
		
		if ( $('#first_name').val() == "" ||
			 $('#last_name').val() == "" ||
			 $('#email').val() == "" ||
			 ( $('#donation_fast').val() == "Donation Amount" && $('#donation_hospital').val() == "Donation Amount" ) ||
			 ( $('#donation_fast').val() == "" && $('#donation_hospital').val() == "" ) )
		{
			alert( "Please ensure you have filled out all form fields correctly." );
			return false;
		}

		$('.payPalButton').css( 'background', 'url(/images/wait.gif) center center no-repeat transparent' );
		$('.payPalButton').click( function(){ return false; });

		$("#amount").val( $('#total-donation .pad').html() );

		$.post( "/donation/submit",
			$('.donationForm').serialize(),
		  	function( data )
			{
				// Close window if success
				if ( data.result == 1 )
				{
					donationFormSubmit = true;
					$('.donationForm').submit();
				}
				else
				{
					$('.payPalButton').css( 'background', 'url(/images/paypal-button.png) 0 0 no-repeat transparent' );
					$('.payPalButton').click( function(){ $('.donationForm').submit() });					
					alert( data.message );
				}
		  	},
			'json'
		);

		return false;
	} );
	
	/* Volunteer Form */
	var volunteerFormSubmit = false;
	$('.volunteerForm').submit( function()
	{
		if ( volunteerFormSubmit == true )
		{
			var result = confirm( getConfirmationMessage() );
			
			if ( !result )
			{
				$('.payPalButton').css( 'background', 'url(/images/paypal-button.png) 0 0 no-repeat transparent' );
				$('.payPalButton').click( function(){ $('.volunteerForm').submit() });									
				return false;
			}
			else
			{
				return true;
			}
		}
		
		if ( $('#first_name').val() == "" ||
			 $('#last_name').val() == "" ||
			 $('#email').val() == "" )
		{
			alert( "Please ensure you have filled out all form fields correctly." );
			return false;
		}

		$('.payPalButton').css( 'background', 'url(/images/wait.gif) center center no-repeat transparent' );
		$('.payPalButton').click( function(){ return false; });

		var cost = parseInt( $('#total-donation .pad').html() ) + ( parseInt( $('#tickets').val() ) * 30 );

		$("#amount").val( cost );

		$.post( "/volunteer/submit",
			$('.volunteerForm').serialize(),
		  	function( data )
			{
				// Close window if success
				if ( data.result == 1 )
				{
					if ( parseFloat( $("#amount").val() ) > 0 ) {
						volunteerFormSubmit = true;
						$('.volunteerForm').submit();
					}
					else {
						window.location = "/volunteer/complete";
					}
				}
				else
				{
					$('.payPalButton').css( 'background', 'url(/images/paypal-button.png) 0 0 no-repeat transparent' );
					$('.payPalButton').click( function(){ $('.volunteerForm').submit() });					
					alert( data.message );
				}
		  	},
			'json'
		);

		return false;
	} );	
	
	/* Riders Form */
	var riderFormSubmit = false;
	$('.riderForm').submit( function()
	{
		if ( riderFormSubmit == true )
		{
			var result = confirm( getConfirmationMessage() );
			
			if ( !result )
			{
				$('.payPalButton').css( 'background', 'url(/images/paypal-button.png) 0 0 no-repeat transparent' );
				$('.payPalButton').click( function(){ $('.riderForm').submit() });									
				return false;
			}
			else
			{
				return true;
			}
		}
		
		if ( $('#first_name').val() == "" ||
			 $('#last_name').val() == "" ||
			 $('#email').val() == "" )
		{
			alert( "Please ensure you have filled out all form fields correctly." );
			return false;
		}

		$('.payPalButton').css( 'background', 'url(/images/wait.gif) center center no-repeat transparent' );
		$('.payPalButton').click( function(){ return false; });

		var cost = updateDonation() + ( parseInt( $('#tickets').val() ) * 30 ) + 100;
		$("#amount").val( cost );

		$.post( "/rider/submit",
			$('.riderForm').serialize(),
		  	function( data )
			{
				// Close window if success
				if ( data.result == 1 )
				{
					riderFormSubmit = true;
					$('.riderForm').submit();
				}
				else
				{
					$('.payPalButton').css( 'background', 'url(/images/paypal-button.png) 0 0 no-repeat transparent' );
					$('.payPalButton').click( function(){ $('.riderForm').submit() });					
					alert( data.message );
				}
		  	},
			'json'
		);

		return false;
	} );		
} );

function error()
{ 
	centerPopup();
	loadPopup();
}