/***
 * This script will add tracking for Google Analytics, when a user clicks on a link to an external site
 * or a link to an e-mail (mailto)
 *
 * Files: 
 * The virtual pageview will register as: "/files/[extension]/[filename]" where [ext] is the file-extension and [filename] is the name of the file
 *
 * Outbound / mailto:
 * The virtual pageview will register as: "/out/[url]" where [url] is the destination url of the link
 * The virtual pageview of mailto-links will register as: "/mailto/[email]" where [email] is the target e-mail-address of the mailto link
 * To exclude this tracking on a link, set the "rel" tag to "notoutbound" like this <a href="http://domain.com" rel="notoutbound">Link</a>
 * To exclude all tracking to certain domains, use the array below
 * 
 * Guava Online Marketing - www.guava.dk
 ***/

var virtualpage_add_ref = true;
var virtualpage_path_out = "/vpv/out/";
var virtualpage_path_mail = "/vpv/mailto/";
var virtualpage_path_files = "/vpv/files/";
var notoutbound_list = new Array();
var thishost = window.location.host;

if (thishost.match(/confex.dk/)) {
	notoutbound_list.push('confex.se');
	notoutbound_list.push('confex.no');
}
if (thishost.match(/confex.se/)) {
	notoutbound_list.push('confex.dk');
	notoutbound_list.push('confex.no');
}
if (thishost.match(/confex.no/)) {
	notoutbound_list.push('confex.se');
	notoutbound_list.push('confex.dk');
}
// notoutbound_list.push('domain.com');

trackOutboundLinks = function() {
  if (_gaq) {

	/* Expression to check for absolute internal links. */
    var isAbsolute = new RegExp("^(https?):\/\/", "i");
    var isMailto = new RegExp("^mailto:", "i");
    var isInternal = new RegExp("^(https?):\/\/" + thishost, "i");

    /* Find all A tags on page and evaluate them for outbound links when they are clicked */
    var links = document.getElementsByTagName("a");
    for (var i=0; i<links.length; i++) {
      links[i].onclick = function() {
		var href = this.getAttribute("href");
      	var rel = this.getAttribute("rel");

        /* Go through list of domains to exclude and match the current link href */
      	var isInExclList = false;
      	if (notoutbound_list.length) {
      		for (var i = 0; i < notoutbound_list.length; i++ ) {      			
      			if (new RegExp(notoutbound_list[i], "i").test(href)) {
      				isInExclList = true;
      			}
      		}
      	}
      	
      	if (href && !(rel && rel.match(/notoutbound/i)) && !isInExclList) {
          var virtualpage = "";

	      /* If the link is a link to an e-mail */
          if (isMailto.test(href)) {
			var email = href.split(":").pop();
			virtualpage = virtualpage_path_mail + email;
		  }

	      /* If the link has absolute address which is not the current domain (an outbound link) */
		  if (isAbsolute.test(href) && !isInternal.test(href)) {
	  		virtualpage = virtualpage_path_out + href;
		  }

		  // Find the path and the potential filename and Filter out get parameters
		  var filename = href.split("/").pop();
		  filename = filename.split("?").shift();
		  // ... and then find extension
		  var ext = filename.split(".").pop();
		  // If the link is a file and the file extension matches known file types, make a virtual pageview call to the page tracker.
		  if (ext.match(/(pdf|doc|xls|ppt|odt|ods|txt|avi|mov|wmv|mp3|mp4|wav|mpg|wma|exe|zip|gz|tar|rar|jpg|jpeg|gif|bmp|tif|tiff|png|flv)/i)) {
			virtualpage = virtualpage_path_files + ext.toLowerCase() + "/" + filename;
		  }

		  if (virtualpage) {
		  	if (virtualpage_add_ref) {
	            // Add location as parameter on the virtual page
	            virtualpage += "?ref=" + location.href;
		  	}
	        _gaq.push(['confexAll._trackPageview', virtualpage]);
	        _gaq.push(['confexNew._trackPageview', virtualpage]);
			//alert(virtualpage); return false; // Use this for testing and debugging
		  }
        }
      }
    }
  }
}

