ScriptLoader = {
    Ready: false,
    QueuedWork: [],

    LoadScripts: function () {
        jQuery.getScript("/js/jquery.hoverIntent.minified.js", function () {
            jQuery.getScript("/js/superfish.js", function () {
                jQuery.getScript("/js/supersubs.js", function () {
                    ScriptLoader.Ready = true;
                    while (ScriptLoader.QueuedWork.length > 0) {
                        var workItem = ScriptLoader.QueuedWork.pop();
                        ScriptLoader.Apply(workItem);
                    }
                });
            });
        });
    },

    Apply: function (func) {
        if (!ScriptLoader.Ready) {
            ScriptLoader.QueuedWork.push(func);
        }
        else {
            func();
        }
    }
}


$(document).ready(function ()
{
    jQuery('#footer').load('/Content/FooterContent.html');

    if (jQuery('#rotatingImages').length > 0)
    {
        setInterval(rotateImages, 5000);
    }

    jQuery('#header').load('/Content/HeaderContent.html', function ()
    {
        jQuery('#topNav').load("/Content/MenuContent.html", function ()
        {
            ScriptLoader.Apply(function ()
            {
                jQuery("#topNav>ul.sf-menu").supersubs({
                    minWidth: 2,
                    maxWidth: 27,
                    extraWidth: 1})
                    .superfish(
                    {
                        animation: { height: 'show' },
                        autoArrows: true,
                        dropShadows: false
                    });
            });
        });
    });


    if (jQuery('#sidebarNav').length > 0)
    {
        var menuUri = ['/Content/MenuContent.html #', sectionName, '>UL'].join('');

        jQuery('#sidebarNav').load(menuUri, function ()
        {
            var imgHTML = ["<img alt='", sectionName, " Title Image' src='/images/LeftSidebarTitles-", sectionName, ".jpg' />"].join("");
            jQuery('#sidebarNav').prepend(imgHTML);
            jQuery('#sidebarNav>UL').addClass('sf-menu sf-vertical');

            var currentPageSelector = '#sidebarNav #' + pageName;
            var parentLiSelector = ["#sidebarNav li:has(#", pageName, ")"].join("");
            var selector = [currentPageSelector, parentLiSelector].join(", ");
            jQuery(selector).addClass('currentPage');

//            ScriptLoader.Apply(function ()
//            {
//                $('#sidebarNav>UL').superfish(
//                {
//                    animation: { height: 'show' },
//                    pathClass: 'currentPage',
//                    speed: 'slow'
//                });
//            });
        });


    }

    ScriptLoader.LoadScripts();
});

function rotateImages()
{
    var currentImage = $('#rotatingImages img:visible');
    var nextImage = currentImage.next();
    if (nextImage.length == 0)
    {
        nextImage = $('#rotatingImages img:first');
    }

    currentImage.fadeToggle(2000);
    nextImage.fadeToggle(2000);
}


