UNPKG

automizy-email-editor

Version:

Design mobile friendly HTML emails in your application.

2,691 lines 87.9 kB
(function($){
var $AA = {};
(function(){
    $AA = new function () {
        var t = this;
        t.d = {
            version: '0.1.0',
            login:false,
            hasOpenLoginForm:true,
            lastAjaxData:{},
            ad:{
                error:function (jqXHR, textStatus, errorThrown) {
                    if(arguments.length === 3) {
                        if ($.inArray(jqXHR.status, [404, 405]) >= 0) {
                            alert(jqXHR.status + ' (' + errorThrown + ')');
                        } else if ($.inArray(jqXHR.status, [403]) >= 0) {
                            $AA.d.lastAjaxData = this;
                            $AA.openLoginDialog();
                        } else if ($.inArray(jqXHR.status, [400, 401]) >= 0) {
                            alert(jqXHR.status + ' (' + errorThrown + ')');
                            $AA.openLoginDialog();
                        } else {
                            console.error(jqXHR.status, textStatus, errorThrown);
                        }
                    }else{
                        console.log('<----------- Automizy Api Error');
                        console.log(jqXHR);
                        console.log(textStatus);
                        console.log(errorThrown);
                        console.log('Automizy Api Error ----------->');
                        $AA.openLoginDialog();
                    }
                }
            }
        };

        var baseUrl = "https://api.automizy.com";
        var apiLoginPhp = "https://app.automizy.com/php/login.php";
        var apiRefreshPhp = "https://app.automizy.com/php/refresh.php";
        t.u = {
            base:baseUrl,
            loginPhp: apiLoginPhp,
            refreshPhp: apiRefreshPhp,
            oauth: baseUrl + '/oauth',
            segments: baseUrl + '/segments',
            campaigns: baseUrl + '/campaigns',
            splitTests: baseUrl + '/split-tests',
            newsletters: baseUrl + '/newsletters',
            contacts: baseUrl + '/contacts',
            customFields: baseUrl + '/custom-fields',
            users: baseUrl + '/users',
            jobs: baseUrl + '/jobs',
            webhooks: baseUrl + '/webhooks',
            images: baseUrl + '/images',
            templates: baseUrl + '/templates',
            forms: baseUrl + '/forms',
            automations: baseUrl + '/automations',
            account: baseUrl + '/account',
            contactImports: baseUrl + '/contact-imports',
            tags: baseUrl + '/tags',
            clients: baseUrl + '/clients',

            emailPreview: baseUrl + '/email-preview'
        };

        t.m = [];
    }();
})();

(function(){
    $AA.cookie = function () {
        function extend () {
            var i = 0;
            var result = {};
            for (; i < arguments.length; i++) {
                var attributes = arguments[ i ];
                for (var key in attributes) {
                    result[key] = attributes[key];
                }
            }
            return result;
        }

        function init (converter) {
            function api (key, value, attributes) {
                var result;

                // Write

                if (arguments.length > 1) {
                    attributes = extend({
                        path: '/'
                    }, api.defaults, attributes);

                    if (typeof attributes.expires === 'number') {
                        var expires = new Date();
                        expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5);
                        attributes.expires = expires;
                    }

                    try {
                        result = JSON.stringify(value);
                        if (/^[\{\[]/.test(result)) {
                            value = result;
                        }
                    } catch (e) {}

                    value = encodeURIComponent(String(value));
                    value = value.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);

                    key = encodeURIComponent(String(key));
                    key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent);
                    key = key.replace(/[\(\)]/g, escape);

                    return (document.cookie = [
                        key, '=', value,
                        attributes.expires && '; expires=' + attributes.expires.toUTCString(), // use expires attribute, max-age is not supported by IE
                        attributes.path    && '; path=' + attributes.path,
                        attributes.domain  && '; domain=' + attributes.domain,
                        attributes.secure  && '; secure'
                    ].join(''));
                }

                // Read

                if (!key) {
                    result = {};
                }

                // To prevent the for loop in the first place assign an empty array
                // in case there are no cookies at all. Also prevents odd result when
                // calling "get()"
                var cookies = document.cookie ? document.cookie.split('; ') : [];
                var rdecode = /(%[0-9A-Z]{2})+/g;
                var i = 0;

                for (; i < cookies.length; i++) {
                    var parts = cookies[i].split('=');
                    var name = parts[0].replace(rdecode, decodeURIComponent);
                    var cookie = parts.slice(1).join('=');

                    if (cookie.charAt(0) === '"') {
                        cookie = cookie.slice(1, -1);
                    }

                    cookie = converter && converter(cookie, name) || cookie.replace(rdecode, decodeURIComponent);

                    if (this.json) {
                        try {
                            cookie = JSON.parse(cookie);
                        } catch (e) {}
                    }

                    if (key === name) {
                        result = cookie;
                        break;
                    }

                    if (!key) {
                        result[name] = cookie;
                    }
                }

                return result;
            }

            api.get = api.set = api;
            api.getJSON = function () {
                return api.apply({
                    json: true
                }, [].slice.call(arguments));
            };
            api.defaults = {};

            api.remove = function (key, attributes) {
                api(key, '', extend(attributes, {
                    expires: -1
                }));
            };

            api.withConverter = init;

            return api;
        }

        return init();
    }();
})();

(function(){
    $AA.date = {};
    $AA.date.now = function () {
        return new Date().getTime();
    };
    $AA.date.timestamp = function () {
        return Math.floor($AA.date.now() / 1000);
    };
})();

(function(){
    var Token = function () {
        var t = this;
        t.d = {
            access_token: $AA.cookie.get('AutomizyApiAccessToken') || false,
            refresh_token: $AA.cookie.get('AutomizyApiRefreshToken') || false,
            client_id: $AA.cookie.get('AutomizyApiClientId') || false,
            username: $AA.cookie.get('AutomizyApiUsername') || false,
            expires: parseInt($AA.cookie.get('AutomizyApiExpires')) || 0,
            scope: $AA.cookie.get('AutomizyApiScope') || null,
            token_type: $AA.cookie.get('AutomizyApiTokenType') || "Bearer",
            error:function(){},
            success:function(){},
            loginError:function(){},
            loginAjaxError:function(){},
            loginAjaxSuccess:function(){},
            refreshAjaxError:function(){},
            refreshAjaxSuccess:function(){},
            refreshLoopTimeout:false
        };
    };

    var p = Token.prototype;

    p.refreshLoop = function(){
        var t = this;
        if ($AA.date.timestamp() > parseInt($AA.cookie.get('AutomizyApiExpires')) - 120) {
            t.refresh();
        }
        clearTimeout(t.d.refreshLoopTimeout);
        t.d.refreshLoopTimeout = setTimeout(function(){
            t.refreshLoop();
        }, 30000);
    };
    p.refreshLoopStop = function(){
        var t = this;
        clearTimeout(t.d.refreshLoopTimeout);
    };
    p.refreshLoopStart = function(){
        var t = this;
        t.refreshLoop();
    };
    p.refreshLoopRestart = function(){
        var t = this;
        t.refreshLoopStop();
        t.refreshLoopStart();
    };

    p.loggedIn = function(loggedIn){
        var t = this;
        if (typeof loggedIn !== 'undefined') {
            t.d.loggedIn = $AA.parseBoolean(loggedIn);
            return t;
        }
        return t.d.loggedIn;
    };

    p.get = function () {
        var t = this;
        if($AA.cookie.get('AutomizyApiAccessToken') !== false){
            t.loggedIn(true);
            t.refreshLoopRestart();
        }else{
            t.refreshLoopStop();
            t.loggedIn(false);
        }
        return $AA.cookie.get('AutomizyApiAccessToken');
    };

    p.set = function (token) {
        var t = this;

        if(typeof token.access_token !== 'undefined'){
            t.d.access_token = token.access_token;
            $AA.cookie.set('AutomizyApiAccessToken', token.access_token, t.cookieAttributes())
        }
        if(typeof token.expires_in !== 'undefined'){
            t.d.expires = $AA.date.timestamp() + parseInt(token.expires_in) - 10;
            $AA.cookie.set('AutomizyApiExpires', t.d.expires, t.cookieAttributes())
        }
        if(typeof token.refresh_token !== 'undefined'){
            t.d.refresh_token = token.refresh_token;
            $AA.cookie.set('AutomizyApiRefreshToken', token.refresh_token, t.cookieAttributes())
        }
        if(typeof token.scope !== 'undefined'){
            t.d.scope = token.scope;
            if(t.d.scope !== false)$AA.cookie.set('AutomizyApiScope', token.scope, t.cookieAttributes());
        }
        if(typeof token.token_type !== 'undefined'){
            t.d.token_type = token.token_type;
            $AA.cookie.set('AutomizyApiTokenType', token.token_type, t.cookieAttributes())
        }
        if(typeof token.client_id !== 'undefined'){
            t.d.client_id = token.client_id;
            $AA.cookie.set('AutomizyApiClientId', token.client_id, t.cookieAttributes())
        }
        if(typeof token.username !== 'undefined'){
            t.d.username = token.username;
            $AA.cookie.set('AutomizyApiUsername', token.username, t.cookieAttributes())
        }

        return true;
    };

    p.refresh = function () {
        var t = this;
        return $.ajax({
            type: "POST",
            url: $AA.u.refreshPhp,
            data: {
                refresh_token: $AA.cookie.get('AutomizyApiRefreshToken'),
                username: $AA.cookie.get('AutomizyApiUsername')
            },
            success: function (data, textStatus, jqXHR) {
                t.set(data);
                t.loggedIn(true);
                t.refreshAjaxSuccess.apply(t, [data, textStatus, jqXHR]);
            },
            error: function(jqXHR, textStatus, errorThrown){
                t.refreshAjaxError.apply(t, [jqXHR, textStatus, errorThrown]);
            }
        });
    };

    p.error = function(error){
        var t = this;
        if (typeof error === 'function') {
            t.d.error = error;
            return t;
        }
        return t.d.error;
    };
    p.success = function(success){
        var t = this;
        if (typeof success === 'function') {
            t.d.success = success;
            return t;
        }
        return t.d.success;
    };
    p.loginError = function(loginError){
        var t = this;
        if (typeof loginError === 'function') {
            t.d.loginError = loginError;
            return t;
        }
        return t.d.loginError;
    };
    p.loginAjaxError = function(loginAjaxError){
        var t = this;
        if (typeof loginAjaxError === 'function') {
            t.d.loginAjaxError = loginAjaxError;
            return t;
        }
        return t.d.loginAjaxError;
    };
    p.loginAjaxSuccess = function(loginAjaxSuccess){
        var t = this;
        if (typeof loginAjaxSuccess === 'function') {
            t.d.loginAjaxSuccess = loginAjaxSuccess;
            return t;
        }
        return t.d.loginAjaxSuccess;
    };
    p.refreshAjaxError = function(refreshAjaxError){
        var t = this;
        if (typeof refreshAjaxError === 'function') {
            t.d.refreshAjaxError = refreshAjaxError;
            return t;
        }
        return t.d.refreshAjaxError;
    };
    p.refreshAjaxSuccess = function(refreshAjaxSuccess){
        var t = this;
        if (typeof refreshAjaxSuccess === 'function') {
            t.d.refreshAjaxSuccess = refreshAjaxSuccess;
            return t;
        }
        return t.d.refreshAjaxSuccess;
    };

    p.passwordLogin = function (obj) {
        var t = this;
        var obj = obj || {};
        obj.username = obj.username || false;
        obj.password = obj.password || false;

        if (obj.username === false || obj.password === false) {
            t.loginError.apply(t, ['The username and password must be provide!']);
            return false;
        }

        $AA.cookie.set('AutomizyApiUsername', obj.username, t.cookieAttributes());

        return $.ajax({
            type: "POST",
            url: $AA.u.loginPhp,
            data: {
                username: obj.username,
                password: obj.password
            },
            success: function (data, textStatus, jqXHR) {
                t.set(data);
                t.loggedIn(true);
                t.refreshLoopRestart();
                t.d.loginAjaxSuccess.apply(t, [data, textStatus, jqXHR]);
            },
            error: function(jqXHR, textStatus, errorThrown){
                t.d.loginAjaxError.apply(t, [jqXHR, textStatus, errorThrown]);
            }
        });
    };

    p.credentialsLogin = function (obj) {
        var t = this;
        var obj = obj || {};
        obj.clientId = obj.clientId || false;
        obj.clientSecret = obj.clientSecret || false;

        if (obj.clientId === false || obj.clientSecret === false) {
            t.loginError.apply(t, ['The clientId and clientSecret must be provide!']);
            return false;
        }

        return $.ajax({
            type: "POST",
            url: $AA.u.oauth,
            data: {
                grant_type: "client_credentials",
                client_id: obj.clientId,
                client_secret: obj.clientSecret
            },
            success: function (data, textStatus, jqXHR) {
                t.set(data);
                t.loggedIn(true);
                t.refreshLoopRestart();
                t.d.loginAjaxSuccess.apply(t, [data, textStatus, jqXHR]);
            },
            error: function(jqXHR, textStatus, errorThrown){
                t.d.loginAjaxError.apply(t, [jqXHR, textStatus, errorThrown]);
            }
        });
    };

    p.login = function (obj) {
        var t = this;
        var obj = obj || {};
        if (typeof obj.username !== 'undefined' && typeof obj.password !== 'undefined') {
            return t.passwordLogin(obj);
        }else if (typeof obj.clientId !== 'undefined' && typeof obj.clientSecret !== 'undefined') {
            return t.credentialsLogin(obj);
        } else {
            t.loginError.apply(t, ['Missing parameters!']);
            return false;
        }
    };

    p.cookieAttributes = function(){
        var obj = {};
        if(location.href.indexOf('.protopmail.com') >= 0){
            obj.domain = '.protopmail.com';
        }else if(location.href.indexOf('.automizy.com') >= 0){
            obj.domain = '.automizy.com';
        }
        return obj;
    };

    p.logout = function(){
        var t = this;
        t.loggedIn(false);
        t.refreshLoopStop();
        t.d.access_token = false;
        t.d.refresh_token = false;
        t.d.username = false;
        t.d.client_id = false;
        t.d.expires = 0;
        $AA.cookie.remove('AutomizyApiAccessToken', t.cookieAttributes());
        $AA.cookie.remove('AutomizyApiRefreshToken', t.cookieAttributes());
        $AA.cookie.remove('AutomizyApiClientId', t.cookieAttributes());
        $AA.cookie.remove('AutomizyApiExpires', t.cookieAttributes());
        $AA.cookie.remove('AutomizyApiUsername', t.cookieAttributes());
    };

    $AA.m.Token = Token;
    $AA.d.token = new Token();
    $AA.token = function () {
        return $AA.d.token;
    }
})();

(function(){
    $AA.initBasicFunctions = function (module, moduleName) {
        var module = module || false;
        if (module === false)
            return false;
        var moduleName = moduleName || false;
        if (moduleName === false)
            return false;
        var moduleNameLower = moduleName.toLowerCase();
        var moduleNameLowerFirst = moduleName.charAt(0).toLowerCase() + moduleName.slice(1);


        var p = module.prototype;

        p.init = p.init || function(){
            var t = this;
            if(typeof t.d.urlSuffix === 'undefined'){
                t.d.urlSuffix = '';
            }
            if(typeof t.d.itemsDir === 'undefined'){
                t.d.itemsDir = false;
            }
            if(typeof t.d.fields === 'undefined'){
                t.d.fields = false;
            }
            if(typeof t.d.format === 'undefined'){
                t.d.format = false;
            }
            if(typeof t.d.limit === 'undefined'){
                t.d.limit = false;
            }
            if(typeof t.d.page === 'undefined'){
                t.d.page = false;
            }
            if(typeof t.d.order_by === 'undefined'){
                t.d.order_by = false;
            }
            if(typeof t.d.order_dir === 'undefined'){
                t.d.order_dir = false;
            }
            if(typeof t.d.links === 'undefined'){
                t.d.links = false;
            }
            if(typeof t.d.set === 'undefined'){
                t.d.set = {};
            }
        };
        p.initParameter = p.initParameter || function(obj){
            var t = this;
            if (typeof obj.urlSuffix !== 'undefined')
                t.urlSuffix(obj.urlSuffix);
            if (typeof obj.itemsDir !== 'undefined')
                t.itemsDir(obj.itemsDir);
            if (typeof obj.format !== 'undefined')
                t.format(obj.format);
            if (typeof obj.fields !== 'undefined')
                t.fields(obj.fields);
            if (typeof obj.limit !== 'undefined')
                t.limit(obj.limit);
            if (typeof obj.page !== 'undefined')
                t.page(obj.page);
            if (typeof obj.orderBy !== 'undefined' || typeof obj.order_by !== 'undefined')
                t.orderBy(obj.orderBy || obj.order_by);
            if (typeof obj.orderDir !== 'undefined' || typeof obj.order_dir !== 'undefined')
                t.orderDir(obj.orderDir || obj.order_dir);
            if (typeof obj.links !== 'undefined')
                t.links(obj.links);
            if (typeof obj.url !== 'undefined')
                t.url(obj.url);
        };

        p.setOptions = p.setOptions || function(obj){
            if (typeof obj.fields !== 'undefined')
                t.d.option.fields = obj.fields;  //mezők vesszővel
            if (typeof obj.format !== 'undefined')
                t.d.option.format = obj.format; //format data
            if (typeof obj.limit !== 'undefined')
                t.d.option.limit = obj.limit; //hány darab
            if (typeof obj.page !== 'undefined')
                t.d.option.page = obj.page;  //hanyadik elemtől
            if (typeof obj.where !== 'undefined')
                t.d.option.where = obj.where;  //feltétel
            if (typeof obj.order_by !== 'undefined')
                t.d.option.order_by = obj.order_by; //mi szerint rendezzen
            if (typeof obj.order_dir !== 'undefined')
                t.d.option.order_dir = obj.order_dir; //desc vagy asc
            if (typeof obj.order !== 'undefined')
                t.d.option.order = obj.order; //name:desc
            if (typeof obj.links !== 'undefined')
                t.d.option.links = obj.links; //milyen linkek kellenek vesszővel
        }
        p.getDataFromParameter = p.getDataFromParameter || function(obj){
            var data = {};
            if (obj.fields !== false)
                data.fields = obj.fields;
            if (obj.format !== false)
                data.format = obj.format;
            if (obj.limit !== false)
                data.limit = obj.limit;
            if (obj.page !== false)
                data.page = obj.page;
            if (obj.where !== false)
                data.where = obj.where;
            if (obj.order_dir !== false)
                data.order_dir = obj.order_dir;
            if (obj.order_by !== false)
                data.order_by = obj.order_by;
            if (obj.order !== false)
                data.order = obj.order;
            if (obj.links !== false)
                data.links = obj.links;
            return data;
        };

        p.get = p.get || function (obj, isMod, async) {
            var t = this;
            var isMod = true;   //modify the options from the 'obj' object
            if(typeof async !== 'undefined'){
                async = $AA.parseBoolean(async);
            }else{
                var async = true;
            }

            if (typeof isMod !== 'undefined') {
                isMod = $AA.parseBoolean(isMod);
            }
            if (isMod && (typeof obj === 'object' || typeof obj === 'array')) {
                t.setOptions(obj)
            }

            var data = {};
            if (typeof obj === 'object' || typeof obj === 'array') {
                data = t.getDataFromParameter(obj);
            }else{
                for(var i in t.d.option){
                    if(t.d.option[i] !== false){
                        data[i] = t.d.option[i];
                    }
                }
            }

            if(typeof data.order === 'undefined' && typeof data.order_by !== 'undefined'){
                data.order = data.order_by+':'+data.order_dir || 'asc'
            }
            var $ajax = $.ajax({
                url: t.d.url + t.d.urlSuffix,
                type: 'GET',
                dataType: 'json',
                async:async,
                data: data,
                headers: {Authorization: 'Bearer ' + $AA.token().get()},
                error: $AA.token().error()
            });
            if(!async){
                return $ajax.responseJSON;
            }
            return $ajax;
        };
        p.getSync = p.getSync || function(obj, isMod){
            var obj = obj || false;
            var isMod = isMod || false;
            return this.get.apply(this, [obj, isMod, false]);
        };
        p.export = p.export || function (obj, isMod, async) {
            var t = this;
            var isMod = true;
            if(typeof async !== 'undefined'){
                async = $AA.parseBoolean(async);
            }else{
                var async = true;
            }

            if (typeof isMod !== 'undefined') {
                isMod = $AA.parseBoolean(isMod);
            }
            if (isMod && (typeof obj === 'object' || typeof obj === 'array')) {
                t.setOptions(obj)
            }

            var data = {};
            if (typeof obj === 'object' || typeof obj === 'array') {
                data = t.getDataFromParameter(obj);
            }else{
                for(var i in t.d.option){
                    if(t.d.option[i] !== false){
                        data[i] = t.d.option[i];
                    }
                }
            }

            if(typeof data.order === 'undefined' && typeof data.order_by !== 'undefined'){
                data.order = data.order_by+':'+data.order_dir || 'asc'
            }
            data.showContent = false;
            var $ajax = $.ajax({
                xhr: function()
                {
                    var xhr = new window.XMLHttpRequest();
                    xhr.addEventListener("progress", function(evt){

                        if (evt.lengthComputable) {
                            var percentComplete = evt.loaded / evt.total;
                            console.log(parseInt(percentComplete*100));
                        }
                    }, false);
                    return xhr;
                },
                url: t.d.url + t.d.urlSuffix,
                type: 'GET',
                //dataType: 'json',
                async:async,
                data: data,
                headers: {
                    Authorization: 'Bearer ' + $AA.token().get(),
                    Accept: 'text/csv'
                },
                error: $AA.token().error()
            });
            if(!async){
                return $ajax.responseJSON;
            }
            return $ajax;
        };

        p.insert = p.insert || function(obj, async){
            var t = this;
            if(typeof async !== 'undefined'){
                async = $AA.parseBoolean(async);
            }else{
                var async = true;
            }

            var data = obj;

            var $ajax = $.ajax({
                url: t.d.url + t.d.urlSuffix,
                type: 'POST',
                dataType: 'json',
                async:async,
                data: data,
                headers: {Authorization: 'Bearer ' + $AA.token().get()},
                error: $AA.token().error()
            });
            if(!async){
                return $ajax.responseJSON;
            }
            return $ajax;
        };
        p.insertSync = p.insertSync || function(obj){
            var obj = obj || false;
            return this.insert.apply(this, [obj, false]);
        };

        p.update = p.update || function(obj, id, async){
            var t = this;
            if(typeof async !== 'undefined'){
                async = $AA.parseBoolean(async);
            }else{
                var async = true;
            }

            var data = obj;
            var id = id || obj.id || 0;
            delete data.id;

            var $ajax = $.ajax({
                url: t.d.url + '/' + id + t.d.urlSuffix,
                type: 'PATCH',
                dataType: 'json',
                async:async,
                data: data,
                headers: {Authorization: 'Bearer ' + $AA.token().get()},
                error: $AA.token().error()
            });
            if(!async){
                return $ajax.responseJSON;
            }
            return $ajax;
        };
        p.updateSync = p.updateSync || function(obj, id){
            var obj = obj || false;
            var id = id || false;
            return this.update.apply(this, [obj, id, false]);
        };

        p.delete = p.delete || function(id, async){
            var t = this;
            if(typeof async !== 'undefined'){
                async = $AA.parseBoolean(async);
            }else{
                var async = true;
            }

            var $ajax = $.ajax({
                url: t.d.url+'/'+id + t.d.urlSuffix,
                type: 'DELETE',
                dataType: 'json',
                async:async,
                headers: {Authorization: 'Bearer ' + $AA.token().get()},
                error: $AA.token().error()
            });
            if(!async){
                return $ajax.responseJSON;
            }
            return $ajax;
        };
        p.deleteSync = p.deleteSync || function(id){
            var id = id || false;
            return this.delete.apply(this, [id, false]);
        };


        /*
         [
             [
                 ['name', '=', 'Tilda'],
                 ['age', '<=', 18]
             ],
             [
                 ['name', 'like', 'Thom%'],
                 ['age', 'in', [18, 20, 21]]
             ],
             [
                 ['name', 'like', 'Thom%'],
                 ['age', 'between', [18, 22]]
             ]
         ]
         */


        p.getAll = p.getAll || function () {
            var t = this;
            return $.ajax({
                url: t.d.url + t.d.urlSuffix,
                type: 'GET',
                dataType: 'json',
                headers: {Authorization: 'Bearer ' + $AA.token().get()},
                error: $AA.token().error()
            });
        };
        p.getRecordById = p.getRecordById || function (id) {
            var t = this;

            var data = {};

            for(var i in t.d.option){
                if(t.d.option[i] !== false){
                    data[i] = t.d.option[i];
                }
            }

            if(typeof data.order === 'undefined' && typeof data.order_by !== 'undefined'){
                data.order = data.order_by+':'+data.order_dir || 'asc'
            }

            return $.ajax({
                url: t.d.url + '/' + id + t.d.urlSuffix,
                type: 'GET',
                dataType: 'json',
                data: data,
                headers: {Authorization: 'Bearer ' + $AA.token().get()},
                error: $AA.token().error()
            });
        };
        p.getFieldById = p.getFieldById || function (id, fieldName) {
            var t = this;
            var fieldTree = fieldName.split('.');
            return $.ajax({
                url: t.d.url + '/' + id + t.d.urlSuffix,
                type: 'GET',
                dataType: 'json',
                data: {fields: fieldTree[0], links: ''},
                headers: {Authorization: 'Bearer ' + $AA.token().get()},
                converters: {
                    'text json': function (result) {
                        var field = $.parseJSON(result)[fieldTree[0]];
                        for (var i = 1; i < fieldTree.length; i++) {
                            field = field[fieldTree[i]];
                        }
                        return field;
                    }
                },
                error: $AA.token().error()
            });
        };
        p.getAllIdNamePair = p.getAllIdNamePair || function (nameFieldName) {
            var t = this;
            var fieldTree = nameFieldName.split('.');
            return $.ajax({
                url: t.d.url + t.d.urlSuffix,
                type: 'GET',
                dataType: 'json',
                data: {fields: 'id,' + fieldTree[0], links: ''},
                headers: {Authorization: 'Bearer ' + $AA.token().get()},
                converters: {
                    'text json': function (result) {
                        var res = $.parseJSON(result)._embedded;
                        res = res[Object.keys(res)[0]];
                        var arr = [];
                        for (var i in res) {
                            var field = res[i][fieldTree[0]];
                            for (var j = 1; j < fieldTree.length; j++) {
                                field = field[fieldTree[j]];
                            }
                            arr.push([res[i]['id'], field]);
                        }
                        return arr;
                    }
                },
                error: $AA.token().error()
            });
        };


        p.urlSuffix = p.urlSuffix || function (urlSuffix) {
            var t = this;
            if (typeof urlSuffix !== 'undefined') {
                t.d.urlSuffix = urlSuffix;
                return t;
            }
            return t.d.urlSuffix;
        };
        p.itemsDir = p.itemsDir || function (itemsDir) {
            var t = this;
            if (typeof itemsDir !== 'undefined') {
                t.d.itemsDir = itemsDir;
                return t;
            }
            return t.d.itemsDir;
        };
        p.fields = p.fields || function (fields) {
            var t = this;
            if (typeof fields !== 'undefined') {
                if(fields !== false && fields.length > 0){
                    t.d.option.fields = fields;
                }
                return t;
            }
            return t.d.option.fields;
        };
        p.limit = p.limit || function (limit) {
            var t = this;
            if (typeof limit !== 'undefined') {
                t.d.option.limit = limit;
                return t;
            }
            return t.d.option.limit;
        };
        p.format = p.format || function (format) {
            var t = this;
            if (typeof format !== 'undefined') {
                t.d.option.format = format;
                return t;
            }
            return t.d.option.format;
        };
        p.page = p.page || function (page) {
            var t = this;
            if (typeof page !== 'undefined') {
                t.d.option.page = page;
                return t;
            }
            return t.d.option.page;
        };
        p.where = p.where || function (where) {
            var t = this;
            if (typeof where !== 'undefined') {
                t.d.option.where = where;
                return t;
            }
            return t.d.option.where;
        };
        p.orderBy = p.order_by = p.orderBy || p.order_by || function (order_by) {
            var t = this;
            if (typeof order_by !== 'undefined') {
                if(order_by === false)return t;
                t.d.option.order_by = order_by;
                return t;
            }
            return t.d.option.order_by;
        };
        p.orderDir = p.order_dir = p.orderDir || p.order_dir || function (order_dir) {
            var t = this;
            if (typeof order_dir !== 'undefined') {
                if(order_dir === false)return t;
                t.d.option.order_dir = order_dir;
                return t;
            }
            return t.d.option.order_dir;
        };
        p.order = p.order || function (order) {
            var t = this;
            if (typeof order !== 'undefined') {
                if(order === false)return t;
                t.d.option.order = order;
                return t;
            }
            return t.d.option.order;
        };
        p.links = p.links || function (links) {
            var t = this;
            if (typeof links !== 'undefined') {
                t.d.option.links = links;
                return t;
            }
            return t.d.option.links;
        };
        p.set = p.values = p.set || p.values || function(set, value){
            var t = this;
            if (typeof set === 'string' && typeof value !== 'undefined') {
                t.d.option.set[set] = value;
                return t;
            }
            if (typeof set === 'object' || typeof set === 'array') {
                for(var i in set){
                    t.d.option.set[i] = set[i];
                }
                return t;
            }
            return t.d.option.set;
        };

        $AA.m[moduleName] = module;
        $AA[moduleNameLowerFirst] = function (obj) {
            var t = new module(obj);
            return t;
        };
    };
})();

(function(){
    var Segments = function (obj) {
        var t = this;
        t.d = {
            a: 3,
            option: {},
            url: $AA.u.segments
        };
        t.init();

        if (typeof obj !== 'undefined') {
            t.initParameter(obj);
        }
    };


    var p = Segments.prototype;
    p.calculateByArray = function(arr){
        var t = this;
        return $.ajax({
            url: t.d.url+'/calculate' + t.d.urlSuffix,
            type: 'GET',
            data: arr,
            dataType: 'json',
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
/**/
    p.calculateById = function(id){
        var t = this;
        return $.ajax({
            url: t.d.url+'/'+id+'/calculate' + t.d.urlSuffix,
            type: 'GET',
            data: {
                waitForResponse:true,
                saveAfterFinish:false
            },
            dataType: 'json',
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.calculateAndSave = function(id){
        var t = this;
        return $.ajax({
            url: t.d.url+'/'+id+'/calculate' + t.d.urlSuffix,
            type: 'GET',
            data: {
                waitForResponse:true,
                saveAfterFinish:true
            },
            dataType: 'json',
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    
    $AA.initBasicFunctions(Segments, "Segments");

})();

(function(){
    var Campaigns = function (obj) {
        var t = this;
        t.d = {
            a: 3,
            option: {},
            url: $AA.u.campaigns
        };
        t.init();

        if (typeof obj !== 'undefined') {
            t.initParameter(obj);
        }
    };


    var p = Campaigns.prototype;

    p.getOpenStatById = function (id, from, to) {
        var t = this;
        var data = {
            format:'total'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getClickStatById = function (id, from, to) {
        var t = this;
        var data = {
            format:'total'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getShareStatById = function (id, from, to) {
        var t = this;
        var data = {
            format:'total'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/shares' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getUnsubscribeStatById = function (id, from, to) {
        var t = this;
        var data = {
            format:'total'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/unsubscribes' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getBounceStatById = function (id, from, to) {
        var t = this;
        var data = {
            format:'total'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/bounces' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getGeoStatById = function (id, from, to) {
        var t = this;
        var data = {
            format:'raw'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/geo-locations' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getOpenTimeLineById = function (id, from, to, step) {
        var t = this;
        var data = {
            format:'timeline'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        if(typeof step !== 'undefined' && step !== false){
            data.step = step;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getClickTimeLineById = function (id, from, to, step) {
        var t = this;
        var data = {
            format:'timeline'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        if(typeof step !== 'undefined' && step !== false){
            data.step = step;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getOpenDevicePieById = function (id, from, to) {
        var t = this;
        var data = {
            format:'aggregate',
            groupBy:'device'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getClickDevicePieById = function (id, from, to) {
        var t = this;
        var data = {
            format:'aggregate',
            groupBy:'device'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getOpenDeviceTimeLineById = function (id, from, to, step) {
        var t = this;
        var data = {
            format:'timeline',
            groupBy:'device'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getClickDeviceTimeLineById = function (id, from, to, step) {
        var t = this;
        var data = {
            format:'timeline',
            groupBy:'device'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getOpenOsPieById = function (id, from, to) {
        var t = this;
        var data = {
            format:'aggregate',
            groupBy:'os'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getClickOsPieById = function (id, from, to) {
        var t = this;
        var data = {
            format:'aggregate',
            groupBy:'os'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getOpenOsTimeLineById = function (id, from, to, step) {
        var t = this;
        var data = {
            format:'timeline',
            groupBy:'os'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getClickOsTimeLineById = function (id, from, to, step) {
        var t = this;
        var data = {
            format:'timeline',
            groupBy:'os'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getOpenBrowserPieById = function (id, from, to) {
        var t = this;
        var data = {
            format:'aggregate',
            groupBy:'browser'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getClickBrowserPieById = function (id, from, to) {
        var t = this;
        var data = {
            format:'aggregate',
            groupBy:'browser'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getOpenBrowserTimeLineById = function (id, from, to, step) {
        var t = this;
        var data = {
            format:'timeline',
            groupBy:'browser'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getClickBrowserTimeLineById = function (id, from, to, step) {
        var t = this;
        var data = {
            format:'timeline',
            groupBy:'browser'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getOpenDomainPieById = function (id, from, to) {
        var t = this;
        var data = {
            format:'aggregate',
            groupBy:'domain'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getClickDomainPieById = function (id, from, to) {
        var t = this;
        var data = {
            format:'aggregate',
            groupBy:'domain'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getOpenDomainTimeLineById = function (id, from, to, step) {
        var t = this;
        var data = {
            format:'timeline',
            groupBy:'domain'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getClickDomainTimeLineById = function (id, from, to, step) {
        var t = this;
        var data = {
            format:'timeline',
            groupBy:'domain'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getHeatMapById = function (id, from, to) {
        var t = this;
        var data = {
            format:'raw'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/heat-map' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    
    $AA.initBasicFunctions(Campaigns, "Campaigns");
    p.send = p.insert;

})();

(function(){
    var SplitTests = function (obj) {
        var t = this;
        t.d = {
            a: 3,
            option: {},
            url: $AA.u.splitTests
        };
        t.init();

        if (typeof obj !== 'undefined') {
            t.initParameter(obj);
        }
    };


    var p = SplitTests.prototype;

    p.getOpenStatById = function (id, from, to) {
        var t = this;
        var data = {
            format:'total'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getClickStatById = function (id, from, to) {
        var t = this;
        var data = {
            format:'total'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getShareStatById = function (id, from, to) {
        var t = this;
        var data = {
            format:'total'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/shares' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getUnsubscribeStatById = function (id, from, to) {
        var t = this;
        var data = {
            format:'total'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/unsubscribes' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getBounceStatById = function (id, from, to) {
        var t = this;
        var data = {
            format:'total'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/bounces' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getGeoStatById = function (id, from, to) {
        var t = this;
        var data = {
            format:'raw'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/geo-locations' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getOpenTimeLineById = function (id, from, to, step) {
        var t = this;
        var data = {
            format:'timeline'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        if(typeof step !== 'undefined' && step !== false){
            data.step = step;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getClickTimeLineById = function (id, from, to, step) {
        var t = this;
        var data = {
            format:'timeline'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        if(typeof step !== 'undefined' && step !== false){
            data.step = step;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getOpenDevicePieById = function (id, from, to) {
        var t = this;
        var data = {
            format:'aggregate',
            groupBy:'device'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getClickDevicePieById = function (id, from, to) {
        var t = this;
        var data = {
            format:'aggregate',
            groupBy:'device'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getOpenOsPieById = function (id, from, to) {
        var t = this;
        var data = {
            format:'aggregate',
            groupBy:'os'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getClickOsPieById = function (id, from, to) {
        var t = this;
        var data = {
            format:'aggregate',
            groupBy:'os'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getOpenBrowserPieById = function (id, from, to) {
        var t = this;
        var data = {
            format:'aggregate',
            groupBy:'browser'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getClickBrowserPieById = function (id, from, to) {
        var t = this;
        var data = {
            format:'aggregate',
            groupBy:'browser'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getOpenDomainPieById = function (id, from, to) {
        var t = this;
        var data = {
            format:'aggregate',
            groupBy:'domain'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/opens' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getClickDomainPieById = function (id, from, to) {
        var t = this;
        var data = {
            format:'aggregate',
            groupBy:'domain'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/clicks' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    
    $AA.initBasicFunctions(SplitTests, "SplitTests");
    p.send = p.insert;

})();

(function(){
    var Newsletters = function (obj) {
        var t = this;
        t.d = {
            a: 3,
            option: {},
            url: $AA.u.newsletters
        };
        t.init();

        if (typeof obj !== 'undefined') {
            t.initParameter(obj);
        }
    };

    var p = Newsletters.prototype;

    p.copy = function (id, data, done) {
        var t = this;
        var data = data || {};
        data.copyData = data.copyData || {};
        var done = done || function(){};
        return t.getRecordById(id).done(function(getData){
            var insertData = {
                name:data.name || ((data.copyData.prefix || '') + getData.name + (data.copyData.suffix || '')),
                subject:data.subject || getData.subject,
                editorCode:data.editorCode || getData.editorCode,
                htmlCode:data.htmlCode || getData.htmlCode,
                maxWidth:data.maxWidth || getData.maxWidth,
                tags:data.tags || getData.tags
            };
            return t.insert(insertData).done(function(localData){
                done.apply(t, [localData]);
            });
        });
    };



    $AA.initBasicFunctions(Newsletters, "Newsletters");

})();

(function(){
    var Contacts = function (obj) {
        var t = this;
        t.d = {
            a: 3,
            option: {},
            url: $AA.u.contacts
        };
        t.init();

        if (typeof obj !== 'undefined') {
            t.initParameter(obj);
        }
    };


    var p = Contacts.prototype;

    p.getActivitiesById = function(id){
        var t = this;
        return $.ajax({
            url: t.d.url + '/' + id + '/activities',
            type: 'GET',
            dataType: 'json',
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    
    $AA.initBasicFunctions(Contacts, "Contacts");

})();

(function(){
    var CustomFields = function (obj) {
        var t = this;
        t.d = {
            a: 3,
            option: {},
            url: $AA.u.customFields
        };
        t.init();

        if (typeof obj !== 'undefined') {
            t.initParameter(obj);
        }
    };


    var p = CustomFields.prototype;

    
    $AA.initBasicFunctions(CustomFields, "CustomFields");

})();

(function(){
    var Users = function (obj) {
        var t = this;
        t.d = {
            a: 3,
            option: {},
            url: $AA.u.users
        };
        t.init();

        if (typeof obj !== 'undefined') {
            t.initParameter(obj);
        }
    };


    var p = Users.prototype;

    p.switch = function(user){
        return $.ajax({
            url: $AA.u.users + '/switch-site',
            type: 'POST',
            data:{site:user},
            dataType: 'json',
            headers: {Authorization: 'Bearer ' + $AA.token().get()}
        }).done(function(data) {
            $AA.token().set(data);
            $AA.token().refreshLoopRestart();
        }).error(function(errData){

        });
    };
    
    $AA.initBasicFunctions(Users, "Users");

})();

(function(){
    var Jobs = function (obj) {
        var t = this;
        t.d = {
            a: 3,
            option: {},
            url: $AA.u.jobs
        };
        t.init();

        if (typeof obj !== 'undefined') {
            t.initParameter(obj);
        }
    };


    var p = Jobs.prototype;


    $AA.initBasicFunctions(Jobs, "Jobs");

})();

(function(){
    var Webhooks = function (obj) {
        var t = this;
        t.d = {
            a: 3,
            option: {},
            url: $AA.u.webhooks
        };
        t.init();

        if (typeof obj !== 'undefined') {
            t.initParameter(obj);
        }
    };


    var p = Webhooks.prototype;


    $AA.initBasicFunctions(Webhooks, "Webhooks");

})();

(function(){
    var Templates = function (obj) {
        var t = this;
        t.d = {
            a: 3,
            option: {},
            url: $AA.u.templates
        };
        t.init();

        if (typeof obj !== 'undefined') {
            t.initParameter(obj);
        }
    };

    var p = Templates.prototype;

    p.copy = function (id, data, done) {
        var t = this;
        var data = data || {};
        data.copyData = data.copyData || {};
        var done = done || function(){};
        return t.getRecordById(id).done(function(getData){
            var insertData = {
                name:data.name || ((data.copyData.prefix || '') + getData.name + (data.copyData.suffix || '')),
                editorCode:data.editorCode || getData.editorCode,
                htmlCode:data.htmlCode || getData.htmlCode,
                maxWidth:data.maxWidth || getData.maxWidth
            };
            return t.insert(insertData).done(function(localData){
                done.apply(t, [localData]);
            });
        });
    };

    
    $AA.initBasicFunctions(Templates, "Templates");

})();

(function(){
    var Forms = function (obj) {
        var t = this;
        t.d = {
            a: 3,
            option: {},
            url: $AA.u.forms
        };
        t.init();

        if (typeof obj !== 'undefined') {
            t.initParameter(obj);
        }
    };


    var p = Forms.prototype;


    p.getFullTimeLineById = function (id, from, to, step) {
        var t = this;
        var data = {
            format:'timeline'
        };
        if(typeof from !== 'undefined' && from !== false){
            data.from = from;
        }
        if(typeof to !== 'undefined' && to !== false){
            data.to = to;
        }
        return $.ajax({
            url: t.d.url + '/' + id + '/conversion' + t.d.urlSuffix,
            type: 'GET',
            dataType: 'json',
            data: data,
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };

    
    $AA.initBasicFunctions(Forms, "Forms");

})();

(function(){
    var Automations = function (obj) {
        var t = this;
        t.d = {
            a: 3,
            option: {},
            url: $AA.u.automations
        };
        t.init();

        if (typeof obj !== 'undefined') {
            t.initParameter(obj);
        }
    };


    var p = Automations.prototype;

    
    $AA.initBasicFunctions(Automations, "Automations");

})();

(function(){
    var ContactImports = function (obj) {
        var t = this;
        t.d = {
            a: 3,
            option: {},
            url: $AA.u.contactImports
        };
        t.init();

        if (typeof obj !== 'undefined') {
            t.initParameter(obj);
        }
    };


    var p = ContactImports.prototype;

    p.getContactsByIdAndType = function(id, type){
        var t = this;
        return $.ajax({
            url: t.d.url + '/' + id + '/' + type,
            type: 'GET',
            dataType: 'json',
            headers: {Authorization: 'Bearer ' + $AA.token().get()},
            error: $AA.token().error()
        });
    };
    p.getForbiddenContactsById = function(id){
        return this.getContactsByIdAndType(id, 'creation-forbidden-errors');
    };
    p.getAlreadyInDatabaseContactsById = function(id){
        return this.getContactsByIdAndType(id, 'already-in-database-errors');
    };
    p.getInvalidContactsById = function(id){
        return this.getContactsByIdAndType(id, 'invalid-email-errors');
    };
    p.getUnsubscribedContactsById = function(id){
        return this.getContactsByIdAndType(id, 'unsubscribed-errors');
    };
    
    $AA.initBasicFunctions(ContactImports, "ContactImports");

})();

(function(){
    var Tags = function (obj) {};

    var p = Tags.prototype;
    p.get = function(){
        return $.ajax({
            url: $AA.u.tags,
            type: 'GET',
            dataType: 'json',
            headers: {Authorization: 'Bearer ' + $AA.token().get()}
        });
    };

    /* No Rest-Api, so the basic functions are disabled */

    $AA.m['Tags'] = Tags;
    $AA['tags'] = function (obj) {
        var t = new Tags(obj);
        return t;
    };

})();

(function(){
    var Account = function (obj) {};

    var p = Account.prototype;
    p.get = function(){
        return $.ajax({
            url: $AA.u.account,
            type: 'GET',
            dataType: 'json',
            headers: {Authorization: 'Bearer ' + $AA.token().get()}
        });
    };
    p.limits = function(){
        return $.ajax({
            url: $AA.u.account + '/limits',
            type: 'GET',
            dataType: 'json',
            headers: {Authorization: 'Bearer ' + $AA.token().get()}
        });
    };
    p.getPackages  = function(){
        return $.ajax({
            url: $AA.u.account + '/limits',
            type: 'GET',
            dataType: 'json',
            headers: {Authorization: 'Bearer ' + $AA.token().get()}
        });
    };

    /* No Rest-Api, so the basic functions are disabled */

    $AA.m['Account'] = Account;
    $AA['account'] = function (obj) {
        var t = new Account(obj);
        return t;
    };

})();

(function(){
    var Clients = function (obj) {
        var t = this;
        t.d = {
            a: 3,
            option: {},
            url: $AA.u.clients
        };
        t.init();

        if (typeof obj !== 'undefined') {
            t.initParameter(obj);
        }
    };


    var p = Clients.prototype;

    p.create = function(){
        return $.ajax({
            url: $AA.u.clients,
            type: 'POST',
            dataType: 'json',
            headers: {Authorization: 'Bearer ' + $AA.token().get()}
        });
    };
    
    $AA.initBasicFunctions(Clients, "Clients");

})();

(function(){
    $AA.parseBoolean = function (value, nullOnFailure) {
        if (typeof value === 'string')
            value = value.toLowerCase();
        switch (value) {
            case true:
            case 'true':
            case 1:
            case '1':
            case 'on':
            case 'yes':
            case 'y':
            case '✓':
            case '✔':
            case '☑':
            case '☒':
                value = true;
                break;
            case false:
            case 'false':
            case 0:
            case '0':
            case 'off':
            case 'no':
            case 'n':
            case 'x':
            case '✗':
            case '✘':
            case '☐':
                value = false;
                break;
            default:
                if (nullOnFailure) {
                    value = null;
                } else {
                    value = false;
                }
                break;
        }
        return value;
    };
})();

(function(){

    $AA.refreshTable = function (table) {
        if (typeof table === 'undefined') {
            return false;
        }
        var apiName = table.data('apiName');
        var apiUrlSuffix = table.data('apiUrlSuffix');
        var apiItemsDir = table.data('apiItemsDir');
        var apiFormat = table.data('apiFormat');
        var refreshComplete = table.data('refreshComplete');
        var orderBy = table.d.orderBy;
        var orderDir = table.d.orderDir;
        var limit = table.perPage();
        var page = table.page();
        var fields = table.data('fields') || false;
        var showId = false;
        if(typeof table.data('xhr') !== 'undefined'){
            table.data('xhr').abort();
        }
        if(typeof apiUrlSuffix === 'undefined'){
            apiUrlSuffix = '';
        }
        if(typeof apiItemsDir === 'undefined'){
            apiItemsDir = false;
        }
        if(typeof apiFormat === 'undefined'){
            apiFormat = false;
        }
        if(typeof refreshComplete !== 'function'){
            refreshComplete = function(){};
        }

        if(fields === false) {
            fields = [];
            var cols = table.cols();
            for (var i = table.selectable() ? 1 : 0; i < cols.length; i++) {
                if (cols[i].active()) {
                    fields.push(cols[i].name());
                }
            }
            if ($.inArray('id', fields) === -1) {
                fields.push('id');
            }else{
                showId = true;
            }
            fields = fields.join();
        }else{
            fields = JSON.stringify(fields);
        }

        var where = [];
        if((typeof table.d.searchValue === 'string' || typeof table.d.searchValue === 'number') && table.d.searchValue.length > 0){
            var cols = table.cols();
            for(var i = 0; i < cols.length; i++){
                console.log(cols[i]);
                var scols = table.d.settings.cols;
                var ss = {};
                for(var j = 0; j < scols.length; j++){
                    ss[scols[j].name || 0] = scols[j].searchable===false?false:true;
                }
                console.log(ss,scols);
                var name = cols[i].name();
                if(ss[name]){
                    where.push([[name, 'like', '%'+table.d.searchValue+'%']]);
                }
            }
        }

        table.d.$checkboxCheckAll.prop('checked', false).change();
        table.loading();

        var xhr = $AA[apiName]().links('').fields(fields).limit(limit).page(page).where(where).orderBy(orderBy).orderDir(orderDir).urlSuffix(apiUrlSuffix).format(apiFormat).get().done(function (data) {
            table.pageMax(data.page_count);
            if(apiItemsDir !== false){
                var dir = apiItemsDir.split('/');
                var records = data;
                for(var i = 0; i < dir.length; i++){
                    records = records[dir[i]];
                }
            }else{
                var records = data['_embedded'][apiName];
            }
            var length = records.length;
            //if (length > 0) {
                var rows = [];
                for (var i = 0; i < length; i++) {
                    var row = {recordId: records[i]['id'] || 0, values: {}};
                    for (var j in records[i]) {
                        if (j.charAt(0) === '_' || (j === 'id' && !showId)) {
                            continue;
                        }
                        if (records[i][j] !== null && (typeof records[i][j] === 'object' || typeof records[i][j] === 'array')) {
                            if($.isArray(records[i][j])){
                                row.values[j] = records[i][j];
                            }else{
                                var isDateObj = false;
                                var dateObjCount = 0;
                                var isCodeValueObj = false;
                                var codeValueObjCount = 0;
                                var isCustomFields = false;
                                for (var k in records[i][j]) {
                                    if (k === 'date' || k === 'timezone' || k === 'timezone_type') {
                                        dateObjCount++;
                                    }
                                    if (k === 'code' || k === 'value') {
                                        codeValueObjCount++;
                                    }
                                    isDateObj = false;
                                    isCodeValueObj = false;
                                    isCustomFields = false;
                                    if (dateObjCount === 3) {
                                        isDateObj = true;
                                    } else if (codeValueObjCount == 2) {
                                        isCodeValueObj = true;
                                    } else if(j === 'customFields'){
                                        isCustomFields = true;
                                    }
                                }
                                if (isDateObj) {
                                    row.values[j] = records[i][j]['date'] || records[i][j][Object.keys(records[i][j])[0]] || '';
                                } else if (isCodeValueObj) {
                                    row.values[j] = records[i][j]['value'] || records[i][j][Object.keys(records[i][j])[0]] || '';
                                } else if (isCustomFields) {
                                    for (var l in records[i][j]) {
                                        if (records[i][j][l] !== null && (typeof records[i][j][l] === 'array' || typeof records[i][j][l] === 'object')) {
                                            if($.isArray(records[i][j][l])){
                                                row.values['customFields.' + l] = records[i][j][l];
                                                if(records[i][j][l].length > 0){
                                                    row.values['customFields.' + l] = '<ul style="margin: 0; padding-left: 17px;"><li>' + records[i][j][l].join('</li><li>') + '</li></ul>';
                                                }
                                            }else {
                                                row.values['customFields.' + l] = records[i][j][l]['date'] || records[i][j][l]['value'] || records[i][j][l][Object.keys(records[i][j][l])[0]] || '';
                                            }
                                        } else {
                                            row.values['customFields.' + l] = records[i][j][l];
                                        }
                                    }
                                } else {
                                    row.values[j] = records[i][j];
                                }
                            }
                        } else {
                            row.values[j] = records[i][j];
                        }
                    }
                    rows.push(row);
                }

                table.deleteRows();
                var col = table.getColByName('customFields');
                if(col !== false){
                    col.$cells().css('display', 'none');
                }
                table.rows(rows);

                /*for(var i = 0; i < rows.length; i++){
                    var ind = i;
                    (function(index) {
                        setTimeout(function(){
                            table.addRows(rows[index]);
                        }, i);
                    })(i);
                }*/

                table.d.stepFunction();

                setTimeout(function(){
                    refreshComplete.apply(table, [table, table.d.$widget]);
                }, 10);

            //}
        });
        table.data('xhr', xhr);
    }
})();

(function(){

    $AA.exportTable = function (table) {
        if (typeof table === 'undefined') {
            return false;
        }
        var apiName = table.data('apiName');
        var apiUrlSuffix = table.data('apiUrlSuffix');
        var exportFields = table.data('exportFields') || [];
        if(exportFields.length > 0) {
            exportFields = exportFields.join(',');
        }else{
            exportFields = false;
        }
        var orderBy = table.d.orderBy;
        var orderDir = table.d.orderDir;
        if(typeof apiUrlSuffix === 'undefined'){
            apiUrlSuffix = '';
        }

        var xhr = $AA[apiName]().orderBy(orderBy).orderDir(orderDir).fields(exportFields).urlSuffix(apiUrlSuffix).export().done(function(data, textStatus, jqXHR){
            window.location.href = jqXHR.getResponseHeader('X-Download-Url');
        });
    };
})();

(function(){

    $AA.downloadContent = function (content, filename) {
        var supportsDownloadAttribute = 'download' in document.createElement('a');
        if(supportsDownloadAttribute) {
            var link = $('<a></a>');
            link.attr({
                href: 'data:attachment/csv,' + encodeURI(content),
                target: '_blank',
                download: filename
            })[0].click();
            setTimeout(function() {
                link.remove();
            }, 50);
        } else if(typeof safari !== 'undefined') {
            window.open('data:attachment/csv,' + encodeURI(content));
        }else if (window.navigator.msSaveOrOpenBlob) {
            var blob = new Blob([content], {type: "text/plain"});
            navigator.msSaveBlob(blob, filename);
        } else {
            alert($A.translate('Your browser is not supported the csv export.'))
        }
    };

})();

(function(){
    console.log('%c AutomizyJsApi module loaded! ', 'background: #000000; color: #bada55; font-size:14px');
})();
window.$AA = $AA;
window.AutomizyJsApi = $AA;
})($);