var jQuery_1_8_2 = $.noConflict();
(function ($, undefined) {
	$(function () {
		var datagrid = ($.fn.datagrid !== undefined);
		
		if ($("#grid").length > 0 && datagrid) {
			var job_id = '';
			
			if(pjGrid.job_id !== undefined)
			{
				job_id = '&job_id='+pjGrid.job_id;
			}
			function formatDefault (str, obj) {
				if (obj.role_id == 3) {
					return '<a href="#" class="pj-status-icon pj-status-' + (str == 'F' ? '0' : '1') + '" style="cursor: ' +  (str == 'F' ? 'pointer' : 'default') + '"></a>';
				} else {
					return '<a href="#" class="pj-status-icon pj-status-1" style="cursor: default"></a>';
				}
			}
			function showJob(str, obj)
			{
				if(str == '0')
				{
					return 0;
				}else{
					return '<a href="index.php?controller=pjAdminJobs&action=pjActionUpdate&id='+obj.job_id+'">'+str+'</a>';
				}
			}
			function onBeforeShowView(obj)
			{
				return true;
			}
			if(pjGrid.isEmployer == 'false')
			{
				var $grid = $("#grid").datagrid({
					buttons: [{type: "view", url: "index.php?controller=pjAdminApplications&action=pjActionView&id={:id}", title: 'View', beforeShow: onBeforeShowView},
					          {type: "delete", url: "index.php?controller=pjAdminApplications&action=pjActionDeleteApplication&id={:id}", title: 'Delete'}
					          ],
					columns: [{text: myLabel.appliedjob, type: "text", sortable: true, editable: false, width: 90, renderer: showJob},
					          {text: myLabel.name, type: "text", sortable: true, editable: false, width: 120},
					          {text: myLabel.email, type: "text", sortable: true, editable: false, width: 180},
					          {text: myLabel.created, type: "date", sortable: true, editable: false, width: 80, renderer: $.datagrid._formatDate, dateFormat: pjGrid.jsDateFormat},
					          {text: myLabel.status, type: "select", sortable: true, editable: true, options: [
			                                                                                             {label: myLabel.new, value: "N"},     
					                                                                                     {label: myLabel.approved, value: "T"}, 
					                                                                                     {label: myLabel.rejected, value: "F"}
					                                                                                     ], applyClass: "pj-status"}],
					dataUrl: "index.php?controller=pjAdminApplications&action=pjActionGetApplication" + pjGrid.queryString,
					dataType: "json",
					fields: ['job_refid', 'name', 'email', 'created', 'status'],
					paginator: {
						actions: [
						   {text: myLabel.delete_selected, url: "index.php?controller=pjAdminApplications&action=pjActionDeleteApplicationBulk", render: true, confirmation: myLabel.delete_confirmation},
						   {text: myLabel.revert_status, url: "index.php?controller=pjAdminApplications&action=pjActionStatusApplication", render: true},
						   {text: myLabel.exported, url: "index.php?controller=pjAdminApplications&action=pjActionExportApplication", ajax: false}
						],
						gotoPage: true,
						paginate: true,
						total: true,
						rowCount: true
					},
					saveUrl: "index.php?controller=pjAdminApplications&action=pjActionSaveApplication&id={:id}",
					select: {
						field: "id",
						name: "record[]"
					}
				});
			}else{
				var $grid = $("#grid").datagrid({
					buttons: [{type: "view", url: "index.php?controller=pjAdminApplications&action=pjActionView&id={:id}", title: 'View', beforeShow: onBeforeShowView},
					          {type: "delete", url: "index.php?controller=pjAdminApplications&action=pjActionDeleteApplication&id={:id}", title: 'Delete'}
					          ],
					columns: [{text: myLabel.appliedjob, type: "text", sortable: true, editable: false, width: 90, renderer: showJob},
					          {text: myLabel.name, type: "text", sortable: true, editable: false, width: 120},
					          {text: myLabel.email, type: "text", sortable: true, editable: false, width: 180},
					          {text: myLabel.created, type: "date", sortable: true, editable: false, width: 80, renderer: $.datagrid._formatDate, dateFormat: pjGrid.jsDateFormat},
					          {text: myLabel.status, type: "select", sortable: true, editable: false, options: [
					                                                                                     {label: myLabel.new, value: "N"},     
					                                                                                     {label: myLabel.approved, value: "T"}, 
					                                                                                     {label: myLabel.rejected, value: "F"}
					                                                                                     ], applyClass: "pj-status"}],
					dataUrl: "index.php?controller=pjAdminApplications&action=pjActionGetApplication" + job_id,
					dataType: "json",
					fields: ['job_refid', 'name', 'email', 'created', 'status'],
					paginator: {
						actions: [
						   {text: myLabel.delete_selected, url: "index.php?controller=pjAdminApplications&action=pjActionDeleteApplicationBulk", render: true, confirmation: myLabel.delete_confirmation},
						   {text: myLabel.exported, url: "index.php?controller=pjAdminApplications&action=pjActionExportApplication", ajax: false}
						],
						gotoPage: true,
						paginate: true,
						total: true,
						rowCount: true
					},
					saveUrl: "index.php?controller=pjAdminApplications&action=pjActionSaveApplication&id={:id}",
					select: {
						field: "id",
						name: "record[]"
					}
				});
			}
		}
		
		$(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=pjAdminApplications&action=pjActionGetApplication" + job_id, "fname", "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=pjAdminApplications&action=pjActionGetApplication"  + job_id, "fname", "ASC", content.page, content.rowCount);
			return false;
		}).on("click", ".pj-status-1", function (e) {
			if (e && e.preventDefault) {
				e.preventDefault();
			}
			return false;
		}).on("click", ".pj-status-0", function (e) {
			if (e && e.preventDefault) {
				e.preventDefault();
			}
			$.post("index.php?controller=pjAdminApplications&action=pjActionSetActive", {
				id: $(this).closest("tr").data("object")['id']
			}).done(function (data) {
				$grid.datagrid("load", "index.php?controller=pjAdminApplications&action=pjActionGetApplication"  + job_id);
			});
			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=pjAdminApplications&action=pjActionGetApplication"  + job_id, "id", "ASC", content.page, content.rowCount);
			return false;
		});
	});
})(jQuery_1_8_2);