var hasCalibri = true;

// Font Detection

var font = (function() {
    var test_string = 'mmmmmmmmmwwwwwww';
    var test_font = '"Comic Sans MS"';
    var notInstalledWidth = 0;
    var testbed = null;
    var guid = 0;

    return {
        // must be called when the dom is ready
    setup: function() {
        if ($('#fontInstalledTest') != null)
            if ($('#fontInstalledTest').length) return;
        if ($('head') != null)
            $('head').append('<' + 'style> #fontInstalledTest, #fontTestBed { position: absolute; left: -9999px; top: 0; visibility: hidden; } #fontInstalledTest { font-size: 50px!important; font-family: ' + test_font + ';}</' + 'style>');
        if ($('body') != null)
            $('body').append('<div id="fontTestBed"></div>').append('<span id="fontInstalledTest" class="fonttest">' + test_string + '</span>');
        if ($('#fontTestBed') != null)
            testbed = $('#fontTestBed');
        if ($('#fontInstalledTest') != null)
            notInstalledWidth = $('#fontInstalledTest').width();
        },

        isInstalled: function(font) {
            guid++;

            var style = '<' + 'style id="fonttestStyle"> #fonttest' + guid + ' { font-size: 50px!important; font-family: ' + font + ', ' + test_font + '; } <' + '/style>';

            if ($('head') != null)
                $('head').find('#fonttestStyle').remove().end().append(style);
            if ($('#fontTestBed') != null)   
                testbed.empty().append('<span id="fonttest' + guid + '" class="fonttest">' + test_string + '</span>');

            if ($('#fontTestBed') != null)
                return (testbed.find('span').width() != notInstalledWidth);
        }
    };
})();


var global = function() {

    // private variables

    return {

        initNav: function() {

            // navigation events

            if ($("#nav-item-1,#nav-item-2,#nav-item-3,#nav-item-4,#nav-item-5") != null) {
                $("#nav-item-1,#nav-item-2,#nav-item-3,#nav-item-4,#nav-item-5").mouseenter(
				function() {
				    if ($("#main-menu div") != null)
				        $("#main-menu div").css("visibility", "hidden");
				    if ($(this).find("div:first") != null)
				        $(this).find("div:first").css("visibility", "visible");
				}
			    );
            }

            if ($("#nav-item-1 a.tier-1,#nav-item-2 a.tier-1,#nav-item-3 a.tier-1,#nav-item-4 a.tier-1,#nav-item-5 a.tier-1") != null) {
                $("#nav-item-1 a.tier-1,#nav-item-2 a.tier-1,#nav-item-3 a.tier-1,#nav-item-4 a.tier-1,#nav-item-5 a.tier-1").focus(
				function() {
				    if ($("#main-menu div") != null)
				        $("#main-menu div").css("visibility", "hidden");
				    $(this).next().css("visibility", "visible");
				    if ($("#nav-item-1 a.tier-1,#nav-item-2 a.tier-1,#nav-item-3 a.tier-1,#nav-item-4 a.tier-1,#nav-item-5 a.tier-1") != null)
				        $("#nav-item-1 a.tier-1,#nav-item-2 a.tier-1,#nav-item-3 a.tier-1,#nav-item-4 a.tier-1,#nav-item-5 a.tier-1").removeClass("over");
				}
			    );
            }

            if ($(".tier-1 li") != null) {
                $(".tier-1 li").mouseleave(
				    function() {
				        $("> a", this).removeClass("over");
				        $("> div", this).css("visibility", "hidden");
				    }
			    );
            }

            if ($("ul.tier-1 li a.tier-1") != null) {
                $("ul.tier-1 li a.tier-1").mouseenter(
				    function() {
				        $(this).addClass("over");
				    }
			    );
            }

            if ($("ul.tier-1 li a.tier-1") != null) {
                $("ul.tier-1 li a.tier-1").focus(
				function() {
				    $(this).addClass("over");
				}
			);
            }

            if ($("ul.tier-1 li ul li a") != null) {
                $("ul.tier-1 li ul li a").hover(
				function() {
				    $(this).addClass("over");
				},
				function() {
				    $(this).removeClass("over");
				}
			);
            }

            if ($("ul.tier-1 li ul li a") != null) {
                $("ul.tier-1 li ul li a").focus(
				function() {
				    $(this).addClass("over");
				}
			);
            }

            if ($("ul.tier-1 li ul li a") != null) {
                $("ul.tier-1 li ul li a").blur(
				function() {
				    $(this).removeClass("over");
				}
			);
            }


            if ($("div.tier-2-subitems p.learn-more a") != null) {
                $("div.tier-2-subitems p.learn-more a").hover(
				function() {
				    $(this).addClass("over");
				},
				function() {
				    $(this).removeClass("over");
				}
			);
            }
            // create a faux 2-colum submenu layout by dyanimcally 
            //   setting the width of the submenu container.

            var navItem1Len = 0;
            var navItem3Len = 0;

            if ($("#nav-item-1 .tier-2") != null) {
                $("#nav-item-1 .tier-2").each(function() {
                    navItem1Len = navItem1Len + $(this).width();
                });
            }

            if ($("#nav-item-3 .tier-2") != null) {
                $("#nav-item-3 .tier-2").each(function() {
                    navItem3Len = navItem3Len + $(this).width();
                });
            }

            if ($("#nav-item-1 div") != null)
                $("#nav-item-1 div").css("width", navItem1Len + (hasCalibri ? 84 : 64) + "px");
            if ($("#nav-item-3 div") != null)
                $("#nav-item-3 div").css("width", navItem3Len + 54 + "px").css("paddingBottom", "20px");

        }
    };
} ();


$(document).ready(function() {

    font.setup();
    hasCalibri = font.isInstalled("Calibri");
    if (!hasCalibri && $("body") != null) {
        $("body").css("font-size", "62.5%");
    }

    global.initNav();

    if ($('.search_input') != null) {
        $('.search_input').click(function() {
            $(this).focus();
            $(this).select();
        });
    }



});






