var jQuery_1_8_2 = jQuery_1_8_2 || $.noConflict();
(function ($, undefined) {
    $(function () {
        "use strict";
        var $frmCreateExtra = $("#frmCreateExtra"),
            $frmUpdateExtra = $("#frmUpdateExtra"),
            datagrid = ($.fn.datagrid !== undefined),
            vOpts;

        vOpts = {
            errorPlacement: function (error, element) {
                error.insertAfter(element.parent());
            },
            onkeyup: false,
            errorClass: "err",
            wrapper: "em"
        };

        if ($frmCreateExtra.length > 0) {
            $frmCreateExtra.validate(vOpts);
        }

        if ($frmUpdateExtra.length > 0) {
            $frmUpdateExtra.validate(vOpts);
        }

        if ($("#grid").length > 0 && datagrid) {
            var options = {
                buttons: [
                    {type: "edit", url: "index.php?controller=pjAdminExtras&action=pjActionUpdate&id={:id}"},
                    {type: "delete", url: "index.php?controller=pjAdminExtras&action=pjActionDelete&id={:id}"}
                ],
                columns: [
                    {text: myLabel.name, type: "text", sortable: true, editable: true, width: 500},
                    {text: myLabel.status, type: "select", sortable: true, editable: true, options: [
                        {label: myLabel.active, value: "T"},
                        {label: myLabel.inactive, value: "F"}
                    ], applyClass: "pj-status"}
                ],
                dataUrl: "index.php?controller=pjAdminExtras&action=pjActionGetJson",
                dataType: "json",
                fields: ['name', 'status'],
                paginator: {
                    actions: [
                        {text: myLabel.delete_selected, url: "index.php?controller=pjAdminExtras&action=pjActionDeleteBulk", render: true, confirmation: myLabel.delete_confirmation}
                    ],
                    gotoPage: true,
                    paginate: true,
                    total: true,
                    rowCount: true
                },
                saveUrl: "index.php?controller=pjAdminExtras&action=pjActionSaveJson&id={:id}",
                select: {
                    field: "id",
                    name: "record[]"
                }
            };

            var $grid = $("#grid").datagrid(options);
        }

        $(document).on("click", ".btn-all", function (e) {
            if (e && e.preventDefault) {
                e.preventDefault();
            }
            $(this).addClass("pj-button-active").siblings(".pj-button").removeClass("pj-button-active");
            var content = $grid.datagrid("option", "content"),
                cache = $grid.datagrid("option", "cache");
            $.extend(cache, {
                status: "",
                q: ""
            });
            $grid.datagrid("option", "cache", cache);
            $grid.datagrid("load", "index.php?controller=pjAdminExtras&action=pjActionGetJson", "name", "ASC", content.page, content.rowCount);
            return false;
        }).on("click", ".btn-filter", function (e) {
            if (e && e.preventDefault) {
                e.preventDefault();
            }
            var $this = $(this),
                content = $grid.datagrid("option", "content"),
                cache = $grid.datagrid("option", "cache"),
                obj = {};
            $this.addClass("pj-button-active").siblings(".pj-button").removeClass("pj-button-active");
            obj.status = "";
            obj[$this.data("column")] = $this.data("value");
            $.extend(cache, obj);
            $grid.datagrid("option", "cache", cache);
            $grid.datagrid("load", "index.php?controller=pjAdminExtras&action=pjActionGetJson", "name", "ASC", content.page, content.rowCount);
            return false;
        }).on("submit", ".frm-filter", function (e) {
            if (e && e.preventDefault) {
                e.preventDefault();
            }
            var $this = $(this),
                content = $grid.datagrid("option", "content"),
                cache = $grid.datagrid("option", "cache");
            $.extend(cache, {
                q: $this.find("input[name='q']").val()
            });
            $grid.datagrid("option", "cache", cache);
            $grid.datagrid("load", "index.php?controller=pjAdminExtras&action=pjActionGetJson", "name", "ASC", content.page, content.rowCount);
            return false;
        }).on('click', '[data-add-option]', function (e) {
            if (e && e.preventDefault) {
                e.preventDefault();
            }

            var $options = $('body').find('[data-options]');
            var $firstOption = $options.find('p:last');
            var $clone = $firstOption.clone();
            var optionId = parseInt($clone.find('[data-option-id]').attr('data-option-id'));

            $clone.find('input').val('');
            $clone.find('[data-option-id]').attr('data-option-id', optionId + 1);
            $clone.find('[data-option-id]').text(optionId + 1);
            $clone.find('input[name="options[' + optionId + '][name]"]').attr('name', 'options[' + (optionId + 1) + '][name]');
            $clone.find('input[name="options[' + optionId + '][price]"]').attr('name', 'options[' + (optionId + 1) + '][price]');
            $clone.find('[data-delete-option]').parent().css('display', 'block');

            $options.append($clone);
        }).on('click', '[data-delete-option]', function (e) {
            if (e && e.preventDefault) {
                e.preventDefault();
            }

            $(this).closest('p').remove();
        });
    });
})(jQuery_1_8_2);