(function ()
{
    var focusInInput = false, upLink, nextLink, prevLink, started = false;

    function NavigateThrough (event)
    {
        if (window.event)
            event = window.event;

        if (!(event.ctrlKey || event.altKey) || focusInInput)
            return;

        var link = false;
        switch (event.keyCode ? event.keyCode : event.which ? event.which : null)
        {
            case 0x27:
                link = nextLink;
                break;
            case 0x25:
                link = prevLink;
                break;
            case 0x26:
                link = upLink;
                break;
        }

        if (link)
        {
            document.location = link;
            if (window.event)
                window.event.returnValue = false;
            if (event.preventDefault)
                event.preventDefault();
        }
    }

    function Init()
    {
        if (started || !document.getElementsByTagName || !document.getElementById)
            return;

        started = true;
        var e, i, ae = document.getElementsByTagName('LINK');
        for (i = ae.length; i-- ;)
        {
            e = ae[i];
            if (e.rel == 'next')
                nextLink = e.href;
            if (e.rel == 'prev')
                prevLink = e.href;
            if (e.rel == 'up')
                upLink = e.href;
        }

        if (bIE)
        {
            document.attachEvent('onkeydown', NavigateThrough);

            ae = document.getElementsByTagName('INPUT');
            for (i = ae.length; i-- ;)
            {
                e = ae[i];
                if (e.type == 'text' || e.type == 'search')
                {
                    e.attachEvent('onfocus', function () {focusInInput = true});
                    e.attachEvent('onblur', function () {focusInInput = false});
                }
            }

            ae = document.getElementsByTagName('TEXTAREA');
            for (i = ae.length; i-- ;)
            {
                e = ae[i];
                e.attachEvent('onfocus', function () {focusInInput = true});
                e.attachEvent('onblur', function () {focusInInput = false});
            }
        }
        if (bFF)
        {
            document.addEventListener('keydown', NavigateThrough, true);

            ae = document.getElementsByTagName('INPUT');
            for (i = ae.length; i-- ;)
            {
                e = ae[i];
                if (e.type == 'text' || e.type == 'search')
                {
                    e.addEventListener('focus', function () {focusInInput = true}, true);
                    e.addEventListener('blur', function () {focusInInput = false}, true);
                }
            }

            ae = document.getElementsByTagName('TEXTAREA');
            for (i = ae.length; i-- ;)
            {
                e.addEventListener('focus', function () {focusInInput = true}, true);
                e.addEventListener('blur', function () {focusInInput = false}, true);
            }
        }
    }

    var bFF = document.addEventListener != null;
    var bIE = !bFF && document.attachEvent != null;

    if (bIE)
        attachEvent('onload', Init);
    if (bFF)
    {
        document.addEventListener('DOMContentLoaded', Init, false );
        addEventListener('load', Init, true);
    }

})();
