var Cookieconsent = function () {

    var types = {
        1: 'opt-in',
        2: 'info'
    };

    return {
        init: function () {
            var type = Config.get('cookie_type');

            if (types.hasOwnProperty(type)) {
                var btn = $('<span class="btn-success" style="display:none" />').insertAfter('body'),
                    isOptIn = types[type] === 'opt-in',
                    message = Language.get(isOptIn ? 'cookie_optin_message' : 'cookie_optout_message'),
                    dismiss = Language.get(isOptIn ? 'cookie_optin_button' : 'cookie_optout_button');

                window.cookieconsent.initialise({
                    position: Config.get('cookie-position') || 'bottom-right',
                    type: types[type],
                    content: {
                        message: '<div class="cc-scroll">' + message + '</div>',
                        dismiss: dismiss,
                        allow: Language.get('cookie_optin_button'),
                        deny: Language.get('cookie_deny'),
                        link: Language.get('cookie_linktext'),
                        policy: Language.get('cookie_policy'),
                        href: "/privacy/#cookies",
                        target: "_self"
                    },
                    palette: {
                        popup: {
                            background: 'rgba(0,0,0,.8)'
                        },
                        button: {
                            background: btn.css('background-color'),
                            color: btn.css('color')
                        }
                    },
                    onStatusChange: function (status, chosenBefore) {
                        var type = this.options.type;
                        var didConsent = this.hasConsented();
                        if (type === 'opt-in' && didConsent) {
                            Cookieconsent.loadJavascripts();
                        } else if (type === 'opt-in' && !didConsent) {
                            Cookieconsent.deleteCookies();
                        }
                    }
                });
            }

            btn.remove();
        },

        loadJavascripts: function () {
            $('script.cc').each(function () {
                var scriptData = $(this).html();
                if ($(this).attr('src')) {
                    $('body').append('<script src="' + $(this).attr('src') + '">' + scriptData + '</script>');
                } else {
                    $('body').append('<script>' + scriptData + '</script>');
                }
            });
        },

        deleteCookies: function () {
            var needed_cookies = [
                'SESSION',
                'SESSION_LOCAL',
                'SESSION_DEV',
                'SESSION_STATE',
                'khost',
                'token',
                'cookieconsent_status',
                'et'
            ];

            $.each($.cookie(), function (c) {
                if ($.inArray(c, needed_cookies) === -1) {
                    $.removeCookie(c, {path: '/'})
                }
            });
        }
    }
}();