﻿// Used to keep track of the tab that is visible
var visibleTab = null;

function pageload(hash) {
    if (hash) {
        // Not sure what this does, it came from the jQuery.hostory instructions
        if ($.browser.msie) {
            hash = encodeURIComponent(hash);
        }
        // Hide the currently visible tab and tab panel if there is one
        if (visibleTab != null) {
            $("#marquis-rotator .selected").fadeOut(650).removeClass("selected").css("display", "none");
            $("#marquis_nav li a").removeClass("selected");
        }
        // Show the new tab and tab panel
        $("#marquis-rotator #tabPnl_" + hash).fadeIn(650).addClass("selected");
        $("#" + hash + "_tab a").addClass("selected");
        InitialiseScrollableArea()
        visibleTab = hash;
    } else {
        // Hide the currently visible tab and tab panel if there is one
        if (visibleTab != null) {
            $("#marquis-rotator .selected").fadeOut(650).css("display", "none").removeClass("selected");
            $("#marquis_nav a.selected").removeClass("selected");
        }
        // Show the new tab and tab panel
        $("#marquis-rotator li:first").fadeIn(650).addClass("selected");
        // The tabs are floated right which causes them to reverse visually so the last tab physicaly is the first visually
        $('#marquis_nav li:last a').addClass("selected");
        visibleTab = $('#marquis-rotator li:first').attr("id").replace("tabPnl", "");
        InitialiseScrollableArea()
    }
}

// Calls pageload in the situation where history does not
function forcedPageload() {
    if (location.hash == "") {
        pageload(null);
    }
}

$(document).ready(function() {
    // Hide all of the tab panels
    $('#marquis-rotator > ul > li').css("display", "none");
    // Setup the history object
    $.historyInit(pageload);
    // Call this because if you directly to the page with no hash then history wont call pageload.
    forcedPageload();
    // Bind all the click events on the tabs
    $("a[rel='history']").click(function() {
        var hash = this.href;
        hash = hash.replace(/^.*#/, '');
        $.historyLoad(hash);
        return false;
    });
});
