function getCookieValue(name) {
  var cookievalues = document.cookie;
  var start = cookievalues.indexOf(name + "=");
  cookievalues = cookievalues.substring(start);
  var end = cookievalues.indexOf(";");
  if (end == -1) {
    end = cookievalues.length;
  }
  return unescape(cookievalues.substring(name.length + 1, end));
}

function trimcc(input) {
  var splittet = input.split(",");
  var output = '';
  for (i = 0; i < splittet.length; i++) {
    if (splittet[i].indexOf("cc=") == -1) {
      if (i == 0) {
        output = output + splittet[i];
      }
      else {
        output = output + "," + splittet[i];
      }
    }
  }
  if (output.indexOf(".html") == -1) output = output + ".html";
  return output;
}

var showArticleLink = false;
var articleLink = "#";
if (ad_cat == 'article' || ad_cat == 'advertorial' || ad_cat == 'shoppingarticle') {
  document.cookie = "ui_article=" + document.location.href + ";path=/;";
  document.cookie = "ui_slideshow=false;path=/;";
}
else if (ad_cat == 'slideshow') {
  var cookievalues = document.cookie;
  var slideshowid = ad_sc.substring(ad_sc.lastIndexOf('/') + 1);
  var hasSlideshow = cookievalues.indexOf('ui_slideshow=false') > -1 ? false : true;
  var hasArticle = cookievalues.indexOf('ui_article=false') > -1 ? false : true;
  if (hasArticle && hasSlideshow) {
    var oldslideshowid = getCookieValue('ui_slideshow');
    if (oldslideshowid == slideshowid) { // pager in slideshow
      var articleLink = getCookieValue('ui_article');
      showArticleLink = true;
    }
    else { // related slideshow
      document.cookie = "ui_article=false;path=/;";
    }
  }
  if (hasArticle && !hasSlideshow) { // first run of slideshow
    var articleLink = getCookieValue('ui_article');
    showArticleLink = true;
  }
  document.cookie = "ui_slideshow=" + slideshowid + ";path=/;";
}
else {
  document.cookie = "ui_article=false;path=/;";
  document.cookie = "ui_slideshow=false;path=/;";
}

if (showArticleLink) {
  articleLink = trimcc(articleLink);
}