/**
 * JavaScript Library required for all pages of the public site.
 *
 */

jQuery(document).ready(function()
{
    // Open external links (specified by rel= or class=) in a new browser window:    
    jQuery("a[rel*='external'],a[class*='external']").attr('target','_blank');
    jQuery("#outer-sidebar #follow a").attr('target','_blank');
    
    // Top:
    jQuery("#footer p#top a").bind("click", function() {
        jQuery('html, body').animate({scrollTop:0}, 'slow');
        return false;
    });
    
    // Equal Heights
    jQuery("ul.leadership li").equalHeightsByRow();
    
    // Homepage Slideshow
    jQuery("#intro li").bind("click", function() {
        if (jQuery(this).hasClass("current")) return;
        jQuery('#intro').stopTime();
        slide(jQuery(this));
        jQuery('#intro').oneTime('15s', function() { startCycle(); });  // restart cycle
    });
    
    // Start Homepage Slideshow (Cycle)
    if (jQuery("#intro li").length > 1) startCycle();
    
});

/* ============================================================================
   HELPERS */
   
jQuery.fn.exists = function(){return jQuery(this).length>0;}

/**
 * Equal Heights Row
 *
 * Equalize the heights of elements on the same row, without affecting the
 * heights of precedig or succeeding rows.
 * 
 * Updated:  03/24/2010
 * Author:   Dave Robertson (dave@factory44.net)
 *
 * Usage: jQuery(object).equalHeightsRow();
 * 
 */
jQuery.fn.equalHeightsByRow = function() {
    var heights = new Array();
    var prevOffset = 0;
    this.each(function(index) {
      if (jQuery(this).offset().top != prevOffset) {
        maxHeightThisRow = 0;
        prevOffset = jQuery(this).offset().top;
      }
      maxHeightThisRow = (jQuery(this).height() > maxHeightThisRow) ? jQuery(this).height() : maxHeightThisRow;
      heights[index] = maxHeightThisRow;
		});
    this.each(function(index) {
      jQuery(this).height(heights[index].pxToEm());
    });
}


/* ----------------------------------------------------------------------------
   HOMEPAGE SLIDESHOW HELPERS */

function slide(obj) {
    // prevent overlapping animations:
    if (jQuery("#intro li:animated").length > 0) return;
    
    //lock other li's
    obj.nextAll(':not(.current)').css('position', 'absolute');
    jQuery('#intro li.current').prevAll().css('position', 'static');

    // shift
    jQuery('#intro li.current').animate({ width: 0}, 1152, 'easeOutQuart');
    obj.animate({ width: '576px'}, 1152, 'easeOutQuart', function() {
        obj.nextAll(':not(.current)').css('position', 'static');
        if (jQuery('body').hasClass('ie7')) jQuery("#intro li#intro1").css('margin-right', '20px'); // patch for IE7
    });

    // update current
    jQuery("#intro li.current").removeClass('current');
    obj.addClass('current');
}

function startCycle() {
    jQuery('#intro').everyTime('10s', function() {
        if (jQuery('#intro li.current').next().exists()) {
            slide(jQuery('#intro li.current').next());
        } else {
            slide(jQuery('#intro li').eq(0));
        }
    });
}
