﻿var lastHash = "";

var panelNames = new Array("#ctl00_mainContent_pnlEventArriveDrive",
                    "#ctl00_mainContent_pnlEventSuperPrix",
                    "#ctl00_mainContent_pnlEventGrandPrix",
                    "#ctl00_mainContent_pnlEventRaceNight",
                    "#ctl00_mainContent_pnlEventMiniEndurance",
                    "#ctl00_mainContent_pnlEventTeamEndurance",
                    "#ctl00_mainContent_pnlEventJuniorKarting",
                    "#ctl00_mainContent_pnlEventExclusive",
                    "#ctl00_mainContent_pnlEventTuition",
                    "#ctl00_mainContent_pnlEventVoucher",
                    "#ctl00_mainContent_pnlEventIndividualEndurance",
                    "#ctl00_mainContent_pnlEventIntermediate");
var arrowClasses = new Array(   "arrivedrive",
                                "superprix",
                                "grandprix",
                                "racenight",
                                "miniendurance",
                                "teamendurance",
                                "junior",
                                "exclusive",
                                "tuition",
                                "vouchers",
                                "indiendurance",
                                "intermediate");
var hyperlinks = new Array( "#ctl00_mainContent_hypArriveDrive",
                            "#ctl00_mainContent_hypSuperPrix",
                            "#ctl00_mainContent_hypGrandPrix",
                            "#ctl00_mainContent_hypRaceNight",
                            "#ctl00_mainContent_hypMiniEnduro",
                            "#ctl00_mainContent_hypTeamEnduro",
                            "#ctl00_mainContent_hypJunior",
                            "#ctl00_mainContent_hypExclusive",
                            "#ctl00_mainContent_hypTuition",
                            "#ctl00_mainContent_hypVouchers",
                            "#ctl00_mainContent_hypInidividualEnduro",
                            "#ctl00_mainContent_hypIntermediate");
var hashes = new Array( "arrivedrive",
                        "superprix",
                        "grandprix",
                        "racenight",
                        "minienduro",
                        "teamenduro",
                        "junior",
                        "exclusive",
                        "tuition",
                        "vouchers",
                        "indienduro",
                        "intermediate");


function showContent(contentIndex)
{
    hideAllPanels();
    $(panelNames[contentIndex]).css("display", "block");
    $(hyperlinks[contentIndex]).addClass("selected");
    $("#ctl00_mainContent_pnlArrow").css("display", "block");
    $("#ctl00_mainContent_pnlArrow").addClass("event-selector-arrow " + arrowClasses[contentIndex]);
    lastHash = "#" + hashes[contentIndex];
    window.location.hash = hashes[contentIndex];
    scrollToContent();
    callReplaceTrack();
}

function resetContent()
{
    hideAllPanels();
    $(panelNames[0]).css("display", "block");
    $(hyperlinks[0]).addClass("selected");
    $("#ctl00_mainContent_pnlArrow").css("display", "block");
    $("#ctl00_mainContent_pnlArrow").addClass("event-selector-arrow " + arrowClasses[0]);
    callReplaceTrack();
    
}

function hideAllPanels()
{
    for (panel in panelNames)
    {
        $(panelNames[panel]).css("display", "none");
    }
    
    for (hyp in hyperlinks)
    {
        $(hyperlinks[hyp]).removeClass("selected");
    }
    
    $("#ctl00_mainContent_pnlArrow").css("display", "none");
    $("#ctl00_mainContent_pnlArrow").removeClass();
}

$(document).ready(function () {

    $(hyperlinks[0]).click(function (event) { showContent(0); event.preventDefault(); });
    $(hyperlinks[1]).click(function (event) { showContent(1); event.preventDefault(); });
    $(hyperlinks[2]).click(function (event) { showContent(2); event.preventDefault(); });
    $(hyperlinks[3]).click(function (event) { showContent(3); event.preventDefault(); });
    $(hyperlinks[4]).click(function (event) { showContent(4); event.preventDefault(); });
    $(hyperlinks[5]).click(function (event) { showContent(5); event.preventDefault(); });
    $(hyperlinks[6]).click(function (event) { showContent(6); event.preventDefault(); });
    $(hyperlinks[7]).click(function (event) { showContent(7); event.preventDefault(); });
    $(hyperlinks[8]).click(function (event) { showContent(8); event.preventDefault(); });
    $(hyperlinks[9]).click(function (event) { showContent(9); event.preventDefault(); });
    $(hyperlinks[10]).click(function (event) { showContent(10); event.preventDefault(); });
    $(hyperlinks[11]).click(function (event) { showContent(11); event.preventDefault(); });

    setInterval("checkHash()", 250);
});

function scrollToContent()
{
    $target = $("#content");
    var targetOffset = $target.offset().top + 50;
    $("html,body").stop().animate({scrollTop: targetOffset}, 1000);
}

function checkHash()
{
    var hash = window.location.hash;
    
    if (hash != lastHash)
    {
        
        // Page changed.
        if (hash == "")
        {
            //resetContent();
            pageIndex = $("#ctl00_mainContent_hdnStartPage").val();
            showContent(pageIndex);
        }
        else
        {
            var realHash = hash.substring(1);
            var arrayIndex = jQuery.inArray(realHash, hashes);
            if (arrayIndex != -1)
            {
                showContent(arrayIndex);
            }
        }
    }
}