window.conjur = {
    models: {
        Record: {}
    },
    collections: {},
    views: {
        mixins: {}
    },
    routers: {},
    createEndpoints: function() {
        function genericEndpoint(path) {
            return function() {
                var i= 0, length = arguments.length;
                var localPath = [].concat(path); // clone it

                for(; i < length && _.isString(arguments[i]); i++) {
                    localPath.push(arguments[i]);
                }

                var pathString = _.flatten(localPath.map(function(p) {
                    return p.split('/').map(encodeURIComponent);
                })).join("/");

                if (i < length) {
                    pathString += '?' + jQuery.param(arguments[i]);
                }

                if (pathString.charAt(0) != '/') {
                    pathString = '/' + pathString;
                }

                return pathString;
            };
        }

        conjur.app.endpoints.authz = genericEndpoint(['api/authz', conjur.app.configuration.account]);
        conjur.app.endpoints.core  = genericEndpoint('api');
    },
    initialize: function() {
        conjur.app.namespace = conjur.models.Namespace();
        conjur.app.lists.groups = new conjur.models.ResourceList('group');
        conjur.app.lists.layers = new conjur.models.ResourceList('layer');
        conjur.app.lists.variables = new conjur.models.VariableList();
        conjur.app.lists.policies = new conjur.models.PolicyList();
        conjur.app.lists.users = new conjur.models.UserList();
        conjur.app.lists.hosts = new conjur.models.ResourceList('host');
        conjur.app.configuration = JSON.parse(decodeURIComponent(conjur.utils.readCookie('conjur_configuration')).replace(/\+/g, ' '));
        conjur.app.userId = decodeURIComponent(conjur.utils.readCookie('conjur_userid'));

        conjur.createEndpoints();
    }
};

window.conjur.app = {
    kind: '',
    lists: {
        groups: undefined,
        layers: undefined,
        variables: undefined,
        policies: undefined,
        users: undefined,
        hosts: undefined
    },
    configuration: undefined,
    userId: undefined,
    // components: {},
    router: undefined,
    // globalIds: undefined,
    endpoints: {},
    flash: null
};

window.conjur.utils = {
    errback: function(err) {
        console.log('error:', err);
    },
    pluralize: function(kind) {
        if (kind[kind.length-1] === 'y') {
            return kind.slice(0, kind.length-1) + 'ies';
        } else if (kind[kind.length - 1] != 's') {
            return kind + 's';
        } else {
            return kind;
        }
    },
    // http://www.quirksmode.org/js/cookies.html
    readCookie: function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ')
                c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0)
                return c.substring(nameEQ.length, c.length);
        }
        return null;
    }
};

function get_tabname(header, set) {
    if (!set || !set.length) {
        return header + " (0)";
    };

    return header + " ("+set.length+")";
}