HI = {
	
	_currentMain: null,
	_currentSub: null,
	_currentContent: null,
	_currentNav: null,
	
	hideContent: function( nav )
	{
		// Don't hide anything if we click the current nav
		if ( HI._currentNav == nav )
			return;
	
		// Store reference to element
		HI._currentNav = nav;
		
		// Fade out the current content
		if ( HI._currentContent )
			HI._currentContent.fade( 'out' );
		
	},
	
	showContent: function( content )
	{
		// Don't show it if we are already showing it
		if ( HI._currentContent == content )
			return;
	
		// Store reference to element
		HI._currentContent = content;
	
		// Position it absolutely and fade it in
		content.setStyle( 'position', 'absolute' ).set( 'opacity', 0 ).show().fade( 'in' );
	
	},
	
	initNavigation: function()
	{
		$( 'next' ).addEvent( 'click', function() {
			
			var next = HI._currentNav.getNext( 'a' );
			
			if ( HI._currentNav.getProperty( 'id' ) == 'investorSoftware' )
				next = $( 'tradingSignals' );
				
			if ( HI._currentNav.getProperty( 'id' ) == 'latestNews' )
				next = $( 'investmentNewsletter' );
			
			// $log( next );
			
			if ( next )
				next.fireEvent( 'click' );
			else
				$( 'investorSoftware' ).fireEvent( 'click' );
		});
	
		$( 'previous' ).addEvent( 'click', function() {
		
			var previous = HI._currentNav.getPrevious( 'a' );
			
			if ( HI._currentNav.getProperty( 'id' ) == 'investmentNewsletter' )
				previous = $( 'latestNews' );

			if ( HI._currentNav.getProperty( 'id' ) == 'tradingSignals' )
				previous = $( 'investorSoftware' );
			
			// $log( previous );
			
			if ( previous )
				previous.fireEvent( 'click' );
			else
				$( 'signUp' ).fireEvent( 'click' );
		});
	
	},
	
	initForms: function()
	{	
		// Add the required fields to the invalid fields array
		HI.FormTools._invalidFields = [ 'title', 'firstName', 'lastName', 'email', 'phone', 'address1', 'suburb', 'postCode', 'tid' ];
	
		if ( !$( 'form_signUp' ) )
			return;
	
		$( 'form_signUp' ).addEvent( 'submit', function() {

			$( 'title' ).fireEvent( 'blur' );
			$( 'firstName' ).fireEvent( 'blur' );
			$( 'lastName' ).fireEvent( 'blur' );
			$( 'email' ).fireEvent( 'blur' );
			$( 'phone' ).fireEvent( 'blur' );
			$( 'address1' ).fireEvent( 'blur' );
			$( 'suburb' ).fireEvent( 'blur' );
			$( 'postCode' ).fireEvent( 'blur' );
			$( 'dob' ).fireEvent( 'blur' );
			
			if ( $( 'tid' ) )
				$( 'tid' ).fireEvent( 'blur' );
			else
				HI.FormTools._invalidFields.erase( 'tid' );
			
			$log( HI.FormTools._invalidFields );
		
			if ( HI.FormTools._invalidFields.length ) {
			
				// Make sure error messages are shown
				HI.FormTools._invalidFields.each( function( field ) {
					$( field ).fireEvent( 'blur' );
				});
				
				return false;
			}
			
			return true;
			
		});
	
	},
	
	initButtons: function()
	{
		// Add events to all the navigation buttons
		
		var buttons = [ 'tradingSignals', 'healthCheck', 'chartingTools', 'latestNews', 'investorSoftware', 'investmentNewsletter', 'marketUpdates', 'investmentBook' ]; 
		
		buttons.each( function( button ) {
		
			$( button ).addEvent( 'click', function() {
				HI.hideContent( $( button ) );
				HI.showContent( $( button + '_content' ) );
			});
			
		});
		
		$( 'signUp' ).addEvent( 'click', function() {
			HI.hideContent( this );
			HI.showContent( $( 'signUpform' ) );
		});
		
		$$( '.signUp_content' ).addEvent( 'click', function() {
			$( 'signUp' ).fireEvent( 'click' );
		});
		
		// Add events to the navigation
	    $$( '.normal' ).each( function( element ) {
	    
	        element.addEvent( 'click', function() {
	        
	        	if ( HI._currentSub ) {
	        		HI._currentSub.removeClass( 'subItem_selected' );
	                HI._currentSub.addClass( 'subItem_normal' );
	                HI._currentSub = null;
	            }
	        
	            if ( HI._currentMain ) {
	                HI._currentMain.removeClass( 'selected' );
	                HI._currentMain.addClass( 'normal' ); 
	            }
	        
	            HI._currentMain = element;
	    
	            HI._currentMain.removeClass( 'normal' );
	            HI._currentMain.addClass( 'selected' );
	    
	        });
	    
	    });
	    
	    // Add events to the sub navigation
	    $$( '.subItem_normal' ).each( function( element ) {
	    
	        element.addEvent( 'click', function() {
	        
	        	// Make sure the first menu item is selected
	        	if ( HI._currentMain != $( 'investorSoftware' ) || !HI._currentMain ) {
    				HI._currentMain.removeClass( 'selected' );
    				HI._currentMain.addClass( 'normal' );
    				HI._currentMain = $( 'investorSoftware' );
    		 		HI._currentMain.addClass( 'selected' );
    		 	}
	        
	            if ( HI._currentSub ) {
	                HI._currentSub.removeClass( 'subItem_selected' );
	                HI._currentSub.addClass( 'subItem_normal' );
	            }
	            
	            if ( HI._currentMain && HI._currentMain != $( 'investorSoftware' ) ) {
	                HI._currentMain.removeClass( 'selected' );
	                HI._currentMain.addClass( 'normal' ); 
	            }
	        
	            HI._currentSub = element;
	    
	            HI._currentSub.removeClass( 'subItem_normal' );
	            HI._currentSub.addClass( 'subItem_selected' );
	    
	        });
	    
	    });
	    
	    $( 'signUp_link' ).addEvent( 'click', function() {
			$( 'signUp' ).fireEvent( 'click' );
	    });
	    
	    // If the form is submitted or we have an error message, make sure we show it
	    if ( $( 'allDone' ) || $( 'errorMessage' ) )
	    {
   		    // Make sure the sign up menu item is selected 
		    HI.hideContent( $( 'signUp' ) );
		    
	    	HI._currentMain = $( 'signUp' );
		    
		    HI._currentMain.removeClass( 'normal' );
		    HI._currentMain.addClass( 'selected' );
		    
		    HI._currentContent = $( 'signUpform' );
		    
		    $( 'investorSoftware_content' ).hide();
		    $( 'signUpform' ).show();
	    }
		else
		{			    
		    // Make sure the first menu item is selected
		    HI.hideContent( $( 'investorSoftware' ) );
		    
	    	HI._currentMain = $( 'investorSoftware' );
		    
		    HI._currentMain.removeClass( 'normal' );
		    HI._currentMain.addClass( 'selected' );
		    
		    HI._currentContent = $( 'investorSoftware_content' );
	    
	    }
		
	},
	
	initHints: function()
	{
		HI.FormTools.initValidationHint( { field: 'title', type: 'length', hintMsg: '', errorMsg: 'Please select a title', notRequired: false } );
		HI.FormTools.initValidationHint( { field: 'firstName', type: 'length', hintMsg: 'eg. John', errorMsg: 'Please enter a first name', notRequired: false } );
		HI.FormTools.initValidationHint( { field: 'lastName', type: 'length', hintMsg: 'eg. Smith', errorMsg: 'Please enter a last name', notRequired: false } );
		HI.FormTools.initValidationHint( { field: 'email', type: 'email', hintMsg: 'eg. john@smith.com', errorMsg: 'Please enter a valid email address', notRequired: false } );
		HI.FormTools.initValidationHint( { field: 'phone', type: 'length', hintMsg: '', errorMsg: 'Please enter a valid phone number', notRequired: false } );
		
		HI.FormTools.initValidationHint( { field: 'address1', type: 'length', hintMsg: '', errorMsg: 'Please enter an address', notRequired: false } );
		HI.FormTools.initValidationHint( { field: 'suburb', type: 'length', hintMsg: '', errorMsg: 'Please enter a suburb', notRequired: false } );
		
		HI.FormTools.initValidationHint( { field: 'postCode', type: 'length', hintMsg: '', errorMsg: 'Please enter a postcode', notRequired: false } );
		
		HI.FormTools.initValidationHint( { field: 'dob', type: 'dob', hintMsg: 'eg. DD/MM/YYYY', errorMsg: 'Please enter DOB as DD/MM/YYYY', notRequired: true } );
		
		HI.FormTools.initValidationHint( { field: 'tid', type: 'length', hintMsg: '', errorMsg: 'Please select a source', notRequired: false } );
	}
	
};

