function MessageBox()
{
}

MessageBox.simpleDialog = null;

MessageBox.defaultWidth = "<<uninitialized>>";
MessageBox.btnOk = "<<uninitialized>>";


/**
 * Displays a dialog with the given title, message, and set of buttons
 * @param header the text that goes into the title bar of the dialog
 * @param text the message to display
 * @param buttons the array of button objects.
 * @param icon the icon to display with the dialog.  [optional, default = ICON_HELP]
 * @param width the width of the dialog box [optional, default = "450px"]
 * @param okFunction a function to be called on the OK button following the close of the dialog box. [optional]
 */
MessageBox.displayDialog = function(header, text, buttons, icon, width, okFunction)
{
    var i;
    var okHandler;

    // If there is already a dialog box displayed, return.
    if (MessageBox.simpleDialog !== null)
    {
        return;
    }

    if (!icon)
    {
        icon = YAHOO.widget.SimpleDialog.ICON_HELP;
    }

    if (!width)
    {
        width = MessageBox.defaultWidth;
    }

    // If there are no buttons, add an OK button that closes the dialog
    if (!buttons || buttons.length === 0)
    {
        if (okFunction)
        {
            okHandler = function()
            {
                MessageBox.closeDialog();
                okFunction();
            };
        }
        else
        {
            okHandler = MessageBox.closeDialog;
        }
        buttons = [{ text:MessageBox.btnOk, handler: okHandler, isDefault:true }];
    }
    else
    {
        // Add the close dialog call to the button handlers.
        for (i = 0; i < buttons.length; ++i)
        {
            if (!buttons[i].handler)
            {
                buttons[i].handler = MessageBox.closeDialog;
            }
            else
            {
                buttons[i].handler = function(originalHandler)
                {
                    return function()
                    {
                        MessageBox.closeDialog();
                        originalHandler();
                    };
                }(buttons[i].handler);
            }
        }
    }

    MessageBox.simpleDialog = new YAHOO.widget.SimpleDialog(
            "simpledialog1",
    { width: width,
        fixedcenter: true,
        visible: true,
        draggable: true,
        close: false,
        modal:true,
        text: text,
        icon: icon,
        constraintoviewport: true,
        buttons: buttons
    });
    MessageBox.simpleDialog.setHeader(header);

    YahooDlgHelper.attachKeyEventHandlers(MessageBox.simpleDialog, MessageBox.closeDialog);
    YahooDlgHelper.enableFocusOnShow(MessageBox.simpleDialog);

    MessageBox.simpleDialog.render(document.body);
    MessageBox.simpleDialog.show();
};

/**
 * Closes the currently displayed simple dialog
 */
MessageBox.closeDialog = function()
{
    MessageBox.simpleDialog.hide();
    MessageBox.simpleDialog.destroy();
    MessageBox.simpleDialog = null;
};

/**
 * Display a message box with an error icon, and an OK button
 * @param header the text to display in the header of the dialog box.
 * @param text the text to display in the body of the dialog box.
 * @param width the width of the dialog box
 * @param okFunction a function to be called on the OK button following the close of the dialog box.
 */
MessageBox.error = function(header, text, width, okFunction)
{
    MessageBox.displayDialog(header, text, [], YAHOO.widget.SimpleDialog.ICON_BLOCK, width, okFunction);
};

/**
 * Display a message box with an warning icon, and an OK button
 * @param header the text to display in the header of the dialog box.
 * @param text the text to display in the body of the dialog box.
 * @param width the width of the dialog box
 * @param okFunction a function to be called on the OK button following the close of the dialog box.
 */
MessageBox.warning = function(header, text, width, okFunction)
{
    MessageBox.displayDialog(header, text, [], YAHOO.widget.SimpleDialog.ICON_WARN, width, okFunction);
};

/**
 * Display a message box with an info icon, and an OK button
 * @param header the text to display in the header of the dialog box.
 * @param text the text to display in the body of the dialog box.
 * @param width the width of the dialog box
 * @param okFunction a function to be called on the OK button following the close of the dialog box.
 */
MessageBox.info = function(header, text, width, okFunction)
{
    MessageBox.displayDialog(header, text, [], YAHOO.widget.SimpleDialog.ICON_INFO, width, okFunction);
};

