var conmio = conmio || {};
conmio.analytics = conmio.analytics || {};

(function() {

	conmio.analytics.Google = function(uid) {
        window._gaq = window._gaq || [];
        window._gaq.push(['_setAccount', uid]);
	}

	conmio.analytics.Google.prototype = {
	    init: function() {
            var ga = document.createElement('script'); 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);
        },

        trackLink: function(url, category, action, label, value) {
            var params = null;
            var redirectTo = null;

            if (typeof url == "object") {
                params = url;
                redirectTo = params.url;
            } else if (typeof(url) == "string") {
                params = { category: category, action: action, label: label, value: value };
                redirectTo = url;
            }

            if (params != null) {
                var callback = function() {
                    window.location = redirectTo;
                };

                params.callback = callback;
                params.delay = 500;
                this.trackEvent(params);

                return false;
            }
        },

        trackPageView: function() {
            window._gaq.push(['_trackPageview']);
        },

        setCustomParam: function(slot, name, value, scope) {
            if (typeof slot == "number" &&
                typeof name == "string" && name.length > 0 &&
                typeof value == "string" && value.length > 0) {

                if (typeof scope != "number" || scope < 1 || scope > 3)
                    scope = 3

                window._gaq.push(['_setCustomVar', slot, name, value, scope]);
            }
        },
        
        trackEvent: function(category, action, label, value) {

            var delay = 0;
            var callback = null;

            if (typeof category == "object") {
                var options = category;

                category = options.category;
                action = options.action;
                label = options.label;
                value = options.value;

                if (typeof options.delay == "number")
                    delay = options.delay;

                if (typeof options.callback == "function")
                    callback = options.callback;
            }

            if (typeof category == "string" && category.length > 0 &&
                typeof action == "string" && action.length > 0) {

                var cmd = ["_trackEvent", category, action];

                if (typeof label == "string" && label.length > 0) {
                    cmd.push(label);
                } else if (typeof value == "number" && value >= 0) {
                    cmd.push(null);
                }

                if (typeof(value) == "number" && value >= 0) {
                    cmd.push(value);
                }

                window._gaq.push(cmd);

                if (callback != null) {
                    if (delay == 0) {
                        window._gaq.push(callback);
                    } else {
                        window.setTimeout(function() {
                            window._gaq.push(callback);
                        }, delay);
                    }
                }
            }
        }
	};
})();

