
function ajax_show_dialog( dialog_text ) {

    if (!$('#dialog') || !($('#dialog').length)) {
        $('body').append("<div id='dialog'></div>");
    }

	$('#dialog').html(dialog_text).dialog({
			show: 'fade' ,
			hide: 'fade' ,
			modal: true,
			buttons: {
				OK: function() {
					$(this).dialog( 'close' );
				}
			}
		});
}

document.ajax_in_progress = false;

function ajax_request( method , uri , callback , data , override_method) {

    if( document.ajax_in_progress ) {
		alert( 'Your request is being processed; please stand by.' );
	}else{
		document.ajax_in_progress = true;
	}

	$.ajax({
		type: method ,
		url: uri ,
		data: data ,
		complete: function( request ) {

			document.ajax_in_progress = false;
		
			//fix the form's buttons and such
			var buttons = $('button[disabled][type=submit]'),
			    inputs = $('input[disabled][type=submit]');
			    			    
			if (buttons) {
			    buttons.removeClass('btn-loading');
			    buttons.attr('disabled', false);
			}
			
			if (inputs) {
			    inputs.removeClass('btn-loading');
			    inputs.attr('disabled', false);
			}		
		
			if( request.status != 200 ) {

				indo.Util.showDialog( 'There was a problem processing your request. Please try again later.' );

			}else{

				response = jQuery.parseJSON( request.responseText );

				if( ! response ) {
					response = { 'method' : false };
				}

				if( override_method ) {
					response.supplied_method = response.method;
					response.method = 'callback';
				}

				switch( response.method ) {

					case 'dialog':
						indo.Util.showDialog( response.dialog_text );
						break;

					case 'redirect':
						window.location.href = response.uri;
						break;

					case 'refresh':
						window.location.reload( true );
						break;
						
					case 'highlight_fields':
						fields = response.data;
						for( var i in fields ) {
							$( 'input[' + fields[i] + ']' ).css( 'background-color' , '#fdd' );
						}
						break;

					case 'callback':
					default:
						if( callback ) {
							eval( callback )( response );
						}
						break;
				}

			}

		}

	});
}

function ajaxify_forms() {

	$( '.ajax_request_form' ).each( function() {

		var callback = null;

		if( $( this ).hasClass( 'reload_on_success' ) ) {
			if( $( this ).hasClass( 'reload_opener_on_success' ) ) {
				callback = function() { if( window.opener ) { window.opener.location.reload(); }; window.location.reload(); };
			}else{
				callback = function() { window.location.reload(); };
			}
		}

		if( $( this ).hasClass( 'custom_callback' ) ) {
		    //very very bad
			callback = eval( $( this ).attr( 'callback' ) );
		}

		$(this).submit( function() {

			if( $( this ).hasClass( 'confirm_before_request' ) && ! confirm( 'Are you sure?' ) ) {
				return false;
			}

			ajax_request( 'POST' , $( this ).attr( 'action' ) , callback , $(this).serialize());
			
			//do not allow the button to be clicked again
			var buttons = $( this ).find('button[type=submit]'),
			    inputs = $( this ).find('input[type=submit]');
			    
			if (buttons) {
			    buttons.addClass('btn-loading');
			    buttons.attr('disabled', 'disabled');
			}
			
			if (inputs) {
			    inputs.addClass('btn-loading');
			    inputs.attr('disabled', 'disabled');
			}
			
			return false;

		});

	});

}

function ajaxify_links() {

	$( '.ajax_request' ).click( function() {

		ajax_request( 'GET' , $(this).attr( 'href' ) , function() {
			window.location.reload();
		} );

		return false;

	});

}

$(document).ready( function () {

	ajaxify_forms();
	ajaxify_links();

});

