var Gilt;
if (!Gilt) Gilt = { };

Gilt.Omniture = { };

Gilt.Omniture.buildSCode = function(s, data) {
  $.extend(s, data);

  var user_identifier = fl.Util.readUserId();
  if (user_identifier == null || user_identifier == "") {
    user_identifier = fl.Util.readHashedCookie("guid");
  }

  // Link tracking cid
  s.campaign = s.getCookieParam('cid');

  /* set tracking data for cached pages from the zeus ifs URL param */
  var ifs = document.location.search.match(/\bifs=([^&]*)?/);
  if (ifs) {
    ifs = decodeURIComponent(ifs[1]);
    var mailing_id = ifs.match(/\bmailing_id=([^&]*)?/);
    if (mailing_id) {
      s.campaign = mailing_id[1];
    }
    var link_type = ifs.match(/\blink_type=([^&]*)?/);
    if (link_type) {
      s.eVar2 = s.campaign + ' ' + link_type[1] + ' ' + s.pageName;
      s.prop3 = s.eVar2;
    }
    // Edialog unique identifer will only come from edialog email links
    var edid = ifs.match(/\bedid=([^&]*)?/);
    if (edid) {
      s.eVar7 = edid[1];
      s.prop7 = s.eVar7;
      // Set the below cookie to hold the edid, this is nessicary since
      // omniture reporting is lost when switching subsites, so this is
      // used to hold the edid value and pass it though to the subsite
      // on single sign on, it should be deleted just before single sign
      // on is excuted.
      fl.Util.setSessionCookie('edid', edid[1]);
    }
  }

  /* fire the registration event only if we have the registration cookie and are not on the registration page */
  if(document.cookie.match('t_register') && !document.location.pathname.match(/^\/(account\/register|bb\/register|invite\/.*)$/)){
    s.events = s.events + ',event4';
  }
  
  /* Set User ID from the user id or guid cookie */
  s.prop6 = user_identifier;
  s.eVar12 = s.prop6;
  return s.t();
};

Gilt.Omniture.homepageLinks = {
    'meta': 'div#carousel-nav-top-bar a',
    'nav':  'div#carousel-nav-bottom-bar a',
    'hero': 'a#promos',
    'inv':  '#sales > div.mosaic_promotion a',
    'cal':  'a#calendar_invite',
    'list': '#sales > div.also_like ul li a',
    'edit': '#sales > div.editors_pick ul li a'
}

Gilt.Omniture.registerLinks = function() {
    for(var name in Gilt.Omniture.homepageLinks) { 
        var selector = Gilt.Omniture.homepageLinks[name]; 
        $(selector).data('slug', name).click(function() {
            var name = $(this).data('slug');

            // Heroes are manually tagged, so pull out the unique string from the link's url
            var extra = "";
            if(name == "hero") {
                var heroUrl = $(this).attr('href');
                if(jQuery.url.setUrl(heroUrl).param("cid")) {
                  extra = "-" + jQuery.url.setUrl(heroUrl).param("cid");
                }
            }

            var campaign = "" + Gilt.Omniture.cid() + name + extra;
            fl.Util.setSessionCookie('cid', campaign);
        });
    }
}

Gilt.Omniture.cid = function() {
    var subsite = identify_domain_name(document.location.host);
    if(subsite) {
        var subsiteInitial = subsite.split('')[0];
    }

    if(subsite == "man" || subsite == "fuse")
        return subsiteInitial

    var categoryInitial = '';
    var categoryMatch = document.location.pathname.match(/^\/sale\/([^\/\?]+)/)
    if(categoryMatch && categoryMatch.length == 2) {
        categoryInitial = categoryMatch[1].split('')[0];
    } else {
        var category = $('#carousel-nav-links-left li.selected').id();
        if(category) {
            categoryInitial = category.replace(/category-/, '').split('')[0];
        }
    }

    return "" + subsiteInitial + categoryInitial;
}

// Call on page load
$(function() { 
  Gilt.Omniture.registerLinks(); 
});
