// ==UserScript==
// @name          phpMyAdminMenus
// @namespace     http://roub.net/xul
// @description   Adds 'popup' submenus to the left nav bar of phpMyAdmin
// @include       http://add.your.main.phpMyAdmin.URL.here/*
// ==/UserScript==
//
// History:         http://roub.net/xul/greasemonkey/phpMyAdminMenus.history.txt
// Bug reports:     paul@roub.net
//


var gmPhpAllMenus = new Array();

function gmPhpHideAllMenus(but)
{
    for (var i = 0; i < gmPhpAllMenus.length; ++i)
    {
        var menu = gmPhpAllMenus[i];

        if (menu.id != but)
        {
            menu.style.display = 'none';
        }
    }
}


(function() {
    var linkRegEx = new RegExp("^(.*?/)tbl_properties_structure.php(.*)$");
    var lns = document.evaluate("//a[contains(@href,'tbl_properties_structure.php') and (@class='tblItem')]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

    var menuitems = new Array();
    menuitems['Browse'    ] = 'sql.php';
    menuitems['SQL']        = 'tbl_properties.php';
    menuitems['Search']     = 'tbl_select.php';
    menuitems['Insert']     = 'tbl_change.php';
    menuitems['Operations'] = 'tbl_properties_operations.php';

    menuextras = new Array();
    menuextras['Browse'] = '&sql_query=SELECT+%2A+FROM+%60GMTABLENAME%60&pos=0';

    var postfix = '&goto=tbl_properties_structure.php&back=tbl_properties_structure.php';

    if (lns.snapshotLength > 0)
    {
        for (var i = 0; i < lns.snapshotLength; ++i)
        {
            var ln = lns.snapshotItem(i);
            var target = ln.target;
            var lnId = ln.id;
            var tableName = ln.text.replace(/^\s*|\s*$/g,"");

            var matches = linkRegEx.exec(ln.href);

            if (matches.length < 3)
            {
                continue;
            }

            var udiv = document.createElement('div');
            udiv.id = lnId + 'Div';
            udiv.style.padding = '0';
            udiv.style.margin = '0';

            var ul = document.createElement('ul');
            ul.id = udiv.id + 'Menu';
            ul.style.display = 'none';
            ul.style.listStyle = 'none';
            ul.style.padding = 0;
            ul.style.margin = 0;
            ul.style.marginLeft = '2em';

            for (var label in menuitems)
            {
                var page = menuitems[label];

                var newRef = matches[1] + page + matches[2] + postfix;

                var extra = menuextras[label];

                if (extra)
                {
                    extra = extra.replace("GMTABLENAME", tableName);
                    newRef += extra;
                }

                var li = document.createElement('li');
                li.style.listStyle = 'none';
                li.style.margin    = 0;
                li.style.padding   = 0;
                li.style.fontSize  = 'x-small';

                var a = document.createElement('a');
                a.href      = newRef;
                a.title     = label;
                a.innerHTML = label;
                a.target    = target;

                li.appendChild(a);

                ul.appendChild(li);
            }

            ul.style.display = 'hidden';
            gmPhpAllMenus.push(ul);
            udiv.appendChild(ul);

            ln.parentNode.replaceChild(udiv, ln.nextSibling);

            //  just hide all other menus when we activate.  this avoids a lot of 
            //  messiness trying to keep up with menu closing, and gives us
            //  sticky submenus, which works nicely
            //
            ln.onmouseover = function() { 
                    gmPhpHideAllMenus(this.id + "DivMenu");
                    document.getElementById(this.id + "DivMenu").style.display = 'block';
                };
        }

    }
})();

