$(document).ready(function(){

    /**
     * Resize the main content container when the window resizes.
     */
    $(window).resize(function(){
        if ($(window).width() >= $(document).width()) {
            var rightMargin = $('#scroller').position().left + 1;
        } else {
            var rightMargin = 0 - $(document).scrollLeft();
        }
        $('.menu-button').css('right', rightMargin);
    });
    $(document).scroll(function(){
        $(window).triggerHandler('resize');
    });

    var resizeScroller = function() {
        var contentHeight = $('#scroller').height();
        var imageHeight = $('#scroller .content-pic').height();
        var margins =  parseInt($('#scroller').css('margin-top')) + parseInt($('#scroller .content .scroll').css('margin-top'));
        $('#scroller .content .scroll').height(contentHeight - imageHeight - margins - 8);
    };
    setTimeout(function(){
        resizeScroller();
    }, 2000);


    var menuState = 1;
    /**
     * Shows/hides the right sidebar menu
     */
    var toggleMenu = function() {
        if (menuState) {
            // Save original values
            $('#right').data('oldWidth', $('#right').css('width'));
            $('#left').data('oldMargin', $('#left').css('margin-right'));

            // Minimize menu, expand main area
            $('#right').animate({
                width: '20px'
            }, 'fast', 'linear', function(){
                if ($(window).width() >= $(document).width()) {
                    var rightMargin = $('#scroller').position().left + 1;
                } else {
                    var rightMargin = 0 - $(document).scrollLeft();
                }
                $('.menu-button').css('right', rightMargin).fadeIn();
            });
            $('#left').animate({
                marginRight: '25px'
            }, 'fast');
            $('#menu').animate({
                width: '0px',
                opacity: '0'
            }, 'fast', 'linear', function(){
                // Hide the menu after fading out so that it doesn't still
                // take up space while invisible.
                $('#menu').hide();
                // Resize the text area
                setTimeout(function(){
                    resizeScroller();
                }, 2000);
            });
        } else {
            // Restore menu
            $('.menu-button').hide();
            $('#right').animate({
                width: $('#right').data('oldWidth')
            }, 'fast');
            $('#left').animate({
                marginRight: $('#left').data('oldMargin')
            }, 'fast');
            $('#menu').animate({
                width: '100%',
                opacity: '1'
            }, 'fast');
        }
        menuState = !menuState;
    }
    
    

    /**
     * Respond to a click on a sidebar menu item:
     *  - hide the menu sidebar
     *  - load the new content through AJAX
     *  - transition out the old main content
     *  - transition in the new main content
     */
    $('#menu a').click(function(e){
        // The new AJAX content is located in the <A> link's href
        var src = this.href;
        // Load the content by AJAX
        $.ajax({
            type: 'GET',
            dataType: 'html',
            url: src,
            success: function(data){
                // Temporarily hide the scrollbar so the browser won't get
                // confused about the width/margins 
                $('#scroller .content-text .scroll').css('overflow-y', 'hidden');
                // Hide the old content
                $('#left .content img').fadeTo('fast', 0.10);
                $('#left .content').hide('drop', { direction: 'down' }, 'normal', function(){
                    
                    // Show the new content
                    $('#left .content').html(data).show('drop', { direction: 'up' }, 'slow');
                    $('#left .content img').fadeTo(2000, 1.00);
                    // PNG fix
                    if ($.browser.msie && parseInt($.browser.version) <= 6) {
                        DD_roundies.addRule('.content-pic img', '30px');
                    }
                    // Show the scrollbar again
                    $('#scroller .content-text .scroll').css('overflow-y', 'auto');
                });
                toggleMenu();
            }
        });    
        return false;
    });
    
    $('.menu-button a').click(function(){
        var override = $(this).data('override');
        if (override) {
          override();
        }
        else {
          toggleMenu();
        }
        return false;
    });


    $('#audio-icon').data('isPlaying', false);
    $('#audio-icon').click(function(){
        var isMp3Playing = $(this).data('isPlaying');
        if (isMp3Playing) {
            pause();
        } else {
            play();
        }
        //$(this).data('isPlaying',  !isMp3Playing);
    });


    // Test function.
    // Animates the content area when you click the title logo.
    $('#title').click(function(){
        // Temporarily hide the scrollbar so the browser won't get
        // confused about the width/margins 
        $('#scroller .content-text .scroll').css('overflow-y', 'hidden');
        // Hide the old content
        $('#left .content').hide('drop', { direction: 'down' }, 'fast', function(){
            
            // Load the new content
            $('#left .content').show('drop', { direction: 'up' })
            // PNG fix
            if ($.browser.msie && parseInt($.browser.version) <= 6) {
                DD_roundies.addRule('.content-pic img', '30px');
            }
            // Show the scrollbar again
            $('#scroller .content-text .scroll').css('overflow-y', 'auto');
        });
    });

});

