/******************************************************************************
* gaRuntime.js
*******************************************************************************
Runtime Isotools pour Google analytics et AdWords
*******************************************************************************
*                                                                             *
* Copyright 2010									                          *
*                                                                             *
******************************************************************************/

/********************************* GaAdWordsSender *********************************/
// TODO
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);
}
/***********************************************************************************/

/************************************* GaItem **************************************/
/* deprecated */
function GaItem(orderId, itemId, label, variation, price, quantity)
{
    _gaq.push(['_addItem',
      orderId,  // order ID - necessary to associate item with transaction
      itemId,   // SKU/code - required
      label,    // product name - necessary to associate revenue with product
      variation,// category or variation
      price,    // unit price - required
      quantity  // quantity - required
   ]);
}

/* deprecated */
GaItem.prototype.send = function()
{
}
/***********************************************************************************/


/********************************** GaTransaction **********************************/
function GaTransaction(orderId, affiliation, total, tax, shipping, city, state, country, adWordsKey) 
{
    this._adWordsKey = adWordsKey;
    this._total = total;
    
    _gaq.push(['_addTrans',
       orderId,       // order ID - required
       affiliation,   // affiliation or store name
       total,         // total - required
       tax,           // tax
       shipping,      // shipping
       city,          // city
       state,         // state or province
       country        // country
    ]);
}

GaTransaction.prototype.complete = function()
{
    _gaq.push(['_trackTrans']);
	
	var key = parseInt(this._adWordsKey, 10);
	if(isNaN(key)) return;
	var sender = new GaAdWordsSender(key);
	sender.send(parseFloat(this._total));
}

GaTransaction.prototype.addItem = function(orderId, itemId, label, variation, price, quantity)
{
    _gaq.push(['_addItem',
      orderId,  // order ID - necessary to associate item with transaction
      itemId,   // SKU/code - required
      label,    // product name - necessary to associate revenue with product
      variation, // category or variation
      price,    // unit price - required
      quantity  // quantity - required
    ]);
}

GaTransaction.prototype.send = function()
{
    _gaq.push(['_trackTrans']);
}
/***********************************************************************************/


/************************************ GaRuntime ************************************/
function GaRuntime()
{
	this._virtualLink = null;   // deprecated
	this._transactions = [];

	// variable gaAccount defined when ga code is included within page header
	//this.useGA = (isDiffusion && gaAccount && gaAccount != "")
}

GaRuntime.prototype.useGA = function() {
    //return (isDiffusion && gaAccount && gaAccount != "");
    return true;
}

GaRuntime.prototype.setCampNameKey = function(campNameKey)
{
    if (this.useGA()) _gaq.push(['_setCampNameKey', campNameKey]);
}

GaRuntime.prototype.setCampMediumKey = function(campMediumKey)
{
    if (this.useGA()) _gaq.push(['_setCampMediumKey', campMediumKey]);
}

GaRuntime.prototype.setCampSourceKey = function(campSourceKey)
{
    if (this.useGA()) _gaq.push(['_setCampSourceKey', campSourceKey]);
}

GaRuntime.prototype.setCampContentKey = function(campContentKey)
{
    if (this.useGA()) _gaq.push(['_setCampContentKey', campContentKey]);
}

GaRuntime.prototype.setCampIdKey = function(campIdKey)
{
    if (this.useGA()) _gaq.push(['_setCampIdKey', campIdKey]);
}

GaRuntime.prototype.setCampNOKey = function(campNOKey)
{
    if (this.useGA()) _gaq.push(['_setCampIdKey', _setCampNOKey]);
}

/* deprecated */
GaRuntime.prototype.setVar = function(name)
{
    if (this.useGA()) _gaq.push(['_setVar', name]); // _setVar function deprecated, see online documentation for more
}

GaRuntime.prototype.addOrganic = function(newOrganicEngine, newOrganicKeyword, prepend) 
{
    prepend = (prepend == true);
    if (this.useGA()) _gaq.push(['_addOrganic', newOrganicEngine, newOrganicKeyword, prepend]);
}

GaRuntime.prototype.addNewTransaction = function(orderId, affiliation, total, tax, shipping, city, state, country, adWordsKey) {
    if (this.useGA()) {
        var newTrans = new GaTransaction(orderId, affiliation, total, tax, shipping, city, state, country, adWordsKey);
        this._transactions[this._transactions.length] = newTrans;
        return newTrans;
    }
}

