// -------------------------------------------------------------------------- \\
// WDSCommon_.js
// -------------------------------------------------------------------------- \\
// Local development include common file
// must be overwrite by generated version when deploy
// -------------------------------------------------------------------------- \\
// use of WDSCommon2 and WDSCookie3
// -------------------------------------------------------------------------- \\

// Just to log few things in firebug Firefox extension
function GA_debugLog()
{
        if ( GoogleAnalytics.debugTraces && window.console )
        {
                window.console.log(arguments);
        }
}


// BEGIN CR 01711678

// -------------------------------------------------------------------------- \\
// GoogleAccount
// -------------------------------------------------------------------------- \\
// Class GoogleAnalytics ... constructor
// -------------------------------------------------------------------------- \\
function GoogleAccount ( accountId, url ) 
{
	this.accountId = accountId;
	this.url       = url;
}

// END CR 01711678


// -------------------------------------------------------------------------- \\
// GoogleAnalytics
// -------------------------------------------------------------------------- \\
// Class GoogleAnalytics ... constructor
// -------------------------------------------------------------------------- \\
function GoogleAnalytics () {
	// ---- Public attributes

	// URL for Urchin Javascript in both protocols
	this.HTTP_JS = "http://www.google-analytics.com/urchin.j"+"s";
	this.HTTPS_JS = "https://ssl.google-analytics.com/urchin.j"+"s";

	// ---- Private attributes

	this.events        = new Array();
	this.events.length = 0;
	this.debugTraces   = false; // Almost private, only set in footer.

	// BEGIN CR 01711678
	
	// Some objects used to check hostnames
	this.airCanadaDomainRegex = new RegExp("\.aircanada\.com$", "gi");
	this.eTravelDomainRegex = new RegExp("\.e-travel\.com$", "gi");
	this.amadeusDomainRegex = new RegExp("\.amadeus\.net$", "gi");
	this.eTravelHostRegex = new RegExp("^aircanada", "gi");
	this.amadeusHostRegex = new RegExp("^(?:nce|mucact)", "gi");
	this.knownDevHostRegex = new RegExp("^localhost|127\.0\.0\.1$", "gi");
	this.knownQAHostRegex = new RegExp("^nceetvdev(?:14|7-(?:3|4|10|20))$", "gi");
	
	this.accountList = new Array();
	GoogleAnalytics_initAccountList(this);
	
	// END CR 01711678

	// ---- Methods

	this.getJSURL      = GoogleAnalytics_getJSURL;
	this.setAccount    = GoogleAnalytics_setAccount;
	this.setProfileURL = GoogleAnalytics_setProfileURL;
	this.add      = GoogleAnalytics_add;
	this.addEvent = GoogleAnalytics_addEvent;
	this.addError = GoogleAnalytics_addError;
	
	// BEGIN CR 01711678
	this.addErrorAndSubError = GoogleAnalytics_addErrorAndSubError; 
	this.findProfileAccount  = GoogleAnalytics_findProfileAccount; 

	this.sendECommerce  = GoogleAnalytics_sendECommerce; 

	this.isAirCanadaDotCom = GoogleAnalytics_isAirCanadaDotCom; 
	this.isETravelDotCom = GoogleAnalytics_isETravelDotCom; 
	this.isAmadeusDotNet = GoogleAnalytics_isAmadeusDotNet; 
	// END CR 01711678
	
	this.send     = GoogleAnalytics_send;
}

// set as a Singleton
var GoogleAnalytics = new GoogleAnalytics();


// -------------------------------------------------------------------------- \\
// GoogleAnalytics_getJSURL
// -------------------------------------------------------------------------- \\
function GoogleAnalytics_getJSURL ( )
{
	urchinURI = this.HTTP_JS;
	if ( "https:" == location.protocol )
	{
	  urchinURI = this.HTTPS_JS;
	}
	GA_debugLog("[GoogleAnalytics] script at "+urchinURI); // DEBUG Only
	return urchinURI;
}


// -------------------------------------------------------------------------- \\
// GoogleAnalytics_setAccount
// 	  	accountId      : identifier for the account.
// -------------------------------------------------------------------------- \\
function GoogleAnalytics_setAccount ( accountId )
{
	_uacct = accountId;
}

// -------------------------------------------------------------------------- \\
// GoogleAnalytics_setProfileURL
// 	  	accountId      : identifier for the account.
// -------------------------------------------------------------------------- \\
function GoogleAnalytics_setProfileURL ( url )
{
	_udn = url;
}

