function fixTopbar(topbar, location) {
    if (topbar !== undefined) {
        if (location >= $(window).scrollTop()) {
            if (topbar.hasClass('fixed'))
                topbar.removeClass('fixed');
        } else {
            if (!topbar.hasClass('fixed'))
                topbar.addClass('fixed');
        }
    }
}

// Section fragment identifier
var hash;

if (window.location.hash) {
    hash = window.location.hash;
    window.location.hash = '';
}

$(document).ready(function() {
    // Browser upgrade nag
    if ($.browser.mozilla && $.browser.version < 6) {
        $('div#nag').css('display', 'block');
        $('div#nag .browser').text('Mozilla Firefox');
        $('div#nag .upgrade').attr('href', 'http://www.mozilla.org/en-US/firefox/update/');
    }

    if ($.browser.msie && $.browser.version < 9) {
        $('div#nag').css('display', 'block');
        $('div#nag .browser').text('Internet Explorer');
        $('div#nag .upgrade').attr('href', 'http://windows.microsoft.com/en-US/internet-explorer/downloads/ie');
        $('div#nag .upgrade').parent().append(' You should also consider switching to a browser that releases upgrades more frequently, like <a href="http://google.com/chrome">Google Chrome</a>.');
    }

    if (hash != undefined) {
        var loadScroll = function() { 
            $.scrollTo($(hash), 2000); 
            window.location.hash = hash;    
        };
        setTimeout(function() { loadScroll() }, 1000);
    }

    $.localScroll();

    Section.setup();
    
    $('.viewable').viewer();

    var stage = $('#stage');
    
    var about              = ($('#content.about').length != 0) ? true : false;
    var foundation         = ($('#project.foundation').length != 0) ? true : false;
    var strangeAttractors  = ($('#project.strange_attractors').length != 0) ? true : false;
    var schoolOfDesign     = ($('#project.school_of_design').length != 0) ? true : false;
    var strand             = ($('#project.strand').length != 0) ? true : false;
    var pinterest          = ($('#project.pinterest').length != 0) ? true : false;
    var hasTopbar          = ($('#topbar').length != 0) ? true : false;
    var hasSections        = ($('#topbar nav.right').length !== 0) ? true : false; 
    
    if (hasTopbar) {
        var topbar = $('#topbar');
        var location = topbar.position().top;
    }
    
    if ($.browser.mozilla && (foundation || strangeAttractors)) {
        $('#stage h1').css('font-weight', 500);
    }
    
    if (foundation) {
        // Interactive Circles
        $('.circle').hover(function() {
            var circleContent = $('.callout_content .' + $(this).attr('data-num')); 
            
            circleContent.css('display', 'block');
            circleContent.animate({'opacity' : '1'}, 500);
        }, function() {
            var circleContent = $('.callout_content .' + $(this).attr('data-num')); 
            circleContent.animate({'opacity' : '0'}, 500);
        });
        
        // Interactive Parsel Tester
        updatePlaceholder();
        setInterval("updatePlaceholder()", 4000);
        
        $('#parsel a#send').click(function() {
            sendParsel();
            return false;
        });
        
        $('#parsel .input').keydown(function(e) {
            if (e.which == 13) {
                sendParsel();
                return false;
            }
        });
        
        $('#parsel .input .reset').click(function() {
            resetParsel();
            return false;
        });
    }
    
    if (strangeAttractors || strand) {
        function fadeInCanvas() {
            var canvas = $('#stage canvas'); 
            canvas.css('display', 'block'); 
            canvas.animate({ 'opacity': 1 }, 750);
        }
    
        setTimeout(function() { fadeInCanvas(); }, 250);
    }    
    
    if (pinterest) {
        var pinSlide = function() {
            var count = $('#pinboard ul').children().length;
            
            $('#pinboard ul').children().each(function() {
                var that = $(this);
                setTimeout(function() { that.addClass('moved_up') }, 100 * $(this).index());
            });
        };
    
        setTimeout(function() { pinSlide() }, 500);
    }
    
    $('.fullscreen').live('click', function() {
        var parent = $(this).parent();
        var current = $(this).siblings('.parsel_slideshow').children('.parsel_slideshow_images').children('.parsel_current_slide');
        var index = current.index();
        
        parent.viewer('invoke', index, parent);
    });
    
    $(window).scroll(function() {
        // Topbar --------------------------------------------------------------------- */
        fixTopbar(topbar, location);
                
        if (hasSections) {
            Section.changeSection();
        }
        
        // About Page ----------------------------------------------------------------- */
        if (about)
            Parallax.slideUp($(this), $('#user_photo img'), 'top', 300);
        
        // Foundation ----------------------------------------------------------------- */
        if (foundation) {
            // Perform the slide animation for the stage.
            Parallax.slideDown($(this), stage, stage.children('h1'), 50);

            var philosophy = $('#philosophy');               
            if (philosophy.position().top <= $(window).scrollTop() + 20) {            
                $('.callout .circle').each(function() {
                    var that = $(this);
                    var index = that.index();
                    
                    function animate(that) {
                        that.addClass('bounceIn animated');
                    }
                    
                    setTimeout(function() { animate(that); }, 700 * index);
                });
            }    
        }
        
        // Strange Attractors --------------------------------------------------------- */
        if (strangeAttractors) {
            Parallax.slideUp($(this), stage.children('canvas'), 'margin-top');
            Parallax.slideDown($(this), stage, stage.children('h1'), 20);
        }
        
        // Strand --------------------------------------------------------------------- */
        if (strand) {
            Parallax.slideUp($(this), stage.children('canvas'), 'margin-top');
            Parallax.slideDown($(this), stage, stage.children('h1'), 40);
        }

        
        // School of Design ----------------------------------------------------------- */
        if (schoolOfDesign) {
            Parallax.slideDown($(this), stage, stage.children('.title'), 0);
            Parallax.slideDown($(this), $(), stage.children('#overlay'), 0);
        }

        // School of Design ----------------------------------------------------------- */
        if (pinterest) {
            Parallax.slideUp($(this), stage.children('h1'), 'top', -50);
            Parallax.slideUp($(this), stage.children('#pinboard'), 'margin-top');
        }
    });
});
