/**
 * jQuery Page Key Navigation
 * Version 1.0 - 30.3.2010
 * @author Ladislav -knedle- Sevcuj
 * http://blog.3tecky.cz
 *
 **/

//(function($){
//    $.fn.pageKeyNavigation = function(options) {

jQuery.pageKeyNavigation = function() {

    // if LINK tags not exists
    // create LINK ref='' in BODY from A href='' accesskey='' (if exists one)

    // first - prvni
    if (!$("link[accesskey=first]").length && $("a[accesskey=first]").length == 1) {
        var urlFirst = $('a[accesskey=first]').attr("href");
        $('head').append('<link accesskey="first" href="' + urlFirst + '" />');
    }

    // P - predchozi
    if (!$("link[accesskey=P]").length && $("a[accesskey=P]").length == 1) {
        var urlP = $('a[accesskey=P]').attr("href");
        $('head').append('<link accesskey="P" href="' + urlP + '" />');
    }

    // N - dalsi
    if (!$("link[accesskey=N]").length && $("a[accesskey=N]").length == 1) {
        var urlN = $('a[accesskey=N]').attr("href");
        $('head').append('<link accesskey="N" href="' + urlN + '" />');
    }

    // last - posledni
    if (!$("link[accesskey=last]").length && $("a[accesskey=last]").length == 1) {
        var urlLast = $('a[accesskey=last]').attr("href");
        $('head').append('<link accesskey="last" href="' + urlLast + '" />');
    }

    // load url from LINK tags, if exists
    if ($("link[accesskey=N]").length || $("link[accesskey=P]").length) {        
        var NUrl = '';
        var PUrl = '';
        if ($("link[accesskey=N]").length) {
            NUrl = $("link[accesskey=N]").attr('href');
        }
        if ($("link[accesskey=P]").length) {
            PUrl = $("link[accesskey=P]").attr('href');
        }

        // Register keypress events on the whole document
        $(document).keydown(function(e) {
            // storno keypress on form = inputs, select, button, textarea...
            if( $(e.target).is(":input") ) return;
            switch(e.keyCode) {
                // User pressed "left" arrow
                case 37:
                    if(PUrl != '') {
                        window.location = PUrl;
                    }
                    break;
                // User pressed "right" arrow
                case 39:
                    if(NUrl != '') {
                        window.location = NUrl;
                    }
                    break;
            }
        });
    }
}

//})(jQuery);

