$extend( FY, {

	_processingSignup: false,

	initSignupPopup: function()
	{
		var popup = FY.Signup = new Aurora.Popup({
				width: 567,
				height: 750,
				zIndex: 1000,
				transitionDelay: 4,
				transitionInSteps: 6,
				transitionOutSteps: 6,
				closeButton: false,
				noCanvas: true
			})
			.addEvent( 'ready', function() {
	
				var content = this.$content;
				
				new Request.HTML()
					.get( '/Resources/FY/Sites/FindYoga/HTML/Signup/Signup.html?=' + $time() )
					.addEvent( 'success', (function() {
							
						content.setStyle( 'background', 'none' );
						
						// Insert the html
						content.set( 'html', this.response.html );
						
						// Init the options
						FY.initOptions();
				
					}) );
			
			});
	},
	
	initOptions: function()
	{
		$( 'button_close' ).addEvent( 'click', function() { FY.Signup.close(); });
	
		$( 'option_studio' ).addEvent( 'click', function() { FY.initForm( 'Studio' ); });
		$( 'option_teacher' ).addEvent( 'click', function() { FY.initForm( 'Teacher' ); });
		$( 'option_event' ).addEvent( 'click', function() { FY.initForm( 'Event' ); });
		$( 'option_member' ).addEvent( 'click', function() { FY.initForm( 'Member' ); });
	},

	initForm: function( type )
	{
		new Request.HTML()
			.get( '/Resources/FY/Sites/FindYoga/HTML/Signup/Signup_' + type + '.html?=' + $time() )
			.addEvent( 'success', function() {
				
				var formHTML = this.response.html;
				
				// Insert the html and init everything else
				switch( type )
				{
					case 'Studio':
						FY.initStudioForm( formHTML );
					break;
					
					case 'Teacher':
						FY.initTeacherForm( formHTML );
					break;
					
					case 'Event':
						FY.initEventForm( formHTML );
					break;
					
					case 'Member':
						FY.initMemberForm( formHTML );
					break;
				}
				
			});
	},

	initStudioForm: function( formHTML )
	{
		FY._signupComplete = false;
	
		// Init the maps
		FY.initMaps();
	
		// Insert the html		
		FY.Signup.$content.set( 'html', formHTML );
		
		$( 'button_close' ).addEvent( 'click', function() { FY.Signup.close(); });
	
		// Init validation
		FY.FormTools.initValidationHint( { field: 'name', type: 'length', hintMsg: 'e.g John Smith', errorMsg: 'Please enter a name', notRequired: false } );
		FY.FormTools.initValidationHint( { field: 'emailAddress', type: 'email', hintMsg: 'eg. john@smith.com', errorMsg: 'Please enter a valid email', notRequired: false } );
		
		FY.FormTools.initValidationHint( { field: 'studioName', type: 'length', hintMsg: '', errorMsg: 'Please enter a studio name', notRequired: false } );
		FY.FormTools.initValidationHint( { field: 'phoneNumber', type: 'length', hintMsg: '', errorMsg: 'Please enter a phone number', notRequired: false } );
		FY.FormTools.initValidationHint( { field: 'studioAddress', type: 'length', hintMsg: '', errorMsg: 'Please enter a street address', notRequired: false } );

		FY.FormTools.initValidationHint( { field: 'city', type: 'length', hintMsg: '', errorMsg: 'Please enter a city', notRequired: false } );
		
		FY.FormTools.initValidationHint( { field: 'zipcode', type: 'length', hintMsg: '', errorMsg: 'Please enter a zip code', notRequired: false } );
		FY.FormTools.initValidationHint( { field: 'state', type: 'length', hintMsg: '', errorMsg: 'Please enter a state', notRequired: false } );
		FY.FormTools.initValidationHint( { field: 'country', type: 'length', hintMsg: '', errorMsg: 'Please enter a country', notRequired: false } );
		
		$( 'form_signup' ).addEvent( 'submit', function(e) {
			
			if ( FY._signupComplete )
				return true;
			else
				return false;
		});
		
		$( 'submitForm' ).addEvent( 'click', function() {
		
			if ( FY._processingSignup )
				return;
		
			// Check form
			if ( checkForm() ) {
			
				disableForm();
			
				// Check the email address availability
				checkEmail();
			}
			else
				return false;
									
		});
		
		var disableForm = function()
		{
			FY._processingSignup = true;
			$( 'submitForm' ).getElement( '.wrap2' ).set( 'text', '...' );
			$( 'submitForm' ).set( 'opacity', 0.4 );
		};
		
		var enableForm = function()
		{
			FY._processingSignup = false;
			$( 'submitForm' ).getElement( '.wrap2' ).set( 'text', 'Next' );
			$( 'submitForm' ).set( 'opacity', 1 );
		};
		
		var checkForm = function()
		{
			FY.FormTools._invalidFields = [ 'name', 'emailAddress', 'studioName', 'phoneNumber', 'studioAddress', 'city', 'zipcode', 'state', 'country' ];
		
			$( 'name' ).fireEvent( 'blur' );
			$( 'emailAddress' ).fireEvent( 'blur' );
			$( 'studioName' ).fireEvent( 'blur' );
			$( 'phoneNumber' ).fireEvent( 'blur' );
			$( 'studioAddress' ).fireEvent( 'blur' );
			$( 'city' ).fireEvent( 'blur' );
			$( 'zipcode' ).fireEvent( 'blur' );
			$( 'state' ).fireEvent( 'blur' );
			$( 'country' ).fireEvent( 'blur' );
			
			if ( !$( 'acceptTerms' ).checked ) {
			
				$( 'acceptTerms_container' ).setStyles( 'background-color: #FFFFCA; border: 1px solid #D4DFE3; padding: 2px; color: #FF0000;' );
				enableForm();
				return false;
			}
			
			if ( FY.FormTools._invalidFields.length ) {
			
				$( 'name' ).fireEvent( 'blur' );
				$( 'emailAddress' ).fireEvent( 'blur' );
				$( 'studioName' ).fireEvent( 'blur' );
				$( 'phoneNumber' ).fireEvent( 'blur' );
				$( 'studioAddress' ).fireEvent( 'blur' );
				$( 'city' ).fireEvent( 'blur' );
				$( 'zipcode' ).fireEvent( 'blur' );
				$( 'state' ).fireEvent( 'blur' );
				$( 'country' ).fireEvent( 'blur' );
				
				enableForm();
				return false;
			}
			
			return true;
		
		};
		
		var checkEmail = function()
		{
			// Check email if in use
			var email = $( 'emailAddress' ).value;
		
			if ( !email.length || email == '(required)' )
				return false;
		
			Aurora.callAction({
				
				action: 'member.isEmailAddressInUse',
				data: { emailAddress: email },
				
				onComplete: function( rtnData ) {
					
					if ( !rtnData ) {
						enableForm();
						return false;
					}
			
					if ( !rtnData.success ) {
						alert( 'Oops! Something went wrong. Please check your email address and try again. If you continue to encounter problems, please contact us.' );
						enableForm();
						return false;
					}
					
					if ( rtnData.isInUse ) {
						alert( 'Oops! Someone is already using that email address with Find Yoga, you might already have an account with us.\n\nPlease try and sign in.\n\nOtherwise, try using another email address.' );
						enableForm();
						$log( 'Email in use.' );
						return false;
					}
					
					$log( 'Email not in use.' );
					
					// Validate one last time
					validateForm();
									
				}
			});
			
		};
		
		var validateForm = function()
		{
			// Submit the form
			$log( 'Validating form...' );
			
			if ( checkForm() )
			{
				var location = {
					studioName: $( 'studioName' ).get( 'value' ).trim(),
					studioAddress: $( 'studioAddress' ).get( 'value' ).trim(),
					city: $( 'city' ).get( 'value' ).trim(),
					zipcode: $( 'zipcode' ).get( 'value' ).trim(),
					state: $( 'state' ).get( 'value' ).trim(),
					country: $( 'country' ).get( 'value' ).trim()
				}
				
				// Hide the form
				$( 'form_signup_details' ).hide();
				$( 'form_signup_locating' ).show();
				
				// Enable the form again
				enableForm();
				
				// Find a location
				FY.attemptLocation( location, 'studio' );
				
				$log( 'Validation successful, attempting to find location.' );
				
			}
			else
			{
				$log( 'Validation failed, invalid or required fields.' );
				return false;
			}
		
		};
	
	},
	
	initTeacherForm: function( formHTML )
	{
		FY._signupComplete = false;
	
		// Init the maps
		FY.initMaps();
	
		// Insert the html		
		FY.Signup.$content.set( 'html', formHTML );
		
		$( 'button_close' ).addEvent( 'click', function() { FY.Signup.close(); });
	
		// Init validation
		FY.FormTools.initValidationHint( { field: 'name', type: 'length', hintMsg: 'e.g John Smith', errorMsg: 'Please enter a name', notRequired: false } );
		FY.FormTools.initValidationHint( { field: 'emailAddress', type: 'email', hintMsg: 'eg. john@smith.com', errorMsg: 'Please enter a valid email', notRequired: false } );
		FY.FormTools.initValidationHint( { field: 'phoneNumber', type: 'length', hintMsg: '', errorMsg: 'Please enter a phone number', notRequired: false } );
		
		FY.FormTools.initValidationHint( { field: 'city', type: 'length', hintMsg: '', errorMsg: 'Please enter a city', notRequired: false } );
		FY.FormTools.initValidationHint( { field: 'zipcode', type: 'length', hintMsg: '', errorMsg: 'Please enter a zip code', notRequired: false } );
		FY.FormTools.initValidationHint( { field: 'state', type: 'length', hintMsg: '', errorMsg: 'Please enter a state', notRequired: false } );
		FY.FormTools.initValidationHint( { field: 'country', type: 'length', hintMsg: '', errorMsg: 'Please enter a country', notRequired: false } );
	
		$( 'form_signup' ).addEvent( 'submit', function(e) {
	
			if ( FY._signupComplete )
				return true;
			else
				return false;
		});
	
		$( 'submitForm' ).addEvent( 'click', function() {
		
			if ( FY._processingSignup )
				return;
		
			// Check form
			if ( checkForm() ) {
			
				disableForm();
			
				// Check the email address availability
				checkEmail();
			}
			else
				return false;
									
		});
		
		var disableForm = function()
		{
			FY._processingSignup = true;
			$( 'submitForm' ).getElement( '.wrap2' ).set( 'text', '...' );
			$( 'submitForm' ).set( 'opacity', 0.4 );
		};
		
		var enableForm = function()
		{
			FY._processingSignup = false;
			$( 'submitForm' ).getElement( '.wrap2' ).set( 'text', 'Next' );
			$( 'submitForm' ).set( 'opacity', 1 );
		};
		
		var checkForm = function()
		{
			FY.FormTools._invalidFields = [ 'name', 'emailAddress', 'phoneNumber', 'city', 'zipcode', 'state', 'country' ];
			
			$( 'name' ).fireEvent( 'blur' );
			$( 'emailAddress' ).fireEvent( 'blur' );
			$( 'phoneNumber' ).fireEvent( 'blur' );
			$( 'city' ).fireEvent( 'blur' );
			$( 'zipcode' ).fireEvent( 'blur' );
			$( 'state' ).fireEvent( 'blur' );
			$( 'country' ).fireEvent( 'blur' );
			
			if ( !$( 'acceptTerms' ).checked ) {
				
				$( 'acceptTerms_container' ).setStyles( 'background-color: #FFFFCA; border: 1px solid #D4DFE3; padding: 2px; color: #FF0000;' );
				enableForm();
				return false;
			}
			
			if ( FY.FormTools._invalidFields.length ) {
			
				$( 'name' ).fireEvent( 'blur' );
				$( 'emailAddress' ).fireEvent( 'blur' );
				$( 'phoneNumber' ).fireEvent( 'blur' );
				$( 'city' ).fireEvent( 'blur' );
				$( 'zipcode' ).fireEvent( 'blur' );
				$( 'state' ).fireEvent( 'blur' );
				$( 'country' ).fireEvent( 'blur' );
				
				enableForm();
				return false;
			}
			
			return true;
		
		};
		
		var checkEmail = function()
		{
			// Check email if in use
			var email = $( 'emailAddress' ).value;
		
			if ( !email.length || email == '(required)' )
				return false;
		
			Aurora.callAction({
				
				action: 'member.isEmailAddressInUse',
				data: { emailAddress: email },
				
				onComplete: function( rtnData ) {
					
					if ( !rtnData ) {
						enableForm();
						return false;
					}
			
					if ( !rtnData.success ) {
						alert( 'Oops! Something went wrong. Please check your email address and try again. If you continue to encounter problems, please contact us.' );
						enableForm();
						return false;
					}
					
					if ( rtnData.isInUse ) {
						alert( 'Oops! Someone is already using that email address with Find Yoga, you might already have an account with us.\n\nPlease try and sign in.\n\nOtherwise, try using another email address.' );
						enableForm();
						$log( 'Email in use.' );
						return false;
					}
					
					$log( 'Email not in use.' );
					
					// Validate one last time
					validateForm();
									
				}
			});
			
		};
		
		var validateForm = function()
		{
			// Submit the form
			$log( 'Validating form...' );
			
			if ( checkForm() )
			{
				var location = {
					city: $( 'city' ).get( 'value' ).trim(),
					zipcode: $( 'zipcode' ).get( 'value' ).trim(),
					state: $( 'state' ).get( 'value' ).trim(),
					country: $( 'country' ).get( 'value' ).trim()
				}
				
				// Hide the form
				$( 'form_signup_details' ).hide();
				$( 'form_signup_locating' ).show();
				
				// Enable the form again
				enableForm();
				
				// Find a location
				FY.attemptLocation( location, 'teacher' );
				
				$log( 'Validation successful, attempting to find location.' );
				
			}
			else
			{
				$log( 'Validation failed, invalid or required fields.' );
				return false;
			}
		
		};
		
	},
	
	initEventForm: function( formHTML )
	{
		FY._signupComplete = false;
	
		// Insert the html		
		FY.Signup.$content.set( 'html', formHTML );
		
		$( 'button_close' ).addEvent( 'click', function() { FY.Signup.close(); });
			
		// Init validation
		FY.FormTools.initValidationHint( { field: 'name', type: 'length', hintMsg: 'e.g John Smith', errorMsg: 'Please enter a name', notRequired: false } );
		FY.FormTools.initValidationHint( { field: 'emailAddress', type: 'email', hintMsg: 'eg. john@smith.com', errorMsg: 'Please enter a valid email', notRequired: false } );
		
		FY.FormTools.initValidationHint( { field: 'city', type: 'length', hintMsg: '', errorMsg: 'Please enter a city', notRequired: false } );	
		FY.FormTools.initValidationHint( { field: 'zipcode', type: 'length', hintMsg: '', errorMsg: 'Please enter a zip code', notRequired: false } );
		FY.FormTools.initValidationHint( { field: 'state', type: 'length', hintMsg: '', errorMsg: 'Please enter a state', notRequired: false } );
		FY.FormTools.initValidationHint( { field: 'country', type: 'length', hintMsg: '', errorMsg: 'Please enter a country', notRequired: false } );
		
		FY.FormTools.initValidationHint( { field: 'eventName', type: 'length', hintMsg: '', errorMsg: 'Please enter your event', notRequired: false } );
	
		$( 'form_signup' ).addEvent( 'submit', function(e) {
		
			if ( FY._signupComplete )
				return true;
			else
				return false;
		});
	
		$( 'submitForm' ).addEvent( 'click', function() {
		
			if ( FY._processingSignup )
				return;
		
			// Check form
			if ( checkForm() ) {
			
				disableForm();
			
				// Check the email address availability
				checkEmail();
			}
			else
				return false;
									
		});
		
		var disableForm = function()
		{
			FY._processingSignup = true;
			$( 'submitForm' ).getElement( '.wrap2' ).set( 'text', '...' );
			$( 'submitForm' ).set( 'opacity', 0.4 );
		};
		
		var enableForm = function()
		{
			FY._processingSignup = false;
			$( 'submitForm' ).getElement( '.wrap2' ).set( 'text', 'Next' );
			$( 'submitForm' ).set( 'opacity', 1 );
		};
		
		var checkForm = function()
		{
			FY.FormTools._invalidFields = [ 'name', 'emailAddress', 'city', 'zipcode', 'state', 'country', 'eventName' ];
		
			$( 'name' ).fireEvent( 'blur' );
			$( 'emailAddress' ).fireEvent( 'blur' );
			$( 'city' ).fireEvent( 'blur' );
			$( 'zipcode' ).fireEvent( 'blur' );
			$( 'state' ).fireEvent( 'blur' );
			$( 'country' ).fireEvent( 'blur' );
			$( 'eventName' ).fireEvent( 'blur' );
			
			if ( !$( 'acceptTerms' ).checked ) {
			
				$( 'acceptTerms_container' ).setStyles( 'background-color: #FFFFCA; border: 1px solid #D4DFE3; padding: 2px; color: #FF0000;' );	
				enableForm();
				return false;
			}
			
			if ( FY.FormTools._invalidFields.length ) {
			
				$( 'name' ).fireEvent( 'blur' );
				$( 'emailAddress' ).fireEvent( 'blur' );
				$( 'city' ).fireEvent( 'blur' );
				$( 'zipcode' ).fireEvent( 'blur' );
				$( 'state' ).fireEvent( 'blur' );
				$( 'country' ).fireEvent( 'blur' );
				$( 'eventName' ).fireEvent( 'blur' );
				
				enableForm();
				return false;
			}
			
			return true;
		
		};
		
		var checkEmail = function()
		{
			// Check email if in use
			var email = $( 'emailAddress' ).value;
		
			if ( !email.length || email == '(required)' )
				return false;
		
			Aurora.callAction({
				
				action: 'member.isEmailAddressInUse',
				data: { emailAddress: email },
				
				onComplete: function( rtnData ) {
					
					if ( !rtnData ) {
						enableForm();
						return false;
					}
			
					if ( !rtnData.success ) {
						alert( 'Oops! Something went wrong. Please check your email address and try again. If you continue to encounter problems, please contact us.' );
						enableForm();
						return false;
					}
					
					if ( rtnData.isInUse ) {
						alert( 'Oops! Someone is already using that email address with Find Yoga, you might already have an account with us.\n\nPlease try and sign in.\n\nOtherwise, try using another email address.' );
						enableForm();
						$log( 'Email in use.' );
						return false;
					}
					
					$log( 'Email not in use.' );
					
					// Validate one last time
					validateForm();
									
				}
			});
			
		};
		
		var validateForm = function()
		{
			// Submit the form
			$log( 'Validating form...' );
			
			if ( checkForm() )
			{
				// Hide the form
				$( 'form_signup_details' ).hide();
				$( 'form_signup_done' ).show();
				
				// Enable the form again
				enableForm();
				
				submitForm();
				
				$log( 'Validation successful, submitting form.' );
				
			}
			else
			{
				$log( 'Validation failed, invalid or required fields.' );
				return false;
			}
		
		};
		
		var submitForm = function()
		{
			// Create a JSON structure
			var jsonData = {
				location: {
					address: '',
					suburb: $( 'city' ).get( 'value' ).trim(),
					postcode: $( 'zipcode' ).get( 'value' ).trim(),
					state: $( 'state' ).get( 'value' ).trim(),
					country: $( 'country' ).get( 'value' ).trim(),
					longitude: '',
					latitude: ''
				}
			}
			
			$( 'jsonData' ).set( 'value', JSON.encode( jsonData ) );
			
			FY._signupComplete = true;
	
			// And Submit...
			$( 'form_signup' ).submit.click();
		};
	
	},
		
	initMemberForm: function( formHTML )
	{
		FY._signupComplete = false;
	
		// Insert the html		
		FY.Signup.$content.set( 'html', formHTML );
		
		$( 'button_close' ).addEvent( 'click', function() { FY.Signup.close(); });
	
		// Init validation
		FY.FormTools.initValidationHint( { field: 'name', type: 'length', hintMsg: 'e.g John Smith', errorMsg: 'Please enter a name', notRequired: false } );
		FY.FormTools.initValidationHint( { field: 'emailAddress', type: 'email', hintMsg: 'eg. john@smith.com', errorMsg: 'Please enter a valid email', notRequired: false } );
		
		FY.FormTools.initValidationHint( { field: 'zipcode', type: 'length', hintMsg: '', errorMsg: 'Please enter a zip code', notRequired: false } );
		FY.FormTools.initValidationHint( { field: 'state', type: 'length', hintMsg: '', errorMsg: 'Please enter a state', notRequired: false } );
		FY.FormTools.initValidationHint( { field: 'country', type: 'length', hintMsg: '', errorMsg: 'Please enter a country', notRequired: false } );
	
		$( 'form_signup' ).addEvent( 'submit', function(e) {
			
			if ( FY._signupComplete )
				return true;
			else
				return false;
		});
	
		$( 'submitForm' ).addEvent( 'click', function() {
		
			if ( FY._processingSignup )
				return;
		
			// Check form
			if ( checkForm() ) {
			
				disableForm();
			
				// Check the email address availability
				checkEmail();
			}
			else
				return false;
									
		});
		
		var disableForm = function()
		{
			FY._processingSignup = true;
			$( 'submitForm' ).getElement( '.wrap2' ).set( 'text', '...' );
			$( 'submitForm' ).set( 'opacity', 0.4 );
		};
		
		var enableForm = function()
		{
			FY._processingSignup = false;
			$( 'submitForm' ).getElement( '.wrap2' ).set( 'text', 'Next' );
			$( 'submitForm' ).set( 'opacity', 1 );
		};
		
		var checkForm = function()
		{
			FY.FormTools._invalidFields = [ 'name', 'emailAddress', 'zipcode', 'state', 'country' ];
		
			$( 'name' ).fireEvent( 'blur' );
			$( 'emailAddress' ).fireEvent( 'blur' );
			$( 'zipcode' ).fireEvent( 'blur' );
			$( 'state' ).fireEvent( 'blur' );
			$( 'country' ).fireEvent( 'blur' );
			
			if ( !$( 'acceptTerms' ).checked ) {
				
				$( 'acceptTerms_container' ).setStyles( 'background-color: #FFFFCA; border: 1px solid #D4DFE3; padding: 2px; color: #FF0000;' );
				enableForm();
				return false;
			}
			
			if ( FY.FormTools._invalidFields.length ) {
			
				$( 'name' ).fireEvent( 'blur' );
				$( 'emailAddress' ).fireEvent( 'blur' );
				$( 'zipcode' ).fireEvent( 'blur' );
				$( 'state' ).fireEvent( 'blur' );
				$( 'country' ).fireEvent( 'blur' );
				
				enableForm();
				return false;
			}
			
			return true;
		
		};
		
		var checkEmail = function()
		{
			// Check email if in use
			var email = $( 'emailAddress' ).value;
		
			if ( !email.length || email == '(required)' )
				return false;
		
			Aurora.callAction({
				
				action: 'member.isEmailAddressInUse',
				data: { emailAddress: email },
				
				onComplete: function( rtnData ) {
					
					if ( !rtnData ) {
						enableForm();
						return false;
					}
			
					if ( !rtnData.success ) {
						alert( 'Oops! Something went wrong. Please check your email address and try again. If you continue to encounter problems, please contact us.' );
						enableForm();
						return false;
					}
					
					if ( rtnData.isInUse ) {
						alert( 'Oops! Someone is already using that email address with Find Yoga, you might already have an account with us.\n\nPlease try and sign in.\n\nOtherwise, try using another email address.' );
						enableForm();
						$log( 'Email in use.' );
						return false;
					}
					
					$log( 'Email not in use.' );
					
					// Validate one last time
					validateForm();
									
				}
			});
			
		};
		
		var validateForm = function()
		{
			// Submit the form
			$log( 'Validating form...' );
			
			if ( checkForm() )
			{
				// Hide the form
				$( 'form_signup_details' ).hide();
				$( 'form_signup_done' ).show();
				
				// Enable the form again
				enableForm();
				
				submitForm();
				
				$log( 'Validation successful, submitting form.' );
				
			}
			else
			{
				$log( 'Validation failed, invalid or required fields.' );
				return false;
			}
		
		};
		
		var submitForm = function()
		{
			FY._signupComplete = true;
		
			// And Submit...
			$( 'form_signup' ).submit.click();
		};
	
	}

});
