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').length) return;

            $('head').append('<' + 'style> #fontInstalledTest, #fontTestBed { position: absolute; left: -9999px; top: 0; visibility: hidden; } #fontInstalledTest { font-size: 50px!important; font-family: ' + test_font + ';}</' + 'style>');


            $('body').append('<div id="fontTestBed"></div>').append('<span id="fontInstalledTest" class="fonttest">' + test_string + '</span>');
            testbed = $('#fontTestBed');
            notInstalledWidth = $('#fontInstalledTest').width();
        },

        isInstalled : function(font) {
            guid++;

            var style = '<' + 'style id="fonttestStyle"> #fonttest' + guid + ' { font-size: 50px!important; font-family: ' + font + ', ' + test_font + '; } <' + '/style>';

            $('head').find('#fonttestStyle').remove().end().append(style);
            testbed.empty().append('<span id="fonttest' + guid + '" class="fonttest">' + test_string + '</span>');

            return (testbed.find('span').width() != notInstalledWidth);
        }
    };
})();


var global = function() {

  // private variables

  return {

    initNav: function() {

      // navigation events

      

      $("#nav-item-1,#nav-item-2,#nav-item-3,#nav-item-4,#nav-item-5").mouseenter(
				function() {
				  $("#main-menu div").css("visibility", "hidden");
				  $(this).find("div:first").css("visibility", "visible");
				}
			);

      $("#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() {
				  $("#main-menu div").css("visibility", "hidden");
				  $(this).next().css("visibility", "visible");
				  $("#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");
				}
			);


      $(".tier-1 li").mouseleave(
				function() {
				  $("> a", this).removeClass("over");
				  $("> div", this).css("visibility", "hidden");
				}
			);


      $("ul.tier-1 li a.tier-1").mouseenter(
				function() {
				  $(this).addClass("over");
				}
			);
      $("ul.tier-1 li a.tier-1").focus(
				function() {
				  $(this).addClass("over");
				}
			);

      $("ul.tier-1 li ul li a").hover(
				function() {
				  $(this).addClass("over");
				},
				function() {
				  $(this).removeClass("over");
				}
			);

      $("ul.tier-1 li ul li a").focus(
				function() {
				  $(this).addClass("over");
				}
			);


      $("ul.tier-1 li ul li a").blur(
				function() {
				  $(this).removeClass("over");
				}
			);
			 $("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;

      $("#nav-item-1 .tier-2").each(function() {
        navItem1Len = navItem1Len + $(this).width();
      });

      $("#nav-item-3 .tier-2").each(function() {
        navItem3Len = navItem3Len + $(this).width();
      });

      $("#nav-item-1 div").css("width", navItem1Len + (hasCalibri ? 84 : 64) + "px");
      $("#nav-item-3 div").css("width", navItem3Len + 54 + "px").css("paddingBottom", "20px");

    }
  };
} ();


$(document).ready(function() {

    font.setup();
    hasCalibri = font.isInstalled("Calibri");
    if (!hasCalibri) {
        $("body").css("font-size", "62.5%");
    }

    global.initNav();


    $('.search_input').click(function(){
	$(this).focus();
	$(this).select();
    });
    
    

});






