function enableTooltips(table) {
	$('.navtable .ui-pg-button').tooltip({
		container : 'body'
	});
	$(table).find('.ui-pg-div').tooltip({
		container : 'body'
	});
}

function initLawALGrid(grid_selector, pager_selector, url, width){
	var grid = jQuery(grid_selector).jqGrid({
		subGrid : false,
		url : url,
		datatype : "json",
		height : '100%',
		width : width,
		colNames : ['订单号', '订单完成时间','律师姓名','身份证号码','电话号码','操作类型','业务类型','含税金额','个税','合计税费','不含税金额','剩余积分','余额','备注'],
		colModel : [{ name : 'orderId',
			index : 'orderId',
			width : 120,
			editable : false
		},{	name : 'createDate',
			index : 'createDate',
			width : 120,
			search : false,	
			editable : false,
			readonly : true
		},{	name : 'real_name',
			index : 'real_name',
			width : 100,
			search : false,	
			editable : false,
		},{	name : 'idCard',
			index : 'idCard',
			width : 120,
			editable : false
		},{	name : 'phone',
			index : 'phone',
			width : 120,
			editable : false
		},{
			name : 'typeO',
			index : 'typeO',
			width : 120,
				sortable : false,
			editable : false
		},{
			name : 'lowType',
			index : 'lowType',
			width : 120,
			editable : false
		},{
			name : 'amount',
			index : 'amount',
			width : 120,
			editable : true
		}, {
			name : 'gs',
			index : 'gs',
			width : 120,
				sortable : false,
			editable : false
		}, {
			name : 'taxhj',
			index : 'taxhj',
			width : 120,
			sortable : false,
			editable : false
		}, {
			name : 'income',
			index : 'income',
			width : 120,
			sortable : false,
			editable : false
		}, {
			name : 'points',
			index : 'points',
			width : 120,
			sortable : false,
			editable : false
		}, {
			name : 'money',
			index : 'money',
			width : 120,
			sortable : false,
			editable : false
		},  {
			name : 'remark',
			index : 'remark',
			width : 100,
				sortable : false,
			editable : false
		}],
		//scroll : 1, // set the scroll property to 1 to enable paging with scrollbar - virtual loading of records
		sortname : "",
		sortorder : "",
		viewrecords : true,
		rowNum : 100,
		rowList : [ 100, 500, 1000 ],
		pager : pager_selector,
		altRows : true,
		loadComplete : function() {
			var table = this;
			setTimeout(function(){
				styleCheckbox(table);
				updateActionIcons(table);
				updatePagerIcons(table);
				enableTooltips(table);
			}, 0);
		}
		//caption : "用户管理列表",
		//autowidth : true,
		/**
		grouping : true, 
		groupingView : { 
			 groupField : ['name'],
			 groupDataSorted : true,
			 plusicon : 'fa fa-chevron-down bigger-110',
			 minusicon : 'fa fa-chevron-up bigger-110'
		},
		*/
	});
	return grid;
}

