function showHelp(p, e)
{
    var help_window = 'help_window';
    if ( ! $(help_window) ) {
        new Insertion.Top('content', basicHelpWindow());
    }
    else {
        Effect.Appear(help_window, { duration: 1.0 });
    }
    new Draggable(help_window, { handle: 'help_window_header' });

    if ( $('mceContent') ) {
        tinyMCE.execCommand('mceFocus', false, 'mceContent');
        tinyMCE.execCommand('mceRemoveControl', false, 'mceContent');
    }

    if ( ! p ) {
        p = helpPageOverride;
    }
    p = ( p == 'undefined' ) ? null : p;

    new Ajax.Updater('help_window_content', 'help',
        {
              method:       'get'
            , parameters:   { option_type: 'help', element: e, page: p }
            , onCreate:     showLoading( 'help_window_content' )
        }
    );
}

function basicHelpWindow()
{
    var win =   "<div id='help_window' class='cb'>"
              + "<div class='bt'><div></div></div>"
              + "<div class='i1'><div class='i2'><div class='i3'>"
              + "<div class='content' id='help_window_content'></div>"
              + "</div></div></div>"
              + "<div class='bb'><div></div></div>"
              + "</div>"
    return win;
}

function showLoading( p )
{
    $(p).innerHTML = "<h1 class='help_window_header'>Loading Help - Please Wait.<img src='../images/close_btn-22x22.gif' border='none' onclick='Effect.Fade($(\"help_window\"));' /></h1><img src='../images/big_loading.gif' border='none' style='display:block; margin-left:auto; margin-right:auto' />";
}

function modifyHelp(p, e)
{
    if ( $('mceContent') ) {
        tinyMCE.execCommand('mceFocus', false, 'mceContent');
        tinyMCE.execCommand('mceRemoveControl', false, 'mceContent');
    }

    if ( ! p ) {
        p = helpPageOverride;
    }
    p = ( p == 'undefined' ) ? null : p;

    if ( $('help_window_modify_frm') ) {
        vars = $('help_window_modify_frm').serialize(true);
        vars['page'] = p;
        vars['element'] = e;
        new Ajax.Updater('help_window_content', 'help',
            {
                  method:       'post'
                , parameters:   vars
                , onCreate:     showLoading( 'help_window_content' )
                , evalScripts:  true
            }
        );
    }
}

function addElementHelpers(p)
{
    var helpers = $$('.helpable');
    helpers.each( function(h) {
        if ( ! h.id ) return;
        new Insertion.After(h, "<a href=\"javascript:showHelp('" + p + "','" + h.id + "')\" class='help_img'><img src='../images/help-16x16.gif' border='none' /></a>");
    });
}
