$(function() {

    $('.profile_info_accordion .image-wrapper')
        .append('<div class="add_image"><i class="glyphicon glyphicon-folder-open" aria-hidden="true"></i></div><div class="delete_image"><i class="glyphicon glyphicon-remove" aria-hidden="true"></i></div>');
    if ($(".profile_info_accordion .image-wrapper.hide")
            .hasClass("hide")) {
        $('.profile_info_accordion .image-manager-input')
            .append('<div class="image-wrapper-new"><div class="add_image"><i class="glyphicon glyphicon-folder-open" aria-hidden="true"></i></div></div>');
    } else {
        $('.profile_info_accordion .image-manager-input')
            .append('<div class="image-wrapper-new hide"><div class="add_image"><i class="glyphicon glyphicon-folder-open" aria-hidden="true"></i></div></div>');
    }

    $(document)
        .on('click', '.add_image', function() {
            $(this)
                .parent()
                .parent()
                .find(".open-modal-imagemanager")
                .click();
            $(".image-wrapper-new")
                .addClass('hide');
            $(".image-wrapper")
                .removeClass('hide');

        });
    $(document)
        .on('click', '.delete_image', function() {
            $(this)
                .parent()
                .parent()
                .find(".delete-selected-image")
                .click();
            $(".image-wrapper-new")
                .removeClass("hide");
            return false;
        });
    $(document)
        .on('click', '.slugify_button', function() {
            var id = $(this)
                .data('id');
            var attribute_id = $(this)
                .data('attribute-id');
            var url = $(this)
                .data('url');
            var language_id = $(this)
                .data('language-id');
            var input = $('#' + id);
            var attribute_input = $('#' + attribute_id);
            var value = attribute_input.val();
            if (id !== undefined && url !== undefined && value !== undefined && attribute_id !== undefined && value.length > 0) {
                $.get(url, {
                    text: value,
                    languageId: language_id
                }, function(data) {
                    input.val(data);
                });
            }
        });
    $(document)
        .on('click', '.slugify_cancel_button', function() {
            var id = $(this)
                .data('id');
            var value = $(this)
                .data('value');
            var input = $('#' + id);
            if (id !== undefined && value !== undefined) {
                input.val(value);
            }
        });

    $(document)
        .on('click', 'a.viewed-toggle', function(e) {
            e.preventDefault();
            var a = $(this);
            $.ajax({
                type: "POST",
                url: a.attr('href'),
                success: function(data) {
                    console.log(data);
                    a.parent()
                     .parent()
                     .removeClass('success');
                    a.replaceWith(data.text);
                    $('.feedback-widget')
                        .replaceWith(data.widget);
                    new PNotify({
                        title: data.message.title,
                        text: data.message.text,
                        type: "success",
                        styling: "bootstrap3",
                        icon: "glyphicon glyphicon-exclamation-sign"
                    });
                }
            });
        });
    $(document)
        .on('click', '.main_menu .homepage_link a', function() {
            $(this)
                .parents('.homepage_link')
                .removeClass('active');
        });
    $(document)
        .on('click', '.sitemap-buttons .btn.ajax', function(e) {
            e.preventDefault();
            var url = $(this)
                .attr('href');
            $(this)
                .addClass('active');
            $.get(url, function() {
                $(this)
                    .removeClass('active');
                new PNotify({
                    title: "Карта сайта",
                    text: "Карта сайта успещно сгенерирована",
                    type: "success",
                    styling: "bootstrap3",
                    icon: "glyphicon glyphicon-exclamation-sign"
                });
                if ($(this)
                        .data('need-reload')) {
                    setTimeout(function() {
                        location.reload();
                    }, 1000);
                }
            }.bind(this))
             .fail(function() {
                 $(this)
                     .removeClass('active');
                 new PNotify({
                     title: "Карта сайта",
                     text: "Ошибка при генерации карты сайта",
                     type: "error",
                     styling: "bootstrap3",
                     icon: "glyphicon glyphicon-exclamation-sign"
                 });
             }.bind(this));
        });
    $('.bar_tabs>li.disabled a')
        .removeAttr('data-toggle');
    $(document)
        .on('click', '.artbox-selection-delete', function(e) {
            var selectedRows = $('.grid-view')
                .yiiGridView('getSelectedRows');
            var msg = $(this)
                .data('message');
            var url = $(this)
                .data('url');
            if (selectedRows.length > 0 && confirm(msg)) {
                $.post(url + '?ids=' + encodeURIComponent(selectedRows), function(data) {
                    if (data.success) {
                        location.reload();
                    }
                })
            }
        });
});