/*
  SUPERTRACKING
  
  trackbox        - Tracking container - all links inside a trackbox will be tracked
  trackcat-xxx    - Tracking label - when a link in a trackbox is clicked, the closest trackcat will be used for category
  trackact-xxx    - Tracking label - when a link in a trackbox is clicked, the closest trackact will be used for the action label

*/

$(function() {
    
    $.trackbox();

    /*
  $.track("teachforamerica.org/online",         'online-application')
  $.track("/learnmore",                         'learn-more')
  $.track("/learn-more",                        'learn-more')
  $.track("/what-we-do/get-involved",           'get-involved')
  $.track("admissions/attend-an-online-event",  'online-event')
  $.track("/nextgeneration",                    'nextgeneration')
  $.track("/careers/index.htm",                 'careers')
  
  // have vpvs
  $.track("teachforus.org",                     'teachforus',           'vpv/teachforus' )
  $.track("facebook.com/teachforamerica",       'facebook',             'vpv/facebook'   )
  $.track("twitter.com/teachforamerica",        'twitter',              'vpv/twitter'    )
  $.track("youtube.com/user/teachforamerica",   'youtube',              'vpv/youtube'    )
  $.track("secure.entango.com/donate",          'entango',              'vpv/entango'    )
  $.track("wiley.com",                          'wiley-bookstore',      'vpv/tal/wiley'  )
  $.track("teachforamericastore.com",           'teachforamericastore', 'vpv/store'      )
    */
  
  
});

/*  New Stuff - trackboxes to determin actions and labels */

$.trackbox = function() {
  $.each( $('a', ".trackbox"), function(i, link) {
      
      $(link).click( $.trackbox_onclick );
      
  });
}

$.trackbox_onclick = function(event) {
  
  
  var tar = $(event.currentTarget)
  var cat = $.pullcat(tar, 'trackcat')
  var act = $.pullcat(tar, 'trackact')
  var lab = tar.attr('href')
  var vpv = tar.data("vpv")
  
  event.preventDefault()
  setTimeout(function() {
      var href = tar.attr('href');
      //console.log(href);
      if(href.charAt(0) == '/') {
        window.location.pathname = href
      } else {
        window.location = href;
      }
      
  }, 200);
  
  //$.debugTracking(cat, act, lab);
  
  pageTracker._trackEvent(cat, act, lab);
  
  if (vpv) {
    pageTracker._trackPageview(vpv);
  }
  
}

$.pullcat = function(tar, prefix) {
  var result = ''
  prefix = prefix + '-'
  
  var cats = tar.closest('[class*=' +prefix+ ']')
  cats = cats.attr('class')  
  cats.split(' ')
  $.each(cats, function(i, cat) {
    if (cat.indexOf(prefix) == 0) {
      result = cat.replace(prefix, '')
    }
  });
  return result
}


/* Old Stuff - generic tracking based on link href */
/*
$.track = function(url, actionLabel, vpv) {
  $("a[href*="+url+"]").each(function() {
    var link = $(this)
    link.data("actionLabel", actionLabel)
    if (vpv) {
      link.data("vpv", vpv);
    }
    link.click($.trackClick)
  })
}

$.trackClick = function(event) {
  var sectionName = document.location.pathname.substring(1)
  sectionName = sectionName.substring(0, sectionName.indexOf('/'))
  
  var target = $(event.currentTarget)
  var destination = target.attr('href')
  var actionLabel = target.data("actionLabel")
  var vpv = target.data("vpv")
  
  var supercat = ''
  var cats = target.closest("[class*=supercat]")
  cats = cats.attr('class').split(' ')
  for (i=0; i<cats.length; i++) {
    cat = cats[i];
    if (cat.indexOf('supercat-') == 0) {
      supercat = cat.replace('supercat-', '')
    }
  }
  if (!supercat) {
    supercat = sectionName;  // for legacy - use the url if there is no supercat
  }
  
  if (window.console && window.console.firebug) {
    event.preventDefault()
    debug = "== Tracking =="
    debug += "\n category \n   " + supercat
    debug += "\n action \n     " + actionLabel
    debug += "\n label \n      " + destination
    debug += "\n extra \n      " + ""
    
    if (vpv) { debug += "\n VPV \n      " + vpv }
    
    alert(debug);
  }
  
  pageTracker._trackEvent(sectionName, actionLabel, document.location.pathname)
  
  if (vpv) {
    pageTracker._trackPageview(vpv);
  }
}
*/

$.debugTracking = function(cat, act, lab, vpv) {
  
  if (window.console && window.console.firebug) {
    debug = "== Tracking =="
    debug += "\n category \n   " + cat
    debug += "\n action \n     " + act
    debug += "\n label \n      " + lab
    
    if (vpv) { debug += "\n VPV \n      " + vpv }
    
    alert(debug);
  }
  
}
