// ==UserScript==
// @name          Bruceify!
// @namespace     http://www.brucestockwell.net/
// @description   Bruce having fun with GM and his site.
// @include       http://*.brucestockwell.net/*
// ==/UserScript==
 

var Bruceify = function () {
    // ************ start jquery section *****************************
    // insert user scripts into the head of the webpage
    // so that GM can use them at will
    var commands = {};
    var scripts = [
        'http://www.brucestockwell.net/scripts/jquery.js'
    ];

    for (i in scripts) {
        var script = document.createElement('script');
        script.src = scripts[i];
        script.type = 'text/javascript';
        document.getElementsByTagName('head')[0].appendChild(script);
    };

    window.addEventListener("load", wait_for_jq, false); 

    

    // Check if jQuery's loaded
    function wait_for_jq() {
	if(typeof unsafeWindow.jQuery == 'undefined') {
            window.setTimeout(wait_for_jq,100);
        } else { 
            $ = unsafeWindow.jQuery; use_jq(); 
        }
    };

    // All your GM code must be inside this function
    function use_jq() {
        $(injectLegend());
        $(init());
    };

    function init(){
        commands['h'] = makeCommand(handleKeyHome);
        commands['a'] = makeCommand(handleKeyAbout);
        commands['p'] = makeCommand(handleKeyProjects);
        commands['b'] = makeCommand(handleKeyBlog);
        commands['c'] = makeCommand(handleKeyContact);
        commands['s'] = makeCommand(handleKeySis);
        commands['l'] = makeCommand(handleKeyLegend);
        commands['e'] = makeCommand(handleEasterEgg);

        document.addEventListener('keydown', keypress_handler, false);

        $("input").bind("blur",enableCommands);
        $("select").bind("blur",enableCommands);
        $("textarea").bind("blur",enableCommands);
        $("input").bind("focus",disableCommands);
        $("select").bind("focus",disableCommands);
        $("textarea").bind("focus",disableCommands);
    };


    // ************ shortcut key section *****************************
    // keycode constants
    var keys = {a : 65, b : 66, c : 67,  e : 69, h : 72, l : 76, p : 80, q : 81, s : 83};

    // uri constants
    var c = {
        URI_HOME        : 'http://www.brucestockwell.net/',
        URI_ABOUT       : 'http://www.brucestockwell.net/about',
        URI_PROJECTS    : 'http://www.brucestockwell.net/projects',
        URI_BLOG        : 'http://www.brucestockwell.net/blog',
        URI_CONTACT     : 'http://www.brucestockwell.net/contact',
        URI_SIS         : 'http://www.sis.brucestockwell.net/'
    };

    // handlers

    function handleKeyHome() {
        window.location = c.URI_HOME;
    };

    function handleKeyProjects() {
        window.location = c.URI_PROJECTS;
    };

    function handleKeyAbout() {
        window.location = c.URI_ABOUT;
    };

    function handleKeyBlog() {
        window.location = c.URI_BLOG;
    };

    function handleKeyContact() {
        window.location = c.URI_CONTACT;
    };

    function handleKeySis() {
        window.location = c.URI_SIS;
    };

    function handleKeyLegend() {
        $("#hotkeys").animate({height: 'toggle', opacity: 'toggle'}, 'slow');
    };

    function handleEasterEgg() {
        $("#navigator").animate({height: 'toggle', opacity: 'toggle'}, 'slow');
    };

    function injectLegend() {

        GM_addStyle (
            '#hotkeys {font-size: .75em; position: absolute; z-index: 1; top: 0px; right: 0px; width: 15em; border: 1px dashed #000000; background-color: cyan; padding: 10px;}' +
            '#hotkeys dl {margin: none; text-align: left;}' +
            '#hotkeys h1 {font-size: .95em; color: #000000; padding: none; margin: none; text-align: center;}' +
            '#hotkeys dd {margin-left: 25px; color: #000000;}' +
            '#hotkeys dt {float: left; color: #000000;}' +
            '.bruceify-hidden {display: none;}'
        );

        var div = document.createElement('div');

        div.id = 'hotkeys';
        div.className = 'bruceify-hidden';
        div.innerHTML = '<h1>Keyboard Shortcuts</h1>' +
            '<dl>' +
                '<dt>h</dt><dd>home</dd>' +
                '<dt>a</dt><dd>about</dd>' +
                '<dt>b</dt><dd>blog</dd>' +
                '<dt>p</dt><dd>projects</dd>' +
                '<dt>c</dt><dd>contact</dd>' +
                '<dt>s</dt><dd>sis</dd>' +
                '<dt>l</dt><dd>legend</dd>' +
            '</dl>';

         $(document.body.appendChild(div));
    };

    function keypress_handler(e){
        if(e.keyCode==keys.h){runCommand(commands['h'])};
        if(e.keyCode==keys.a){runCommand(commands['a'])};
        if(e.keyCode==keys.p){runCommand(commands['p'])};
        if(e.keyCode==keys.b){runCommand(commands['b'])};
        if(e.keyCode==keys.c){runCommand(commands['c'])};
        if(e.keyCode==keys.s){runCommand(commands['s'])};
        if(e.keyCode==keys.l){runCommand(commands['l'])};
        if(e.keyCode==keys.e){runCommand(commands['e'])};
    };

    function runCommand(command) {
        if (command.enabled) {
            command.fn();
        }
    };

    function makeCommand(fn) {
        var cmd = new Object;
        cmd.fn = fn;
        cmd.enabled = true;
        return cmd
    };

    function disableCommands() {
        for (cmd in commands) {
            commands[cmd].enabled = false;
        }; 
    };

    function enableCommands() {
        for (cmd in commands) {
            commands[cmd].enabled = true;
        };
    };

};

Bruceify();




