/*
	Author: Frank Dekervel
	a jquery script that adds dcsMultiTrack to every external link.
	external links start with http:
	
	this script should be included just before the </body> tag.
	
	TODO: 
		- add support for mailto: links
		- better way of detecting external links
	
*/


function reportpdf(event)
{
   var li = this;
	var link = this.href;
	var regex = /^((\w+):\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?.+)?/;
	var match = regex.exec(link);
	if (match) {
		var dcssip = match[6];
		var dcsuri = match[8];
		var ti = link;
		if (li.title) {
			ti = title;
		} else {
			tempti = li.innerHTML.replace(/<[^>]*>/,"");
			if (tempti && tempti.replace(/^\s+|\s+$/g, "") != "") {
				ti = tempti;
			}
		}
		if (match[9]) {
			dcsuri = m
		}
		 dcsMultiTrack('DCS.dcsuri', dcsuri, 'WT.ti', ti)
        //alert('multitrack done href="'+ dcsuri + 'title=' + ti);	
	} else {
		//alert('no matchi');
	}
	
   
}


function reportlink(event) {
	var li = this;
	var link = this.href;
	var regex = /^((\w+):\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?.+)?/;
	var match = regex.exec(link);
	if (match) {
		var dcssip = match[6];
		var dcsuri = match[8];
		var ti = link;
		if (li.title) {
			ti = title;
		} else {
			tempti = li.innerHTML.replace(/<[^>]*>/,"");
			if (tempti && tempti.replace(/^\s+|\s+$/g, "") != "") {
				ti = tempti;
			}
		}
		if (match[9]) {
			dcsuri = m
		}
		dcsMultiTrack('DCS.dcssip',dcssip,'DCS.dcsuri',dcsuri,'WT.ti',ti);
		//alert('multitrack done ip="'+dcssip + '" uri="'+ dcsuri + '" ti="' + ti+'"');	
	} else {
		//alert('no matchi');
	}
}

function scanforlinks(){
  
    if (! document.getElementsByTagName){return false};
    if (! document.getElementById){return false};
    links = document.getElementsByTagName('a');
    for (i=0; i < links.length; i++){      
        if (links[i].getAttribute('href')){
            var link = links[i].getAttribute('href')
            //alert(link.toLowerCase());
            // check if the link href is a relative link, or an absolute link to the current host.
            if (link.toLowerCase().indexOf(window.location.protocol+'//'+window.location.host)==0) {
            	// this is an internal link - check for PDF extension
            	
            	//alert(link.toLowerCase().indexOf('.pdf'));
            	if(link.toLowerCase().indexOf('.pdf')>0)
            	{
            	    //alert('report pdf '+link);
            	    links[i].onclick = reportpdf;
				    if (links[i].captureEvents) links[i].captureEvents(Event.CLICK);
            	}
            	
            } else if (link.indexOf('http:') != 0){
            	// not a http link
            }else{
            	//alert('report link '+link);
            	links[i].onclick = reportlink;
				if (links[i].captureEvents) links[i].captureEvents(Event.CLICK);
            }
        }
    }
}

scanforlinks();

