/******************************************************************************
* gaRuntime.js
*******************************************************************************
Runtime Isotools pour Google analytics et AdWords
*******************************************************************************
*                                                                             *
* Copyright 2008									                          *
*                                                                             *
******************************************************************************/
function GaAdWordsSender(key)
{
	this._key = key;
	this._lang = lang.replace("-", "_");
}

GaAdWordsSender.prototype.send = function(totalValue)
{
	google_conversion_id = this._key;
	google_conversion_language = this._lang = lang;
	google_conversion_format = "1";
	google_conversion_color = "666666";
	if(totalValue && !isNaN(totalValue)) {
		google_conversion_value = totalValue;
	}
	google_conversion_label = objThesaurus.translate("ga10");
	var url = "http://www.googleadservices.com/pagead/conversion.js";
	// For test 
	// url = "./conversion.js";
	this.loadScript(url);
}

GaAdWordsSender.prototype.loadScript = function(url)
{
	var code = '<script language="JavaScript" src="' + url + '"></script>'
	document.write(code);
}

function GaItem(itemId, code, label, variation, price, quantity)
{
	this._itemId = itemId;
	this._code = code;
	this._label = label;
	this._variation = variation;
	this._price = price;
	this._quantity = quantity;	
}
GaItem.prototype.send = function(pageTracker)
{
	pageTracker._addItem(
		this._itemId,
		this._code,
		this._label,
		this._variation,
		this._price,
		this._quantity
	);
}
function GaTransaction(orderId, affiliation, total, tax, shipping, city, state, country, adWordsKey)
{
	this._orderId = orderId;
	this._affiliation = affiliation;
	this._total = total;
	this._tax = tax;
	this._shipping = shipping;
	this._city = city;
	this._state = state;
	this._country = country;
	this._adWordsKey = adWordsKey;
	this._items = [];
}

GaTransaction.prototype.complete = function()
{
	var key = parseInt(this._adWordsKey, 10);
	if(isNaN(key)) return;
	var sender = new GaAdWordsSender(key);
	sender.send(parseFloat(this._total));
}

GaTransaction.prototype.addItem = function(itemId, code, label, variation, price, quantity)
{
	this._items[this._items.length] = new GaItem(itemId, code, label, variation, price, quantity);
}

GaTransaction.prototype.send = function(pageTracker)
{
	pageTracker._addTrans(
		this._orderId,
		this._affiliation,
		this._total,
		this._tax,
		this._shipping,
		this._city,
		this._state,
		this._country
	);
	for(var i=0;i<this._items.length;i++) {
		var item = this._items[i];
		item.send(pageTracker);
	}
	pageTracker._trackTrans();
}

function GaRuntime()
{
	this._virtualLink = null;
	this._domainName = null;
	this._cookiePath = null;
	this._cookiePath = null;
	this._allowHash  = null;
	this._detectFlash = null;
	this._cookieTimeOut = null;
	this._detectTitle = null;
	this._sessionTimeOut = null;
	this._campNameKey = null;
	this._campMediumKey = null;
	this._campSourceKey = null;
	this._campTermKey = null;
	this._campContentKey = null;
	this._campIdKey = null;
	this._campNOKey = null;

	this._transactions = [];
	this._vars = [];
	this._organics = {};
}

GaRuntime.prototype.setCampNameKey = function(campNameKey)
{
	this._campNameKey = campNameKey;
}

GaRuntime.prototype.setCampMediumKey = function(campMediumKey)
{
	this._campMediumKey = campMediumKey;
}

GaRuntime.prototype.setCampSourceKey = function(campSourceKey)
{
	this._campSourceKey = campSourceKey;
}

GaRuntime.prototype.setCampContentKey = function(campContentKey)
{
	this._campContentKey = campContentKey;
}

GaRuntime.prototype.setCampIdKey = function(campIdKey)
{
	this._campIdKey = campIdKey;
}

GaRuntime.prototype.setCampNOKey = function(campNOKey)
{
	this._campNOKey = campNOKey;
}

GaRuntime.prototype.setVar = function(name)
{
	this._vars[this._vars.length] = name;
}

GaRuntime.prototype.addOrganic = function(k, v)
{
	this._organics[k] = v;
}

