/* OAS supporting functions */
function filterSitepageOAS(sitepage) {
    if (sitepage == "") {
        sitepage = "dn.se" + window.location.pathname;
        if (sitepage.substring(sitepage.length - 1, sitepage.length) != "/") {
            sitepage += "/";
        }
        var a = sitepage.split("/");
        if (a.length - 2 == 1 && a[a.length - 1] == "") {
            // section and if nothing to the right of last slash
            sitepage += "hem/";
        }
        else if (a[1] == "blogg" || a[1] == "tema") {
            sitepage += "/";
        }
    }
    else {
        // if last letter is backslash
        if (sitepage.substring(sitepage.length - 1, sitepage.length) != "/") {
            sitepage += "/";
        }
    }

    var last_char = Math.max(sitepage.lastIndexOf("-"), sitepage.lastIndexOf("/"));

    if (last_char >= 0) {
        sitepage = sitepage.substring(0, last_char);
    }

    sitepage.replace("å", "a").replace("ä", "a").replace("ö", "o").replace("Å", "A").replace("Ä", "A").replace("Ö", "O");

    if (sitepage == "dn.se") {
        sitepage = "dn.se/nyheter/ettan";
    }

    return sitepage;
};

// Nielsen content value
function filterSitepageNielsen(sitepage) {
  return sitepage.substring(sitepage.indexOf("/")+1).replace(/\//g, "-");
};

// Check if ad is written or not
function initAdvert(adId) {
    var wrap = document.getElementById("adwr-" + adId);
    if (wrap.getElementsByTagName("*").length > 2) {
        var showAd = true;
        var anchors = wrap.getElementsByTagName("a");

        if (anchors.length > 0) {
            for (var i = 0; i < anchors.length; i++) {
                if (anchors[i].href.indexOf("default/empty.gif") != -1) {
                    showAd = false;
                    break;
                }
            }
        }
        else {
            var scriptTags = wrap.getElementsByTagName("script");
            showAd = scriptTags.length > 1;
        }

        var contr = document.getElementById('HiddenPub' + adId);
        if (showAd) {
            if (contr != null) {
                contr.style.visibility = "visible";
                if (contr.id.indexOf("bottom") != -1) {
                    var bottom = document.getElementById('PubBottom');
                    if (bottom != null) {
                        bottom.style.visibility = "visible";
                    }
                }
            }
            wrap.parentNode.parentNode.className = wrap.parentNode.parentNode.className + " advert-init";
        }
        else {
            if (contr != null && contr.id.indexOf("bottom") != -1) {
                var bottom = document.getElementById('PubBottom');
                if (bottom != null) {
                    bottom.style.visibility = "hidden";
                }
            }
        }
    }
}

// Show ad at this position
function dnAd(position, element, show) {
    if (typeof OAS_delayed == 'undefined') {
        return;
    }
        if (OAS_delayed.match(position) != null) {
            if (show == null) {
                return;
            }
        }
    var wrap = document.getElementById("dnse");
    if (wrap.className.indexOf("oasad") >= 0) {
        OAS_AD(position);
    } else {
        window.Fusion.space(position);
    }
    if (element) {
        initAdvert(element);
    }
}

/* OAS AD CODE */
function getObj(name) {
  if(document.getElementById) {
    this.obj = document.getElementById(name);
  } else if (document.all) {
    this.obj = document.all[name];
  } else if (document.layers) {
    this.obj = document.layers[name]; 
  }
  if(this.obj) {
    this.style = this.obj.style; 
  }
}
function OAS_AD(pos) {
  if(OAS_version >= 11) {
    OAS_RICH(pos);
  } else {
    OAS_NORMAL(pos);
  } 
}
function OAS_NORMAL(pos) {
  document.write('<a href="' + OAS_url + 'click_nx.ads/' + OAS_sitepage + '/1' + OAS_rns + '@' + OAS_listpos + '!' + pos + '?' + OAS_query + '" target=' + OAS_target + '>'); 
  document.write('<img src="' + OAS_url + 'adstream_nx.ads/' + OAS_sitepage + '/1' + OAS_rns + '@' + OAS_listpos + '!' + pos + '?' + OAS_query + '" /></a>');
}

function ShowDelayedAds() {
    if (typeof OAS_delayed == 'undefined') {
        return;
    }
    var ads = OAS_delayed.split(",");
    for (var i in ads) {
            var ad = "";
            $(function() {
                document.write = function(adsource) {
                    ad += adsource;                    
                }
            });
            var div = document.getElementById('HiddenPub' + ads[i]);
            if (div != null) {
                dnAd(ads[i], null, true);
                $('div#HiddenPub' + ads[i]).html(ad);
                initAdvert(ads[i]);
            }
    }
}
