/*
 * Called on every omniture event, our opportunity to setup some variables
 */
function omniture_custom_setup(s)
{
  omniture_setup_channels(s);
}

/*
 * sets up the channel and s.prop1-3 variables
 * 
 * These need only be set once, they're a load event
 */
function omniture_setup_channels(s)
{
  if (s.channel)
  {
    // no need to set again
    return;
  }
  
  var pageArr = s.pageName.split(':');
  jQuery.each(pageArr, function(i, val) {
    if (i == 0)
    {
      s.channel = val;
    }
    else
    {
      var propName = 'prop'+i;
      s[propName] = val;
    }
  });
}

/*
 * Call this function from an onclick to register a click on s.prop16.
 * Used for tracking the activity of very specific links
 *
 * @param string name The coded name to record as the click
 */
function omni_internal_link(name)
{
  var s=s_gi(s_account);
  s.linkTrackVars='prop16';
  s.prop16=name;

  // o means "generic custom link", e = exit ink, d = download link
  s.tl(this,'o','Targeted link click');

  // reset the variables
  s.linkTrackVars='';
  s.prop16='';  

  return true;
}

/**
 * Records a tabstack interaction (on prop9) for the given tabstack component
 *
 * This also records an eVar specific to each of the 5 big 5 tab stack components.
 *
 * @param string $name The name of the tabstack component
 */
function tabstack_interaction(name)
{
  var s=s_gi(s_account);

  // set prop9 (support tools) to the name on the tab
  s.prop9 = name;

  // see if this is the big 5 and setup a specific eVar if it is
  var eVarNumber = null;
  if (name == 'frequently asked questions')
  {
    eVarNumber = 'eVar'+19;
  }
  else if (name == 'pay calculator')
  {
    eVarNumber = 'eVar'+20;
  }
  else if (name == 'jobs')
  {
    eVarNumber = 'eVar'+21;
  }
  else if (name == 'calendar')
  {
    eVarNumber = 'eVar'+22;
  }
  else if (name == 'news')
  {
    eVarNumber = 'eVar'+23;
  }

  if (eVarNumber)
  {
    s.linkTrackVars='prop9,'+eVarNumber;
    s[eVarNumber] = 'interaction';
  }
  else
  {
    s.linkTrackVars='prop9';
  }

  // o means "generic custom link", e = exit ink, d = download link
  s.tl(this,'o','Tabstack tab head click');

  s.prop9 = '';

  return true;
}

// register any onload events that may be needed
$(document).ready(function(){
  // register the omni_internal_link on the meganav, guard_forums link
  $('#menu3_menu #guard_forums').click(function(){
    omni_internal_link('meganav_ttas_forums');
    return true;
  });
  $('#menu3_menu #guard_chat').click(function(){
    omni_internal_link('meganav_ttas_chat');
    return true;
  });


  // register the omni_internal_link on the meganav, apply online
  $('#menu1 #menu1-link').click(function(){
    omni_internal_link('meganav_apply_online');
    return true;
  });

  // register the omni_internal_link on the meganav, about the guard link
  $('#menu2 #menu2_link').click(function(){
    omni_internal_link('meganav_about');
    return true;
  });

  // tracking any tab_header clicks (tab stack)
  $('.tab_header').click(function() {
    tabstack_interaction($(this).html().toLowerCase());

    return true;
  });

  // tracking any activity inside the tabs themselves
  $('.sb_content').click(function() {
    var tab_header = $(this).prev();

    // if the tab header cannot be found, just bail
    if (!tab_header.hasClass('tab_header'))
    {
      return;
    }

    tabstack_interaction(tab_header.html().toLowerCase());

    return true;
  });
});

