/*
 * Protect window.console method calls, e.g. console is not defined on IE
 * unless dev tools are open, and IE doesn't define console.debug
 *
 * http://stackoverflow.com/questions/3326650/console-is-undefined-error-for-internet-explorer
 */
(function () {
    if (!window.console) {
        window.console = {};
    }
    // union of Chrome, FF, IE, and Safari console methods
    var m = [
        "log", "info", "warn", "error", "debug", "trace", "dir", "group",
        "groupCollapsed", "groupEnd", "time", "timeEnd", "profile", "profileEnd",
        "dirxml", "assert", "count", "markTimeline", "timeStamp", "clear"
    ];
    // define undefined methods as noops to prevent errors
    for (var i = 0; i < m.length; i++) {
        if (!window.console[m[i]]) {
            window.console[m[i]] = function () {
            };
        }
    }
})();


(function () {

    var Counter = function (server) {
        if (!server) {
            server = 'http://counter.duowan.com';
        }
        this._server = server;
    };

    Counter.prototype.incr = function (key, step) {
        if (!key) {
            key = 'clickOnPage';
        }

        if (!step) {
            step = 1;
        }

        var counter = this;

        var data = {
            domain: 'lolbox',
            subkey: key,
            count: step
        };

        var options = {
            type:'POST',
            url:counter._server + '/open/daily/collect',
            crossDomain: true,
            data: data,
            timeout:3 * 1000,
            cache: false
        };

        $.ajax(options)
            .done(
            function (data, textStatus, jqXHR) {
                if (jqXHR.status !== 200) {
                    console.log('counter incr failed');
                }
            })
            .fail(
            function (jqXHR, textStatus, errorThrown) {
                console.error(jqXHR);
                console.error(textStatus);
                console.error(errorThrown);
            });

    };

    window.Counter = window.Counter ? window.Counter : Counter;
})();
