﻿var resizeTimer = null; 
var intWindowWidth;
var intWindowHeight;
var strOpen = "Open";
var strClose = "Close";

$(document).ready(function()
{
    initTopMenu();
    initLeftMenu();

    //Necessary on districtlookup.aspx: most browsers wouldn't jump to the #Results anchor from FORM's ACTION 
    if ($('h2.results').length > 0)
    {
        window.location.hash = "#Results";
    }

    if ($('.imageRotate').length > 0)
    {
        $('.imageRotate').cycle
        ({
            timeout: 10000,
            delay: 0,
            speed: 1000
        });

        $('.imageRotate img.hidden').removeClass('hidden');
    }

    if ($('img[usemap]').length > 0)
    {
        $('img[usemap]').maphilight();
    }

    if (($.browser.msie) && (($.browser.version == "6.0") || ($.browser.version == "7.0")))
    {
        $(window).resize
        (
            function()
            {
                if (resizeTimer)
                {
                    clearTimeout(resizeTimer);
                }
                
                resizeTimer = setTimeout(initTopMenu, 100);
            }
        );
    }
    else
    {
        $(window).resize(initTopMenu);
    }
    
    if ($('#divPageTitleBar.print').length > 0)
    {
        var strPrintLink = "<a href=\"#\" class=\"printPage\" title=\"Print\"><span class=\"hide\">Print</span></a>";
        
        if ($('#divPageTitleBar .pageOptions ul').length > 0)
        {
            $('#divPageTitleBar .pageOptions ul:first').prepend("<li>" + strPrintLink + "</li>");
        }
        else
        {
            $('#divPageTitleBar .pageOptions').html(strPrintLink);
        }
    }

    if ($('#divPageBottomTools.print').length > 0)
    {
        var strPrintLink = "<a href=\"#\" class=\"printPage\">Print</a>";

        if ($('#divPageBottomTools ul').length > 0)
        {
            $('#divPageBottomTools ul:first').prepend("<li>" + strPrintLink + "</li>");
        }
        else
        {
            $('#divPageBottomTools').html(strPrintLink);
        }
    }
    
    $('a.printPage').click
    (
        function()
        {
            window.print();
            return false;
        }
    );
});

function initTopMenu()
{
    var strSubMenuTitle = "";
    
    intWindowWidth = $(window).width();
    intWindowHeight = $(window).height();
    
    //Display the Fully Dynamic Nav Bar
    if ((intWindowWidth > 550) && (intWindowHeight > 200))
    {
        if (!$('.topNav').hasClass('topNavDynamic'))
        {
            $('.topNav').addClass('topNavDynamic');
        }
        
        $('.topNav .subMenuHeader').each
        (
            function()
            {
                strSubMenuTitle = $(this).text().replace('&', '\&amp;');
                
                if (($(this).parent().find('li').find('.currentPage').length == 0) && (!$(this).hasClass('menuOpenByDefault')))
                {
                    $(this).replaceWith('<a href="#" title="The ' + strSubMenuTitle + ' submenu">' + strSubMenuTitle + '</a>');
                }
                else
                {
                    $(this).replaceWith('<a href="#" title="The ' + strSubMenuTitle + ' submenu" class="subMenuSection">' + strSubMenuTitle + '</a>');
                }
            }
        );

        if (($.browser.msie) && ($.browser.version == "6.0"))
        {
            fixTopNavIE6();
        }

        makeTopNavAccessible();
    }
    //Window Resized Too Small:  Remove the Dynamic Nav Bar
    else if ($('.topNav').hasClass('topNavDynamic'))
    {
        if (($.browser.msie) && ($.browser.version == "6.0"))
        {
            $('.topNavDynamic li ul').css('width', 'auto');
        }

        $('.topNav').removeClass('topNavDynamic');
    }
}