function initNavButtons(grid_selector, pager_selector, contextPath){
	// navButtons
	jQuery(grid_selector).jqGrid('navGrid', pager_selector,  { // navbar options
		edit : false,
		editicon : 'ace-icon fa fa-pencil blue',
		add : false,
		addicon : 'ace-icon fa fa-plus-circle purple',
		del : false,
		delicon : 'ace-icon fa fa-trash-o red',
		search : false,
		searchicon : 'ace-icon fa fa-search orange',
		refresh : true,
		refreshicon : 'ace-icon fa fa-refresh blue',
		view : false,
		viewicon : 'ace-icon fa fa-search-plus grey'
	}, {
		// edit record form
		// closeAfterEdit: true,
		// width: 700,
		recreateForm : true,
		beforeShowForm : function(e) {
			var form = $(e[0]);
			form.closest('.ui-jqdialog').find('.ui-jqdialog-titlebar').wrapInner('<div class="widget-header" />')
			style_edit_form(form);
		},
		errorTextFormat: function (response) {
			var result = eval('('+response.responseText+')');
		    return result.message;
		}
	});
	
	jQuery(grid_selector).jqGrid('navButtonAdd', pager_selector,{
	   caption : "",
       title : "导出Excel",
       buttonicon : "ace-icon fa fa-file-excel-o green", 
       onClickButton : function () { 
    	   var keys = [], ii = 0, rows = "";
    	   var ids = $(grid_selector).getDataIDs(); // Get All IDs
    	   var row = $(grid_selector).getRowData(ids[0]); // Get First row to get the labels
    	   var label = $(grid_selector).jqGrid('getGridParam','colNames');
    	   for(var k in label){
    	   	   rows = rows + label[k] + "\t"; // output each Column as tab delimited
    	   }
		       for (var k in row) {
    	   	   keys[ii++] = k; // capture col names
    	   }
    	   rows = rows + "\n"; // Output header with end of line
    	   var tIds = {"orderId":true,"createDate":true,"real_name":false,"idCard":true,"phone":true,"typeO":false,"lowType":false,
			           "amount":false,"tax":false,"income":false,"points":false,"money":false,"taxhj":false,"gs":false,"remark":false};
    	   for (var i = 0; i < ids.length; i++) {
    	   	   row = $(grid_selector).getRowData(ids[i]); // get each row
    	   	   for (var j = 0; j < keys.length; j++){
    	   		   if(tIds[keys[j]]){
    	   		   		rows = rows + '="' + row[keys[j]] + '"\t'; // output each Row as tab delimited
    	   		   }else{
    	   				rows = rows + row[keys[j]] + '\t'; // output each Row as tab delimited   
    	   		   } 
    	   	   }
    	   	   rows = rows + "\n"; // output each row with end of line
    	   }
    	   rows = rows + "\n"; // end of line at the end
    	   var form = "<form name='csvexportform' action='" + contextPath + "/fs/report/getExcel?fileName=lawyerAccountLow' method='post'>";
    	   form = form + "<input type='hidden' name='csvBuffer' value='" + encodeURIComponent(rows) + "'>";
    	   form = form + "</form><script>document.csvexportform.submit();</sc" + "ript>";
    	   OpenWindow = window.open('', '');
    	   OpenWindow.document.write(form);
    	   OpenWindow.document.close();
       } 
	});
}

// it causes some flicker when reloading or navigating grid
// it may be possible to have some custom formatter to do this as the grid is being created to prevent this
// or go back to default browser checkbox styles for the grid
function styleCheckbox(table) {
	/**
	 * $(table).find('input:checkbox').addClass('ace') .wrap('<label />') .after('<span class="lbl align-top" />') $('.ui-jqgrid-labels th[id*="_cb"]:first-child')
	 * .find('input.cbox[type=checkbox]').addClass('ace') .wrap('<label />').after('<span class="lbl align-top" />');
	 */
}

// unlike navButtons icons, action icons in rows seem to be hard-coded
// you can change them like this in here if you want
function updateActionIcons(table) {
	/**
	 * var replacement = { 'ui-ace-icon fa fa-pencil' : 'ace-icon fa fa-pencil blue', 'ui-ace-icon fa fa-trash-o' : 'ace-icon fa fa-trash-o red', 'ui-icon-disk' : 'ace-icon fa fa-check green', 'ui-icon-cancel' :
	 * 'ace-icon fa fa-times red' }; $(table).find('.ui-pg-div span.ui-icon').each(function(){ var icon = $(this); var $class = $.trim(icon.attr('class').replace('ui-icon', '')); if($class in replacement)
	 * icon.attr('class', 'ui-icon '+replacement[$class]); })
	 */
}

// replace icons with FontAwesome icons like above
function updatePagerIcons(table) {
	var replacement = {
		'ui-icon-seek-first' : 'ace-icon fa fa-angle-double-left bigger-140',
		'ui-icon-seek-prev' : 'ace-icon fa fa-angle-left bigger-140',
		'ui-icon-seek-next' : 'ace-icon fa fa-angle-right bigger-140',
		'ui-icon-seek-end' : 'ace-icon fa fa-angle-double-right bigger-140'
	};
	$('.ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon').each(function() {
		var icon = $(this);
		var $class = $.trim(icon.attr('class').replace('ui-icon', ''));

		if ($class in replacement)
			icon.attr('class', 'ui-icon ' + replacement[$class]);
	})
}