(function ($) {
    $(document).ready(function () {
        var rqHandler = new $.AclRequestHandler(__rCore.ajax);

        $('body').on('click.cir', '.removeConfigImage', function (e) {
            e.preventDefault();
            var target = $(this).attr('acl-target');
            rqHandler.Arc({
                context: __rCore.context.cir,
                intent: true,
                data: {
                    cir: target,
                },
                target: target,
                luacl: $(this),
                self: $(this)
            });
        });
        $('body').on('change.aclimageupload', '.aclimageupload_input' , function(){
            var target = $(this).attr('for');
            var parent = $(this).closest('.form-group');
            var preview = parent.find('img#'+target+'preview');
            var button = $(this).parent().find('span');
            var file = this.files[0];
            if(file && file.type.match('image/jpeg|image/png')) {
                var rd = new FileReader();
                rd.onload = function (e) {
                    preview.attr('src', e.target.result).attr('alt',file.name);
                    $('#'+target).val(file.name);
                    button.text('Change');
                };
                rd.readAsDataURL(file);
            } else {
                if(preview.hasAttr('acl-actual')){
                    preview.attr({
                        src : __rCore.path.images + preview.attr('acl-actual'),
                        alt : preview.attr('acl-actual')
                    });
                    $('#'+target).val(preview.attr('acl-actual'));
                    button.text('Change');
                } else {
                    preview.attr('src', __rCore.path.images + preview.attr('acl-default'));
                    $(this).val();
                    button.text('Choose File');
                    $('#'+target).val('');
                }
            }

        });
        $('body').tooltip({
            selector: '[data-toggle="tooltip"]'
        });
        tinymce.init({
            selector: "textarea#tinymce",
            theme: "modern",
            height: 300,
            plugins: [
                'aclplaceholder',
                'advlist autolink lists link image charmap print preview hr anchor pagebreak',
                'searchreplace wordcount visualblocks visualchars code fullscreen',
                'insertdatetime media nonbreaking save table contextmenu directionality',
                'emoticons template paste textcolor colorpicker textpattern imagetools'
            ],
            toolbar1: 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
            toolbar2: 'print preview media | forecolor backcolor emoticons',
            image_advtab: true
        });
        tinymce.suffix = ".min";
        tinyMCE.baseURL = __rCore.path.plugin + 'tinymce';
    });

    $.AclRequestHandler.prototype['response'+__rCore.context.cir] = function(request) {
        var label = $('body').find('label[for="'+request.target+'"]').text();
        var preview = $('body').find('img#'+request.target+'preview');
        label = label || 'File';
        swal("Successfull", label+' '+(this.response.message?this.response.message:'removed successfully'), "success");
        $('body').find('input#'+request.target).val('');
        $('body').find('input[name="'+request.target+'"]').val('');
        preview.attr('src', __rCore.path.images + preview.attr('acl-default')).attr('alt', preview.attr('acl-default'));
        if(preview.hasAttr('acl-actual')) preview.removeAttr('acl-actual');
        request.self.parent().find('.file-field').find('span').text('Choose File');
        request.self.remove();
    };

    $.AclRequestHandler.prototype['error'+__rCore.context.cir] = function(request, response) {
        swal("Failed", (response.message?response.message:'File cannot be removed'), "error");
    };

    $.AclRequestHandler.prototype['loading'+__rCore.context.cir] = function(elem) {
        if(elem.attr('acl-is-loading')){
            elem.button('reset');
            elem.removeAttr('acl-is-loading')
        } else {
            elem.button('loading');
            elem.attr('acl-is-loading', true)
        }
    };

    $.fn.hasAttr = function (attr) {
        attr = attr?$(this).attr(attr):false;
        return (typeof attr !== typeof undefined && attr !== false);
    }
    $.fn.outerHTML = function(){
        return (!this.length) ? this : (this[0].outerHTML || (
            function(el){
                var div = document.createElement('div');
                div.appendChild(el.cloneNode(true));
                var contents = div.innerHTML;
                div = null;
                return contents;
            }
        )(this[0]));
    }


}(jQuery));