function YahooDlgHelper()
{
}


/**
 * This is the handler of the arrow keys in the dialog boxes.
 * Note that the this is in the context of the yahoo dialog object.
 */
YahooDlgHelper.handleArrowKeys = function()
{
    var btnIdx;
    var btns = this.getButtons();

    // Get the index of the button that has focus
    for (btnIdx = 0; btnIdx < btns.length; ++btnIdx)
    {
        if (btns[btnIdx].hasFocus())
        {
            break;
        }
    }

    var event = arguments[1];
    if (event[0] == 39) // Right arrow key
    {
        btnIdx++;
        if (btnIdx > btns.length - 1)
        {
            btnIdx = 0;
        }
    }
    else
    {
        btnIdx--;
        if (btnIdx < 0)
        {
            btnIdx = btns.length - 1;
        }
    }

    btns[btnIdx].focus();
};

/**
 * Attaches key event handlers to the dialog box
 * @param dlg the dialog box object
 * @param closeFunction the function that should be called to close the dialog
 */
YahooDlgHelper.attachKeyEventHandlers = function(dlg, closeFunction)
{
    // Create key listeners for the escape key and right/left arrow keys
    var escapeKeyListener = new YAHOO.util.KeyListener(document, { keys:27 },
    { fn: closeFunction,
        scope: dlg,
        correctScope:true });

    var arrowKeyListener = new YAHOO.util.KeyListener(document, { keys:[37,39] },
    { fn: YahooDlgHelper.handleArrowKeys,
        scope: dlg,
        correctScope:true });

    dlg.cfg.queueProperty("keylisteners", [escapeKeyListener, arrowKeyListener]);

};

/**
 * Override the default behavior onShow so that the default button gets focus
 * @param dlg the dialog to enable this behavior on
 */
YahooDlgHelper.enableFocusOnShow = function(dlg)
{
    dlg.showEvent.subscribe(
            function()
            {
                var btns = this.getButtons();

                if (btns.length > 0)
                {
                    var hasYuiBtns = (YAHOO.widget.Button && btns[0] instanceof YAHOO.widget.Button);
                    if (hasYuiBtns)
                    {
                        for (var i = 0; i < btns.length; i++)
                        {
                            var btn = btns[i];
                            if (btn.hasClass("default"))
                            {
                                btn.focus();
                                break;
                            }
                        }
                    }
                    else
                    {
                        // Ideally, should be able to use this for both
                        // YUI Button widgets and HTML Buttons. This will be fixed
                        this.focusDefaultButton();
                    }
                }
            }
            );
};

