// Google Analytics - Custom measurement functions
// (c) Sampsa Suoninen
// 2008-02-15

// Set GA account number (remember to switch when moving from development to production):

//var gaAccount = "UA-3797320-1"; // Production
var gaAccount = "UA-3797320-2"; // Development

//
/* Measurement code start - Do not change anything beyond this line! */
//

function urchinTrack(){
	// Variable default values
	this.urchinDownload = '\.pdf$|\.zip$|\.rar$|\.gz$|\.bz2$|\.exe$|\.tar$|\.7z$'; // Download types or dynamic files defined as downloads, delimiter character: |
	this.urchinAudio = '\.mp3$|\.ogg$|\.wma$|\.3gp$'; // Audio file (podcasts etc.) types that should not be counted as downloads, delimiter character: |
	this.urchinPageName = ''; // Page name (leave empty if you don't know what you're doing)
	this.urchinRSS = '\.rss$|\.xml$'; // RSS link filetypes (can be dynamic), delimiter character: |
	this.urchinInternalDomains = ''; // Comma separated list of domains concidered as internal links (format: '|domain.com|domain.net')
	
	// Start of measurement functionality
	var pageTrack = _gat._getTracker(gaAccount);
	pageTrack._initData(); // New GA data initalization
	
	
		var tmpDomain = document.domain.split(".");
		if(document.domain.search(".co.") == -1) {
			tmpDomain = tmpDomain.slice(-2);
		} else {
			tmpDomain = tmpDomain.slice(-3);
		}
		this.urchinDomain = tmpDomain.join(".");
	
		// Main function, sends Google Analytics "page view" hit.
	this.urchinRun = function(){
		if(this.urchinPageName&&this.urchinPageName != "") {
			this.urchinPageName = this.urchinPageName;
		} else {
			this.urchinPageName = location.pathname + location.search; 
		}
		pageTrack._trackPageview(this.urchinPageName.toLowerCase());
	}
	
	// This function enables tracking of anchor (a) HTML elements. Includes:
	// - Download links
	// - Exit links
	// - Mail links
	// - RSS links
	this.urchinLinks = function(){
		var a = document.getElementsByTagName('a');
		var domain = /^(http|https):\/\/([a-z-.0-9]+)[\/]{0,1}/i.exec(window.location);
		var isDownload = new RegExp("("+this.urchinDownload+")", "i");
		var isAudio = new RegExp("("+this.urchinAudio+")", "i");
		var isInternalLink = new RegExp("("+this.urchinDomain+this.urchinInternalDomains+")", "i");
		var isRSS = new RegExp("("+this.urchinRSS+")", "i");
		for(var i = 0; i < a.length; i++){
			var currentBind = a[i].getAttribute("onclick");
			var actionBind = 'click'; // What event to bind to (do not define "onclick"? Default "click"
			var actionIdent = '/'; // Content group to be used? Default '/'
			// Until a better solution presents itself, change "this.href" to "this.alt", "this.title" or "this.innerHTML" depending on what you want to be the link name
			// Wargning DO NOT USE innerHTML unless you are 110% sure there are no image etc. links with HTML as the link text. Will ruin your day.
			
			if(a[i].protocol != "javascript:") {
				if(a[i].protocol == "mailto:") {
					this.runTracker('email', a[i], a[i].href, actionBind, actionIdent, currentBind, false);
				} else if(this.urchinRSS && isRSS.test(a[i].href)) {
					this.runTracker('rss', a[i], a[i].href, actionBind, actionIdent, currentBind, false);
				} else if(this.urchinDownload && isDownload.test(a[i].href)){
					this.runTracker('downloads', a[i], a[i].href, actionBind, actionIdent, currentBind, false);
				} else if(this.urchinAudio && isAudio.test(a[i].href)){
					this.runTracker('listen', a[i], a[i].href, actionBind, actionIdent, currentBind, false);
				} else if(!isInternalLink.test(a[i].href)) {
					this.runTracker('exit', a[i], a[i].href, actionBind, actionIdent, currentBind, false);
				}
			}
		}
	}
	
	this.runTracker = function(pageType, pageItem, pageName, pageBind, pageIdent, currentBind, pageCondition) {
		switch (pageType)
		{
			case "email":
				urchinEventBind(pageItem, pageBind, function(){ pageTrack._trackPageview('/email'+pageIdent+pageName.substring(7)) + ((currentBind != null) ? ";"+currentBind : ""); }, pageCondition);
				break;
			case "rss":
				urchinEventBind(pageItem, pageBind, function(){ pageTrack._trackPageview('/rss'+pageIdent+pageName.replace(/^(http|https):\/\/([a-z-.0-9]+)\//i, '')) + ((currentBind != null) ? ";"+currentBind : ""); }, pageCondition);
				break;
			case "downloads":
				urchinEventBind(pageItem, pageBind, function(){ pageTrack._trackPageview('/download'+pageIdent+pageName.replace(/^(http|https):\/\/([a-z-.0-9]+)\//i, '')) + ((currentBind != null) ? ";"+currentBind : ""); }, pageCondition);
				break;
			case "listen":
				urchinEventBind(pageItem, pageBind, function(){ pageTrack._trackPageview('/listen'+pageIdent+pageName.replace(/^(http|https):\/\/([a-z-.0-9]+)\//i, '')) + ((currentBind != null) ? ";"+currentBind : ""); }, pageCondition);
				break;
			case "exit":
				urchinEventBind(pageItem, pageBind, function(){ pageTrack._trackPageview('/offsite'+pageIdent+pageName.replace(/^http:\/\/|https:\/\//i, '')) + ((currentBind != null) ? ";"+currentBind : ""); }, pageCondition);
				break;
			default:
		}
	}
	
}

// Helper functions

function urchinEventBind(obj, evt, newhandler, captures)
{
	if (obj.attachEvent)
		obj.attachEvent('on' + evt, newhandler);
	else if (obj.addEventListener)
		obj.addEventListener(evt, newhandler, captures);
	else
	{
		var oldhandler;
		if (oldhandler = obj['on' + evt])
			obj['on' + evt] = function() { oldhandler(); newhandler(); }
		else obj['on' + evt] = newhandler;
	}
}

// End of helper functions

