/* Copyright 2014 Evernote Corporation. All rights reserved. */
(function () {
    "use strict";

    function Analytics() {}

    // props are assumed to be already URL-encoded
    Analytics.init = function (props) {
        var baseParams = [];

        for (var name in props) {
            baseParams.push(name + "=" + props[name]);
        }

        Analytics.baseParams = baseParams.join('&');
        Analytics.img = document.createElement('img');
    };

    Analytics.send = function (url) {
        if (Analytics.img) Analytics.img.src = url;
    };

    Analytics.getBaseURL = function () {
        var url;

        url = 'http://www.google-analytics.com/collect?' + [
            'v=1',
            Analytics.baseParams,
            'z=' + Date.now() + Math.random()
        ].join('&');

        return url;
    };

    // screenName is not URL-encoded
    Analytics.reportScreen = function (screenName) {
        var url = Analytics.getBaseURL() + '&t=screenview&cd=' + encodeURIComponent(screenName);
        Analytics.send(url);
    };

    // params are not URL-encoded
    Analytics.reportEvent = function (category, action, label, value) {
        var url = Analytics.getBaseURL();

        url += '&t=event&ec=' + encodeURIComponent(category) + '&ea=' + encodeURIComponent(action);
        if (label) url += '&el=' + encodeURIComponent(label);
        if (value !== undefined) url += '&ev=' + encodeURIComponent(value);

        Analytics.send(url);
    };

    window.Analytics = Analytics;
}());