fixTopNavIE6 = function()
{
    //Simulate the CSS li:hover effect
    $('.topNavDynamic li').hover
    (
        function()
        {
            $(this).addClass("sfhover");
        },
        function()
        {
            $(this).removeClass("sfhover");
        }
    );
    
    //Set the 2nd Tier's Width to fill the screen
    var cssClassFullWidth = {width: (intWindowWidth - 13)};
    $('.topNavDynamic li ul').css(cssClassFullWidth);
}

makeTopNavAccessible = function()
{
    $('.topNavDynamic li ul').hover
    (
        function()
        {
            $(this).parent().find("a:first").addClass("sffocus");
        },
        function()
        {
            $(this).parent().find("a:first").removeClass("sffocus");
        }
    );
    
    $('.topNavDynamic a').focus
    (
        function()
        {
            $(this).addClass("sffocus");            //a:focus
            $(this).parent().addClass("sfhover");   //li < a:focus
            
            if ($(this).parent().parent().parent().get(0).tagName.toLowerCase() == 'li')
            {
                $(this).parent().parent().parent().addClass("sfhover");  //li < ul < li < a:focus
                $(this).parent().parent().parent().find("a:first").addClass("sffocus");  //li < a:hover < ul < li < a:focus
            }
        }
    );

    $('.topNavDynamic a').blur
    (
        function()
        {
            $(this).removeClass("sffocus");
            $(this).parent().removeClass("sfhover");

            if ($(this).parent().parent().parent().get(0).tagName.toLowerCase() == 'li')
            {
                $(this).parent().parent().parent().removeClass("sfhover");
                $(this).parent().parent().parent().find("a:first").removeClass("sffocus");
            }
        }
    );
}

function initLeftMenu()
{    
    var strSubMenuTitle = "";
    
    $('#divLeftColumn div.subMenuHeader').each
    (
        function()
        {
            strSubMenuTitle = $(this).text().replace('&', '\&amp;');
            
            if (($(this).parent().find('li').find('.currentPage').length == 0) && (!$(this).hasClass('subMenuOpenByDefault')))
            {
                $(this).parent().find('ul').hide();
                $(this).replaceWith('<a href="#" title="' + strOpen + ' the ' + strSubMenuTitle + ' submenu" class="subMenuClosed">' + strSubMenuTitle + '</a>');
            }
            else
            {
                $(this).replaceWith('<a href="#" title="' + strClose + ' the ' + strSubMenuTitle + ' submenu" class="subMenuOpened">' + strSubMenuTitle + '</a>');
            }
        }
    );

    $('#divLeftColumn a.subMenuClosed').toggle
    (
        function()
        {
            $(this).addClass("subMenuOpened");
            $(this).removeClass('subMenuClosed');
            $(this).attr('title', $(this).attr('title').replace(strOpen, strClose));
        },
        function()
        {
            $(this).addClass("subMenuClosed");
            $(this).removeClass("subMenuOpened");
            $(this).attr('title', $(this).attr('title').replace(strClose, strOpen));
        }
    );

    $("#divLeftColumn a.subMenuClosed").click
    (
        function()
        {
            $(this).next().slideToggle('normal');
        }
    );

    $('#divLeftColumn a.subMenuOpened').toggle
    (
        function()
        {
            $(this).addClass("subMenuClosed");
            $(this).removeClass('subMenuOpened');
            $(this).attr('title', $(this).attr('title').replace(strClose, strOpen));
        },
        function()
        {
            $(this).removeClass("subMenuClosed");
            $(this).addClass('subMenuOpened');
            $(this).attr('title', $(this).attr('title').replace(strOpen, strClose));
        }
    );

    $('#divLeftColumn a.subMenuOpened').click
    (
        function()
        {
            $(this).next().slideToggle('normal');
        }
    );
}

function querystring(parameterName)
{
    parameterName = parameterName.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");

    var regex = new RegExp("[\\?&]" + parameterName + "=([^&#]*)");
    
    var results = regex.exec(window.location.href);
    
    if (results != null)
    {
        return results[1];
    }
    else
    {
        return "";
    }
}
