if( Drupal.jsEnabled ) {

$(document).ready(function() {
  // install event trackers for google analytics
  if( ! window.pageTracker ) return;

  // track downloads of documents (in select subdomains of wri.org)
  $('#content a').click(function() {
	if( this.href.match(/^http:\/\/(pdf|docs|multimedia|powerpoints)\.wri\.org/) ) {
	  var str = this.href.replace(/^http:\/\/(\w+)\.wri\.org/, '{$1}');
	  pageTracker._trackEvent('Downloads', 'Download', str);
	}

	if( this.href.match(/^http:\/\/([\w\.]+)\.rackcdn\.com/) ) {
	  var str = this.href;
	  pageTracker._trackEvent('Downloads', 'Download', str);
	}

	return true;
  });

  // clicks in the "Our Work" menu
  $('#menu-projects a').click(function() { return ui_track('Our Work Menu', this); });

  // clicks in the "Happening Now" block on home page
  $('#hp-billboard #tic-subnav a').click(function() { return ui_track('Happening Now', this); });

  // clicks on home page carousel items: title/position > url
  var pos=1;
  $('#hp-billboard div.ntype-feature').each(function() {
    var pos_ = pos++;
	var story_ = this;
	$('a', this).each(function() {
	  this.pos_ = pos_;
	  this.label_ = $('h2.node-title a', story_).text();
	  $(this).click(function() {
	    return ui_track('Carousel', this);
	  });
	});
  });

  // clicks on topics buttons on home page
  $('#hp-topics a').click(function() { return ui_track('Topics Buttons', this); });

  // clicks on other home page blocks below the fold
  $('body.front #views-popular a').click(function() { return ui_track('Home Page: Most Popular', this); });
  $('body.front #wri-latest-blogs a').click(function() { return ui_track('Home Page: Latest Blog Posts', this); });
  $('body.front #block-vocus-feed a').click(function() { return ui_track('Home Page: In The News', this); });
  $('body.front #wri-insights-latest a').click(function() { return ui_track('Home Page: Latest From Insights', this); });

  // clicks on sub-navigation links
  $('#sidebar #tic-subnav a').click(function() { return ui_track('Sub Navigation', this); });
  $('#sidebar #tic-related-links a').click(function() { return ui_track('Related Links', this); });

  // clicks on social media links
  $('#header ul.social-media a').each(function() {
    this.label_ = $(this).attr('class').replace(/ .+/, '');
	$(this).click(function() {
	  return ui_track('Social Media Links', this);
	});
  });


  // clicks on Donate, Support WRI, etc
  $('#tic-support-wri a, #block-support-staff a, #header li.donate a, #tic-advert a').click(function() { return ui_track('Engagement', this); });
});

function ui_track(area, a) {
  var str;

  str = a.label_ ? a.label_ : $(a).text();
  if( a.pos_ != undefined ) str = str + '/' + a.pos_ + ' ';
  if( str.length ) str = str + ' > ';

  str = str + a.href.replace(/^http:\/\//, '');

  pageTracker._trackEvent('Navigation', area, str);
  return true;
}

}

