// ==UserScript==
// @name			Scifipedia Tweaks
// @namespace		http://decafbad.com
// @description		Make Scifipedia a bit more like a real Wikipedia
// @include			http://scifipedia.scifi.com/*
// ==/UserScript==

(function() {

    function main() {
    
        var body = document.getElementsByTagName('body')[0];
        
        // Remove the topmost scifi.com branding and banner.
        rm('//table[@class="SciFiBanner"]/..');
        
        // Shrink and move the logo.
        var logo = xpath('//img[@alt="SCIFIPEDIA"]');
        logo.parentNode.removeChild(logo);
        
        var new_logo = document.createElement('img');
        with(new_logo) { 
            src    = logo.src;
            width  = "142"; 
            height = "38"; 
        };

        var home_lnk = document.createElement('a');
        home_lnk.href = 'http://scifipedia.scifi.com/index.php/Main_Page';
        home_lnk.appendChild(new_logo);
        body.insertBefore(home_lnk, body.firstChild);        
        
        with(home_lnk.style) {
            position = 'absolute';
            top      = '30px';
            left     = '0px';
            border   = 'none';
        };        

        // Snip a vew more bits.
        rm('//img[@src="/skins/scifipedia/sfpheader02.jpg"]/../..');
        rm('//td[@background="/skins/scifipedia/sfpheader06.jpg"]');
        rm('//input[@id="searchGoButton"]');
        rm('//input[@src="/skins/scifipedia/searchall.gif"]');
        
        var cats = xpath('//div[@class="Cats"]');
        cats.innerHTML = '';
        
        var srch = xpath('//div[@class="SearchBox"]');
        srch.parentNode.removeChild(srch);
        body.insertBefore(srch, body.firstChild);
        with(srch.style) {
            position = 'absolute';
            top      = '43px';
            right    = '10px';
            backgroundImage = 'url(http://scifipedia.scifi.com/skins/scifipedia/wikieditbg.jpg)';
        };
        
        // Collapse what's left of the secondary top bar.
        var top_bar = xpath('//div[@class="PurpleCount"]/../../..');
        top_bar.innerHTML = "<div>&nbsp;</div>";
        with (top_bar.firstChild.style) {
            width  = '100%';
            height = '12px';
            borderTop    = '3px solid #b5abb8';
            borderBottom = '1px solid #816e8a';
            backgroundImage = 'url(http://scifipedia.scifi.com/skins/scifipedia/wikieditbg.jpg)';
        }
        
        // Move the page tools from the page bottom to top of left-nav
        var tools_list = xpaths('//div[@class="HomeToolBox"]');
        for (var i=0,tools; tools=tools_list.snapshotItem(i); i++) {
        
            // Move the toolbox title
            var tools_title = xpath('../../p[@class="Toolz"]', tools);
            var new_title = document.createElement('div');
            with (new_title) {
            	style.paddingTop    = '1em';
            	style.paddingBottom = '1em';
            	innerHTML           = tools_title.innerHTML;
            }
            cats.appendChild(new_title);
            
            // Move the tool box links.
            tools.parentNode.removeChild(tools);
            cats.appendChild(tools);
        }
        rm('//td[@class="ToolbarBox"]');

        // Add a Recent Changes link.
        var recent = document.createElement('li');
        recent.className = 'Tools';
        recent.innerHTML = '<a class="PurpleNoDec" href="/index.php/Special:Recentchanges">Recent Changes</a>';        
        var tools_ul = xpath('//div[@class="HomeToolBox"]/ul');
        tools_ul.insertBefore(recent, tools_ul.firstChild);
        
        // TODO: Make the left-nav tool links a lighter color
        var tool_links = xpaths('//div[@class="HomeToolBox"]//a');
        /*
        for (var i,ele; ele=tool_links.snapshotItem(i); i++) {
            with (ele.style) {
                color = "#ffffff";
            }
        }
        */
        
    }

    // Handy dandy shortcut to XPath lookups.
    function xpaths(path, root) {
        if (!root) root=document;
        return document.evaluate( 
            path, 
            root, 
            null, 
            XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, 
            null
        );
    }
    function xpath(path, root) { return xpaths(path, root).snapshotItem(0); }
    function rm(path, root)    { 
        var eles = xpaths(path, root); 
        for (var i=0,ele; ele=eles.snapshotItem(i); i++)
            ele.parentNode.removeChild(ele); 
    }

    main();
    
})();
