// if @font-face isn't supported
// Modernizr._fontfaceready(function(bool){
//   if (!bool) {
//    $.getScript('/javascripts/cufon-with-font.js', function(){
//Cufon.replace('#nav h1, h2,a.phbuttonlarge, .phoneDirectoryTable, h3, #content h4, #nav li > span > a');
//      Cufon.replace('h2.title', { hover: true });
//      Cufon.replace('#bottom-links h5', { letterSpacing: "-1px" });
//      Cufon.now();
//    });
//   }
$.fn.googleMap = function(address, options) {
// });

  var defaults = {
    zoom: 14,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  
  options = $.extend(defaults, options || {});
  
  var center = new google.maps.LatLng(44.081996, -123.0286928);
  var map = new google.maps.Map(this.get(0), $.extend(options, { center: center }));

  var geocoder = new google.maps.Geocoder();
  geocoder.geocode({ address: address }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK && results.length) {
      if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
        map.set_center(results[0].geometry.location);
        var marker = new google.maps.Marker({
          position: results[0].geometry.location,
          map: map
        });
      }
    }
  });
}

$.fn.equalHeight = function() {
  var maxHeight = 0;
  this.each(function() {
    if ($.browser.msie && $.browser.version < 7)
      $(this).css({ width: $(this).width() });    
    
    var myHeight = $(this).height();
    if (myHeight > maxHeight)
      maxHeight = myHeight;
  });
  this.css({ height: maxHeight });
};

$.subnav = function() {
  // Re-order z-index for IE6 and IE7
  if ($.browser.msie && $.browser.version < 8) {
    var zIndexNumber = 1000;
    $('div').filter(function() {
      var elem = $(this);
      return (elem.closest('.ms-MenuUIPopupBody').length < 1);
    }).each(function() {
      zIndexNumber -= 1;
      $(this).css('zIndex', zIndexNumber);
    });
  }
  
  var top_nav = $('#nav');
  top_nav.data('originalZ', top_nav.css("z-index"));
  
  function subnav_defaults(subnav_elem) {
    // Automatically set last column
    var columns = subnav_elem.find('.col');
    columns.eq(columns.length - 1).addClass('last');

    // Calculate the width of all the columns
    var nav_width = 0;
    columns.each(function() {
      nav_width += $(this).width();
    });
    nav_width += columns.length - 1;
    
    subnav_elem.children().wrapAll('<div class="wrapper" />');
    subnav_elem.css({ width: nav_width, zIndex: 5000 });
    
    subnav_elem.find('h3:first-child').addClass('first');
    
    // Set all the columns to be equal height
    subnav_elem.find('.links').equalHeight();
    subnav_elem.find('.wrapper').css({ height: subnav_elem.find('.col').height() });
    
    // Handle floating over form controls in IE
    // Modified height and width in order to fix odd issue not getting height correct. - Adam
    if ($.browser.msie)
        subnav_elem.bgiframe({ height: 500, width: 1000 });
      
    return nav_width;
  }
  
  var currentlyVisibleSubnav = null;
  
  function onHoverOut() {
    if (!$(this).hasClass('hovered'))
      return;
    
    if ($.browser.msie && $.browser.version < 8) {
      //$(this).data('zIndexElems').css("z-index", 1);
      top_nav.css("z-index", top_nav.data('originalZ'));
    }

    $(this).removeClass('hovered');
  }
  
  // Monitor sidebar subnavs
  $('.submenu').each(function() {
    var container_li = $(this).closest('li'),
        subnav_elem  = container_li.children('.subnav'),
        has_subnav   = subnav_elem.length > 0,
        first_run    = true;
    
    container_li.data('zIndexElems', container_li.nextAll()
                                                // .add($('.cufon'))
                                                 .add(top_nav)
                                                 .add($('.submenu').not(container_li)));
        
    container_li.hoverIntent({
      timeout: 150,
      over: function() {
        if (currentlyVisibleSubnav)
          onHoverOut.apply(currentlyVisibleSubnav.get(0));
        
        if ($.browser.msie && $.browser.version < 8) {
          container_li.data('zIndexElems').css("z-index", 1);
          //container_li.data('zIndexElems').hover(function(){$(this).css("z-index", 10000);});
                  

        }
        
        //container_li.siblings('.hovered').removeClass('hovered');
          
        container_li.addClass('hovered');
        currentlyVisibleSubnav = container_li;

        // On first hover
        if (has_subnav && first_run) {          
          subnav_defaults(subnav_elem);
          
          var link_position = container_li.position(),
              top_offset,
              left_offset;

          // If we are on the right side
          if (link_position.left > 500) {
            top_offset  = parseInt(subnav_elem.height() / -(4/3));
            left_offset = -subnav_elem.width();
          } else {
            top_offset  = parseInt(subnav_elem.height() / -4),
            left_offset = 227;
          }
          
          if ($.browser.msie && $.browser.version < 7) {
            left_offset -= 15;
          }
          
          subnav_elem.css({ top: top_offset,
                            left: left_offset });

          first_run = false;
        }
      },
      out: onHoverOut
    });
  });
  
  // Monitor mousing over the top nav
  top_nav.find('li .inner a').each(function() {
    var cufon_elem   = $(this),
        container_li = $(this).closest('li'),
        subnav_elem  = container_li.children('.subnav'),
        has_subnav   = subnav_elem.length > 0,
        first_run    = true;
        
    cufon_elem.data('origColor', cufon_elem.css('color'));

    container_li.hover(function() {
      container_li.addClass('hovered');
      top_nav.get(0).className = 'active_' + container_li.attr('id');
     // if (Cufon) Cufon.replace(cufon_elem.get(0), { color: '#fff' });
      
      // On first hover
      if (has_subnav && first_run) {
        var nav_width = subnav_defaults(subnav_elem);

        // If we're about to overflow offscreen, shift position
        var remaining_space = parseInt(940 - container_li.position().left),
            overflow        = remaining_space - nav_width;
        if (overflow < 0)
          subnav_elem.css({ left: overflow });
        
        first_run = false;
      }
    }, function() {
      top_nav.get(0).className = '';
      container_li.removeClass('hovered');
      //if (Cufon) Cufon.replace(cufon_elem.get(0), { color: cufon_elem.data('origColor') });
    });
  });
  
  return this;
};

$(document).ready(function() {
  $.subnav();
  $('#hospital-links, #medical-group, #laboritories, #safety').equalHeight();
  $('.submenu').hover(function(){$(this).css("z-index", 10000);},function(){$(this).css("z-index", 1);});
});
