/* 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'
 */
 
var cat_portal_url = "http://" + document.domain + "/lst";

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
                
                //goddamnit, IE is dumb, relative links have http:// in them anyway.
                //but due to deployment, all links are now absolute.
                linkTypeMagic(links[i]);
                
            } else if (linkval.indexOf('http:') != 0 || isLocalHost(linkval)) {
                // 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', 'feed', 'webcal']; //'https' removed
                             
                protos = {'mailto':'mail_icon', 'ftp':'file_icon', 'news':'newsitem_icon',
                'https':'lock_icon', 'feed':'rss', 'webcal':'event_icon'};
                var undefined;
                // 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
                        if ( links[i].getElementsByTagName('img').length == 0 )
                          //if (navigator.userAgent.indexOf("MSIE")==-1) {
                        //	addClassName(links[i],'link-'+protocols[p]);
                        //  } else if (protos[protocols[p]] != undefined) {
                        	injectNodeWithText(links[i],protos[protocols[p]],protocols[p]+' link');
                        //  }
                    	//wrapNode(links[i], 'span', 'link-'+protocols[p]);
                        break;
                    }
                } // end for

				if (linkval.indexOf('://')==-1 || (linkval.indexOf('http://')==0
					&& isLocalHost(linkval))) {
            	 	// This link is non-protocol, it's an internal, relative link
            	 	// look for an extension
            	 	linkTypeMagic(links[i]);
                }
            
            } 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
                    //if (navigator.userAgent.indexOf("MSIE")==-1) {
                    //	addClassName(links[i],'external');
                    //} else {
			if (!linkTypeMagic(links[i])) //attempt to do file extension for internal links
				injectNodeWithText(links[i],'link_icon','External Link');
                    //}
                    //wrapNode(links[i], 'span', 'link-external');
                    // uncomment the next line if you want external links to be
                    // opened in a new window.
                    // links[i].setAttribute('target', '_blank');
                }
            }
        }
    }
};

function linkTypeMagic(linknode) {
	var undefined; //for compatability
	extensions = {'pdf':'pdf','ppt':'ppt','mov':'vdo','wmv':'vdo',
                			'mpg':'vdo','mpeg':'vdo','asx':'vdo'};
	var linkval = linknode.getAttribute('href');
	var fileparts = linkval.toLowerCase().split('.');
	var extension = fileparts[fileparts.length-1];

	if (extensions[extension] != undefined) {
		//if (navigator.userAgent.indexOf("MSIE")==-1) {
		//   	addClassName(linknode,extensions[extension]);
		//} else {
			injectNodeWithText(linknode,extensions[extension],extension + ' file');
		//}
		return true;
	}
	return false;
}

function injectNodeWithText(node, addedtext,alttext) {
	var isIEsix = (navigator.userAgent.indexOf("MSIE 6")!=-1);
	//var cat_portal_url = '&dtml-portal_url;';
	if (isIEsix) {
		var a = alttext.split(' ')[0];
		if (a == "External" || a == "mailto") return;
		node.innerHTML = node.innerHTML + ' <span class="injected">['+alttext.split(' ')[0]+']</span>';
	} else {
		node.innerHTML = node.innerHTML + '<img class="injected" src="'+cat_portal_url+'/static/images/'+addedtext+'.gif" alt="'+alttext+'"/>';
	}
}

function isLocalHost(link) {
	var locals = ['catalyst.washington.edu','catalysttools.washington.edu,washington.edu/lst'];
	for (var i = 0; i < locals.length; i++) {
		var idx = link.indexOf('://'+locals[i]);
		if (idx != -1 && idx < 11) //check < 11 to make sure it simply doesn't appear later in the url
			return true;
	}
	return false;
}

function wrapNodeWithText(node, wrappertype, wrapperclass,addedtext) {
    // utility function to wrap a node "node" in an arbitrary element of type
    // "wrappertype" , with a class of "wrapperclass"
    wrapper = document.createElement(wrappertype);
    wrapper.className = wrapperclass;
    innerNode = node.parentNode.replaceChild(wrapper,node);
    wrapper.appendChild(innerNode);
    //var cat_portal_url = '&dtml-portal_url;';
    wrapper.innerHTML = wrapper.innerHTML + '<img class="injected" src="'+cat_portal_url+'/static/images/'+addedtext+'.gif" alt=""/>';
}

if (typeof W3CDOM != "undefined") registerPloneFunction(scanforlinks);
