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() {
    var link_elements = ['a', 'area'];

    for (var j=0; j<link_elements.length; j++) {
        // Fetch all the elements for the given tag in the document.
        var links = document.getElementsByTagName(link_elements[j]);

        // 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 elem = links[i-1];
	    
	    // If the element doesn't have an href, skip it.
	    if (!elem.href) continue;        
	    
	    // If the url is not http or https skip it
	    if (!elem.href.match(/https?:\/\//)) continue;
	    
	    // If its in the white list skip it
	    if (isInWhiteList(elem.href)) continue;
	    
	    // Set link target to _blank
	    elem.setAttribute('target', '_blank')
        }
    }
}

registerPloneFunction(makeExternalLinksPopup);
