// Javascript file for Google analytics
//
// Summery
// Makes sure that the javascript is only run and loaded once the page has fulled loaded.
// and allows for the transfer to information between one or more domains in the same family. 
// Shows all domains which link away from your site in google analytics as /outboundlinks/
// Only works in broswers which allow modification of the DOM.

// Set goalTrackingURL to enable goal tracking on AJAX / aspx pages that always have the same state.
// 10-6-2007 This code has not been optimised for subdomains. 

//set for testing to see how each link on the page is handeled, 
var TestScript = false;

//Sets all outbound to open in a new broswer window
var externalWindow = false;

//Sets all links to lowercase set to true, if case sesitive is required. 
var isCaseSensitiveLinks = true;

// Get the page elemnts from the DOM by class. 
function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements)
}

function registerEvent(elem, evnt, func, usecapture) {
    if(usecapture == null)
        usecapture = false;
	if (typeof document.addEventListener == "function")
		elem.addEventListener(evnt, func, usecapture);
    else
        elem.attachEvent("on" + evnt, func, usecapture);

}

// The addOnload method by -- Scott Andrew
// makes sure that the page is fully loaded.
function addOnLoad(func) {
    var prevOnLoad = window.onload;
    if(typeof prevOnLoad == 'function') {
        window.onload = function () {
            prevOnLoad();
            func();
        }
    } else {
        window.onload = func;
    }
}

// loops though all the <a links on a page if the link is part of the domain family 
// it makes sure it carries the right google function within to enable cross domain
// tracking, if the link is external it will make sure the liunk is tracked by google 
// as an external 
function initLinks() 
{
	//Array of other domains which will share the same tracking. 
	domainArray = new Array();
	domainArray[0] = document.domain.toLowerCase();
	//domainArray[1] = "www.your-other-domain.com";
	
	// Add any other family domain here. 

	
    var links = document.getElementsByTagName("a");
    if(!links)
        return;
    var i;
    for(i=0; i<links.length; i++) 
    {
        var link = links[i];
        
        if(link.getAttribute("href") != null)
        {
        var href = link.getAttribute("href").toLowerCase();

			//check that link is over 8 letters
			if(href.length >= 8 || href.substring(0, 1) == "/") 
			{
				//make sure the first letters are either http:// or https://
				if((href.substring(0, 7) == "http://") || (href.substring(0, 8) == "https://") || (href.substring(0, 1) == "/"))
				{
					//make sure that the link is not to the current domains 
					domainname = document.domain;
					domainnamelength = domainname.length;

					var isExternalLink = true, isFamilyLink = false;
					
						//loop though the domains which are part of the site to check is they are an internal or external link. 
						for(j=0; j<domainArray.length; j++)
						{
							href = link.getAttribute("href").toLowerCase();
							if(((href.length >= domainArray[j].length + 7) && (href.substring(0, 7+domainArray[j].length) == "http://" + domainArray[j]))
								||
								((href.length >= domainArray[j].length + 8) && (href.substring(0, 8+domainArray[j].length) == "https://" + domainArray[j])) || (href.substring(0, 1) == "/"))		
							{
								isExternalLink = false;
								
								if(j != 0) 
								{
									isFamilyLink = true;
								}
							}
						}
						
						if(!isExternalLink)
						{	
						
						    var hrefInternal					
							//set links to lowercase
							if(isCaseSensitiveLinks)
							{
							    hrefInternal = link.getAttribute("href");
							}
							else
							{
							    hrefInternal = link.getAttribute("href").toLowerCase();
							    var hrefInternaltrimmed = hrefInternal.replace(/^\s+|\s+$/g, '');
							    link.setAttribute("href", hrefInternaltrimmed);
							}
							
							if(TestScript)
							{
								alert("is Not ExternalLink " +hrefInternal);
							}
						}
						
						//Amend the link with the right javascript if its part of the domain family	
						if(isFamilyLink) 
						{						
							var hrefInternal
							
							if(isCaseSensitiveLinks)
							{
							    hrefInternal = link.getAttribute("href");
							}
							else
							{
							    hrefInternal = link.getAttribute("href").toLowerCase();
							}
							
							if(TestScript)
							{
								alert("is isFamilyLink " +hrefInternal);
							}	
							
							var hrefInternaltrimmed = hrefInternal.replace(/^\s+|\s+$/g, '');
							link.setAttribute("href", "javascript:__utmLinker(\'" +hrefInternaltrimmed +"\');");
							link.setAttribute("target", "_self")
						}
						
						//add all external links to a folder in Google Anaylytics called /outbound/
						if(isExternalLink)
						{
							var hrefExternal
							if(isCaseSensitiveLinks)
							{
							    hrefExternal = link.getAttribute("href");
							}
							else
							{
							    hrefExternal = link.getAttribute("href").toLowerCase();
							}
							
							if(TestScript)
							{
								alert("is ExternalLink " +hrefExternal);
							}
							
							var hrefExternaltrim = hrefExternal.replace(/^\s+|\s+$/g, '');

							hrefExternaltrim = hrefExternaltrim.replace(/http:\/\//gi, "");
							hrefExternaltrim = hrefExternaltrim.replace(/\//gi, "_");
							hrefExternaltrim = hrefExternaltrim.replace(/\./gi, "_");
						
							hrefExternaltrim = "/outboundlink/" +hrefExternaltrim;

                            if(externalWindow == true)
                            {
        					    link.setAttribute("target", "_blank");
        					}
        					
        					link.urchinhref = hrefExternaltrim;
           					registerEvent(link, "click",function(evt){
								var target;
								if(evt)
									target = evt.srcElement;
								if(target == null)
									target = this;
	                            var i = 0;
		                     while(target.tagName.toLowerCase() != "a") 
		                     {
			                        target = target.parentNode;
				                    i++;
					                if(i == 5)
						                return;
							        if(!target)
								        return;
							}
                            if(target.urchinhref)
            				    urchinTracker(target.urchinhref);
        					});
        				}
					}
				
			}
			}
	}
	SetGoogleMultiDomain(domainArray);
}

function SetGoogleMultiDomain(domainArray)
{
	if(domainArray.length > 1)
	{
		_udn="none";
		_ulink=1; 
	}
}

function SetGoogleGoalTracking()
{
    urchinTracker(goalTrackingURL);
}


//Run
addOnLoad(initLinks);

if(goalTrackingURL == null)
{
    addOnLoad(urchinTracker);
}
else
{
    addOnLoad(SetGoogleGoalTracking);
}