function Step( left ) {
	this.left = left;
	this.validator = new NullValidator();
}
Step.prototype.setValidator = function( validator ) { this.validator = validator; };
Step.prototype.validate = function( steps, next_step ) {
	var errors = this.validator.validate();
	
	if( errors.length == 0 ) {
		return this.next( steps, next_step );
	} else {
		return this.previous( steps, next_step, errors );
	}
};
Step.prototype.next = function( steps, next_step ) {
	if( steps[next_step] ) {
		var next_left = steps[next_step].left;
		$( "#selector" ).animate( { left: next_left + "px" }, 500, "easeInOutQuad" );
		$( "#signup-steps" ).cycle( next_step );
	}
	return next_step;
};
Step.prototype.previous = function( steps, next_step, errors ) {
	showErrorsDialog( "Errors", errors );
	var left = this.left;
	var next_left = steps[next_step].left;
	$( "#selector" ).animate( {
		left: ( left + ( next_left - left ) / 2 ) + "px" },
		250,
		"easeInOutQuad",
		function() {
			$( "#selector" ).animate( { left: left + "px" }, 250, "easeInOutQuad" );
		}
	);
	return next_step - 1;
};

function ConfirmStep( left ) {
	this.base = Step;
	this.base( left );
};
ConfirmStep.prototype = new Step;
ConfirmStep.prototype.previous = function( steps, next_step, errors ) {
	var i = 1;
	var result = i;
	while( steps[i - 1] && i != next_step && ( result = steps[i - 1].validate( steps, i ) ) == i ) { i++; }
	if( i == next_step ) {
		showErrorsDialog( "Errors", errors );
	}
	return result;
};

function NullValidator() { this.validate = function() { return []; } }

function PersonalInformationValidator() {
	this.validate = function() {
		var errors = [];
		var i = 0;
		var form = $( "#signup-form" );
		if( !$( "input[name=username]", form ).val() ) { errors[i++] = "Please enter a username"; }
		if( !$( "input[name=first_name]", form ).val() ) { errors[i++] = "Please enter your first name"; }
		if( !$( "input[name=last_name]", form ).val() ) { errors[i++] = "Please enter your last name"; }
		if( !isValidEmail( $( "input[name=email]", form ).val() ) ) { errors[i++] = "Please enter a valid e-mail address"; }
		var password = $( "input[name=password]", form ).val();
		if( !password ) { errors[i++] = "Please enter a password"; }
		if( password && ( $( "input[name=confirm_password]", form ).val() != password ) ) { errors[i++] = "Passwords do not match"; }
		if( !$( "input[name=password_reminder]", form ).val() ) { errors[i++] = "Please enter a password reminders"; }
		return errors;
	}
}

function TermsAndConditionsValidator() {
	this.validate = function() {
		var errors = [];
		var i = 0;
		if( !$( "input[name=terms_and_conditions]" ).is( ":checked" ) ) { errors[i++] = "Please indicate that you have read and understood the terms and conditions of BlueNewt Sports"; }
		return errors;
	}
}

function ConfirmValidator() {
	this.validate = function() {
		var errors = [];
		
		$( "#signup-form" ).ajaxSubmit( {
			async: false,
			dataType: "json",
			success: function( data ) {
				if( data.errors ) {
					$( "#signup-form input[type=image]" ).removeAttr( "disabled" );
					errors = data.errors;
				} else {
					
				}
			}
		} );
		
		return errors;
	}
}

function isValidEmail( email ) {
	return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test( email );	
}

$( function() {
	$( "input[name=email]" ).blur( function() {
		$( "#confirmation-email" ).text( $( this ).val() );		
	} );
			
	var steps = [];
	var i = 0;
	
	steps[0] = new Step( 45 );
	steps[0].setValidator( new PersonalInformationValidator() );
	steps[1] = new Step( 189 );
	steps[1].setValidator( new TermsAndConditionsValidator() );
	steps[2] = new ConfirmStep( 378 );
	steps[2].setValidator( new ConfirmValidator() );
	steps[3] = new Step( 571 );
	steps[4] = new Step( 571 );
	
	$( "#signup-steps" ).cycle( {
		fx: "scrollLeft",
		speed: 500,
		easing: "easeInOutQuad",
		startingSlide: i,
		timeout: 0
	} );
	
	if( i > 0 ) {
		$( "#selector" ).animate( { left: steps[i].left + "px" }, 500, "easeInOutQuad" );
	}
	
	$( ".continue-button" ).click( function() {
		if( ++i < steps.length ) {
			i = steps[i - 1].validate( steps, i );
			return false;
		}
	} );
	
	$( "#sign-up-now" ).click( function() {
		window.open( $( this ).attr( "href" ) );
		return false;
	} );
} );