// USAGE: to force all links that download assets to go through Eloqua first


// customer's path to the Eloqua redirection script
_ELQ_TRACKING_URI = '/Sites/9/templates/js/elqNow/elqRedir.htm?ref=';
//_ELQ_FILETYPES = new Array('pdf','zip','doc','wmv');  // not currently used

function attachLoadEvent(fn) {
	if (window.addEventListener) {
		window.addEventListener('load',fn,false);
	}
	else if (window.attachEvent) {
		window.attachEvent('onload',fn);
	}
}

// validates a string based on _ELQ_FILETYPES
// Note: function is not currently used
/*
function isValidFiletype() {
	var arrFn = arguments[0].split('.');
	var isValid = false;
	if (arrFn.length > 1) {
		var arrFt = arrFn[arrFn.length-1].split('?');
		for (var i=0; i<_ELQ_FILETYPES.length; i++) {
			isValid = isValid || _ELQ_FILETYPES[i] == arrFt[0].toLowerCase();
		}
	}
	return isValid;
}
*/

function isDigitalAsset(url) {
	var assetFolderName = "/assets/";
	if (url.toLowerCase().indexOf(assetFolderName) === -1 ) {
		return false;
	} else {
		return true;
	}
}

function isUsingLightbox(objLink) {
	var lightboxString = "lightbox";
	var status = false;
	if (objLink.rel && objLink.rel.toLowerCase().indexOf(lightboxString) !== -1) {
		status = true;
	}
	return status;
}

function isQTMovie(url) {
	if (url.toLowerCase().indexOf(".mov") === -1 ) {
		return false;
	} else {
		return true;
	}
}

function sameDomain(url) {
	var currDomain = document.location.hostname;
	if (url.toLowerCase().indexOf(currDomain) === -1) {
		return false;
	} else {
		return true;
	}
}

function hasPageAnchor(url) {
	if (url.indexOf("#") === -1) {
		return false;
	} else {
		return true;
	}
}

// attach the load event
attachLoadEvent(function() {
	if (document.getElementsByTagName) {
	
		var objLinks = document.getElementsByTagName('a');
		var url, objLink;
		for (var i=0; i<objLinks.length; i++) {
			objLink = objLinks[i];
			url = objLink.href;

			/* Prepend the tracking URI whenever:
			   - there's a link to an asset
			   - asset is in the same domain
			   - asset is not a Quicktime movie (or else there's conflict w/ dynamic loading)
			   - link is not using Lightbox (or else asset won't load)
			   - link doesn't have a page anchor (#)
			*/
			if (isDigitalAsset(url) && sameDomain(url) && !isUsingLightbox(objLink) && !isQTMovie(url) && !hasPageAnchor(url)) {
				objLinks[i].href = _ELQ_TRACKING_URI + url;
			}
		}
	
	}
});