
/* ----- mark_special_links.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; }

    var container = document.getElementById('akb_body');
    if (container) {
        var links_body = container.getElementsByTagName('a');
        if (links_body) {
            var links_body_len = links_body.length;
        }
        else {
            var links_body_len = 0;
        }
    }
    else {
        var links_body = new Array();
        var links_body_len = 0;
    }
    var container = document.getElementById('portletsWrapper');    
    if (container) { 
        var links_portlet = container.getElementsByTagName('a');
        if (links_portlet) {
            var links_portlet_len = links_portlet.length;
        }
        else {
            var links_portlet_len = 0;
        }
    }
    else {
        var links_portlet = new Array();
        var links_portlet_len = 0;
    }
    
    var links = new Array();
    for (i=0; i<links_body_len; i++) {
        links[i]=links_body[i]
    }
    for (i=0; i<links_portlet_len; i++) {
        links[i+links_body_len]=links_portlet[i]
    }
    links_len = links.length;

    if (!links) { 
        // alert("No links!")
        return false;
    }
    for (i=0; i < links.length; i++) {
        if ((links[i].getAttribute('href'))
         && (links[i].className.indexOf('link')==-1) 
         && (links[i].className.indexOf('visualIconPadding')==-1)
         && (links[i].getElementsByTagName('img').length == 0)
         ) {
            var linkval = links[i].getAttribute('href');
            // check if the link href is a relative link, or an absolute link to
            // the current host.
            var serveraddress = window.location.protocol+'//'+window.location.host.toLowerCase()
            if ((linkval.toLowerCase().indexOf(serveraddress)==0)
             || (linkval.indexOf('resolveuid')==0)
             || (links[i].host==window.location.host)
             ) {
                wrapNode(links[i], 'span', 'link-internal');
            } else if (linkval.indexOf('http:') != 0) {
                // not a http-link. Possibly an internal relative link, but also
                // possibly a mailto or other protocol add tests for relevant
                // protocols as you like.
                protocols = ['mailto', 'ftp', 'news', 'irc', 'h323', 'sip',
                             'callto', 'https', 'feed', 'webcal'];
                // h323, sip and callto are internet telephony VoIP protocols
                for (p=0; p < protocols.length; p++) {
                    if (linkval.indexOf(protocols[p]+':') == 0) {
                        // if the link matches one of the listed protocols, add
                        // className = link-protocol
                        wrapNode(links[i], 'span', 'link-'+protocols[p]);
                        break;
                    }
                }
            } else {
                    wrapNode(links[i], 'span', 'link-external');
                    links[i].setAttribute('target', '_blank');
            }
        }
    }
};

registerPloneFunction(scanforlinks);

