//Conventions:
// Functions are form someFunctionName;
// Variables are lower case, prefixed g_ for global, l_ for local, and a_ for arguments



// Ok, so the page has just loaded for the first time
$(document).ready(function() {

    setupLinks();

   //Now, we need to set up dealing with hashchanges, which are how we
   // navigate between posts
   $(window).hashchange( function(){
        changeContent();
    });

    // Now, let's get some post data
   firstLoad();

 });



function changeContent()
{

    var l_hash = location.hash.replace( /^#/, '' );

    if (!l_hash)
    {
        l_hash = "music";
    }

    if (l_hash)
    {
        var l_components = l_hash.split("/");

        if (l_components.length == 1)
        {
            if (l_components[0] != g_section)
            {
                $('a.selectedmenuitem').attr("class", "menuitem");
                //We have a section but no subsection - simply load that section.
                loadPage(l_components[0]);
            }
            else
            {
                $('a.selectedsubmenuitem').attr("class", "submenuitem");
                $('#'+g_subsection+'.item').fadeOut(250, function(){
                        $('#default').fadeIn(250);
                    });
                g_subsection = "default";
            }
        }
        else if (l_components.length == 2)
        {
            if (l_components[0] == g_section)
            {
                if (l_components[1] != g_subsection)
                {
                    $('a.selectedsubmenuitem').attr("class", "submenuitem");

                    $('#'+g_subsection).fadeOut(250, function(){
                        $('#'+l_components[1]).fadeIn(250);
                        $('a#submenuitem'+l_components[1]).attr("class", "selectedsubmenuitem")});
                    g_subsection = l_components[1];
                }
            }
            else
            {
                $('a.selectedmenuitem').attr("class", "menuitem");
                l_nopretty = true;
                loadPage(l_components[0], l_components[1]);
            }
        }

        _gaq.push(['_trackPageview', l_hash]);

    }   
}

function setupLinks(a_notall)
{
    $(a_notall ? '#submenu a, a.closer' : '#menu a, #submenu a, a.closer').each(function(){
        var l_href = $(this).attr("href");

        l_href = "#" + l_href.replace(g_root, "");
            $(this).attr("href", l_href);        
    });

}

var g_fadeoutcomplete = false;

function loadPage(a_page, a_subpage)
{
    g_fadeoutcomplete = false;
    $('#content').fadeOut(250, function() {$('#submenu').fadeOut(250, function() {g_fadeoutcomplete = true;})});
    $.get(g_root + 'ajax/' + a_page + (a_subpage ? '/'+a_subpage : ""), function (a_data)  {
        spinPage(a_data, a_page);
    });

    g_section = a_page;
    g_subsection = a_subpage ? a_subpage : "default";
}

function spinPage(a_data, a_page)
{
    if ($('#dummy:visible').length == 1)
    {
        $('#dummy').fadeOut(100, function()
        {
            if (g_fadeoutcomplete)
            {
                showPage(a_data, a_page);
            }
            else
            {
                spinPage(a_data, a_page);
            }
        });
    }
    else
    {
        $('#dummy').fadeIn(100, function()
        {
            if (g_fadeoutcomplete)
            {
                showPage(a_data, a_page);
            }
            else
            {
                spinPage(a_data, a_page);
            }
        });
    }
}


function spinPage2()
{
    if ($('#dummy:visible').length == 1)
    {
        $('#dummy').fadeOut(100, function()
        {
            if (g_firstloadcomplete)
            {
                prettyFade();
            }
            else
            {
                spinPage2();
            }
        });
    }
    else
    {
        $('#dummy').fadeIn(100, function()
        {
            if (g_firstloadcomplete)
            {
                prettyFade();
            }
            else
            {
                spinPage2();
            }
        });
    }
}

function showPage(a_data, a_page)
{
    $('#swapout').hide();
    $('#swapout').html(a_data);
    $('#content').hide();
    $('#submenu').hide();
    $('#swapout').show();

    setupLinks(true);

    $('a#menuitem'+a_page).attr("class", "selectedmenuitem");
    $('#submenu').fadeIn(250, function() {
        $('#content').fadeIn(250);
    });
}

var g_firstloadcomplete = true;
function firstLoad()
{

    var l_hash = location.hash.replace( /^#/, '' );
    
    if (l_hash != "")
    {
        var l_components = l_hash.split("/");
        var l_newsection = l_components.length > 0 ? l_components[0] : g_section;
        var l_newsubsection = l_components.length > 0 ? (l_components.length > 1 ? l_components[1] : "default") : g_subsection;

        if (l_newsection != g_section)
        {
            g_firstloadcomplete = false;
            $('a.selectedmenuitem').attr("class", "menuitem");
            $('a#menuitem'+l_newsection).attr("class", "selectedmenuitem");
            g_section = l_newsection;
            g_subsection = l_newsubsection;

            $.get(g_root + 'ajax/' + l_newsection + '/'+l_newsubsection, function (a_data)  {
                $('#swapout').hide();
                $('#swapout').html(a_data);
                $('#content').hide();
                $('#submenu').hide();
                $('#swapout').show();

                setupLinks(true);
                g_firstloadcomplete = true;
            });

            spinPage2();
        }
        else if (l_newsubsection != g_subsection)
        {
            $('#'+g_subsection).hide();
            $('#'+l_newsubsection).show();
            $('a.selectedsubmenuitem').attr("class", "submenuitem");
            $('a#submenuitem'+l_newsubsection).attr("class", "selectedsubmenuitem");
            g_subsection = l_newsubsection;
            prettyFade();

        }
        else
        {
            prettyFade();
        }

    }


    $('#logo, #subheader, #divider, #menu, #submenu, #content').hide();
    $('#outermost').show();

    if (l_hash == "")
    {
        prettyFade();
    }
}

function prettyFade()
{
    var l_array = ["logo","subheader", "divider", "menu", "submenu", "content"];
    var l_counter = 0;
    showNext(l_array, l_counter, 250);
}

function showNext(a_array, a_counter, a_fadeTime)
{
    if (a_array.length > a_counter)
    {
        var l_element = $('#'+ a_array[a_counter]);
        l_element.fadeIn(a_fadeTime, function()
        {
            if (!l_element.is(":visible"))
            {
                l_element.fadeIn(a_fadeTime, function(){showNext(a_array, a_counter + 1, a_fadeTime);})
            }
            else
            {
                showNext(a_array, a_counter + 1, a_fadeTime);

            }
        });
        //l_element.show();
        //showNext(a_array, a_counter + 1, a_fadeTime);
    }
}


