;(function($) {
  //Function to calculate total width of all ul's
  $.fn.calcSubWidth = function() {
    var rowWidth = 0;
    //Calculate row
    $("ul",this).each(function() { //for each ul...
      rowWidth += $(this).outerWidth(); //Add each ul's width together
    });
    return rowWidth;
  };
})(jQuery);

$(document).ready(function() {

  var isMSIE=/*@cc_on!@*/false;
  // http://dean.edwards.name/weblog/2007/03/sniff/
  var IEpre7=(isMSIE&&(typeof document.body.style.maxHeight=='undefined'));

  $("ul#topnav li .sub").before('<span class="itemBg"></span>');

  //On Hover Over
  function megaHoverOver(){
    $(".sub",this).stop().fadeTo('fast', .95).show(); //Find sub and fade it in

    if(IEpre7) {
      var w=$(".sub",this).parent().width();
      $(".sub",this).prev('.itemBg').css({width:w-1});
    }

    $(".sub",this).prev('.itemBg').fadeTo('fast',1).css({display:'block'});

    if ( $(".row",this).length > 0 ) { //If row exists...

      var biggestRow = 0;

      $(".row",this).each(function() {
         //Call function to calculate width of all ul's
         var rowWidth = $(this).calcSubWidth();
         //Find biggest row
         if(rowWidth > biggestRow) {
            biggestRow = rowWidth;
         }
      });

      $(".sub",this).css({'width':biggestRow}); //Set width
      $(".row:last",this).css({'margin':'0'});  //Kill last row's margin

    } else { //If row does not exist...

      var rowWidth = $(this).calcSubWidth();  //Call function to calculate width of all ul's
      $(".sub",this).css({'width' : rowWidth}); //Set Width
    }
  }
  //On Hover Out
  function megaHoverOut(){
    $(".sub",this).stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
      $(this).hide();  //after fading, hide it
    });
    $(".sub",this).prev('.itemBg').stop().fadeTo('fast', 0, function() {
      $(this).hide();
    });
  }
  //Set custom configurations
  var config = {
    sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
    interval: 100, // number = milliseconds for onMouseOver polling interval
    over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
    timeout: 500, // number = milliseconds delay before onMouseOut
    out: megaHoverOut // function = onMouseOut callback (REQUIRED)
  };

  $("ul#topnav li .sub").css('opacity','0'); //Fade sub nav to 0 opacity on default
  $("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations
});
// Add class of fade to images contained within a link, remove any eternal link background
//  - modified to only affect images in Header section of page. SR 04-Aug-10.
$(document).ready(function() {
   $('#header').find("a:has(img)").addClass("fade").removeClass("new_win");
});

// Fade images with a class of fade on mouseover
$(document).ready(function() {
    // make sure the image is at full opacity on load
    $(".fade").css("opacity", "1.0");

	// Change speed/opactity of mouseover state
	$('.fade').mouseover(function() {
		$(this).stop().fadeTo(400, 0.6);
	});

	// Change speed/opactity of mouseout state
	$('.fade').mouseout(function() {
		$(this).stop().fadeTo(500, 1.0);
	});
});