HI.FormTools = {
	
	_invalidFields: [],
	
	initValidationHint: function( data )
	{
		var $field = $( data.field );
		var $hint = $( data.field + '_hint' );
		
		if ( !$field )
			return;
			
		// Style changes
		var switchToError = function() {
		
			if ( !$hint )
				return;
		
			$hint.removeClass( 'suggestion' ).removeClass( 'valid' ).addClass( 'error' ).set( 'html', data.errorMsg );
		};
		
		var switchToSuggestion = function() {
		
			if ( !$hint )
				return;
		
			if ( data.hintMsg )
				$hint.removeClass( 'error' ).removeClass( 'valid' ).addClass( 'suggestion' ).set( 'html', data.hintMsg );
			else
				$hint.hide();
		};
		
		// Invalid fields
		var addInvalidField = function() {		
			if ( !this._invalidFields.contains( data.field ) )
				this._invalidFields.push( data.field );
				
		}.bind( this );
		
		var removeInvalidField = function() {
			if ( this._invalidFields.contains( data.field ) )
				this._invalidFields.erase( data.field );
			
		}.bind( this );
		
		// Generic focus event
		$field.addEvent( 'focus', function() {
				
			if ( data.hint )
				$hint.show();
				
    	}.bind( this ) );
		
		// Other events
		switch( data.type )
		{
			/* Length */
			case 'length':
			
				$field.addEvents({
    				
    				'blur': ( function() {
      					if ( !$field.value.length && !data.notRequired ) {
    						switchToError();
    						addInvalidField();
       						$hint.show();
       					} else {
       						removeInvalidField();
       						switchToSuggestion();
       					}
       					
    				}.bind( this ) )
    				
				}, this );
				
			break;
			
			/* Numeric */
			case 'numeric':
			
				$field.addEvents({
    				
    				'blur': ( function() {
      					if ( $field.value.length > 0 && !/^ *[0-9]+ *$/.test( $field.value ) ) {
    						switchToError();
    						addInvalidField();
    						$hint.show();
    					} else if ( !$field.value.length && !data.notRequired ) {
    						switchToError();
    						addInvalidField();
       						$hint.show();
       					} else {
       						switchToSuggestion();
       						removeInvalidField();
       					}
       					
    				}.bind( this ) )
    				
				});
				
			break;
			
			/* DOB */
			case 'dob':
			
				$field.addEvents({
    				
    				'blur': ( function() {
      					if ( $field.value.length > 0 && !$field.value.match( /(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d/ ) ) {
    						switchToError();
    						addInvalidField();
       						$hint.show();
       					} else if ( !$field.value.length && !data.notRequired ) {
    						switchToError();
    						addInvalidField();
       						$hint.show();
       					} else {
       						switchToSuggestion();
       						removeInvalidField();
       					}
       					
    				}.bind( this ) )
    				
				});
				
			break;
		
			/* Email */
			case 'email':
			
				$field.addEvents({
    				
    				'blur': ( function() {
      					if ( $field.value.length > 0 && !/^[\w\-]+(\.[\w\-]+)*@[\w\-]+\.([\w\-]+\.)*[a-z]{2,}$/i.test( $field.value ) ) {
    						switchToError();
    						addInvalidField();
       						$hint.show();
       					} else if ( !$field.value.length && !data.notRequired ) {
    						switchToError();
    						addInvalidField();
       						$hint.show();
       					} else {
       						switchToSuggestion();
       						removeInvalidField();
       					}
       					
    				}.bind( this ) )
    				
				});
				
			break;
		
			default:
			
				$field.addEvents({
    				
    				'blur': ( function() {
      					$hint.hide();
      					
    				}.bind( this ) )
    				
				});
			
			break;
		
		}
		
		return $field;
	
	},
	
	removeDefaults: function( data, fields )
	{
		for ( var i in fields )
		{
			if ( data[ i ] == fields[ i ] )
				data[ i ] = '';
		}
		
		return data;
	},
	
	validateFields: function( data, fields )
	{
		var rtn = true;
		
		for ( var i in fields )
		{
			if ( !$chk( data[ i ] ) )
			{
				alert( fields[ i ] );
				rtn = false;
				break;
			}
		}
		
		return rtn;
			
	},
	
	initDefaultValueAsHint: function( field, defaultValue, passwordField )
	{
		var $field = $( field );
		
		if ( !$field )
			return;
		
		if ( !$chk( field.value ) )
			$field.value = defaultValue;
		
		$field.addEvent( 'focus', function() {
				
				if ( this.value != defaultValue )
					return;
				
				if ( passwordField && Browser.Engine.name != 'trident' )
					this.setProperty( 'type', 'password' );
				
				this.value = '';
								
				this.setStyle( 'color', '#4D4D4D' );
			
			})
			.addEvent( 'blur', function() {
				
				if ( !this.value.length )
				{
					if ( passwordField && Browser.Engine.name != 'trident' )
						this.setProperty( 'type', 'text' );
					
					this.value = defaultValue;
					
					this.setStyle( 'color', '#999999' );
				}
			
			});
		
		return $field;
	}
		
};

window.addEvent( 'domready', function() {

	if ( !$( 'hasScript' ) )
		return;
		
	$( 'hasScript' ).show();
	
	// Init
	HI.initNavigation();
	HI.initForms();
	HI.initHints();
	HI.initButtons();
	
	// Redirect to signup immediately (if set)
	if ( window.location.hash == '#signUpNow' )
	{
		new Fx.Scroll( window ).toElement( 'mainWrapper' );
		
		(function() {
			$( 'signUp' ).fireEvent( 'click' );
		}).delay( 500 );
	}

});