// Google Analytics - Custom measurement functions
// (c) Satama Interactive 2007
// 2007-1-26

// Global variables:
var urchinDownloadExtensions="pdf,zip,rar,gz,bz2,exe"; // Download extensions in a comma separated list
var urchinDynamicDownloadPaths=""; // Dynamic download paths (example "download.php")
/* Fill in the domain name if measuring subdomains to same account. 
If several domains for same account is necessary, consult the manual */
var tmpDomain = document.domain.split(".");
if(document.domain.search(".co.") == -1) {
  tmpDomain = tmpDomain.slice(-2);
} else {
  tmpDomain = tmpDomain.slice(-3);
}
var urchinDomain = tmpDomain.join(".");
var urchinCampaign = 0; // Set this value 1 if campaign tracking is used
// Define variable names for campaign URL query string, if the google defaults won't work (see documentation for details)
// These three variables need to be defined
//var urchinCampaignName="camp";
//var urchinCampaignSource="adver";
//var urchinCampaignMedium="type";
// Leave the next two values blank if they are not used 
//var urchinCampaignTerm="kwd";
//var urchinCampaignVersion="version"; 

/*
// Functions start here, no need to edit below this line
*/
var uTarget=""; // Target page, always empty by default

function urchinLockTarget(evt){
	if (evt.preventDefault&&evt.target.href){
		evt.preventDefault();
		uTarget=evt.target.href;
	}
}

function urchinUnlockTarget(evt){
	if (uTarget.length>0){
		window.location=uTarget;
		uTarget="";
	}
}

function urchinEvent(evt,tag){
	var e=evt.target||evt.srcElement;
	while (e.tagName&&(e.tagName!=tag)){
		e=e.parentElement||e.parentNode;
	}
	return e;
}

function urchinEventBind(elem,event,func){
	if ((typeof(window[func])=="function")&&elem){
		if (elem.addEventListener){
			elem.addEventListener(event, window[func], true);
		}
		else if(elem.attachEvent){
			elem.attachEvent("on"+event, window[func]);
		}
	}
}

function urchinTrackFrame(win) {
	urchinEventBind(win.document.body, "click", "urchinLinkTrack");
	urchinEventBind(win.document.body, "click", "urchinFormButton");
	urchinEventBind(win.document.body, "mousedown", "urchinRightClick");
	for (var i = 0; i < win.frames.length; i++) {
		urchinTrackFrame(win.frames[i]);
	}	
}

function urchinTrackFrames() {
	for (var i = 0; i < window.frames.length; i++) {
		urchinTrackFrame(window.frames[i]);
	}	
}

function urchinEventTrack() {
	urchinEventBind(window.document.body, "click", "urchinLinkTrack");
  urchinEventBind(window.document.body, "click", "urchinFormButton");
	urchinEventBind(window.document.body, "mousedown", "urchinRightClick");
	urchinEventBind(window, "load", "urchinTrackFrames");
}

function urchinRightClick(evt){
	evt=evt||(window.event||"");
	var urchinPageName = "";
	if (evt){
		var btn=evt.which||evt.button;
		if (btn!=1){
			var e=evt.target||evt.srcElement;
			if (urchinCheckProtocol(e)){
				var types=urchinDownloadExtensions;
				if (types.indexOf(e.pathname.substring(e.pathname.lastIndexOf(".")+1,e.pathname.length))!=-1){
					var urchinDownloadPathName=e.pathname?((e.pathname.indexOf("/")!=0)?"/"+e.pathname:e.pathname):"/";
					urchinPageName = "/Download" + urchinDownloadPathName;
				}
			}
			// Offsite links
			if(urchinPageName == "")
			{
				if (e.hostname&&!urchinInternalDomain(e.hostname)){
					if (e.protocol&&(e.protocol != "javascript:")) {
						var path=e.pathname?((e.pathname.indexOf("/")!=0)?"/"+e.pathname:e.pathname):"/";
						urchinPageName="/Offsite RightClick/" + e.hostname;
					}
				}
			}
			if(urchinPageName != "") {
				urchinTracker(urchinPageName);
				urchinPageName="";
			}
		}
	}
}

