LD = {
	
	_initHooks: [],
	_initialised: false,
	
	init: function()
	{
		LD._initHooks.each( function( fn ) { fn(); } );
		
		LD._initialised = true;
		
		LD.initBetaPopup();
		
		new SmoothScroll();
	},
	
	onInit: function( fn )
	{
		if ( LD._initialised )
			fn();
		else
			LD._initHooks.push( fn );
	},
	
	initBetaPopup: function()
	{
		var sendFeedback = function() {
		
			if ( LD._processingFeedback )
				return;
		
			LD._processingFeedback = true;
		
			// Define data
			var data = {
				contactName: $( 'form_feedback_contactName' ).value,
				emailAddress: $( 'form_feedback_emailAddress' ).value,
				rating: LD.FormTools.getValue( 'form_feedback', 'rating' ),
				message: $( 'form_feedback_message' ).value
			};
			
			
			// Check values
			if ( data.contactName == 'Name' || !data.contactName )
				data.contactName = '';
			
			if ( !/^[\w\-]+(\.[\w\-]+)*@[\w\-]+\.([\w\-]+\.)*[a-z]{2,}$/i.test( data.emailAddress ) )
			{
				alert( 'That is not a valid email address, please try again.' );
				$( 'form_feedback_emailAddress' ).focus();
				LD._processingFeedback = false;
				return;
			}
			
			if ( data.message == 'Message' || !data.message )
				data.message = '';
			
			
			// Send name string as summary
			data.name = data.contactName + ' - ' + ( data.message > 250 ? data.message.slice( 0, 250 ) + '...' : data.message );
			
			
			// Validate fields
			if ( !LD.FormTools.validateFields( data, {
					'contactName': 'Please enter your name before trying to send the form.',
					'message': 'Please enter your message before trying to send the form.'
				}))
				return;
			
			
			$( 'form_feedback_submit' ).set( 'value', '...' )
				
			
			Aurora.callAPI({
				
				key: 'create feedback',
				data: data,
				
				onComplete: function( rtnData ) {
					
					LD._processingFeedback = false;
					
					if ( rtnData.success )
					{
						$( 'form_container' ).hide();
						$( 'success_container' ).show();
					
						(function() {
							LD._feedbackPopup.close();
						}).delay( 2000 );
					
					}
					else
						alert( "Sorry, there was an error sending your feedback. Please reload the page and try again.\n\nIf you continue to get this message, please get in contact with us." );
					
				}
				
			});
	
		}
	
		var initPopup = function() {

			$( 'form_feedback_cancel' ).addEvent( 'click', function() {
				LD._feedbackPopup.close();
			});
			
			$( 'form_feedback_submit' ).addEvent( 'click', function() {
				sendFeedback();
			});
		
		}
	
		var openPopup = function() {
		
			LD._feedbackPopup = popup = new Aurora.Popup({
				width: 555,
				height: 750,
				zIndex: 99999,
				transitionDelay: 4,
				transitionInSteps: 6,
				transitionOutSteps: 6,
				closeButton: false,
				noCanvas: true
			})
			.addEvent( 'ready', function() {
	
				var content = this.$content;
				
				new Request.HTML()
					.get( '/Resources/LowDown/Sites/Lowdown/HTML/form_beta.html?=' + $time() )
					.addEvent( 'success', (function() {
							
						content.setStyle( 'background', 'none' );
						
						// Insert the html
						content.set( 'html', this.response.html );
						
						initPopup();
				
					}) );
					
			});
		}
		
		// Add popup event to links
		$$( '.sendFeedback' ).addEvent( 'click', function() {
			openPopup();
		});
		
	}
	
}

LD.FormTools = {
	
	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', '#333333' );
			
			})
			.addEvent( 'blur', function() {
				
				if ( !this.value.length )
				{
					if ( passwordField && Browser.Engine.name != 'trident' )
						this.setProperty( 'type', 'text' );
					
					this.value = defaultValue;
				}
			
			});
		
		return $field;
	},
	
	getValue: function( form, key )
	{
	    var form = $( form );
	    var inputValue = form[ key ].value; // default to the value of the field in case we sent the function a text input field or a select field
	    
	    // just return the value if its an invalid field (see above)
	    if ( !form[ key ].length )
	        return inputValue;
	
	    // otherwise loop through all the fields until we get a true value, then return it
	    for ( var i = 0; i <= form[ key ].length - 1; i++) {
	    
	        var inputValue = form[ key ][ i ].checked;
	
	        if ( inputValue )
	        {
	            // $log( 'found true value, returning: ' + inputValue )
	            
	            var inputValue = form[ key ][ i ].value;
	            
	            return inputValue; 
	        }
        
		}

	}
	
}

window.addEvent( 'domready', function() {
	
	LD.init();
	
})