$(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.00\n";
		}		
		
		if (fast > 0) {
			confirmMessage += "\tFAST Donation: $" + fast.toFixed( 2 ) + "\n";
		}
		if (hosp > 0) {
			confirmMessage += "\tHospital Donation: $" + hosp.toFixed( 2 ) + "\n";
		}
		
		// Transportation Cost
		var transportation = 0;
		if ( $('#transportation:checked').length )
		{
			transportation = 10;
			confirmMessage += "\tTransportation: $10.00\n";
		}
		
		// Jersey
		var jersey = 0;
		if ( $('#jersey').length )
		{
			if ( $('#jersey').val() != '' )
			{
				jersey = 90;
				confirmMessage += "\tJersey: $90.00\n";
			}
		}
		
		if ( parseInt( $('#tickets').val() ) > 0 )
		{
			tickets = ( parseInt( $('#tickets').val() ) * 35 );	
			confirmMessage += "\tDinner Tickets: $" + tickets.toFixed( 2 ) + "\n";	
		}

		$("#amount").val( fast + hosp + tickets + registration + jersey + transportation );
		
		confirmMessage += "\n\t** Total: $" + parseInt( $("#amount").val() ).toFixed( 2 ) + "\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()
	{
		// Submit to PayPal
		if ( donationFormSubmit ) {	return true; }
		
		// Validation
		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;
		}

		var result = confirm( getConfirmationMessage() );
		if ( !result )
		{
			$('.payPalButton').css( 'background', 'url(/images/paypal-button.png) 0 0 no-repeat transparent' );
			return false;
		}		
		
		$('.payPalButton').css( 'background', 'url(/images/wait.gif) center center no-repeat transparent' );

		var cost = updateDonation();

		$("#amount").val( cost );

		$.post( "/donation/submit",
			$('.donationForm').serialize(),
		  	function( data )
			{
				// Close window if success
				if ( data.result == 1 )
				{
					donationFormSubmit = true;
					$('.donationForm').submit();
				}
				else
				{
					donationFormSubmit = false;
					$('.payPalButton').css( 'background', 'url(/images/paypal-button.png) 0 0 no-repeat transparent' );
					alert( data.message );
				}
		  	},
			'json'
		);

		return false;
	} );
	
	/* Volunteer Form */
	var volunteerFormSubmit = false;
	$('.volunteerForm').submit( function()
	{
		// Submit to PayPal
		if ( volunteerFormSubmit ) { return true; }

		// Validation
		if ( $('#first_name').val() == "" ||
			 $('#last_name').val() == "" ||
			 $('#email').val() == "" )
		{
			alert( "Please ensure you have filled out all form fields correctly." );
			return false;
		}		

		var cost = updateDonation();
		cost += parseInt( $('#tickets').val() ) * 35;

		$("#amount").val( cost );
		
		// Confirmation
		if ( parseFloat( $("#amount").val() ) > 0 )
		{
			var result = confirm( getConfirmationMessage() );
			if ( !result )
			{
				$('.payPalButton').css( 'background', 'url(/images/paypal-button.png) 0 0 no-repeat transparent' );
				return false;
			}
		}
			
		$('.payPalButton').css( 'background', 'url(/images/wait.gif) center center no-repeat transparent' );

		/* Update PayPal custom field with unique code */
		date = new Date();
		$("#custom").val( $('#email').val() + '-' + parseInt( date.getTime() ) );		
		
		$.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 {
						volunteerFormSubmit = false;
						window.location = "/volunteer/complete";
						return false;
					}
				}
				else
				{
					volunteerFormSubmit = false;
					$('.payPalButton').css( 'background', 'url(/images/paypal-button.png) 0 0 no-repeat transparent' );
					alert( data.message );
				}
		  	},
			'json'
		);

		return false;
	} );	
	
	/* Riders Form */
	var riderFormSubmit = false;
	$('.riderForm').submit( function()
	{
		// Submit to PayPal
		if ( riderFormSubmit ) { return true; }

		// Validation
		if ( $('#first_name').val() == "" ||
			 $('#last_name').val() == "" ||
			 $('#email').val() == "" )
		{
			alert( "Please ensure you have filled out all form fields correctly." );
			return false;
		}

		// Confirmation
		var result = confirm( getConfirmationMessage() );
		if ( !result )
		{
			$('.payPalButton').css( 'background', 'url(/images/paypal-button.png) 0 0 no-repeat transparent' );
			return false;
		}

		$('.payPalButton').css( 'background', 'url(/images/wait.gif) center center no-repeat transparent' );

		/* Update Costs - START */
			// Donation
			var cost = updateDonation();
			
			// Additional Dinner Tickets
			cost += parseInt( $('#tickets').val() ) * 35;
			
			// Rider Registration
			cost += 100;
			
			// Transportation Cost
			if ( $('#transportation:checked').length )
			{
				cost += 10;
			}
			
			// Jersey
			if ( $('#jersey').val() != '' )
			{
				cost += 90;
			}
			
			$("#amount").val( cost );
		/* Update Costs - END */
		
		/* Update PayPal custom field with unique code */
			date = new Date();
			$("#custom").val( $('#email').val() + '-' + parseInt( date.getTime() ) );
			
		$.post( "/rider/submit",
			$('.riderForm').serialize(),
		  	function( data )
			{
				// Close window if success
				if ( data.result == 1 )
				{
					riderFormSubmit = true;
					$('.riderForm').submit();
				}
				else
				{
					riderFormSubmit = false;
					$('.payPalButton').css( 'background', 'url(/images/paypal-button.png) 0 0 no-repeat transparent' );
					alert( data.message );
				}
		  	},
			'json'
		);

		return false;
	} );		
} );

function error()
{ 
	centerPopup();
	loadPopup();
}