/** 
 * @fileoverview This file is to be used for implementing Google Analytics on a website.
 * It is a wrapper class around the GA tracker class and allows you to add custom functionality
 * to GA tracking.
 *
 * @author Andre Wei andrewei@vkistudios.com
 */

 /* Function List
  * Privileged Function list
  * addListener
  * getBaseDomain
  * addTrans
  * addItem
  * trackTrans
  * tagCrossDomainLinks
  */
 
var GAWebPropID = '';

if (location.hostname.search(/vkistudios.net/) > -1)
	var GAWebPropID = "UA-5002093-1"; // Test GA account to track data to
else
	var GAWebPropID = "UA-3626409-2"; // Production GA account to track data to
  
var GACrossDomainsList = "wdt.sax.softvoyage.com,canadatravels.com"; // comma-separated list of base domains to treat as cross-domain.
var numBaseDomainParts = 2; // number of parts in the base domain.  ie www.example.com = 2, www.example.co.uk = 3

/* BEGIN: VKIGA Class */

/**
 * Construct a new VKIGA object.
 * @class This is the GA wrapper class
 * @constructor
 * @param {string} crossDomainsList Comma-separated list of base domains to treat as cross-domain
 * @param {int} numDomainParts Number of parts in the base domain
 * @returns A new VKI tracker
 */
	  
