/**
 * Defines the namespace for the preferences UI panel
 */
function PreferencesUI()
{
}

/**
 * The function to call following the setting of the preferences.
 */
PreferencesUI.afterUpdateFunction = null;

/**
 * Strings used for the interface
 */
PreferencesUI.saveButtonText = "saveButtonText";
PreferencesUI.cancelButtonText = "cancelButtonText";

/**
 * The settable width of the dialog box.
 */
PreferencesUI.width = "<<uninitialized>>";

/**
 * The preferences dialog
 */
PreferencesUI.preferencesDlg = null;

/**
 * Loads the preferences from the server via an ajax call.
 * The callback will display the dialog box.
 */
PreferencesUI.loadPreferences = function()
{
    PreferencesConnector.getUserPreferences(PreferencesUI.getUserPreferencesDWRObject);
};

/**
 * The error handler for the call to getUserPrefernces
 * @param message the error message
 * @param info the info object which contains additional information about the error.
 */
PreferencesUI.getUserPreferences_errorHandler = function(message, info)
{
    DWRHelper.displayDWRError("PreferencesUI.getUserPreferences", message, info);
};

/**
 * The call back function which reads in the preferences into the dialag and then
 * shows the dialog
 * @param preferences the preferences object
 */
PreferencesUI.getUserPreferences_callback = function(preferences)
{
    // Load the data into the dialog box
    document.getElementById("resultsPerPageid").value = preferences.resultsPerPage;

    var refreshModeElements = document.getElementsByName("refreshMode");
    for (var i = 0; i < refreshModeElements.length; ++i)
    {
        if (refreshModeElements[i].value == preferences.refreshMode)
        {
            refreshModeElements[i].checked = true;
            break;
        }
    }

    if (PreferencesUI.preferencesDlg === null)
    {
        PreferencesUI.initDialog();
    }
    PreferencesUI.preferencesDlg.show();
};

/**
 * The DWR callback object which calls into PreferecnesConnector.getUserPreferences
 */
PreferencesUI.getUserPreferencesDWRObject = {
    callback:     PreferencesUI.getUserPreferences_callback,
    errorHandler: PreferencesUI.getUserPreferences_errorHandler,
    timeout:      DWRHelper.ajaxTimeout
};

/**
 * The error handler for the call to setUserPrefernces
 * @param message the error message
 * @param info the info object which contains additional information about the error.
 */
PreferencesUI.setUserPreferences_errorHandler = function(message, info)
{
    DWRHelper.displayDWRError("PreferencesUI.setUserPreferences", message, info);
};

/**
 * The callback function to the call to setUserPreferences. Does nothing.
 */
PreferencesUI.setUserPreferences_callback = function(prefs)
{
    if (PreferencesUI.afterUpdateFunction)
    {
        PreferencesUI.afterUpdateFunction(prefs);
    }
};

/**
 * The DWR callback object which calls into PreferecnesConnector.setUserPreferences
 */
PreferencesUI.setUserPreferencesDWRObject = {
    callback:     PreferencesUI.setUserPreferences_callback,
    errorHandler: PreferencesUI.setUserPreferences_errorHandler,
    timeout:      DWRHelper.ajaxTimeout
};

/**
 * The method called to initialize the prefernces ui object
 */
PreferencesUI.initDialog = function()
{
    document.getElementById("preferencesDialog").style.display = "";

    // Define various event handlers for Dialog
    var handleSubmit = function()
    {
        var prefs = { refreshMode:this.getData().refreshMode, resultsPerPage:this.getData().resultsPerPage[0] };
        PreferencesConnector.setUserPreferences(prefs, PreferencesUI.setUserPreferencesDWRObject);
        this.cancel();
    };

    var handleCancel = function()
    {
        this.cancel();
    };

    // Instantiate the Dialog
    PreferencesUI.preferencesDlg = new YAHOO.widget.Dialog("preferencesDialog",
    { width : PreferencesUI.width,
        fixedcenter : true,
        visible : false,
        constraintoviewport : true,
        modal:true,
        buttons : [ { text:PreferencesUI.cancelButtonText, handler:handleCancel, isDefault:true },
            { text:PreferencesUI.saveButtonText,   handler:handleSubmit }
        ]
    });

    YahooDlgHelper.attachKeyEventHandlers(PreferencesUI.preferencesDlg, PreferencesUI.preferencesDlg.cancel);
    YahooDlgHelper.enableFocusOnShow(PreferencesUI.preferencesDlg);

    // Render the Dialog
    PreferencesUI.preferencesDlg.render();
};

