;(function($){
var rp_ge = null;
$.jgrid.extend({
	editGridRow1 : function(rowid, p){
		p = $.extend({
			top : 0,
			left: 0,
			width:  'auto',
			height: 'auto',
			dataheight: 'auto',
			modal: false,
			drag: true,
			resize: true,
			url: null,
			mtype : "POST",
			clearAfterAdd :true,
			closeAfterEdit : false,
			reloadAfterSubmit : true,
			onInitializeForm: null,
			beforeInitData: null,
			beforeShowForm: null,
			afterShowForm: null,
			beforeSubmit: null,
			afterSubmit: null,
			onclickSubmit: null,
			afterComplete: null,
			onclickPgButtons : null,
			afterclickPgButtons: null,
			editData : {},
			jqModal : true,
			closeOnEscape : false,
			addedrow : "first",
			topinfo : '',
			bottominfo: '',
			saveicon : [],
			closeicon : [],
			savekey: [false,13],
			navkeys: [false,38,40],
			checkOnSubmit : false,
			checkOnUpdate : false,
			_savedData : {},
			processing : false,
			onClose : null,
			ajaxEditOptions : {},
			serializeEditData : null,
			viewPagerButtons : true
		}, $.jgrid.edit, p || {});
		rp_ge = p;
		return this.each(function(){
			var $t = this;
			if (!$t.grid || !rowid) { return; }
			var gID = $t.p.id,
			frmgr = "FrmGrid_"+gID,frmtb = "TblGrid_"+gID,
			IDs = {themodal:'editmod'+gID,modalhead:'edithd'+gID,modalcontent:'editcnt'+gID};
//			onBeforeShow = $.isFunction(rp_ge.beforeShowForm) ? rp_ge.beforeShowForm : false,
//			onAfterShow = $.isFunction(rp_ge.afterShowForm) ? rp_ge.afterShowForm : false,
//			onBeforeInit = $.isFunction(rp_ge.beforeInitData) ? rp_ge.beforeInitData : false,
//			onInitializeForm = $.isFunction(rp_ge.onInitializeForm) ? rp_ge.onInitializeForm : false,
//			copydata = null,
//			maxCols = 1, maxRows=0,	gurl, postdata, ret, extpost, newData, diff;
			if (rowid=="new") {
				rowid = "_empty";
				p.caption=p.addCaption;
			} else {
				p.caption=p.editCaption;
			}
			var closeovrl = true;
//			if(p.checkOnUpdate && p.jqModal && !p.modal) {
//				closeovrl = false;
//			}
			//如果已经存在编辑窗口
            if ( $("#"+IDs.themodal).html() != null ) {
                //修改iframe的链接地址
                $("#iframe_"+gID).attr("src",p.url+rowid)
                $("#iframe_"+gID).attr("keyid",rowid)
                if(rowid=="_empty" || !rp_ge.viewPagerButtons) {
					$("#pData, #nData","#"+frmtb+"_2").hide();
				} else { 
					$("#pData, #nData","#"+frmtb+"_2").show();
				}
				viewModal("#"+IDs.themodal,{gbox:"#gbox_"+gID,jqm:p.jqModal, jqM: false, closeoverlay: closeovrl, modal:p.modal});
			} else {
			//不存在编辑窗口，是第一次编辑记录
			    var rtlb = $t.p.direction == "rtl" ? true :false,
				bp = rtlb ? "nData" : "pData",
				bn = rtlb ? "pData" : "nData",
//    			    valref = createData(rowid,$t,tbl,maxCols),
			    // buttons at footer
				bP = "<a href='javascript:void(0)' id='"+bp+"' class='fm-button ui-state-default ui-corner-left'><span class='ui-icon ui-icon-triangle-1-w'></span></div>",
				bN = "<a href='javascript:void(0)' id='"+bn+"' class='fm-button ui-state-default ui-corner-right'><span class='ui-icon ui-icon-triangle-1-e'></span></div>",
			    bS  ="<a href='javascript:void(0)' id='sData' class='fm-button ui-state-default ui-corner-all'>"+p.bSubmit+"</a>",
			    bC  ="<a href='javascript:void(0)' id='cData' class='fm-button ui-state-default ui-corner-all'>"+p.bCancel+"</a>";
			    var bt = "<table border='0' class='EditTable' id='"+frmtb+"_2'><tbody><tr id='Act_Buttons'><td class='navButton ui-widget-content'>"+(rtlb ? bN+bP : bP+bN)+"</td><td class='EditButton ui-widget-content'>"+bS+bC+"</td></tr>";
			    bt += "<tr style='display:none' class='binfo'><td class='bottominfo' colspan='2'>"+rp_ge.bottominfo+"</td></tr>";
			    bt += "</tbody></table>";
			    p.gbox = "#gbox_"+gID;
			    var tbl = $("<iframe src=\""+p.url+(rowid=="_empty" ? "" : rowid)+"\" marginwidth=\"0\" id=\"iframe_"+ gID + "\" keyid=\"" + rowid + "\" marginheight=\"0\" framespacing=\"0\" frameborder=\"0\" hspace=\"0\" vspace=\"0\" scrolling=\"no\" />").css({ width: $(window).width()-8, height: $(window).height()-80});
			    var tms = $("<div></div>").append(tbl).append(bt);
			    createModal1(IDs,tms,p,"#gview_"+$t.p.id,$("#gbox_"+$t.p.id)[0]);
			    if(rowid=="_empty" || !rp_ge.viewPagerButtons) { $("#pData,#nData","#"+frmtb+"_2").hide(); } else { $("#pData,#nData","#"+frmtb+"_2").show(); }
			    viewModal("#"+IDs.themodal,{gbox:"#gbox_"+gID,jqm:p.jqModal, modal:p.modal});
			    $("#sData", "#"+frmtb+"_2").click(function(e){
			        $('#btnSave',$('#iframe_'+gID)[0].contentWindow.document).click();
                    if(rp_ge.reloadAfterSubmit) {setTimeout(function(){$($t).trigger("reloadGrid");},1000);}
				    if(rp_ge.closeAfterEdit) {
					    hideModal("#"+IDs.themodal,{gb:"#gbox_"+gID,jqm:p.jqModal,onClose: rp_ge.onClose});
				    }
				    return false;
			    });
			    $("#cData", "#"+frmtb+"_2").click(function(e){
				    hideModal("#"+IDs.themodal,{gb:"#gbox_"+gID,jqm:p.jqModal,onClose: rp_ge.onClose});
				    return false;
			    });
				$("#nData", "#"+frmtb+"_2").click(function(e){
					var npos = getCurrPos();
					npos[0] = parseInt(npos[0],10);
					if(npos[0] != -1 && npos[1][npos[0]+1]) {
						$("#iframe_"+gID).attr("src",p.url+npos[1][npos[0]+1]);
						$("#iframe_"+gID).attr("keyid",npos[1][npos[0]+1]);						
						$($t).jqGrid("setSelection",npos[1][npos[0]+1]);
//						updateNav(npos[0]+1,npos[1].length-1);
					}
					return false;
				});
				$("#pData", "#"+frmtb+"_2").click(function(e){
					var ppos = getCurrPos();
					if(ppos[0] != -1 && ppos[1][ppos[0]-1]) {
						$("#iframe_"+gID).attr("src",p.url+ppos[1][ppos[0]-1]);
						$($t).jqGrid("setSelection",ppos[1][ppos[0]-1]);
						$("#iframe_"+gID).attr("keyid",ppos[1][ppos[0]-1]);	
//						updateNav(ppos[0]-1,ppos[1].length-1);
					}
					return false;
				});
		    }//end if
//		    var posInit =getCurrPos();
//			updateNav(posInit[0],posInit[1].length-1);
//			function updateNav(cr,totr,rid){
//				if (cr===0) { $("#pData","#"+frmtb+"_2").addClass('ui-state-disabled'); } else { $("#pData","#"+frmtb+"_2").removeClass('ui-state-disabled'); }
//				if (cr==totr) { $("#nData","#"+frmtb+"_2").addClass('ui-state-disabled'); } else { $("#nData","#"+frmtb+"_2").removeClass('ui-state-disabled'); }
//			}
		    function getCurrPos() {
				var rowsInGrid = $($t).jqGrid("getDataIDs"),
                selrow = $("#iframe_"+gID).attr("keyid");
				pos = $.inArray(selrow,rowsInGrid);
				return [pos,rowsInGrid];
			}
		});
	}
});
})(jQuery);
