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

        if ($frmResendEmail.length > 0) {
            tinymce.init({
                selector: "textarea.mceEditor",
                theme: "modern",
                width: 540,
                height: 350,
                plugins: [
                    "advlist autolink lists link image charmap print preview anchor",
                    "searchreplace visualblocks code fullscreen",
                    "insertdatetime media table contextmenu paste"
                ],
                toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
            }); 
        }

        vOpts = {
            rules: {
                email: {
                    required: true,
                    email: true,
                    remote: "index.php?controller=pjAdminClients&action=pjActionCheckEmail"
                }
            },
            messages: {
                email: {
                    remote: myLabel.email_taken
                }
            },
            errorPlacement: function (error, element) {
                error.insertAfter(element.parent());
            },
            onkeyup: false,
            errorClass: "err",
            wrapper: "em"
        };

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

        if ($frmUpdateClient.length > 0) {
            vOpts.rules.email.remote = vOpts.rules.email.remote + "&id=" + $frmUpdateClient.find("input[name='id']").val();
            $frmUpdateClient.validate(vOpts);
        }

        if ($("#grid").length > 0 && datagrid) {
        	var columnOpts = [];
        	var fieldOpts = ['email', 'username', 'password'];
        	if(pjGrid.isTutor)
    		{
        		columnOpts.push({text: myLabel.email, type: "text", sortable: true, editable: true, width: 300});
            	columnOpts.push({text: myLabel.username, type: "text", sortable: true, editable: true, width: 150});
            	columnOpts.push({text: myLabel.password, type: "text", sortable: true, editable: true, width: 130});
    		}else{
    			columnOpts.push({text: myLabel.email, type: "text", sortable: true, editable: true, width: 200});
            	columnOpts.push({text: myLabel.username, type: "text", sortable: true, editable: true, width: 130});
            	columnOpts.push({text: myLabel.password, type: "text", sortable: true, editable: true, width: 110});
            	columnOpts.push({text: myLabel.tutor, type: "text", sortable: true, editable: true, width: 130});
            	fieldOpts = ['email', 'username', 'password', 'tutor'];
    		}
            var options = {
                buttons: [
                    {type: "email", url: "index.php?controller=pjAdminClients&action=pjActionResend&id={:id}"},
                    {type: "edit", url: "index.php?controller=pjAdminClients&action=pjActionUpdate&id={:id}"},
                    {type: "delete", url: "index.php?controller=pjAdminClients&action=pjActionDelete&id={:id}"}
                ],
                columns: columnOpts,
                dataUrl: "index.php?controller=pjAdminClients&action=pjActionGetJson",
                dataType: "json",
                fields: fieldOpts,
                paginator: {
                    actions: [
                        {text: myLabel.delete_selected, url: "index.php?controller=pjAdminClients&action=pjActionDeleteBulk", render: true, confirmation: myLabel.delete_confirmation}
                    ],
                    gotoPage: true,
                    paginate: true,
                    total: true,
                    rowCount: true
                },
                saveUrl: "index.php?controller=pjAdminClients&action=pjActionSaveJson&id={:id}",
                select: {
                    field: "id",
                    name: "record[]"
                }
            };

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

        $(document).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=pjAdminClients&action=pjActionGetJson", "email", "ASC", content.page, content.rowCount);
            return false;
        });
    });
})(jQuery_1_8_2);