(function($){
// Write referrer and landing cookies. Requires: webvanta-admin-utils-1.0.0.min.js and jquery.url.js
// If there is no referrerData cookie, write one, otherwise:
if(WebvantaAdmin.readCookie('referrerData') !== null){
  
  // If the referrer domain name contains 'tridentsociety', do nothing, otherwise:
  if($.url(document.referrer).attr('host').indexOf('tridentsociety') < 0){   
    
    // Queue up old data
    var referrer = $.url(decodeURIComponent(WebvantaAdmin.readCookie('referrerData'))),
        landingURL = $.url(decodeURIComponent(WebvantaAdmin.readCookie('landingData'))),
        keywords, source;            
    if (referrer.attr('host').indexOf('yahoo') < 0){
      keywords = referrer.param('q');
    } else {
      keywords = referrer.param('p');
    }      
    source = (landingURL.param('source') !== undefined) ? decodeURIComponent(landingURL.param('source')) : '';    
    
    // Queue up new data
    var newReferrer = $.url(document.referrer),
        newLandingURL = $.url(window.location),
        newKeywords, newSource;
    if (newReferrer.attr('host').indexOf('yahoo') < 0){
      newKeywords = newReferrer.param('q');
    } else {
      newKeywords = newReferrer.param('p');
    }     
    newSource = (newLandingURL.param('source') !== undefined) ? decodeURIComponent(newLandingURL.param('source')) : '';    
    
    // If something has changed, save new cookies
    if(keywords != newKeywords || source != newSource || referrer.attr('host') != newReferrer.attr('host')){
      WebvantaAdmin.writeCookie('referrerData', document.referrer, {path:'/',expires:30});
      WebvantaAdmin.writeCookie('landingData', window.location, {path:'/',expires:30});
    }
  }
} else {
  WebvantaAdmin.writeCookie('referrerData', document.referrer, {path:'/',expires:30});
  WebvantaAdmin.writeCookie('landingData', window.location, {path:'/',expires:30});
}
})(jQuery);
