String.prototype.startsWith = function(str){
    return (this.indexOf(str) === 0);
}

var cache = {};
var topPageSlideSpeed = 'medium';

function hideTopPage() {
    window.location.hash = '';

    updateTopPageFromCache();
    resetNavigation();
}

function updateTopPageFromCache(url) {
    // Hide other top pages
    $('#top_page .content:visible').slideUp(topPageSlideSpeed);

    if(!url)
        return;

    // Prepend the content to the parent container
    cache[url].prependTo('#top_page');

    // Show the new page
    cache[url].slideDown(topPageSlideSpeed);

    // Fire an "update_complete" event
    var e = $.Event("update_complete");
    $("body").trigger(e, [window.location.hash.substring(2)]);
}

function showTopPageLoading() {
    // Hide other top pages
    $('#top_page .content:visible').slideUp(topPageSlideSpeed);
    $('#top_page .loading').slideDown(topPageSlideSpeed);
}

function initNavigation() {
    $('#nav li a[href^=#]').each(function() {
        var $this = $(this);
        var $parent = $this.parent();

        // Add a down arrow
        $parent.prepend('<div class="arrow" class="js_center_parent" />');

        //var $arrow = $this.siblings('.arrow');
        //$arrow.css('left', $parent.outerWidth()/2);

        return true;
    });
}

function resetNavigation() {
    $('#nav li.active').removeClass('active');

    $('#nav li').each(function(index, value) {
        elem = $(value)
        a = elem.children('a');

        if(window.location.pathname.startsWith(a.attr('href'))) {
            elem.addClass('active');

            // Stop here, we've already found our item
            return false;
        }

        return true;
    });
}

$(function() {
    initNavigation();
    resetNavigation();

    $(".js_center_parent").each(function() {
        var $this = $(this);
        var $parent = $this.parent();

        $this.css('left', ($parent.outerWidth()/2) - ($this.outerWidth()/2));
    });

    $("header .logo").hover(function() {
        $(this).children("h1.normal_text").slideToggle(100);
        $(this).children("h1.hover_text").slideToggle(100);
    });

    var ignoreNextPageClick = false;

    $('#nav li a').click(function() {
        ignoreNextPageClick = true;

        if(!$(this).parent().hasClass('active'))
            return true;

        hideTopPage();

        // Don't navigate to the href
        return false;
    });

    // If the page content is clicked,
    $('#page_content').click(function() {
        if(ignoreNextPageClick) {
            ignoreNextPageClick = false;
            return true;
        }

        $('#nav li.active a[href^=#]').trigger('click');
        return true;
    });

    $(window).bind('hashchange', function(e) {
        var url = e.fragment; // no leading #

        if(!url)
            return;

        $('#nav li.active').removeClass('active');
        $('a[href="#' + url + '"]').parent().addClass('active');

        url = url.substring(1); // remove the exclamation mark

        if(cache[url]) {
            updateTopPageFromCache(url);
        } else {
            showTopPageLoading();

            cache[url] = $('<div class="content" />')

            // Don't show it until we've loaded
            .hide()

            // Load external content via AJAX
            .load(url, function() {
                // Hide loading text
                $('#top_page .loading').hide();

                // Show us, hide others, etc.
                updateTopPageFromCache(url);
            });
        }
    })

    // Since the event is only triggered when the hash changes, we need to
    // trigger the event now, to handle the hash the page may have loaded with.
    .trigger('hashchange');
});

