$(document).ready(function() {

//Establish Suite ID to send with tracking links
  if (location.hostname.indexOf('test') > -1) {
    var omnitureAccount = "msacccommunitydev";
    }
  else {
    var omnitureAccount = "msacccommunity";
    }


//Click-tracking function for primary navigation
  $('div.NavigationArea ul li a').click(function() {
    var navigationLink  = $(this).text();
    trackLink(navigationLink, 'primary site navigation', 'o');
    });

//Click-tracking function for 'sign in' link
  $('dl.CurrentUserStatistics.SignedOut dd ul li a[href*="login.aspx"]').click(function() {
    var signInLinkName  = $(this).text();
    trackLink(signInLinkName, 'top user information area', 'o', 'signin');
    });

//Click-tracking function for Edit Profile link
  $('.CurrentUserStatistics a[href*="editprofile"]').click(function() {
    trackLink('Edit Profile', 'top user information area', 'o');
    });

//Click-tracking functions for Forum Questions sidebar links
  $('div.Module.Questions a[href*=5]').click(function() {
    trackLink('adCenter Support Forums', 'right sidebar Questions module', 'o');
    });
  $('div.Module.Questions a[href*=9]').click(function() {
    trackLink('pubCenter Support Forums', 'right sidebar Questions module', 'o');
    });

//Click-tracking functions for 'Start Advertising' button links
  $('div.Module.Advertise a').click(function() {
    var advertiseLinkSource             = 'rightsidebar:StartAdvertisingBtn';
    var advertiseLinkId                 = $(this).attr('id');
    var advertiseLinkUrl                = $(this).attr('href');
    if (advertiseLinkId.indexOf('BlogHome') > -1) {
      advertiseLinkSource       = 'BlogHome:' + advertiseLinkSource;
      }
    else if (advertiseLinkId.indexOf('BlogMSA') > -1) {
      advertiseLinkSource       = 'BlogMSA:' + advertiseLinkSource;
      }
    else if (advertiseLinkId.indexOf('BlogAsia') > -1) {
      advertiseLinkSource       = 'BlogAsia:' + advertiseLinkSource;
      }
    else {
      advertiseLinkSource       = 'CommunityHome:' + advertiseLinkSource;
      }
    trackLink('Start Advertising', advertiseLinkSource, 'e', 'advertise', advertiseLinkUrl);
    });

//Click-tracking function for sidebar 'Follow' links
  $('.Follow a').click(function() {
    var followLinkName, followLinkType;
    var followLinkUrl   = $(this).attr('href');
    if (followLinkUrl.indexOf('facebook') > -1) {
      followLinkName    = 'Facebook';
      followLinkType    = 'e';
      }
    else if (followLinkUrl.indexOf('twitter') > -1) {
      followLinkName    = 'Twitter';
      followLinkType    = 'e';
      }
    else if (followLinkUrl.indexOf('linkedin') > -1) {
      followLinkName    = 'LinkedIn';
      followLinkType    = 'e';
      }
    else {
      followLinkName    = 'RSS';
      followLinkType    = 'o';
      }
    trackLink(followLinkName + ' Follow', 'right sidebar follow', followLinkType, 'follow', followLinkUrl);
    });

//Click-tracking function for sharing buttons
  $('.Sharing div.Option, .Sharing td.PrintPage').click(function() {
    var shareLinkName, shareLinkType;
    if ($(this).attr('class').indexOf('LinkedIn') > -1) {
      shareLinkName     = 'LinkedIn';
      shareLinkType     = 'e';
      }
    else if ($(this).attr('class').indexOf('ShareThis') > -1) {
      shareLinkName     = 'ShareThis';
      shareLinkType     = 'e';
      }
    else {
      shareLinkName     = 'Print';
      shareLinkType     = 'o';
      }
    trackLink(shareLinkName, 'sharing toolbar', shareLinkType, 'share');
    });


//Click-tracking function for sidebar tag cloud tags
  $('#SidebarArea a[rel="tag"]').click(function() {
    var currentTag      = $(this).text();
    trackLink('Tag: ' + currentTag, 'right sidebar tags', 'o');
    });

//Click-tracking function for tags page tag cloud tags
  $('div.ContentArea ul.CommonTagCloud a[rel="tag"]').click(function() {
    var currentTag      = $(this).text();
    trackLink('Tag: ' + currentTag, 'browse tags tagcloud tags', 'o');
    });

//Click-tracking function for sidebar categories
  $('div[id*="TopicNavigation"] a').click(function() {
    var currentCategory = $(this).text();
    trackLink('Category: ' + currentCategory, 'right sidebar categories', 'o');
    });

//Click-tracking function for sidebar links list
  $('.sidebarLinks ul li a').click(function() {
    var linksListLink   = $(this).text();
    trackLink(linksListLink, 'right sidebar links list', 'e');
    });

//Click-tracking function for Member Directory link in Top Contributors module
  $('.Contributors a[href*="Members"]').click(function() {
    trackLink('Member Directory', 'right sidebar Top Contributors', 'o');
    });

//OMNITURE LINK-TRACKING HELPER FUNCTION
  function trackLink(linkName, linkSource, linkType, eventType, exitLinkUrl) {
    var s                       = s_gi(omnitureAccount);
    s.linkTrackVars             = 'prop21,prop22,events';
    s.linkTrackEvents           = 'event12';
    if (eventType) {
      if (eventType.indexOf('signin') > -1) {
        s.linkTrackEvents       = 'event1,' + s.linkTrackEvents;
        }
      if (eventType.indexOf('follow') > -1) {
        s.linkTrackVars         = s.linkTrackVars + ',eVar11';
        s.linkTrackEvents       = 'event7,' + s.linkTrackEvents;
        s.eVar11                = linkName;
        }
      if (eventType.indexOf('share') > -1) {
        s.linkTrackVars         = s.linkTrackVars + ',eVar12';
        s.linkTrackEvents       = 'event6,' + s.linkTrackEvents;
        s.eVar12                = linkName;
        }
      }
    s.events                    = s.linkTrackEvents;
    s.prop21                    = linkName;
    s.prop22                    = linkSource;
  //Ensure exit links are not double-tracked
    if (linkType == "e") {
      var lt                    = (exitLinkUrl != null)? s.lt(exitLinkUrl) : "";
      if (lt == "") {
        s.tl(this,'e', linkName);
        }
      }
    else {
      s.tl(this, linkType, linkName);
      }
    }

  });