function VKIGA (crossDomainsList, numDomainParts) {

	var baseDomain = "";
	var numBaseDomainParts = numDomainParts;
	var vkiCookie = "__utmvki";
	
	// set the base domain
	
	var splitHost = document.location.hostname.split('.');
	
	for (var i = 1; i <= numBaseDomainParts; i++) {
		
		baseDomain = '.' + splitHost[splitHost.length - i] + baseDomain;
	}
	
	baseDomain = baseDomain.substr(1);
	
	/**
	 * Comma-separated list of base domains to treat as cross-domain
	 * @private
	 * @type string
	 */
	var crossDomains = "";
	
	if (crossDomainsList != null && typeof(crossDomainsList) == "string")
		crossDomains = crossDomainsList;
	
	/* BEGIN: Privileged Functions */
	
	/**
	 * Returns the base domain of the website
	 *
	 * @privileged
	 */
	this.getBaseDomain = function() {
		return baseDomain;
	}
	
	/**
	 *  Adds event handler for specified event to an element
	 *  
	 * @privileged
	 * @param {object} element Element to add event listener to
	 * @param {string} type Event to listen for.  Do not prepend event with 'on', as the functions automatically prepends it
	 * @param {object} expression Javascript function to execute on event.  Can be either a function name or anonymous function
	 * @param {boolean} bubbling Sets whether to register the event on bubbling phase (true) or capturing phase (false).  Only applies to W3C compliant browsers.
	 * @returns	True on success, false on failure
	 * @type boolean
	 */
	this.addListener = function (element, type, expression, bubbling) {
		bubbling = bubbling || false;
		
		if (window.addEventListener) { // Standard
			element.addEventListener(type, expression, bubbling);
			return true;		
		} else if(window.attachEvent) { // IE
			element.attachEvent('on' + type, expression);
			return true;
		} else
			return false;
	}
	
	/**
	 *  Adds transaction to GA tracking object
	 *  
	 * @privileged
	 * @param {string} unique order ID
	 * @param {string} affiliate store
	 * @param {string} order total excluding taxes and shipping
	 * @param {string} tax amount
	 * @param {string} shipping amount
	 * @param {string} city of customer
	 * @param {string} state of customer
	 * @param {string} country of customer
	 */
	 
	this.addTrans = function (orderID, affiliate, total, tax, shipping, city, state, country) {
		try {
			VKIPageTracker._addTrans(orderID, affiliate, total, tax, shipping, city, state, country);
		}
		catch (e) {}
	}
	
	/**
	 *  Adds item to GA tracking object
	 *  
	 * @privileged
	 * @param {string} unique order ID
	 * @param {string} unique SKU/identifier of the product
	 * @param {string} descriptive product name
	 * @param {string} category of product
	 * @param {string} unit price of product
	 * @param {string} quantity purchased
	 */
	this.addItem = function (orderID, sku, productName, category, price, quantity) {
		try {
			VKIPageTracker._addItem(orderID, sku, productName, category, price, quantity);
		}
		catch (e) {}
	}
	
	/**
	 *  Sends transaction tracking request to GA if the order hasn't already been sent
	 *  
	 * @privileged
	 * @param {string} unique order ID
	 */
	this.trackTrans = function (orderID) {
		try {
			if (this.checkSetTrans(orderID)) {
				VKIPageTracker._trackTrans();
			}
		}
		catch (e) {}
	}
	
	/**
	 *  Checks if order has already been sent or not.  Sets session cookie to track orders that have been sent.
	 *  
	 * @privileged
	 * @param {string} unique order ID
	 */
	 
	this.checkSetTrans = function (orderID) {
		// Check if this transaction has been sent to GA before and this is a reload of the thankyou page
		var strCookieName = vkiCookie;
		var strCookieValue = 'e.' + orderID;
		var strControlCookie = this.readCookie(strCookieName) || '';
		var isNew = (strControlCookie.search(strCookieValue) == -1);
		this.createCookie(vkiCookie, strCookieValue, 1);
		return isNew;
	}
	
	/**
	 *  Creates cookie
	 *  
	 * @privileged
	 * @param {string} cookie name
	 * @param {string} cookie value
	 * @param {string} days until cookie expires
	 * @param {string} optional - domain to set the cookie for
	 */
	 
	this.createCookie = function (name, value, days, cookieDomain) {
	
		var domain = "";
		var expires = "";
		
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = " expires="+date.toGMTString() + ";";
		}
		
		if (typeof(cookieDomain) != 'undefined')
			domain = " domain=" + cookieDomain + "; ";
		
		document.cookie = name + "=" + value + ";" + expires + domain + "path=/";
	}
	
	/**
	 *  Reads cookie
	 *  
	 * @privileged
	 * @param {string} cookie name
	 * @returns	value of the cookie, or null if cookie not set
	 * @type mixed
	 */
	 
	this.readCookie = function (name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}

	/**
	 *  Deletes cookie
	 *  
	 * @privileged
	 * @param {string} cookie name
	 */
	 
	this.eraseCookie = function (name, cookieDomain) {
		cookieDomain = cookieDomain || getDomainName();
		this.createCookie(name, "", -1, cookieDomain);
	}
	
	this.splitQS = function (url) {		
		
		var url = url.substring(url.indexOf('?') + 1);
		var params = url.split('&');
		var delimPos;
		var ret = new Object();
		
		for (var i = 0; i < params.length; i++) {
			delimPos = params[i].indexOf('=');
			name = params[i].substring(0, delimPos);
			val = params[i].substring(delimPos + 1);
			ret[name] = val;
		}
		
		return ret;
	}
	
	/* END: Priviledged Functions */
	
	/* BEGIN: Optional Functionality */
	
	/**
	 * Appends GA cookie information to all anchor tags that are cross-domain links 
	 *
	 * @privileged
	 */
	this.tagCrossDomainLinks = function() {
		
		if (typeof(VKIPageTracker) == "object") {
			// check if getElementsByTagName function exists and we are doing cross-domain tracking
			if ((typeof(document.getElementsByTagName) == "function" || typeof(document.getElementsByTagName) == "object") && crossDomains != "") {
				var anchors = document.getElementsByTagName("a");
				var forms = document.getElementsByTagName("form");
				var domainsList = crossDomains.split(",");
				var i, j, k, form, anchor, params, param, delimPos, name, val;
				
				// loop through all anchor tags
				for (i = 0; i < anchors.length; i++) {
					anchor = anchors[i];
					
					// loop through all our list of cross-domains
					for (j = 0; j < domainsList.length; j++) {
						// don't tag links to this base domain
						
						if (baseDomain != domainsList[j]) {
							regexp = new RegExp("^http://.*" + domainsList[j] + "(/?.*)?");
							
							// if the link matches, append cookie information to link
							if (regexp.test(anchor.href)) {
								anchor.href = VKIPageTracker._getLinkerUrl(anchor.href);
							}
						}
					}
				}
				
				// loop through all form tags
				for (i = 0; i < forms.length; i++) {
					form = forms[i];
					
					// loop through all our list of cross-domains
					for (j = 0; j < domainsList.length; j++) {
						// don't tag links to this base domain
						
						if (baseDomain != domainsList[j]) {
							regexp = new RegExp("^http://.*" + domainsList[j] + "(/?.*)?");
							
							// if the link matches, append cookie information to link
							if (regexp.test(form.action)) {
								
								var url = VKIPageTracker._getLinkerUrl(form.action);
								
								params = _vkiga.splitQS(url);
								
								if (form.method.toUpperCase() == 'GET') {
									
									for (name in params)
									{
										if (name.search('__utm') > -1) {
											val = params[name];
											
											hidden = document.createElement('input');
											hidden.setAttribute('type', 'hidden');
											hidden.setAttribute('name', name);
											hidden.setAttribute('value', val);
											form.appendChild(hidden);
										}
									}
								}
								else
									form.action = url;
							}
						}
					}
				}
			}
		}
	}
	
	/* END: Optional Functionality */
}

var _vkiga = new VKIGA(GACrossDomainsList, numBaseDomainParts);

/* BEGIN: initialize page tracking object */

try {

	var params = _vkiga.splitQS(document.location.href);
	var hash = "#";
	
	for (var name in params) {
		if (name.search('__utm') > -1) {
			if (hash != '#')
				hash += '&';
				
			hash += name + '=' + decodeURIComponent(params[name]);
		}
	}
	
	if (hash != '#') {
		document.location.hash = hash;
	}
		
	var VKIPageTracker = _gat._getTracker(GAWebPropID);
	VKIPageTracker._setDomainName("." + _vkiga.getBaseDomain());
	VKIPageTracker._setAllowHash(false);
	VKIPageTracker._setAllowAnchor(true);
	
	/* OPTIONAL */
	// if cross-domain is enabled, tag all links
	if (GACrossDomainsList != "") {
		VKIPageTracker._setAllowLinker(true);
		_vkiga.addListener(window, 'load', _vkiga.tagCrossDomainLinks);
	}
	/* OPTIONAL */
	
	if (document.strTrackPageView) {
		VKIPageTracker._trackPageview(document.strTrackPageView);
	} else {
		VKIPageTracker._trackPageview();
	}
} catch(e) {}

/* END: initialize page tracking object */