
/* - linkpopper.js - */
linkpopperWhiteList = [
        window.location.hostname
];

function isInWhiteList(href){
    domain = href.split('/')[2];
    for (var i=0; i<linkpopperWhiteList.length; i++){
        if (domain.indexOf(linkpopperWhiteList[i])!=-1) {
            return true;
        }
    }
    return false;
}

function makeExternalLinksPopup() {
    // Fetch all the a elements in the document.
    var links = document.getElementsByTagName('a');

    // Loop through the a elements in reverse order
    // for speed.
    for (var i = links.length; i != 0; i--) {
        
        // Pull out the element for this iteration.
        var a = links[i-1];
        
        // If the element doesn't have an href, skip it.
        if (!a.href) continue;        
        
        // If the url is not http or https skip it
        if (!a.href.match(/https?:\/\//)) continue;
        
        // If its in the white list skip it
        if (isInWhiteList(a.href)) continue;

        // Set link target to _blank
        a.setAttribute('target', '_blank')
    }
}

registerPloneFunction(makeExternalLinksPopup);


