// Javascript file for Google analytics
// Copyright Designate PPL 2006 All rights reserved
//
// To use this file you must gain permission of the copyright holder. 
//
// Summery
// Makes sure that the javascript is only run and load once the page has fulled loaded.
// and allows for the transfer to information between one or more domains in the same family. 
// 15-12-2006 This code has not been optimised for subdomains. 


//set for testing to see how each link on the page is handeled, 
var TestScript = false;

// 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);

}

// 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.poferries.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];
        var href = link.getAttribute("href").toLowerCase();

			//check that link is over 8 letters
			if(href.length >= 8) 
			{
				//make sure the first letters are either http:// or https://
				if((href.substring(0, 7) == "http://") || (href.substring(0, 8) == "https://"))
				{
					//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])))		
							{
								isExternalLink = false;
								
								if(j != 0) 
								{
									isFamilyLink = true;
								}
							}
						}
						
						if(!isExternalLink)
						{
							if(TestScript)
							{
								alert("is Not ExternalLink " +href);
							}
						}
							
						if(isFamilyLink) 
						{
							if(TestScript)
							{
								alert("is isFamilyLink " +href);
							}	
							
							var hrefInternal = link.getAttribute("href")
							link.setAttribute("href", "javascript:__utmLinker(\'" +hrefInternal +"\');");
							link.setAttribute("target", "_self")
						}
						
						if(isExternalLink)
						{
							if(TestScript)
							{
								alert("is ExternalLink " +href);
							}
							
							var hrefExternal = link.getAttribute("href")

							hrefExternal = hrefExternal.replace(/http:\/\//gi, "");
							hrefExternal = hrefExternal.replace(/\//gi, "_");
							hrefExternal = hrefExternal.replace(/\./gi, "_");
						
							hrefExternal = "/outboundlink/" +hrefExternal;

        					link.setAttribute("target", "_blank");
        					link.urchinhref = hrefExternal;
           					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; 
	}
}

addOnLoad(initLinks);
addOnLoad(urchinTracker);

