// This javascript annotates links with icons that indicate the underlying file type,
// etc. <img class="xLinkIcon"> elements are inserted after link text for .doc, .xls,
// .pps, .ppt, .pdf. 
//
// External links and #top links are also marked. For top links, the link text 
// is replaced with an icon (use 'top' as the default text, so it renders well in non-
// JavaScript clients).
//
// Any links containing /mailto in the href property are assumed to be directed
// to an email form, and are annotated with an email icon.
//
// Links with class="xLinkOff" are skipped altogether.

var xLinkImageAbsDir = '/images/xlink/';
if (typeof(sThemePath) != 'undefined') {
    xLinkImageAbsDir = sThemePath + 'images/';
}

xLinkSetOnLoad();

function xLinkSetOnLoad() {
    // Set the init function to fire after page has loaded
    var oldOnload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = xLinkInit;
    }
    else {
        window.onload = function() {
            oldOnload();
            xLinkInit();
        }
    }
}
function xLinkInit(){
    var imgNodes  = new Array();
    var sTarget, sHref, el;

    if (!document.getElementById) {
        return;
    }

    // Collect list of all 'A' tags
    imgNodes = document.getElementsByTagName('a'); 
    allLinks: for (i = 0; i < imgNodes.length; i++) { 
        el = imgNodes[i];
        sHref = el.getAttribute('href');
        sTarget = el.getAttribute('target');

        // Skip anchor links
        if (sHref == null) {
            continue allLinks;
        }

        // Skip any links that are marked with xLinkOff class
        if (el.className.match('xLinkOff')) {
            continue allLinks;
        }

        if (el.firstChild != null) {
            // Skip linked images...
            if (el.firstChild.tagName == 'IMG') {
                continue allLinks;
            }
        }

        // Assign icons for know file types
        if (sHref.match('\.doc$')) {
            el.innerHTML = el.innerHTML + ' <img alt="[Word]" class="xLinkIcon" src="' + xLinkImageAbsDir + 'xLink-doc.gif">';
            continue allLinks;
        }
        if (sHref.match('\.mp3$')) {
            el.innerHTML = el.innerHTML + ' <img alt="[MP3]" class="xLinkIcon" src="' + xLinkImageAbsDir + 'xLink-mp3.gif">';
            continue allLinks;
        }
        if (sHref.match('\.pdf$')) {
            el.innerHTML = el.innerHTML + ' <img alt="[PDF]" class="xLinkIcon" src="' + xLinkImageAbsDir + 'xLink-pdf.gif">';
            continue allLinks;
        }
        if (sHref.match('\.pp[st]$')) {
            el.innerHTML = el.innerHTML + ' <img alt="[PowerPoint]" class="xLinkIcon" src="' + xLinkImageAbsDir + 'xLink-ppt.gif">';
            continue allLinks;
        }
        if (sHref.match('\.xls$')) {
            el.innerHTML = el.innerHTML + ' <img alt="[Excel]" class="xLinkIcon" src="' + xLinkImageAbsDir + 'xLink-xls.gif">';
            continue allLinks;
        }

        // Check for links to mailer form
        if (sHref.match('javascript:xContact') || sHref.match('javascript:xAuthor')) {
            el.innerHTML = el.innerHTML + ' <img alt="[send email]" class="xLinkIcon" src="' + xLinkImageAbsDir + 'xLink-email.gif">';
            continue allLinks;
        } 
        if (sHref.match('mailto')) {
            el.innerHTML = el.innerHTML + ' <img alt="[send email]" class="xLinkIcon" src="' + xLinkImageAbsDir + 'xLink-email.gif">';
            continue allLinks;
        } 

        // Check for external link
        if ((sTarget != '') & (sTarget != null) & (sTarget != '_top') & (sTarget != '_parent')) {
            el.innerHTML = el.innerHTML + ' <img alt="[external link]" class="xLinkIcon" src="' + xLinkImageAbsDir + 'xLink-external.gif">';
            continue allLinks;
        }

        // Check for links to anchors
        if (sHref.match('\#top')) {
            // Replace HTML with top icon (i.e. this substitutes a 'top' link with a nice top icon)
            el.innerHTML = '<img alt="[top]" class="xLinkIcon" src="' + xLinkImageAbsDir + 'xLink-anchor-top.gif">';
            continue allLinks;
        } 
//        if (sHref.match('\#')) {
//            el.innerHTML = el.innerHTML + ' <img class="xLinkIcon" src="xLink-anchor.gif">';
//            continue allLinks;
//        }
    }
}

