var _gaq = _gaq || [];

function GA() {
    this._accounts  = [ "" ];
    this.consoleLog = false;
    
    this._push = function(account, action) {
        var params = [ (account != "" ? account+"." : "")+"_"+action ];
        for (var i = 2; i < arguments.length; ++i) {
            params.push(arguments[i].toString());
        }
        
        if (GA._enabled) {
            _gaq.push(params);
        }
        if (this.consoleLog && typeof(console) != "undefined" && typeof(console.log) == "function") {
            console.log(params);
        }
        else {
            GA._queue.push(params);
        }
    };
    
    this._addAccount = function(accountName, account) {
        this._accounts.push(accountName);
        if (typeof(GA._globalAccounts[accountName]) == "undefined") {
            this._push(accountName, "setAccount", account);
            GA._globalAccounts[accountName] = account;
        }
    };
    
    this.setAccounts = function(accountsList) {
        this._accounts = [ ];
        for (var accountName in accountsList) {
            this._addAccount(accountName, accountsList[accountName]);
        }
    };
    
    this.push = function(action) {
        for (var i in this._accounts) {
            var params = [ this._accounts[i], action ];
            for (var j = 1; j < arguments.length; ++j) {
                params.push(arguments[j]);
            }
            this._push.apply(this, params);
        }
    };
    
    this.domainName = function(domainName) {
        this.push("setDomainName", domainName);
    };
    
    this.allowLinker = function(allow) {
        this.push("setAllowLinker", allow);
    };
    
    this.allowHash = function(allow) {
        this.push("setAllowHash", allow);
    };
    
    this.event = function(field2, field3, field4) {
        this.push("trackEvent", field2, field3, field4);
    };
    
    this.pageLoadTime = function() {
        this.push("trackPageLoadTime");
    };
    
    this.pageView = function() {
        if (!arguments.length) {
            this.push("trackPageview");
        }
        else {
            this.push("trackPageview", arguments[0]);
        }
    };
    
    this.svar = function(value) {
        this.push("setVar", value);
    };
    
    this.customVar = function(index, name, value, scope) {
        this.push("setCustomVar", index, name, value, scope);
    };
}
GA._enabled = !window.location.href.match(/srv-drm-dev/);
GA._queue = [ ];
GA._loading = false;
GA._loaded = false;
GA._globalAccounts = { };
GA._onload = function() {
    if (!GA._loaded &&
        (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
        GA._loading = false;
        GA._loaded = true;
    }
};
GA._load = function() {
    // GA snippet
    (function() {
      var ga = document.createElement('script');
      ga.onload = ga.onreadystatechange = GA._onload;
      ga.type = 'text/javascript';
      ga.async = true;
      ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
      var s = document.getElementsByTagName('script')[0];
      s.parentNode.insertBefore(ga, s);
    })();
};
GA.init = function() {
    if (GA._enabled && !GA._loaded && !GA._loading) {
        GA._loading = true;
        GA._load();
    }
};

function GASexyAvenue() {
    this.setIsLogged = function(isLogged, isNew) {
        this.customVar(2, "User_Type", isLogged ? (isNew ? "New_Member" : "Member_logged_in") : "Not_logged_in", 2);
    };
    
    this.setComfrom = function(comfrom) {
        this.customVar(1, "affiliate_comfrom", comfrom, 2);
    };
    
    this.setClientInfos = function(id, date) {
        this.svar("CustomerNb-"+id+"_Created-"+date);
    };
    
    this.produitConsulte = function(id, slug) {
        this.customVar(5, "Product_Viewed", "Product"+id+"_"+slug, 3);
    };
    
    this.trackNewsletter = function(cfsa0) {
        this.customVar(3, "Traffic_from_Emails", "cfsa-"+cfsa0, 2);
    };
    
    this.homePromo = function(ope) {
        this.event("Promo Home", "Click Promo", "("+ope+")");
    };
    
    this.insertPromoCode = function(code) {
        this.event("Promo Code", "Insert Promo Code", code);
    };
    
    this.basketRetrieved = function() {
        this.pageView("Cart option - basket retrieved");
    };
    
    this.printedCatalogueForm = function() {
        this.pageView("Printed catalogue form submitted");
    };
    
    this.searchResult = function(type, param) {
         this.pageView("Search Result?searchparam"+type+"="+param);
    };
    
    this.pageProduitAjoutCommentaire = function() {
        this.event("Product review form", "Product review submitted", "");
    };
    
    this.pageProduitPrecedentSuivant = function(isSuivant) {
        this.event("Previous and Next Product Page Buttons", (isSuivant ? "Next" : "Previous")+" button clicked", "");
    };
    
    this.pageProduitAjoutProduit = function(idProduit) {
        this.event("Add to Basket", "Add Product to Basket", idProduit);
    };
    
    this.pageProduitAjoutProduitSuggere = function(idProduit, idSuggere, isPeerius) {
        this.event("Add to Basket", "Add Suggested Product"+(isPeerius ? " - Peerius" : ""), "PdtPg"+idProduit+"_SugPdt"+idSuggere);
    };
    
    this.pageProduitInteractionProduitSuggere = function(type, idProduit, idSuggere, isPeerius) {
        this.event("Interact with Suggested Products", "Click on Suggested Product link "+type+(isPeerius ? " - Peerius" : ""), "PdtPg"+idProduit+"_SugPdt"+idSuggere);
    };
    
    this.pageProduitAssistantTaille = function(idProduit) {
        this.event("Product Page Interactions", "Size help", "Clicked on size help button on product page "+idProduit);
    };
    
    this.abPeerius = function(hasPeerius) {
        this.customVar(4, "MVT", hasPeerius ? "Peerius suggested products" : "Suggested products by us", 2);
    };
}
GASexyAvenue.prototype = new GA();

var _gaAll = new GASexyAvenue();
var _gaPv = new GASexyAvenue();

_gaAll.setAccounts({
    SexyAvenue:  "UA-1920731-1",
    SexyAvenue2: "UA-22101453-1",
    SexyAvenue3: "UA-24231520-1"
});
_gaPv.setAccounts({
    SexyAvenue:  "UA-1920731-1",
    SexyAvenue2: "UA-22101453-1"
});

