
// REQUIRES:
//	site-specific.js (which must provide a function googleAccountNumber)
//

// this object is for private use in this file. use access functions instead
// of modifying properties directly.
//
GGlob = {
	convString : "",
	forcePageView : "",
	doneMainTracking : false,
	extraTracks : new Array,
	local : runningLocally(),
	testMode : false,
	
	phoneHome : function() { return !GGlob.testMode && !GGlob.local; },
	trace : function(x) {
		if ( GGlob.local && GGlob.testMode) {
			try { console.log(x); }
			catch(e) { alert(x); }
		}
	}
};

addEvent( window, "load", setupGoogle, true);


//===== PUBLIC

// call this any time BEFORE the page has finished loading to have google
// track a custom page view, rather than the actual url. this is useful
// for conversion tracking, when the a page (e.g., thankyou.asp) is called
// for failed conversions as well as successful ones.
//
function forceGooglePageView( page) {
	GGlob.forcePageView = page;
}

// for tracking multiple pages/actions on a single page
//
function extraGoogleTrack( page) {
	if ( GGlob.doneMainTracking && !GGlob.local)
		urchinTracker( page);
	else
		GGlob.extraTracks.push( page);
}


//===== END PUBLIC

// don't call this directly; it's automatically called on page load
function setupGoogle() {

	// we can't modify the DOM until after the body loads!
	var e = document.createElement("script");
	
	if (location.protocol.toLowerCase() == "https:")
		e.src = "https://ssl.google-analytics.com/urchin.js";
	else
		e.src = "http://www.google-analytics.com/urchin.js";
	e.language= "javascript";
	e.type = "text/javascript";
	document.getElementsByTagName("head")[0].appendChild(e);
	googleFollowThru();
}

function googleFollowThru() {
	if ( typeof(urchinTracker) == 'undefined')	// the new script needs parsing
		setTimeout( googleFollowThru, 100);
	else if ( typeof(googleAccountNumber) != 'undefined') {
		_uacct = googleAccountNumber();
		GGlob.trace( "tracking page: " + (GGlob.forcePageView || '(current url)'));
		if ( GGlob.phoneHome())
			urchinTracker( GGlob.forcePageView );	// fine if this str is empty
		GGlob.doneMainTracking = true;

		// do the extra tracking, if requested
		//		
		for( var i=0, ct=GGlob.extraTracks.length; i<ct; ++i) {
			GGlob.trace( "tracking extra: " + GGlob.extraTracks[i]);
			if ( GGlob.phoneHome())
				urchinTracker( GGlob.extraTracks[i]);
		}
		GGlob.extraTracks.length = 0;
		
		// run the conversion script
		if ( GGlob.convString.length > 0 )
			googleConversionFollowThru();
	}
}

//--- for conversion tracking. usage:
//
//		convDetails = new GoogleConversionDetails( 23443, 25, 'US');
//		(OPTIONAL) convDetails.addLineItem( 'ENX-DONATION', "Donation", "Donation-monthly", '50', 1);
//		googleConversion( convDetails);
//

		GoogleConversionDetails = function( orderID, amt, country) {
			this.orderID = orderID;
			this.amount = amt;
			this.country = country;
			this.lineItems = '';
		}
		GoogleConversionDetails.prototype.addLineItem = function( sku, prodName, category, price, qty) {
			var li = 'UTM:I|'+this.orderID+'|'+sku+'|'+prodName+'|'+category+'|'+price+'|'+qty;
			if ( this.lineItems.length > 0) this.lineItems += '\n';
			this.lineItems += li;
		}
		GoogleConversionDetails.prototype.toString = function() {
			var s = 'UTM:T|'+this.orderID+'||'+this.amount+'|0|0|||'+this.country;
			if ( this.lineItems.length > 0) s += '\n' + this.lineItems;
			return s;
		}

function googleConversion( convDetails) {
	GGlob.convString = convDetails.toString();
	
	// run the conversion NOW if the main script has already loaded
	if ( typeof(urchinTracker) != 'undefined')
		googleConversionFollowThru();
}

function googleConversionFollowThru() {
	var f, ta;
	f = document.createElement('form');
		f.style.cssText = 'display:none';
		f.setAttribute('name','utmform');
	ta = document.createElement('textarea');
		ta.id = 'utmtrans';
		ta.appendChild( document.createTextNode(convDetails));
	f.appendChild( ta);
	document.body.appendChild(f);

	GGlob.trace( "conversion track: " + GGlob.convString);
	if ( GGlob.phoneHome())
		__utmSetTrans();
	GGlob.convString = '';		// prevent resubmission
}
