$( document ).ready( function() {

	$("form select.favourite_restaurant").each( function( i ) {
		$( this ).parent().addClass( "label_inline" );
		$( this ).parent().children( "label" ).text( "" );
	});
	
	
	
	
	$("form input").each( function( i ) {
		
		var oElement = $( this );
		
		if( oElement.hasClass( 'contact_number' ) ) {
			addFormFieldBackground( oElement, "/media/images/forms/contact_number.gif" );
		}
		if( oElement.hasClass( 'friend_name' ) ) {
			addFormFieldBackground( oElement, "/media/images/forms/friend_name.gif" );
		}
		if( oElement.hasClass( 'friend_email_address' ) ) {
			addFormFieldBackground( oElement, "/media/images/forms/friend_email_address.gif" );
		}
		if( oElement.hasClass( 'your_name' ) ) {
			addFormFieldBackground( oElement, "/media/images/forms/your_name.gif" );
		}
		if( oElement.hasClass( 'your_email_address' ) ) {
			addFormFieldBackground( oElement, "/media/images/forms/your_email_address.gif" );
		}
		if( oElement.hasClass( 'password' ) ) {
			addFormFieldBackground( oElement, "/media/images/forms/password.gif" );
		}
		if( oElement.hasClass( 'confirm_password' ) ) {
			addFormFieldBackground( oElement, "/media/images/forms/confirm_password.gif" );
		}
		if( oElement.hasClass( 'town' ) ) {
			addFormFieldBackground( oElement, "/media/images/forms/town.gif" );
		}
		if( oElement.hasClass( 'postcode' ) ) {
			addFormFieldBackground( oElement, "/media/images/forms/postcode.gif" );
		}
		if( oElement.hasClass( 'your_password' ) ) {
			addFormFieldBackground( oElement, "/media/images/forms/your_password.gif" );
		}
		
	});

	
	
	function addFormFieldBackground( a_oFormElement, a_sBackgroundImage ) {
		oLabel = a_oFormElement.parent().children( "label" );
		a_oFormElement.parent().addClass( "label_inline" );
		
		if( oLabel.children( "span" ).length ) {
			oLabel.html( "<span class=\"required\">*</span>" );
		} else {
			oLabel.text( "" );
		}
		
		var sValue = String( a_oFormElement.attr( "value" ) );
		
		if( sValue.length == 0 ) {
			a_oFormElement.css( "background-image", "url( " + a_sBackgroundImage + " )" )
				.css( "background-position", "left center" )
				.css( "background-repeat", "no-repeat" );
				
			a_oFormElement.click( function( event ) {
				$( this ).css( "background-image", "none" );
			});
				
			a_oFormElement.focus( function( event ) {
				$( this ).css( "background-image", "none" );
			});
		}
				
		a_oFormElement.blur( function( event ) {
			if( String( a_oFormElement.attr( "value" ) ).length == 0 ) {
				a_oFormElement.css( "background-image", "url( " + a_sBackgroundImage + " )" );
			}
		});
	}
	
} );