/**
 * 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='' rel='' (if exists one)

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

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

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

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

    // load url from LINK tags, if exists
    if ($("link[rel=next]").length || $("link[rel=previous]").length) {        
        var nextUrl = '';
        var prevUrl = '';
        if ($("link[rel=next]").length) {
            nextUrl = $("link[rel=next]").attr('href');
        }
        if ($("link[rel=prev]").length) {
            prevUrl = $("link[rel=prev]").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(prevUrl != '') {
                        window.location = prevUrl;
                    }
                    break;
                // User pressed "right" arrow
                case 39:
                    if(nextUrl != '') {
                        window.location = nextUrl;
                    }
                    break;
            }
        });
    }
}

//})(jQuery);
