var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-28417814-1']);
_gaq.push(['_setDomainName', 'o2online.ie']);
_gaq.push(['_addIgnoredRef', 'o2online.ie']);
_gaq.push(['_trackPageview']);

(function () {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	var gaJsHost2 = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	ga.src = gaJsHost2 + 'google-analytics.com/ga.js';
	
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	
})();


/**
 *	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(
	"bethedifference.o2online.ie","UA-1807669-17",
	"mywidgets.o2online.ie","UA-1807669-15",
	"www.freesims.ie","UA-1807669-8",
	/*"community.o2online.ie","UA-1807669-24", Commented by Neil to try fix issues on community*/
	/* ARF - have their own ga-02 file */
	"dailytreats.o2online.ie","UA-1807669-20",
	"help.o2online.ie","UA-1807669-18",
	"recycle.o2online.ie","UA-1807669-25",
	"refer.o2online.ie","UA-1807669-26",
	"webkit.o2online.ie","UA-1807669-13",
	"ideasstation.o2online.ie","UA-1807669-23",
	/* end */
	
	/* ARF - inline in there */
	"webkit.o2online.ie/ideasroom","UA-1807669-14", 
	"www.o2online-media.ie","UA-1807669-9",
	/* end */
	
	/* ARF - same code as ideasstation.o2online.ie inline in pages, but here to none existing*/
	"cr2009.o2online-media.ie","UA-1807669-21",
	
	/* ARF - Same code as daily??? */
	"specialtreats.o2online.ie","UA-1807669-20",
	
	/* ARF - ??? */
	"localhost","testy testing"
);

/**
 *	Filter which UA channel to use and safely console.log it for easy checking...
 */
var runGA = function(){
	// If statement below added to disable GA on community pages as we are having issues on it.
	if (document.location.toString().indexOf("community.o2online.ie") == -1) {
		if (document.location.toString().indexOf("communitypre.o2online.ie") == -1) {
			for(var filter_value=0;filter_value<gaFilters.length;filter_value=filter_value+2) {
				if (typeof GAAccToUse == "undefined" && gaCurLocation.indexOf(gaFilters[filter_value])>-1) {
					GAAccToUse = gaFilters[filter_value+1];
					filter_value=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);
	// Commented the below line to try fix issue on community.o2online.ie and replaced with 3 lines below - NS
	//_googleInterval = setInterval(activateGoogle,250);
	if(typeof _gat != 'undefined') 
	{
		var pageTracker = _gat._getTracker(GAAccToUse);
		pageTracker._initData();
		pageTracker._trackPageview(gaCurLocation);
	}
	
}

/**
 *	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);
