function loadContents(link) {
    $('#content').load(link);
}
// Set the events for ajax loading.
function setAjaxLink () {
    $.each($("#menu li"), function (i, li) {
        $('a', li).click(function (e) {
            if ($(this).attr('target') != '_blank') {
                var link = $(this).attr('href');
                var ul = $(this).parent().parent();
                var li = $(this).parent();
                // Rewrite the hash
                window.location.hash = link
                // Hilight selected.
                ul.find('li').removeAttr('class');
                li.attr('class', 'current_page_item');
                // Load contents
                loadContents(link);
                return false;
            }
        });
    });
}

function loadUri() {
    var link = window.location.hash.slice(1);
    var mapping = {
        '/home' : '/home',
        '/live' : '/live',
        '/member': '/member',
        '/links': '/links'
    };
    if (typeof(mapping[link]) != 'undefined') {
        return mapping[link];
    }
    else {
        return '/home';
    }
}

function highlightSelected(uri) {
    $.each($("#menu li"), function (i, li) {
        $(li).removeAttr('class'); 
        if ($('a', li).attr('href') == uri) {
            $(li).attr('class', 'current_page_item');
        }
    });
}

google.load('jquery', '1.3.2', {uncompressed:false});
google.setOnLoadCallback(function () { // Called after jQuery is loaded.
    $(function () {
        var uri = loadUri()
        loadContents(uri);
        highlightSelected(uri);
        setAjaxLink();
        setTimeout(function () { // Display contents.
            $('#message').hide();
            $('#main-contents').fadeIn('slow');
        }, 500);
        return false;
    });
});
