jQuery(function($){
    //$.event.special.keystrokes.debug = true;
    $(document).bind('keystrokes', [
        { 
            keys: ['j'], 
            success: function(e) {
                $.scrollTo('+=300px', 0);
            }
        },
        {
             keys: ['k'], 
             success: function(e) {
                $.scrollTo('-=300px', 0);
            }
        },
        {
             keys: ['g','g'], 
             success: function(e) {
                $.scrollTo('0', 0);
            }
        },
        {
             keys: ['shift+g'], 
             success: function(e) {
                    $.scrollTo('100%', 0);
            }
        },
        {
             keys: ['n'],
             success: function(e) {
                var next_post = $('link[rel=next]');
                if (next_post.length) {
                    window.location = next_post.attr('href');
                }
            }
        },
        {
             keys: ['p'], 
             success: function(e) {
                var prev_post = $('link[rel=prev]');
                if (prev_post.length) {
                    window.location = prev_post.attr('href');
                }
            }
        },
        {
             keys: ['r'], 
             success: function(e) {
                var author = $('input#author');
                var email = $('input#email');
                var comment = $('textarea#comment');
                if (author.length && author.val() == '') {
                    $.scrollTo(author, 0, {
						offset: {top:-100}, 
						onAfter: function() { author.focus() }
					});
                } else if (email.length && email.val() == '') {
                    $.scrollTo(email, 0, {
						offset: {top:-100}, 
						onAfter: function() { email.focus() }
					});
                } else if (comment.length) {
                    $.scrollTo(comment, 0, { 
						offset: {top:-100}, 
						onAfter: function() { comment.focus() }
					});
                };
            }
        },
        {
             keys: ['g', 'h'], 
             success: function(e) {
                var homepage = $('link[rel=index]');
                if (homepage.length) {
                    window.location = homepage.attr('href');
                }
            }
        },
        {
             keys: ['g','s'], 
             success: function(e) {
                var searchform = $('input#s');
                if (searchform.length) {
                    $.scrollTo(searchform, 0, { onAfter: function() {
                        searchform.focus() }
                    });
                }
            }
        }
    ], function() {
        return false;
    });

    $(':input').keydown(function(e) {
        if (e.which == 27) {
            this.blur();
            return false;
        }
    });
});
