// ==UserScript==
// @name           Lingr Words History
// @namespace      http://n.h7a.org/
// @author         HIROSHIMA Naoki <n at h7a dot org>
// @description    History functionality for your silly words.
// @include        http://www.lingr.com/room/*
// ==/UserScript==

// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html

// ver 0.1: 2006-09-28
//  - experimental release 

(function ()
{
    function $(id)
    {
        return document.getElementById(id);
    }

    function onLoad()
    {
        var speakField = $("speakField");
        if (speakField == null) return;
        speakField.addEventListener("keyup", onKeyup, false);
    }

    var history = 0;
    var index = 0;

    function onKeyup(e) {
        var Event = unsafeWindow["Event"];

        switch (e.keyCode) {
        case Event.KEY_UP:
            if (e.ctrlKey && history && 0 < index) {
                this.value = unescape(GM_getValue(--index, ''));
                Event.stop(e);
                return false;
            }
            break;

        case Event.KEY_DOWN:
            if (e.ctrlKey && history && history > index) {
                this.value = unescape(GM_getValue(++index, ''));
                Event.stop(e);
                return false;
            }
            break;

        case Event.KEY_RETURN:
            if (this.value.length) {
                GM_setValue(history, escape(this.value));

            } else if (GM_getValue(index, '').length) {
                GM_setValue(history++, GM_getValue(index, ''));
                GM_setValue(history, '');
                index = history;
            }
            break;

        default:
            GM_setValue(history, escape(this.value));
            break;
        }
    }

    window.addEventListener("load", onLoad, false);
})();
