$(document).ready(function() {
    
    // Navigation Dropdown
    
    // Events & Exhibitions - Dropdown
    $('#menu-item-2152').hover(
        function() {
            $('#subNavEventsAndExhibitions').show();
        },
        function() {
            $('#subNavEventsAndExhibitions').hide();          
        }
    )
    
    // Artists - Dropdown
    $('#menu-item-2153').hover(
        function() {
            $('#subNavArtists').show();
        },
        function() {
            $('#subNavArtists').hide();          
        }
    )
    
    // Books & Editions - Dropdown
    $('#menu-item-2150').hover(
        function() {
            $('#subNavBooksAndEditions').show();
        },
        function() {
            $('#subNavBooksAndEditions').hide();          
        }
    )
    
    // Toggle Newsletter Form Visibility
    $('#newsletter').hide();
    $('a#newsletterAnchor').click(function() {
        $('#newsletter').fadeToggle('fast', 'linear');
    })

    // Keyboard Navigation Because I hate My Mighty Mouse. Or Magic Mouse. Or Whatever It's Called Now.
    // Vertical Keyboard Navigation - Scroll Down
    function scrollDown() {
        var scroll_position = $(window).scrollTop();
        $('.entry').each(function(i, div) {
            div_top = $(div).offset().top;
            if (scroll_position < div_top) {
                anchor_hash = $(this).attr("id")
                $.scrollTo(div, 800, {
                    onAfter:function() {
                        document.location.hash = anchor_hash;
                    }
                });
                return false; 
            }
        });
    }
    // Vertical Keyboard Navigation - Scroll Up
    function scrollUp() {
        var scroll_position = $(window).scrollTop();
        var scrollToThis = null;
        $('.entry').each(function(i, div) {
            div_top = $(div).offset().top;
            if (scroll_position > div_top) {
                scrollToThis = div;
                anchor_hash = $(this).attr("id");
            } else {
                return false;
            }
        });

        if(scrollToThis != null) {
            $.scrollTo(scrollToThis, 800, {
                onAfter:function() {
                    document.location.hash = anchor_hash;
                }
            });
        }
    }

    // Horizontal Keyboard Pagination
    function pageNext() {
        var x = $("span.pageNext a").attr("href");
        window.location = x;
    }
    
    function pagePrev() {
        var y = $("span.pagePrev a").attr("href");
        window.location = y;
    }
    
    // Map Functions To Keyboard Events
    shortcut.add("down", function() {
        scrollDown();
    });
    shortcut.add("up", function() {
        scrollUp();
    });
    shortcut.add("right", function() {
        pageNext();
    });
    shortcut.add("left", function() {
        pagePrev();
    });
    
    // Disable keyboard navigation when text input areas are in focus.
    if ($('input, textarea').focus() == true) {
        shortcut.remove("down");
        shortcut.remove("up");
        shortcut.remove("right"); 
        shortcut.remove("left"); 
    };

});
