var inlieEditorCurrentElement = null;
var kcfinderDialog = function (type, dir, replaceAttr) {
    window.KCFinder = {
        callBack: function (url) {
            var pattern = new RegExp(replaceAttr + '=".*?"');
            var data = inlieEditorCurrentElement.html().replace(pattern, replaceAttr + '="' + url + '"');
            $.post('/ajax/inlineeditor/inlineeditor_admin/save', {'id': inlieEditorCurrentElement.attr('data-id'), 'lang': '{id_lan}', 'data': data}, function (rs) {
                if (rs.status == 1) {
                    inlieEditorCurrentElement.html(data);
                    $('#inlineeditor_dialog').hide();
                }
            });
            window.KCFinder = null;
        }
    };
    var url = '/assets/plugins/kcfinder/browse.php?type=' + type + '&dir=' + type;
    if (dir) {
        url += '/' + dir;
    }
    window.open(url, 'kcfinder_textbox',
            'status=0, toolbar=0, location=0, menubar=0, directories=0, ' +
            'resizable=1, scrollbars=0, width=800, height=600'
            );
};
var adapters = {
    'img': function (dir) {
        kcfinderDialog('images', dir, 'src');
        return false;
    },
    'file': function (dir) {
        kcfinderDialog('files', dir, 'href');
        return false;
    }
};
$(function () {
    var isEditor = false;
    $('body').prepend('{dialog}');
    $('.inlineeditor').click(function (event) {
        inlieEditorCurrentElement = $(this);

        // Search in adapters
        if (inlieEditorCurrentElement.attr('data-type') && adapters[inlieEditorCurrentElement.attr('data-type')]) {
            return adapters[inlieEditorCurrentElement.attr('data-type')](inlieEditorCurrentElement.attr('data-dir'));
        }

        if (isEditor && inlieEditorCurrentElement.attr('data-texteditor') == 'off') {
            CKEDITOR.instances['inlineeditor_data'].destroy();
            isEditor = false;
        } else if (!isEditor && inlieEditorCurrentElement.attr('data-texteditor') != 'off') {
            $("#inlineeditor_data").ckeditor({toolbar: 'RTpanel'});
            isEditor = true;
        }

        // Load data
        $.get('/ajax/inlineeditor/inlineeditor_admin/load/' + inlieEditorCurrentElement.attr('data-id') + '/{id_lan}', function(data){
            if (data.result) {
                $('#inlineeditor_data').val(data.result);
                var dialog = $('#inlineeditor_dialog');
                var dialogHeight = dialog.height();
                var dialogWidth = dialog.width();
                var top = event.clientY;
                var left = event.clientX;
                if (top + dialogHeight > $(window).height()) {
                    top = top - dialogHeight;
                }
                if (left + dialogWidth > $(window).width()) {
                    left = left - dialogWidth;
                }
                dialog.attr('style', 'top: ' + top + 'px;left: ' + left + 'px').show();
            }
        });


        return false;
    });
    $(document).on('click', '.inlineeditor_btn', function () {
        var action = $(this).attr('href').replace('#', '');
        if (action === 'cancel') {
            $('#inlineeditor_dialog').hide();
        }
        if (action === 'save') {
            $.post('/ajax/inlineeditor/inlineeditor_admin/save', {'id': inlieEditorCurrentElement.attr('data-id'), 'lang': '{id_lan}', 'data': $('#inlineeditor_data').val()}, function (data) {
                if (data.status == 1) {
                    inlieEditorCurrentElement.html($('#inlineeditor_data').val());
                    $('#inlineeditor_dialog').hide();
                }
            });
        }
        return false;
    });
});