RTW = window.RTW || {}

RTW.TRACKING = {

	gaqFound: false,
	
	conversionCodes: [
		{ 'code': '1060205698', 'label': 'yhOZCLqSjAEQgunF-QM' }
	],

	ecommerceCodes: {
		"journey-planner": {
			orderID: "3337871320833113",
			sku: "Journey Planner",
			productName: "Journey Planner",
			category: "Journey Planner"
		},
		"contact-us": {
			orderID: "8590571320833358",
			sku: "General Enquiry",
			productName: "General Enquiry",
			category: "General Enquiry"
		},
		"product-enquiry": {
			orderID: "5590337088132358",
			sku: "Product Enquiry",
			productName: "Product Enquiry",
			category: "Product Enquiry"
		},
		"travel-butler": {
			orderID: "1871378123330333",
			sku: "Travel Butler",
			productName: "Travel Butler",
			category: "Travel Butler"
		}
	},

	init: function() {
		if(typeof _gaq !== 'undefined') {
			this.gaqFound = true;
		}
	},

	/* Enquiry Success
	** This function will record a virtual page view for the type of enquiry
	** as well as firing off all conversion tracking.
	**/
	enquirySuccess: function(pageCode,callback,scope) {
		callback = callback || function() { return true; };
		scope = scope || this;
		this.trackConversions();
		if(this.gaqFound) {
			_gaq.push(['_trackPageview','/'+pageCode+'/enquiry-success']);
			this.ecommerceTracking(pageCode,'0.00','1');
		}
		callback.call(scope);
	},

	/* Enquiry Failure
	** This function will record a virtual page view for the type of enquiry failure.
	**/
	enquiryFailure: function(pageCode,fieldsFailed,callback,scopefailure) {
		callback = callback || function() { return true; };
		scope = scope || this;
		var self = this;
		if(this.gaqFound) {
			_gaq.push(['_trackPageview','/'+pageCode+'/enquiry-failure']);	
			if(fieldsFailed !== null) {
				$.each(fieldsFailed,function(i,val) {
					self.eventTracking(category,action,label)
				});
			}
		}
		callback.call(scope);
	},

	eventTracking: function() {
		var eventArray = arguments;
		eventArray.unshift('_trackEvent');
		_gaq.push(eventArray);
	},

	ecommerceTracking: function(key,value,quantity) {
		var codes = this.ecommerceCodes[key] || null;
		if(codes !== null) {
			_gaq.push(['_addItem',codes.orderID,codes.sku,codes.productName,codes.category,value,quantity]);
			_gaq.push(['_addTrans',codes.orderID,'',value,'','','','','']);
			_gaq.push(['_trackTrans']);
		}
	},

	trackConversions: function() {
		$.each(this.conversionCodes,function(i,obj) {
			$('body').append('<img height="1" width="1" class="conversionCode" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/'+obj.code+'/?label='+obj.label+'&amp;guid=ON&amp;script=0"/>');
		});
	}
};

$(function() { RTW.TRACKING.init(); })