// -------------------------------------------------------------------------- \\
// GoogleAnalytics_add
// 	  	url    : the URL describing the event
//      params : associative array for dynamic parameters
// -------------------------------------------------------------------------- \\
function GoogleAnalytics_add ( url, params )
{
	if ( url.length > 0 )
	{
		if ( null != params )
		{
		        var paramPart = new Array();
		        for ( var paramName in params )
			{
			  var paramValue = params[ paramName ];
				paramPart[ paramPart.length ] = paramName + "=" + paramValue;
			}
			var paramURL = paramPart.join("&");
			url = url + "?" + paramURL;
		}
		this.events[ this.events.length ] = url;
		//GA_debugLog("[GoogleAnalytics] added "+url); // DEBUG Only
	}
}

// -------------------------------------------------------------------------- \\
// GoogleAnalytics_addEvent
// 	  	id          : the identifier describing the event
//      description : the short description of the event
//      params      : associative array for dynamic parameters on event
// -------------------------------------------------------------------------- \\
function GoogleAnalytics_addEvent ( id, description, params )
{
	var baseURL = "/event/" + id + "/" + description;
	this.add( baseURL, params );
}

// -------------------------------------------------------------------------- \\
// GoogleAnalytics_addError
// 	  	id          : the identifier describing the event
//      description : the short description of the event
//      params      : associative array for dynamic parameters on event
// -------------------------------------------------------------------------- \\
function GoogleAnalytics_addError ( id, description, params )
{
	var baseURL = "/error/" + id + "/" + description;
	this.add( baseURL, params );
}

// BEGIN CR 01711678

// -------------------------------------------------------------------------- \\
// GoogleAnalytics_addErrorAndSubError
// 	  	errorId      : the identifier describing the event
// 	  	subErrorId   : the identifier describing the sub-error code
// 	  	subErrorType : Either 1 for external or 0 for internal error.
//      description  : the short description of the event
//      params       : associative array for dynamic parameters on event
// -------------------------------------------------------------------------- \\
function GoogleAnalytics_addErrorAndSubError ( errorId, subErrorId, subErrorType, description, params )
{
	var type = "unknownType_" + subErrorType;
	if ( 0 == subErrorType )
	{ 
		type= "internal";
	}
	else if ( 1 == subErrorType )
	{
		type= "external";
	}
	
	var baseURL = "/error/" + errorId + "/" + subErrorId + "/" + type + "/" + description;
	this.add( baseURL, params );
}


// -------------------------------------------------------------------------- \\
// GoogleAnalytics_sendECommerce
// 
// Send eCommerce statistics, this must be called as onload event handler on the
// CONF page.
// 
// -------------------------------------------------------------------------- \\
function GoogleAnalytics_sendECommerce()
{
	__utmSetTrans();
}


// -------------------------------------------------------------------------- \\
// GoogleAnalytics_initAccountList
// 	  	instance : the object to initialize
//
// This is not a method but a static function applicable on a particular
// object. 
// -------------------------------------------------------------------------- \\
function GoogleAnalytics_initAccountList(instance)
{
	instance.accountList["REF"] = new Array();
	instance.accountList["REF"]["e-travel.com"]  = new GoogleAccount("UA-308119-2", "e-travel.com");
	instance.accountList["REF"]["amadeus.net"]   = new GoogleAccount("UA-308119-3", "amadeus.net");
	instance.accountList["REF"]["aircanada.com"] = new GoogleAccount("UA-308119-4", "aircanada.com");

	instance.accountList["QA"] = new Array();
	instance.accountList["QA"]["e-travel.com"]  = new GoogleAccount("UA-308119-2", "e-travel.com");
	instance.accountList["QA"]["amadeus.net"]   = new GoogleAccount("UA-308119-3", "amadeus.net");
	instance.accountList["QA"]["aircanada.com"] = new GoogleAccount("UA-308119-4", "aircanada.com");

	instance.accountList["UAT"] = new Array();
	instance.accountList["UAT"]["e-travel.com"]  = new GoogleAccount("UA-308119-2", "e-travel.com");
	instance.accountList["UAT"]["amadeus.net"]   = new GoogleAccount("UA-308119-3", "amadeus.net");
	instance.accountList["UAT"]["aircanada.com"] = new GoogleAccount("UA-308119-4", "aircanada.com");

	instance.accountList["DEV"] = new Array();
	instance.accountList["DEV"]["e-travel.com"]  = new GoogleAccount("UA-308119-2", "e-travel.com");
	instance.accountList["DEV"]["amadeus.net"]   = new GoogleAccount("UA-308119-3", "amadeus.net");
	instance.accountList["DEV"]["aircanada.com"] = new GoogleAccount("UA-308119-4", "aircanada.com");

	// Special case for production, only one toplevel domain
	instance.accountList["PROD"] = new GoogleAccount("UA-230216-1", "book.aircanada.com");
}


