
/* ----- scanforlinks.js ----- */
/* Scan all links in the document and set classes on them if
 * they point outside the site, or are special protocols
 * To disable this effect for links on a one-by-one-basis,
 * give them a class of 'link-plain'
 */

function scanforlinks() {
    // terminate if we hit a non-compliant DOM implementation
    if (!W3CDOM) { return false; }

    // retrieve the main content area
    contentarea = getContentArea();
    if (!contentarea) { return false; }
    
    
    var fix_links = function(links) {
        for (i=0; i < links.length; i++) {
            if ( (links[i].getAttribute('href'))
                 && (links[i].className.indexOf('link-plain')==-1) ) {
                var linkval = links[i].getAttribute('href');
    
                // check if the link href is a relative link, or an absolute link to
                // the current host.
                if (linkval.toLowerCase().indexOf(window.location.protocol
                                                  + '//'
                                                  + window.location.host)==0) {
                    // absolute link internal to our host - do nothing
                } else if (linkval.indexOf('http') == 0) {
                    links[i].setAttribute('target', '_blank');
                }
            }
        }
    }

    
    
    var links = contentarea.getElementsByTagName('a');
    fix_links(links); 
    
    // get the navigation portlet 
    // NOTE: this is a custom addition for the provost site 
    var nav_area = document.getElementById('portlet-navigation-tree');
    if (nav_area) {
       fix_links(nav_area.getElementsByTagName('a'));
    }
};

registerPloneFunction(scanforlinks);

