//
// for handling json returning functions reporting needs
//
// two basic functions report_error() and report_success()
//
// any json returning function should always call report_error before continuing.
// it's up to the programmer to determine if report_success should be called.
//
// See function docs for particulars
//
// Justin Coffey <justin@cooleremail.com> 2009/03/11
//
var DEFAULT_ERROR_TITLE   = 'An error has been reported';
var DEFAULT_SUCCESS_TITLE = 'Success!';
var DEFAULT_ALERT_TITLE   = 'Alert!';

$(document).ready(function(){
    // set up the error dialog
    $('#error_dialog').dialog({
        autoOpen    : false,
        modal       : true,
        resizable   : true,
        draggable   : true,
        width       : 450,
        buttons     : { "Close": function() { $(this).dialog("close"); } },
        dialogClass : 'ui-state-error'
    });

    // set up the success dialog
    $('#success_dialog').dialog({
        autoOpen    : false,
        modal       : true,
        resizable   : true,
        draggable   : true,
        width       : 450,
        buttons     : { "Continue": function() { $(this).dialog("close"); } }
    });

    // set up the success dialog
    $('#alert_dialog').dialog({
        autoOpen    : false,
        modal       : true,
        resizable   : true,
        draggable   : true,
        width       : 450,
        buttons     : { "Cancel": function(){ $(this).dialog("close"); return false; }, "Continue": function() { $(this).dialog("close"); return true; } }
    });

});

//
// Report an error
//
// ACCEPTS: [ jquery json object ]
//
// If the supplied object has a value for the "error" key, then
// this function will report it via a dialog and return true.
// If not, then we return false.
//
// RETURNS: true/false
//
function report_error(data) {
    // if we have no error message, return false
    if (!data.error) return false;

    // set the title
    var title = (data.error_title) ? data.error_title : DEFAULT_ERROR_TITLE;
    $('#error_dialog').dialog('option','title',title);

    // set the content
    $('#error_dialog').html(data.error);

    // open it
    $('#error_dialog').dialog( 'open' );

    return true;
}

//
// Report success
//
// ACCEPTS: [ jquery json object ]
//
// If the supplied object has a value for the "success" key, then
// this function will report it via a dialog and return true.
// If not, then we return false.
//
// RETURNS: true/false
//
function report_success(data) {

    // if we have no success message, return false
    if (!data.success) return false;

    // set the title
    var title = (data.success_title) ? data.success_title : DEFAULT_SUCCESS_TITLE;
    $('#success_dialog').dialog('option','title',title);

    // set the html
    $('#success_dialog').html(data.success);

    // open it
    $('#success_dialog').dialog( 'open' );

    return true;
}

//
// Report an alert dialog (ie ask the user if they want to continue or cancel)
//
// ACCEPTS: [ jquery json object ]
//
// If the supplied object has a value for the "alert" key, then
// this function will report it via a dialog and return true.
// If not, then we return false.
//
// RETURNS: true/false
//
function report_alert(data) {

    // set the title
    var title = (data.alert_title) ? data.alert_title : DEFAULT_ALERT_TITLE;
    $('#alert_dialog').dialog('option','title',title);

    // set the buttons
    if (data.buttons) {
        $('#alert_dialog').dialog('option','buttons',data.buttons);
    }
    else {
        $('#alert_dialog').dialog('option','buttons',{ OK: function() { $(this).dialog("close"); } });
    }

    // set the html
    $('#alert_dialog').html(data.alert);

    // open it
    $('#alert_dialog').dialog( 'open' );
}

