$(document).ready(function() {
    
    /*
    $('img').lazyload({
        effect: "fadeIn"
    });
    */

    $('#sidebar img').css({opacity: 0.5});
    $('#sidebar img').hover(function() {
            $(this).fadeTo('fast', 1.0);
        }, function() {
            $(this).fadeTo('fast', 0.5);
        })
    
    // Nth-Child Selector for E&E and Artists overview.
    // $('div.ee:nth-child(3n+3)').addClass('omega');   Use if title is absent. ^DK.
    $('div.ee:nth-child(3n+4)').addClass('omega');      // Use if title is present. ^DK.
    // Navigation Dropdown
    
    // Events & Exhibitions - Dropdown 
    
    $('#menu-item-2154').hover(
        function() {
            $('#subNavArtists').removeClass('visible');
            $('#subNavBooksAndEditions').removeClass('visible');
            $('#subNavEventsAndExhibitions').addClass('visible');
        }
    )
    
    $('#subNavEventsAndExhibitions').mouseleave(
        function() {
            $('#subNavEventsAndExhibitions').removeClass('visible');
        }
    );
    
    
    // Artists - Dropdown
    $('#menu-item-2152').hover(
        function() {
            $('#subNavEventsAndExhibitions').removeClass('visible');
            $('#subNavBooksAndEditions').removeClass('visible');
            $('#subNavArtists').addClass('visible');
        }
    )
    
    $('#subNavArtists').mouseleave(
        function() {
            $('#subNavArtists').removeClass('visible');
        }
    );
    
    // Books & Editions - Dropdown
    $('#menu-item-2153').hover(
        function() {
            $('#subNavEventsAndExhibitions').removeClass('visible');
            $('#subNavArtists').removeClass('visible');
            $('#subNavBooksAndEditions').addClass('visible');
        }
    )
    
    $('#subNavBooksAndEditions').mouseleave(
        function() {
            $('#subNavBooksAndEditions').removeClass('visible');
        }
    );
    
    $('div.oNavigation').mouseleave(
        function() {
            $('#subNavEventsAndExhibitions').removeClass('visible');
            $('#subNavArtists').removeClass('visible');
            $('#subNavBooksAndEditions').removeClass('visible');
        }
    );
    
    $('#menu-item-2150').hover(
        function() {
            $('#subNavEventsAndExhibitions').removeClass('visible');
            $('#subNavArtists').removeClass('visible');
            $('#subNavBooksAndEditions').removeClass('visible');
        }
    );
    
    $('#menu-item-2155').hover(
        function() {
            $('#subNavEventsAndExhibitions').removeClass('visible');
            $('#subNavArtists').removeClass('visible');
            $('#subNavBooksAndEditions').removeClass('visible');
        }
    );
    
    $('#menu-item-2151').hover(
        function() {
            $('#subNavEventsAndExhibitions').removeClass('visible');
            $('#subNavArtists').removeClass('visible');
            $('#subNavBooksAndEditions').removeClass('visible');
        }
    );
    
    // Toggle Newsletter Form Visibility
    $('#newsletter').hide();
    $('a#newsletterAnchor').click(function() {
        $('#newsletter').fadeToggle('fast', 'linear');
    })

    // Vertical Keyboard Navigation. Because I hate the Mighty Mouse. Or Magic Mouse. Or whatever It's called now.
    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; 
            }
        });
    }
    
    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;
                }
            });
        }
    }
    
    shortcut.add("down", function() {
        scrollDown();
    });
    shortcut.add("up", function() {
        scrollUp();
    });

    // Horizontal Keyboard Navigation.
    $("span.right a").addClass("pageNext");
    $("span.left a").addClass("pagePrev");
    
    function pageNext() {
        var x = $("span.right a").attr("href");
        window.location = x;
    }
    
    function pagePrev() {
        var y = $("span.left a").attr("href");
        window.location = y;
    }
    
    if ($("a.pageNext").length) { 
        shortcut.add("right", function() {
            pageNext();
        });
    }
    
    if ($("a.pagePrev").length) {
        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"); 
    };

});