// SATAMA modifications
// Code section for Track form button clicks.
function urchinFormButton(evt){
	evt=evt||(window.event||"");
	if (evt){
		var e=urchinEvent(evt,"INPUT");
		var type=e.type||"";
		if (type&&((type=="submit")||(type=="image"))||((type=="text")&&((evt.which||evt.keyCode)==13))){
			// Use e.form.action if necessary
			urchinPageName = e.form.id;
			urchinTracker("/Form/" + urchinPageName);
			urchinPageName="";
		}
	}
}

function urchinGetContents(e) {
	if (e.nodeType == 3) return e.nodeValue;
	var contents = new Array();
	var i=0;
	while(e.childNodes[i]) {
		contents[contents.length] = urchinGetContents(e.childNodes[i]);
     	i++;
	}
	return contents.join("");
}

function urchinInternalDomain(hostName){
		var Domains=urchinDomain;
    var allDomains=Domains.split(',');
    for (var i=0;i<allDomains.length;i++){
		if (hostName.indexOf(allDomains[i])!=-1){
		       return 1;
		}
    }
    return 0;
}

function urchinCheckProtocol(e){
	return (e.href&&e.protocol&&(e.protocol.indexOf("http")!=-1))?true:false;
}

function urchinLinkTrack(evt){
	var doUrchinLinkTrack = 0;
	evt=evt||(window.event||"");
	if (evt){
		var e=urchinEvent(evt,"A");
		
		// Email links
		if (doUrchinLinkTrack == 0) {
			if (e.protocol=="mailto:"){
				_udl.hostname="";
				urchinPageName="/Email/"+e.href.substring(7)+".email";
				doUrchinLinkTrack = 1;			
			}
		}
		
		// Static and dynamic download links
		if (doUrchinLinkTrack == 0) {
			if (e.pathname) {
				var doDownloadTrack = false;
				// Check for static download extensions			
				var types=urchinDownloadExtensions;
				doDownloadTrack = (types.indexOf(e.pathname.substring(e.pathname.lastIndexOf(".")+1,e.pathname.length)) >= 0);
				if (!doDownloadTrack) {
					// Check for dynamic download paths
		   			var DLPaths = urchinDynamicDownloadPaths.split(',');
	   				for (var i = 0; i < DLPaths.length; i++) {
		   				if (DLPaths[i].length > 0) {
							if (e.pathname.indexOf(DLPaths[i]) != -1) {
								doDownloadTrack = true;
								break;
							}
						}
					}
				}
				// Request download tracking if necessary
				if (doDownloadTrack) {
					var path=e.pathname?((e.pathname.indexOf("/")!=0)?"/"+e.pathname:e.pathname):"/";
					
          urchinPageName="/Download" + path;
					doUrchinLinkTrack = 1;			
				} 
			}
		}

		// Offsite links
		if (doUrchinLinkTrack == 0) {
			if (e.hostname&&!urchinInternalDomain(e.hostname)){
				if (e.protocol&&(e.protocol != "javascript:")) {
					var path=e.pathname?((e.pathname.indexOf("/")!=0)?"/"+e.pathname:e.pathname):"/";
					
					urchinPageName="/Offsite/" + e.hostname;
					doUrchinLinkTrack = 1;
				}
			}
		}
				
		// Send hit if requested
		if (doUrchinLinkTrack != 0) {
			var qry=e.search?e.search.substring(e.search.indexOf("?")+1,e.search.length):"";
			urchinLockTarget(evt);
			urchinTracker(urchinPageName);
			urchinUnlockTarget();
			urchinPageName="";
		}
	
	}
}

function urchinFunc(func){
	if (typeof(window[func])=="function"){
		window[func]();
	}
}
function urchinRun(urchinPageName,urchinContentGroup) {
	if(urchinDomain != "") {
		var _udn=urchinDomain;
	}
	if(urchinCampaign != 0)
	{
		var _uccn=urchinCampaignName;
		var _ucsr=urchinCampaignSource;;
		var _ucmd=urchinCampaignMedium;
		var _uctr=urchinCampaignTerm;
		var _ucct=urchinCampaignVersion;
	}

	if(urchinPageName != "") {
		urchinTracker(urchinPageName);
	} else {
		urchinTracker();
	}
	urchinEventTrack();
}

