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

    contentarea = getContentArea();
    if (!contentarea) { return false; }

    links = contentarea.getElementsByTagName('a');
    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
                if (linkval.toLowerCase().indexOf('media/videos') != -1
                    && window.location.pathname.indexOf('media/videos') == -1
                    && linkval.toLowerCase().indexOf('/media/videos/folder_contents') == -1
                    && linkval.toLowerCase().lastIndexOf('/media/videos/') != linkval.length - 14){
                    links[i].onclick = videoPopup;
                }
            } 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.
                protocol = false;
                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]);
                        protocol = true;
                        break;
                    }
                }
                if (!protocol && linkval.toLowerCase().indexOf('media/videos') != -1
                      && window.location.pathname.indexOf('media/videos') == -1
                      && linkval.toLowerCase().indexOf('/media/videos/folder_contents') == -1
                      && linkval.toLowerCase().lastIndexOf('/media/videos/') != linkval.length - 14){
                    links[i].onclick = videoPopup;
                }
            } else {
                // we are in here if the link points to somewhere else than our
                // site.
                if ( links[i].getElementsByTagName('img').length == 0 ) {
                    // we do not want to mess with those links that already have
                    // images in them
                    wrapNode(links[i], 'span', 'link-external');
                    // if you want external links to be
                    // opened in a new window, let this line with target=blank.
                    links[i].setAttribute('target', '_blank');
                }
            }
        }
    }

    photops = contentarea.getElementsByTagName('p');
    for (i=0; i < photops.length; i++) {
        if (photops[i].className == 'photo') {
            links = photops[i].getElementsByTagName('img');
            for (j=0; j < links.length; j++) {
                links[j].parentNode.onclick = videoPopup;
            }
        }
    }
};


function videoPopup(e){
    url = ''+this;
    if (url.lastIndexOf('/view') == url.length - 5){
        url = url.substr(0, url.length - 5);
    }
    window.open(url + '/popup_video', 'Video', 'width=350,height=350');
    return false;
}

registerPloneFunction(scanforlinks);


