/*** Custom Omniture Tracking ***/

/* Steve Test
$(document).ready(function() {
  $("script[rel!='']").each(function() {
    var fish = $(this).attr('rel');
    if (fish != null) {
      alert(fish);
    };
  });
});
*/

// var s = s_gi('pentonkrangdev'); was removed from every function since it's set globally in s_code.js


/**** Universal link tracking function ****/

var pentonTrackLink;

// pentonTrackLink[0] = s.eVar11 (name of area)
// pentonTrackLink[1] = e(exit link), d(file download), o(other)
// pentonTrackLink[2] = name of link (link detail)

function linkCodeUniversal(obj) {
  s.linkTrackVars='eVar11';
  s.eVar11=pentonTrackLink[0];
  s.tl(this,pentonTrackLink[1],pentonTrackLink[2]);
}

/**** Custom event tracking function ****/

var pentonTrackEvent;

// pentonTrackEvent[0] = s.linkTrackVars
// pentonTrackEvent[1] = s.linkTrackEvents
// pentonTrackEvent[2] = s.events
// pentonTrackEvent[3] = e(exit link), d(file download), o(other)
// pentonTrackEvent[4] = name of event

function linkCodeEvent(obj) {
  s.linkTrackVars=pentonTrackEvent[0];
  s.linkTrackEvents=pentonTrackEvent[1];
  s.events=pentonTrackEvent[2];
  var lt=obj.href!=null?s.lt(obj.href):"";
  if (lt=="") { s.tl(obj,pentonTrackEvent[3],pentonTrackEvent[4]); }
}


$(document).ready(function() {



/*** Ads ***/
/*
  $("div.ad").click(function () {
    var index = $("div.ad").index(this);
    $("span").text("That was div index #" + index);
  });
*/

//  $('div.ad a').click(function() {
  $('div.ad').click(function() {
    var adSize = $(this).closest('.ad').attr('class');
    pentonTrackEvent = Array("events","event5","event5","o",adSize);
    linkCodeEvent(this);
  });
//I've also seen just 'embed'
/*
  $('div.ad object').click(function() {
    var adSize = $(this).closest('.ad').attr('class');
    pentonTrackEvent = Array("events","event5","event5","o",adSize);
    linkCodeEvent(true);
  });
*/
  // $('div.ad iframe') can't be tracked because it's 3rd party content. We'll have to get these ad mumbers from DART

/*** end Ads ***/



/*** Universal link tracker ***/

  $('div.custom-track a, div.custom-track submit, div.custom-track button').click(function() {

    var itemName = 'itemName not set';
    var itemBox = $(this).closest('div.custom-track');
    var itemBoxH2 = itemBox.find('h2:first');
    var itemBoxH3 = itemBox.find('h3:first');

    if (itemBoxH2.text() != '') {
      itemName = itemBoxH2.text();   
    }
    else if (itemBoxH3.text() != '') {
      itemName = itemBoxH3.text();    
    }
    else {
      itemName = itemBox.attr('class');
    }

// Dan Mentioned specific item count isn't needed, so instead I'm using itemName twice (second one would potentially be the count (itemLinkNum)
//    var itemLinkNum = itemName + ' the count'; 

    pentonTrackLink = Array(itemName,'o',itemName);

    if ($(this).hasClass('custom-track')) { 
      //This is for a.custom-track. do nothing so we can control it later with specific function
    }
    else {
      if ( $(this).is('a') ) {
        linkCodeUniversal(this); //'this' for a
      }
      else {
        linkCodeUniversal(true); //'true' for non-a (submit, button, div, etc)
      }
    }

  });

/*** end Universal link tracker ***/

/*** Top Nav ***/

  $('div.topNav a').click(function() { 
    var itemLinkDetail = "Top Nav - " + $(this).text();
    pentonTrackLink = Array('Top Nav','o',itemLinkDetail);
    linkCodeUniversal(this);  
  });

/*** end Top Nav ***/

/*** Subscribe links ***/
//in header
  $('#topCat div.utilNav a.custom-track').click(function() { 
    var itemLinkDetail = 'Subscribe - top util nav';
    pentonTrackLink = Array('Subscribe link','o',itemLinkDetail);
    linkCodeUniversal(this);  
  });
//in footer
  $('#footer div.utilNav a.custom-track').click(function() { 
    var itemLinkDetail = 'Subscribe - footer';
    pentonTrackLink = Array('Subscribe link','o',itemLinkDetail);
    linkCodeUniversal(this);  
  });
//in right column Resources box
  $('div.resources a.custom-track').click(function() { 
    pentonTrackEvent = Array('events','event3','event3','o','Resources - subscribe');
    linkCodeEvent(this);
  });



/*** Search Related ***/

  //Sort By Date
  $('a.sort-date').click(function() { 
    pentonTrackLink = Array('sort by date click','o','sort by date click');
    linkCodeUniversal(this);  
  });

  //Sort By Relevancy
  $('a.sort-relevancy').click(function() { 
    pentonTrackLink = Array('sort by relevancy click','o','sort by relevancy click');
    linkCodeUniversal(this);  
  });

/*** Top Search Button ***/
  $('div#searchBox button').click(function() { 
    pentonTrackLink = Array('site search - top','o','site search - top');
    linkCodeUniversal(true);  
  });

/*** Bottom Search Button ***/
  $('div#searchBoxBtm button').click(function() { 
    pentonTrackLink = Array('site search - bottom','o','site search - bottom');
    linkCodeUniversal(true);  
  });

/**** 
  This grabs the number of search results. This will be used for omniture tracking of which search item was clicked.
  * This will potentially need alteration for each site, depending on the resultsheader2 text.
****/

  var omnitureSearchInfo = $('div.resultsheader2').text();
  var omnitureSearchTotal = Array();
  var omnitureSearchTotal = omnitureSearchInfo.split('of');
  var omnitureSearchCountClicked;

	// # of internal search results, omnitureSearchTotal set in tools.js	

// Omniture function
  function omnitureSearchCount(obj) {
    s.linkTrackVars='prop5,eVar5';
    if ($('div.resultsheader2').text()=='') {
      s.prop5='No matching records found';
    } else {
      s.prop5=omnitureSearchTotal[1];    
    }
    s.eVar5=s.prop5;
    s.tl(this,'o','# of internal search results');
  }

  if ($('div.resultsheader2').length) {
    omnitureSearchCount(true);
  }

// parse the url for variables (pg=2, pg=3 etc)
  function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
      var pair = vars[i].split("=");
      if (pair[0] == variable) {
        return pair[1];
      }
    } 
//    alert('Query Variable ' + variable + ' not found');
  }


