var _gaq = _gaq || [];
var _filetypes = /\.(zip|exe|pdf|doc|xls|ppt|mp(3|4)|eps|docx|rar|csv|gz|dmg|xml|ashx)$/i;
_gaq.push(['_setAccount', 'UA-514721-3']);
_gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

//-------------------------------
// MANAGING ONLOAD EVENTS
//-------------------------------
function add_load_event(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

//------------------------------
// ADDING TRACKING TO LINKS
//------------------------------
function extend_tracking() {
	var a = document.getElementsByTagName("a");
	for (var i = 0; i<a.length; i++) {
		var href = a[i].getAttribute("href");
		var onclick = a[i].getAttribute("onclick");
		var target = a[i].getAttribute("target");
		var prev_js = ((onclick != null) ? onclick + "; " : "");
		var page = location.pathname;
		if (href) {
			if ((href.match(/^#?https?\:/i)) && (!href.match(document.domain))) {
				var ext_link = href.replace(/^https?\:\/\//i, '').replace(/^#/,'');
				a[i].setAttribute("onclick", prev_js + "_gaq.push(['_trackEvent', 'External links', 'Click', '" + ext_link + "'])");
			} else if (href.match(/^mailto\:/i)) {
				var mail_link = href.replace(/^mailto\:/i, '');
				a[i].setAttribute("onclick", prev_js + "_gaq.push(['_trackEvent','Email', 'Click', '" + mail_link + "'])");
			} else if (href.match(_filetypes)) {
				var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
				a[i].setAttribute("onclick", prev_js + "_gaq.push(['_trackEvent', 'Download', '" + extension + "', '" + href + "'])");
			}
		}
	}
}

add_load_event(extend_tracking);
