// ---- SiteCatalyt Click Events ----//

// <a href="#" 
//    ref="sc" 
//    scCategory="AreaTest" 
//    scVars="14" 
//    scVals="Data" 
//    scEvents="5"  
//    scProducts="%TITLE%" 
//    scLabel="test">Test</a>
// 
//    
//    If you'd like to omit value of attributes 
//    in scProducts,scLabel,scCategory and scVals , 
//    you can set the following value to the attributes.
//    
//    %TITLE% = this anchor text
//    %LOC% = location.href(page url)
//    %HREF% = this anchor href
//    

$(function(){
  $("a").click(function () {
    var ref = $(this).attr("ref");
    if (typeof(ref) == 'undefined' || ref != 'sc') return;
    
    // event
    var events = $(this).attr("scEvents");
    if(typeof(events) == 'undefined'){
        events = '';
    } else {
      events = events.split(',');
    }

    // category
    var category = $(this).attr("scCategory");
    if(typeof(category) == 'undefined'){
        category = '';
    } 
    category = rep_val(category, $(this));
    
    // products
    var products = $(this).attr("scProducts");
    if(typeof(products) == 'undefined'){
        products = $(this).text();
    }
    products = rep_val(products, $(this));

    // eVar
    var eVars = $(this).attr("scVars");
    if(typeof(eVars) == 'undefined'){
        eVars = '';
    } else {
      eVars = eVars.split(',');
    }
    
    // eVal
    var eVals = $(this).attr("scVals");
    if(typeof(eVals) == 'undefined'){
        eVals = '';
    } else {
      eVals = eVals.split(',');
    }
    
    // LABEL
    var linkname = $(this).attr("scLabel");
    if(typeof(linkname) == 'undefined'){
        linkname = $(this).text();
    }
    linkname = rep_val(linkname, $(this));
    
    var s=s_gi(s_account);

    // Set linkTrackVars
    var linkTrackVars = '';
    for ( var i=0; i < eVars.length  ; i++  ){
      linkTrackVars +=  "eVar" + eVars[i];
      if (i != eVars.length -1 ){
         linkTrackVars += ",";
      }
    }
    if (products){
       if (linkTrackVars){
          linkTrackVars += ",products";
       } else {
          linkTrackVars += "products";
       }
    }
    if (events.length > 0){
       if (linkTrackVars){
          linkTrackVars += ",events";
       } else {
          linkTrackVars += "events";
       }
    }
    s.linkTrackVars = linkTrackVars  ;
    
    // Set linkTrackEvents
    var linkTrackEvents = '';
    for ( var i=0; i < events.length  ; i++  ){
      linkTrackEvents +=  "event" + events[i];
      if (i != eVars.length -1 ){
         linkTrackVars += ",";
      }
    }
    s.linkTrackEvents = linkTrackEvents;
    // Set events
    s.events = linkTrackEvents;
    // Set products
    s.products = category + ";" + products;
    
    // Set eVar99
    if ( eVars && eVals ){
       for ( var i=0; i < eVars.length  ; i++  ){
            eVals[i] = rep_val(eVals[i], $(this));
            eval( "s.eVar" + eVars[i] + "='" + eVals[i] + "'");
       }
       
    }
    
    s.tl(this,'o',linkname);
  });
  
  function rep_val(val, elem) {
    if (val) {
      val = val.replace('%TITLE%', elem.text(), 'g');
      val = val.replace('%HREF%', elem.attr('href'), 'g');
      val = val.replace('%LOC%', location.href, 'g');
    }
    return val;
  }
});