// get index value of clicked search link

  $('a.resultsleadin').click(function () {

    var index = $('a.resultsleadin').index(this);
    index = index + 1; //starts at 0 by default, this makes it start at 1

    var searchResultsPage;
    var searchpage = getQueryVariable('pg');
    if (searchpage=='') {
      searchResultsPage = 0;      
    } else {
      searchResultsPage = (searchpage - 1);            
    }

// * 20 is the count per page in this case, will need to change per site
    omnitureSearchCountClicked = index + (searchResultsPage * 20); 

//    alert("result number" + omnitureSearchCountClicked);
    omnitureSearchCountSelected(this);

//    pentonTrackEvent = Array('events','event16','event16','o','Search item clicked');
//    linkCodeEvent(true); 
  });


//Omniture function
  function omnitureSearchCountSelected(obj) {
    s.linkTrackVars='prop8,eVar8,events';
    s.linkTrackEvents='event16';
    s.prop8=omnitureSearchCountClicked;
    s.eVar8=s.prop8;
    s.events='event16';
    s.tl(this,'o','search result # clicked');
  }

/**** end of Search Results counter etc ****/


/*** Article Tools ***/

  //article tools - Email
  $('li.clickabilityEmail').click(function() { 
    pentonTrackEvent = Array('events','event11','event11','o','Content Emailed');
    linkCodeEvent(true);
  });

  //article tools - Saved
  $('li.clickabilitySave').click(function() { 
    pentonTrackEvent = Array('events','event10','event10','o','Content Saved');
    linkCodeEvent(true);  
  });

  //article tools - Printed
  $('li.clickabilityPrint').click(function() { 
    pentonTrackEvent = Array('events','event12','event12','o','Content Printed');
    linkCodeEvent(true);  
  });

  //article tools - Reprinted
  $('li.reprint').click(function() { 
    pentonTrackEvent = Array('events','event8','event8','o','Content Reprinted');
    linkCodeEvent(true);  
  });

/*** end Article Tools ***/


/*** Disqus comment submit ***/
  $('button#dsq-post-button').click(function() { 
    pentonTrackEvent = Array('events','event7','event7','o','Disqus Comment Submit');
    linkCodeEvent(true);  
  });


  /*** Random ***/

  //poll submit
  $('button.pollSubmit').click(function() { 
    pentonTrackEvent = Array('events','event13','event13','o','Poll Submit');
    linkCodeEvent(true);  
  });

  //Whitepapers
  $('div.whitepapers a').click(function() { 
/*
    pentonTrackEvent = Array('events','event2','event2','o','White Paper Function');
    linkCodeEvent(true);  
*/
/*
    var s=s_gi('pentonkrangdev');
    s.linkTrackVars='eVar11,events';
    s.linkTrackEvents='event2';
    s.eVar11='Whitepaper 3';
    s.events='event2';
    s.tl(this,'o','Link Name');
*/

    function linkCodeWhitepaper(obj) {
      s.linkTrackVars='eVar11,events';
      s.linkTrackEvents='event2';
      s.eVar11='Whitepaper Click';
      s.events='event2';
      var lt=obj.href!=null?s.lt(obj.href):"";
      if (lt=="") { s.tl(obj,'o','White Paper Click'); }
    }

    linkCodeWhitepaper(this);

  });  





  /*** end Random ***/

});