;(function($) {
  function log() {
    if (window && window.console && window.console.log)
      for(var i=0, len = arguments.length; i < len; i++)
        console.log(arguments[i]);
  }
  
  $.fn.flyout = function() {
    var is_home = $('#body_home').length > 0;
    
    return this.each(function() {      
      var flyout = $(this).find('ul:first'),
          left   = $(this).find('a:first').width(),
          bridge = $(this).append('<div class="bridge"></div>').find('div.bridge:first');
      
      bridge.css('left', left + 5);
      
      var bridge_left = parseInt(bridge.css('left')),
          flyout_top  = flyout.height()/2 - bridge.height()/2 + 2,
          flyout_left = bridge.width() + bridge_left;
      
      // home page has different flyouts, so if not on home page,
      // need to add left positioning
      if (!is_home) flyout.css({top: -flyout_top, left:flyout_left});
      
      $(this).hover(
        function() { $(this).addClass('flyout'); }, 
        function() { $(this).removeClass('flyout'); }
      );
    });
  }
  
  if (typeof Array.prototype['max'] == 'undefined') {
    Array.prototype.max = function() {
      return Math.max.apply({},this);
    } 
  }
  
  $.fn.equalHeightChildren = function() {
    return this.each(function() {
      var children = $(this).children();
      
      var tallest  = $.map(children, function(child) {
        return $(child).height();
      }).max();
      
      children.each(function() {
        var padding = parseInt($(this).css('padding-top')) + 
                      parseInt($(this).css('padding-bottom'));
        $(this).height(tallest - padding);
      });
    });
  }
})(jQuery);

jQuery(function($) {
  // creates flyout menus on navigation and audience links
  $('#nav_audiences > li:has(ul)').flyout();
  $('#nav_primary > li:has(ul)').flyout();
  
  // 
  if ($.browser.safari) {
    setTimeout(function() { $('body.subpage #content').equalHeightChildren(); }, 50);
  } else {
    $('body.subpage #content').equalHeightChildren();
  }
  
  $('#quicklinks select').change(function() {
    var location = $(this).val();
    if (location != 'Quicklinks') {
      window.location = location;
    }
  });
});