// -------------------------------------------------------------------------- \\
// GoogleAnalytics_isAirCanadaDotCom
// 	  	hostname : Name of the host,partially supports shortname
// -------------------------------------------------------------------------- \\
function GoogleAnalytics_isAirCanadaDotCom ( hostname )
{
	var isAirCanadaDotCom = this.airCanadaDomainRegex.test(hostname);

	return isAirCanadaDotCom;
}

// -------------------------------------------------------------------------- \\
// GoogleAnalytics_isETravelDotCom
// 	  	hostname : Name of the host,partially supports shortname
// -------------------------------------------------------------------------- \\
function GoogleAnalytics_isETravelDotCom ( hostname )
{
	var isETravelDotCom = 
		this.eTravelDomainRegex.test(hostname) ||
		this.eTravelHostRegex.test(hostname);

	return isETravelDotCom;
}

// -------------------------------------------------------------------------- \\
// GoogleAnalytics_isAmadeusDotNet
// 	  	hostname : Name of the host,partially supports shortname
// -------------------------------------------------------------------------- \\
function GoogleAnalytics_isAmadeusDotNet ( hostname )
{
	var isAmadeusDotNet = 
		this.amadeusDomainRegex.test(hostname) ||
		this.amadeusHostRegex.test(hostname);

	return isAmadeusDotNet;
}

// -------------------------------------------------------------------------- \\
// GoogleAnalytics_findProfileAccount
// 	  	hostname : Name of the host,partially supports shortname
//      platform : Identifier of the platform, default fallback is PROD
// -------------------------------------------------------------------------- \\
function GoogleAnalytics_findProfileAccount ( hostname, platform )
{
	var account = null;
	
	switch ( platform )
	{
		case 'REF':
		case 'UAT':
		case 'QA':
			var accountList = this.accountList[ platform ];
			if ( this.isAmadeusDotNet(hostname) )
			{
				GA_debugLog("[GoogleAnalytics] hostname '"+hostname+"' in domain amadeus.net ("+platform+")");
				account = accountList["amadeus.net"];
			}
			else if ( this.isETravelDotCom(hostname) )
			{
				GA_debugLog("[GoogleAnalytics] hostname '"+hostname+"' in domain e-travel.com ("+platform+")");
				account = accountList["e-travel.com"];
			}
			else if ( this.isAirCanadaDotCom(hostname) )
			{
				GA_debugLog("[GoogleAnalytics] hostname '"+hostname+"' in domain aircanada.com ("+platform+")");
				account = accountList["aircanada.com"];
			}
			else if ( this.knownDevHostRegex.test(hostname) )
			{
				GA_debugLog("[GoogleAnalytics] identified hostname '"+hostname+"' in domain amadeus.net (Ref)");
				accountList = this.accountList[ "REF" ];
				account = accountList["amadeus.net"];
			}
			else if ( this.knownQAHostRegex.test(hostname) )
			{
				GA_debugLog("[GoogleAnalytics] identified hostname '"+hostname+"' in domain amadeus.net (QA)");
				accountList = this.accountList[ "QA" ];
				account = accountList["amadeus.net"];
			}
			else // Fallback on PRODuction setting
			{
				GA_debugLog("[GoogleAnalytics] findProfileAccount() hostname '"+hostname+"' not identified, fallback to production domain book.aircanada.com");
				account = this.accountList[ "PROD" ];
			}
			break;

		case 'PROD':
		default:
			account = this.accountList[ platform ];
	}
	
	return account;
}

// END CR 01711678


// -------------------------------------------------------------------------- \\
// GoogleAnalytics_send
//
// Send all events registered in this object. The object is then cleared.
//
// -------------------------------------------------------------------------- \\
function GoogleAnalytics_send()
{
	// For each tracked events
	for ( var idx = 0 ; idx < this.events.length ; idx++ )
	{
		url = this.events[ idx ];
		if ( null != url )
		{
			urchinTracker( url );
  		GA_debugLog("[GoogleAnalytics] tracked "+url); // DEBUG Only
		}
	}

	this.events.length = 0;
}
