/* 
 * statisitche sui donwload via WebTrends
 *
 */
 
GA_DEBUG_FLAG=false;
function debugMsg(aMsg) {	if (GA_DEBUG_FLAG) alert(aMsg); }

function webTrendsMediaTracker() {

	regExDomain = new RegExp("^http.*" + document.location.hostname + "", "i");
	sliced = this.href.replace(regExDomain, "");
	debugMsg('media: '+sliced);	
	dcsMultiTrack('DCS.dcsuri', sliced);
}
function webTrendsImgTracker(e) {
	
	try {
		if (e.button == 2 || e.button == 3) {
			regExDomain = new RegExp("^http.*" + document.location.hostname + "", "i");
			sliced = this.src.replace(regExDomain, "");
			debugMsg('image: '+sliced);	
			dcsMultiTrack('DCS.dcsuri', sliced);
		}
	} catch (err) {}
}
function detectLinks() {
	if (typeof dcsMultiTrack != 'function') {
		debugMsg('[ERROR] dcsMultiTrack function not found, check your google analytics installation!');
		return;
	}
	
	debugMsg('[INFO] dcsMultiTrack correctly found.');
	//links list in the current document
	var theLinks = document.getElementsByTagName("a");
	debugMsg('[INFO] hostname='+document.location.hostname);
	//regular expression to retrieve internal link
	regExInt = new RegExp("^http.*(" + document.location.hostname + ").*$", "i");
	//regular expression to retrieve media link (pdf, image ...)
	regExMedia = new RegExp("^.*(\.pdf|\.ppt|\.gif|\.jpeg|\.jpg|\.doc|\.xls|\.xml|\.phps|\.txt|\.zip|\.gz|\.js|\.wmf|\.mpg)$", "i");
	for (i = 0; i < theLinks.length; i++) {
		debugMsg(i+') href='+theLinks[i].href);
		if (theLinks[i].href.substring(0,4)=='http') {
			//is an onclick event already defined?
			if (theLinks[i].onclick==undefined || theLinks[i].onclick==null) {
				//is an internal link?
				if (regExInt.test(theLinks[i].href)) {
					if (regExMedia.test(theLinks[i].href)) {
						debugMsg("[INFO] internal link to media ["+theLinks[i].href+"]");
						theLinks[i].onclick=webTrendsMediaTracker;
					}
				}
			}
		}
	}
	//links list in the current document
	var theImgs = document.getElementsByTagName("img");
	debugMsg('[INFO] hostname='+document.location.hostname);
	//regular expression to retrieve media link (pdf, image ...)
	regExImg = new RegExp("^.*(\.gif|\.jpeg|\.jpg)$", "i");
	for (i = 0; i < theImgs.length; i++) {
		debugMsg(i+') href='+theImgs[i].src);
		if (theImgs[i].src.substring(0,4)=='http') {
			//is an onmousedown event already defined?
			if (theImgs[i].onmousedown==undefined || theImgs[i].onmousedown==null) {
				//is an internal link?
				if (regExInt.test(theImgs[i].src)) {
					if (regExMedia.test(theImgs[i].src)) {
						debugMsg("[INFO] internal link to image ["+theImgs[i].src+"]");
						theImgs[i].onmousedown=webTrendsImgTracker;
					}
				}
			}
		}
	}	
}