jQuery( function( $ ) {
	$( '#ebay-form-link' ).click( function() {

		$.get( $( this ).attr( 'href' ), function( data ) {

			var myDiv = $( '<div></div>' ).attr( 'title', 'eBay Search Form' ).html( data ).dialog( {
				buttons:	{
					'Cancel':	function() {
								$( this ).dialog( 'close' ).dialog( 'destroy' );
							},
					'Search':	function() {
								$( this ).dialog( 'close' ).dialog( 'destroy' );

								var loader_image_div = $( '<div></div>' ).attr( 'class', 'js-loading-image' ).css( 'background-image', 'url(/'
									+ drupalSettings.loader_image_url + ')')
								$( 'body' ).append( loader_image_div );
								loader_image_div.show();

								$form		=	$( this ).find( 'form' );
								$.post( $form.attr( 'action' ), $form.serialize(), function( data ) {
									$( '.js-loading-image' ).remove();
									$( '<div></div>' ).attr( 'title', 'eBay Search Return' ).html( data ).dialog( {
										width:		1024,
										buttons:	{
											'Close':	function() {
														$( this ).dialog( 'close' ).dialog( 'destroy' );
													},
										},
									} ); // dialog for results
								} ); // post for results
							},
				}
			} ); // dialog for form

			// hide the Drupal submit button because we're going to use the jQueryUI dialog button instead
			myDiv.find( '#edit-submit' ).hide();
		} );

		return false;
	} );
} );