GaRuntime.prototype.addNewTransaction = function(orderId, affiliation, total, tax, shipping, city, state, country, adWordsKey)
{
	return this._transactions[this._transactions.length] = new GaTransaction(orderId, affiliation, total, tax, shipping, city, state, country, adWordsKey);
}

GaRuntime.prototype.setVirtualLink = function(virtualLink)
{
	this._virtualLink = virtualLink;
}

GaRuntime.prototype.setSessionTimeOut = function(sessionTimeOut)
{
	sessionTimeOut = parseInt(sessionTimeOut, 10);
	if(isNaN(sessionTimeOut)) return;
	this._sessionTimeOut = sessionTimeOut + "";
}

GaRuntime.prototype.setCookieTimeOut = function(cookieTimeOut)
{
	cookieTimeOut = parseInt(cookieTimeOut, 10);
	if(isNaN(cookieTimeOut)) return;
	this._cookieTimeOut = cookieTimeOut + "";
}

GaRuntime.prototype.setDomainName = function(domainName)
{
	this._domainName = domainName;

}

GaRuntime.prototype.setAllowLinker = function(allowLinker)
{
	this._allowLinker = allowLinker == true;

}

GaRuntime.prototype.setAllowAnchor = function(allowAnchor)
{
	this._allowAnchor = allowAnchor == true;

}

GaRuntime.prototype.setCookiePath = function(cookiePath)
{
	this._cookiePath = cookiePath;

}

GaRuntime.prototype.setClientInfo = function(clientInfo)
{
	this._clientInfo = clientInfo == true;

}
GaRuntime.prototype.setAllowHash = function(allowHash)
{
	this._allowHash = allowHash == true;

}
GaRuntime.prototype.setDetectFlash = function(detectFlash)
{
	this._detectFlash = detectFlash == true;

}
GaRuntime.prototype.setDetectTitle = function(detectTitle)
{
	this._detectTitle = detectTitle == true;

}

var pageTracker;

GaRuntime.prototype.exec = function(key)
{
	if(key == null) return;
	pageTracker = _gat._getTracker(key);
	if(this._domainName == null) {
		pageTracker._setDomainName(this._domainName);
	}
	if(this._allowLinker) {
		pageTracker._setAllowLinker(true);
	}
	if(this._allowAnchor) {
		pageTracker._setAllowAnchor(true);
	}
	if(this._cookiePath) {
		pageTracker._setCookiePath(this._cookiePath);
	}
	if(this._cookiePath != null) {
		pageTracker._setClientInfo(this._clientInfo);
	}
	if(this._allowHash != null) {
		pageTracker._setAllowHash(this._allowHash);
	}
	if(this._detectFlash != null) {
		pageTracker._setDetectFlash(this._detectFlash);
	}
	if(this._detectTitle != null) {
		pageTracker._setDetectTitle(this._detectTitle);
	}
	if(this._sessionTimeOut != null) {
		pageTracker._setSessionTimeOut(this._sessionTimeOut);
	}
	if(this._cookieTimeOut != null) {
		pageTracker._setCookieTimeOut(this._cookieTimeOut);
	}
	for(var i=0;i<this._vars.length;i++) {
		pageTracker._setVar(this._vars[i]);
	}
	for(var k in this._organics) {
		pageTracker._addOrganic(k, this._organics[k]);
	}
	if(this._campNameKey != null) {
		pageTracker._setCampNameKey(this._campNameKey); // name
	}
	if(this._campMediumKey != null) {
		pageTracker._setCampMediumKey(this._campMediumKey); // medium
	}
	if(this._campSourceKey != null) {
		pageTracker._setCampSourceKey(this._campSourceKey); // source
	}
	if(this._campTermKey != null) {
		pageTracker._setCampTermKey(this._campTermKey); // term/keyword
	}
	if(this._campContentKey != null) {
		pageTracker._setCampContentKey(this._campContentKey); // content
	}
	if(this._campIdKey != null) {
		pageTracker._setCampIdKey(this._campIdKey); // id number
	}
	if(this._campNOKey != null) {
		pageTracker._setCampNOKey(this._campNOKey); // don't override
	}

	pageTracker._initData();
	if(this._virtualLink == null) {
		pageTracker._trackPageview();
	} else {
		pageTracker._trackPageview(this._virtualLink);
	}
	for(var i=0;i<this._transactions.length;i++) {
		var transaction = this._transactions[i];
		transaction.send(pageTracker);
	}
}

var gaRuntime = new GaRuntime();