
//            Javascript for automatic tagging File Download Links, External Links and Outbound Links
//            for use with the WiredMinds tracking. 
//            Please include this file in the <BODY> section at the bottom of every page, after the
//            WiredMinds code. 
//            WARNING: This script is using DOM manipulation and  is provided 'AS IS' without any warranties. 
//            It might not be compatible with every webpage. Conflicts with other JavaScripts are possible. 
//            Please test carefully in different browsers.
//            WiredMinds does not guarantee that, by using this script, the web page still works as intendend.
//            +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//            Created by:        WiredMinds AG
//            Last updated:    27-Jul-2011
//            Copyright: WiredMinds AG (www.wiredminds.de)
//            +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


//------CONFIG
var wmat_trackDownlodsAsEvents 	= false; //Track Downloads as Events true/false (if false, they will be tracked as Pages)
var wmat_trackExtLinksAsEvents	= false; //Track ExternalLinks as Events true/false (if false, they will be tracked as Pages)
var wmat_trackMailLinkAsEvents	= false; //Track Mailto Links as Events true/false(if false, they will be tracked as Pages)

var wmat_trackDownlodsWithPath 	= true; //If true, every Download Name will include the path to it. If false, the path is "Downloads|"

var wmat_path_event_downloads	= "DL/";
var wmat_path_event_extlinks	= "ExtLinks/";
var wmat_path_event_mailto		= "MailTo/";

var wmat_path_page_downloads	= "DL|";
var wmat_path_page_extlinks		= "ExtLink|";
var wmat_path_page_mailto		= "MailTo|";

var wmat_debugging				= false;  // set to true to get debugging info into FireBug console
//------END CONFIG

var wmat_links = document.getElementsByTagName("a");
var wmat_linkPathname = "";
var wmat_sourceURL=location.hostname; 

// for each link in the page, add corresponding tracking event
for (var i in wmat_links) {
	if(String(i)!='item' && String(i)!='length' && String(i)!='namedItem'){
		//add tracking
		try {
			var wmat_linkPathname = wmat_links[i].pathname;
			var wmat_linkURL = String(wmat_links[i]);
			var wmat_currentDestinURL=wmat_links[i].hostname; 
			
			//check each type of link: 1. mailTo
			if (wmat_linkURL.match(/^mailto:/i)) {
				wmat_logme('Tracking '+i+' '+String(wmat_links[i])+' as MailTo'); 
				wmat_addListener("mailto",wmat_links[i]);
			}
			//check each type of link: 2. Document
			else if (wmat_compareDomains(wmat_currentDestinURL,wmat_sourceURL)) {
				if(wmat_linkPathname.match(/\.(pdf|txt|doc|docx|xls|ppt|js|vsd|vxd|css|rar|zip|tar|gz|dmg|exe|wma|mov|avi|wmv|mp3|mp4|vcf)$/)) {
					wmat_logme('Tracking '+i+' '+String(wmat_links[i])+' as DOC');
					wmat_addListener("fileorlink",wmat_links[i]);
				}
			}
			//check each type of link: 3. ExernalLink
			else  {wmat_logme('Tracking '+i+' '+String(wmat_links[i])+' as LINK');wmat_addListener("fileorlink",wmat_links[i]);}
		}
		catch(err) { wmat_logme('Tracking error: '+err);}
	}
}



// Add a listener each link
function wmat_addListener(type, link) {
	if (type=="fileorlink") {
		if (link.addEventListener) {link.addEventListener('mousedown', trackfiles_and_extlinks, true);} 
		else if (link.attachEvent) {link.attachEvent("on" + 'mousedown', trackfiles_and_extlinks);}
	}
	if (type=="mailto") {
		if (link.addEventListener) {link.addEventListener('mousedown', trackMailLink, true);} 
		else if (link.attachEvent) {link.attachEvent("on" + 'mousedown', trackMailLink);}
	}	
}

// Track file links
function trackfiles_and_extlinks(currentLink) {
	var file_path = "";
	var destinURL = (currentLink.srcElement) ? currentLink.srcElement.hostname : this.hostname;

	
	// Track an external link
	if (!wmat_compareDomains(wmat_sourceURL, destinURL)){
		file_fullname 	= ((currentLink.srcElement) ? currentLink.srcElement.hostname : this.hostname);
		wm_elem_type 	= "extlink";
		wmat_logme('An ExtLink was clicked!');
	}
	// Track an internal link (a file)
	else {
		file_path = ((currentLink.srcElement) ? "/" + currentLink.srcElement.pathname : this.pathname);	
		var file_details = file_path.split('/');
		var file_fullname =file_details[(file_details.length-1)];
		wm_elem_type = "download";
		wmat_logme('A FILE was clicked!');
	}
	wiredmindsTrack(wm_elem_type, file_fullname);
}

// Generate page view for a mailto: link
function trackMailLink(currentLink) {
	wmat_logme('A MAILTO was clicked!');
	var tmpEmail = ((currentLink.srcElement) ? currentLink.srcElement.href : this.href).split('mailto:');
	var eMail = tmpEmail[1]; // remove 'mailto:' from the string and leave only the e-mail address
	wm_elem_type = "mailto";
	wiredmindsTrack(wm_elem_type, eMail);
}

function wiredmindsTrack(elementType, eventname){
	//If wiredminds is not defined, just return
	if (typeof wiredminds == 'undefined') {
		return false;
	}
	//Track Downloads
	if (elementType == "download") {
		if (wmat_trackDownlodsAsEvents){
			wiredminds.push(['trackEvent', wmat_path_event_downloads+eventname])
		}
		else {
			if (wmat_trackDownlodsWithPath) {
				wiredminds.push(['count', eventname]);
			}
			else {
			wiredminds.push(['setTrackParam', 'wm_page_name', wmat_path_page_downloads+eventname]); 
			wiredminds.push(["count"]);
			}
		}
	}
	//Track External Links
	if (elementType == "extlink") {
		if (wmat_trackExtLinksAsEvents){
			wiredminds.push(['trackEvent', wmat_path_event_extlinks+eventname])

		}
		else {
			wiredminds.push(['setTrackParam', 'wm_page_name', wmat_path_page_extlinks+eventname]); 
			wiredminds.push(["count"]);
		}
	}
	//Track Mailtos
	if (elementType == "mailto") {
		if (wmat_trackMailLinkAsEvents){
			wiredminds.push(['trackEvent', wmat_path_event_mailto+eventname])
			
		}
		else {
			wiredminds.push(['setTrackParam', 'wm_page_name', wmat_path_page_mailto+eventname]); 
			wiredminds.push(["count"]);
		}
	}
}

function wmat_compareDomains(domain1,domain2){
	return wmat_getDomain(domain1)==wmat_getDomain(domain2);
}

function wmat_getDomain(strURL){
	var index = strURL.lastIndexOf(".");
	index = strURL.lastIndexOf(".",index-1);
	
	var topDomain =  strURL.substring(index+1);
	return topDomain;
}

function wmat_logme(myString){
	if(wmat_debugging==true){
		console.log(myString);
	}
}