/* deprecated */
GaRuntime.prototype.setVirtualLink = function(virtualLink) {
    if (this.useGA()) {
        _gaq.push(['_trackPageview', virtualLink]);
    }
}

GaRuntime.prototype.trackPageView = function(virtualLink) 
{
    if (this.useGA()) {
        if (virtualLink == null || virtualLink == "")
            _gaq.push(['_trackPageview']);
        else
            _gaq.push(['_trackPageview', virtualLink]);
    }
}

GaRuntime.prototype.trackEvent = function(category, action, label, value) {
    if (this.useGA()) _gaq.push(['_trackEvent', category, action, label, value]);
}


/*deprecated : use setSessionCookieTimeout instead*/
GaRuntime.prototype.setSessionTimeOut = function(sessionTimeOut) {
    if (this.useGA()) {
        sessionTimeOut = parseInt(sessionTimeOut, 10);
        if (isNaN(sessionTimeOut)) return;
        this.setSessionCookieTimeout(sessionTimeOut * 1000); // seconds to milliseconds
    }
}

GaRuntime.prototype.setSessionCookieTimeout = function(sessionTimeOutMs) { // timeout in milliseconds
    if (this.useGA()) {
        if (typeof (sessionTimeOutMs) == "string") {
            sessionTimeOut = parseInt(sessionTimeOutMs, 10);
        }
        if (isNaN(sessionTimeOutMs)) return;
        _gaq.push(['_setSessionCookieTimeout', sessionTimeOutMs]);
    }
}

/* deprecated : use setCampaignCookieTimeout instead*/
GaRuntime.prototype.setCookieTimeOut = function(cookieTimeOut) {
    if (this.useGA()) {
        cookieTimeOut = parseInt(cookieTimeOut, 10); // timeout in milliseconds
        if (isNaN(cookieTimeOut)) return;
        this.setCampaignCookieTimeout(cookieTimeOut * 1000); // seconds to milliseconds
    }
}   

GaRuntime.prototype.setCampaignCookieTimeout = function(cookieTimeOutMs) {
    if (this.useGA()) {
        if (typeof (cookieTimeOutMs) == "string") {
            sessionTimeOut = parseInt(cookieTimeOutMs, 10);
        }
        if (isNaN(cookieTimeOutMs)) return;
        _gaq.push(['_setCampaignCookieTimeout', cookieTimeOutMs]);
    }
}

GaRuntime.prototype.setDomainName = function(domainName)
{
    if (this.useGA()) _gaq.push(['_setDomainName', domainName]);
}

GaRuntime.prototype.setAllowLinker = function(allowLinker)
{
    if (this.useGA()) _gaq.push(['_setAllowLinker', allowLinker]);
}

GaRuntime.prototype.setAllowAnchor = function(allowAnchor)
{
    if (this.useGA()) _gaq.push(['_setAllowAnchor', allowAnchor]);
}

GaRuntime.prototype.setCookiePath = function(cookiePath)
{
    if (this.useGA()) _gaq.push(['_setCookiePath', cookiePath]);
}

GaRuntime.prototype.setClientInfo = function(clientInfo)
{
    if (this.useGA()) _gaq.push(['_setClientInfo', clientInfo]);
}
GaRuntime.prototype.setAllowHash = function(allowHash)
{
    if (this.useGA()) _gaq.push(['_setAllowHash', allowHash]);
}
GaRuntime.prototype.setDetectFlash = function(detectFlash) 
{
    detectFlash = (detectFlash == true)
    if (this.useGA()) _gaq.push(['_setDetectFlash', detectFlash]);
}
GaRuntime.prototype.setDetectTitle = function(detectTitle)
{
    detectTitle = (detectTitle == true);
    if (this.useGA()) _gaq.push(['_setDetectTitle', detectTitle]);
}

GaRuntime.prototype.exec = function() {
    /*if (this.useGA()) {
        for (var i = 0; i < this._transactions.length; i++) {
            var transaction = this._transactions[i];
            transaction.send();
        }
    }*/
}
/***********************************************************************************/

var gaRuntime = new GaRuntime();
