/**
 *	TH20100106: - Filters for different dubdomains
 *	
 *	An indexOf("mywidgets.o2online.ie") > -1 match against document.location.toString() applies
 *	an alternate google analytics account for posting the info to.
 *	"mywidgets.o2online.ie", "UA-1807669-15" is a pair - treat any additions accordingly...
 *	Make sure these filters are good and explicit, first match found will exclude following potential matches...
 *	default is UA-1807669-3 for www.o2online.ie - set below...
 */

var newga = true;
 
 
var gaCurLocation = document.location.toString();
var hostname = document.location.hostname.toString();
var GAAccToUse;
var _googleInterval;
var defaultGAAccount = "UA-1807669-3";

gaCurLocation = gaCurLocation.replace(/ /g,"+");
gaCurLocation = gaCurLocation.replace(/%20/g,"+");

 
var gaFilters = new Array(
	"dailytreats.o2online.ie","UA-1807669-20",
	"bethedifference.o2online.ie","UA-1807669-17",
	"cr2009.o2online-media.ie","UA-1807669-21",
	"specialtreats.o2online.ie","UA-1807669-20",
	"help.o2online.ie","UA-1807669-18",
	"mywidgets.o2online.ie","UA-1807669-15",
	"webkit.o2online.ie/ideasroom","UA-1807669-14",
	"webkit.o2online.ie","UA-1807669-13",
	"www.o2online-media.ie","UA-1807669-9",
	"www.freesims.ie","UA-1807669-8",
	"localhost","testy testing",
	"ideasstation.o2online.ie","UA-1807669-23",
	"community.o2online.ie","UA-1807669-24",
	"recycle.o2online.ie","UA-1807669-25",
	"refer.o2online.ie","UA-1807669-26"
);

/**
 *	Filter which UA channel to use and safely console.log it for easy checking...
 */
var runGA = function(){
	for(f=0;f<gaFilters.length;f=f+2) {
		if (typeof GAAccToUse == "undefined" && gaCurLocation.indexOf(gaFilters[f])>-1) {
			GAAccToUse = gaFilters[f+1];
			f=gaFilters.length;
		}
	}
	if (typeof GAAccToUse == "undefined") GAAccToUse = defaultGAAccount;
	gaCurLocation = gaCurLocation.substring(gaCurLocation.indexOf(hostname)+hostname.length,gaCurLocation.length);
	if (typeof console != "undefined") console.log(GAAccToUse,gaCurLocation,hostname);
	addGoogleTracking(); // call next function
}

/**
 *	Adds the regular google tracking code - with our own modifications...
 */

function addGoogleTracking()
{
	var sc = document.createElement('script');
	sc.type = 'text/javascript';
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	sc.src = gaJsHost + 'google-analytics.com/ga.js';
	document.getElementsByTagName("head").item(0).appendChild(sc);
	_googleInterval = setInterval(activateGoogle,250);
}

/**
 *	Finally, hit the button to track the page with google
 */

function activateGoogle()
{
	if(typeof _gat != 'undefined') 
	{
		clearInterval(_googleInterval);
		var pageTracker = _gat._getTracker(GAAccToUse);
		pageTracker._initData();
		pageTracker._trackPageview(gaCurLocation);
	} else {
		clearInterval(_googleInterval);
	}
}

/**
 *	But don't do any of it until the page is ready...
 */
 
$(document).ready(runGA);
