AdminJobsGridJsParams = "width=800,height=600,scrollbars=yes";
AdminJobsGridInterval = null;
AdminJobsGridLastPoll = 0;

function AdminJobsGridModify( uid ) {
	window.open( 'edit.php?mode=modify&uid='+uid , '_blank' , AdminJobsGridJsParams );
}

function AdminJobsGridAction( n , action , params ) {
	if ($("#"+FNTHSuperGrid_ToolbarId( n , action )).hasClass( "dsb" )) return;
	switch (action) {
	case "new":
		window.open( 'edit.php?mode=new' , '_blank' , AdminJobsGridJsParams );break;
	case "modify":
		AdminJobsGridModify( $("#"+n).data( "selecteds" )[0] , AdminJobsGridJsParams );
		break;
	case "delete":
		if (!window.confirm( aJSLangDefs.jobspage_confirm_Delete_jobs )) return;
		BGAction( n , action );
		break;
	case "filter":
		$("#frm_AdminJobsGrid").append( "<input type=\"hidden\" name=\"task\" value=\"filter\" />" );
		document.frm_AdminJobsGrid.submit();
		break;
	case "all_admin_ack":
		if (!window.confirm( aJSLangDefs.jobspage_confirm_Ack_jobs )) return;
		BGAction( n , action );
		break;
	case "admin_ack":
		if (!window.confirm( aJSLangDefs.jobspage_confirm_Ack_job )) return;
		BGAction( n , action , params );
		break;
	case "all_send":
		if (!window.confirm( aJSLangDefs.jobspage_confirm_Send_jobs )) return;
		BGAction( n , action );
		break;
	case "send":
		if (!window.confirm( aJSLangDefs.jobspage_confirm_Send_job )) return;
		BGAction( n , action , params );
		break;
	case "all_admin_resend":
		if (!window.confirm( aJSLangDefs.jobspage_confirm_Resend_jobs )) return;
		BGAction( n , action , params );
		break;

	}
}

function AdminJobsGridSelectionChanged( tblid , chk , id ) {
	BGSelectionChanged( tblid , chk , id );
	BGSelectionChangedDefaultAMD( tblid , chk, id );
	var t = $("#"+tblid );
	var a = t.data( "selecteds" );
	if (a.length>0) {
		$("#"+tblid+"_tbrbutton_all_admin_resend").removeClass( "dsb" );
		$("#"+tblid+"_tbrbutton_all_admin_ack").removeClass( "dsb" );
		$("#"+tblid+"_tbrbutton_all_send").removeClass( "dsb" );
	} else {
		$("#"+tblid+"_tbrbutton_all_admin_resend").addClass( "dsb" );
		$("#"+tblid+"_tbrbutton_all_admin_ack").addClass( "dsb" );
		$("#"+tblid+"_tbrbutton_all_send").addClass( "dsb" );
	}
}

function AdminJobsGridRefreshRowsCallback( datas ) {
	var i,tdid;

	for (i in datas.ids) {
		var job_id = datas.ids[i];
		var item = datas.datas[job_id];

		var prfx = "#"+datas.id + "_row"+job_id;

		var row = $(prfx);

		var ojob_title = $(prfx+"_job_title");
		var oposter_acknowledged = $(prfx+"_poster_acknowledged");
		var oadmin_acknowledged = $(prfx+"_admin_acknowledged");
		var ois_sent = $(prfx+"_is_sent");
		var oadmin = $(prfx+"_admin");

		// job title
		if (ojob_title.html()!=item.job_title) {
			eval( item.job_title_pre_js );
			ojob_title.html( item.job_title );
			eval( item.job_title_post_js );
		}

		// poster_acknowledged
		if (oposter_acknowledged.html()!=item.poster_acknowledged) {
			oposter_acknowledged.html( item.poster_acknowledged );
		}
		
		// admin_acknowledged
		if (oadmin_acknowledged.html()!=item.admin_acknowledged) {
			oadmin_acknowledged.html( item.admin_acknowledged );
		}

		// is sent
		if (ois_sent.html()!=item.is_sent) {
			eval( item.is_sent_pre_js );
			ois_sent.html( item.is_sent );
			eval( item.is_sent_post_js );
		}

		// admin
		if (oadmin.html()!=item.admin) {
			eval( item.admin_pre_js );
			oadmin.html( item.admin );
			eval( item.admin_post_js );
		}

		if (row.data("tech_mtstamp")==item.tech_mtstamp) continue; // egyaltalan nem valtozott
		row.data( "tech_mtstamp" , item.tech_mtstamp );	// dirty eltarolva
	}
}

function AdminJobsGridRefreshRows( tblid , ids ) {
	$.ajax(
		{
			type: "POST",
			cache: false,
			data: (
				{
					inajax : 1,
					ajaxevent : "refresh_grid_rows",
					id : tblid,
					uids : ids,
					last_poll : AdminJobsGridLastPoll
				}
			),
			success: function(ret) {
				var d = eval( ret );

				AdminJobsGridRefreshRowsCallback( d );
				if (typeof d.next_poll == "undefined" ) return;
				AdminJobsGridLastPoll = d.next_poll;

				AdminJobsGridInterval = setTimeout( "AdminJobsGridTimer('"+tblid+"');" , AdminJobsGridLastPoll );
			}
		}
	);
}

function AdminJobsGridTimer( tblid ) {
	var o = $("#"+tblid );
	var datas = o.data( "griddata" );
	AdminJobsGridRefreshRows( tblid , datas.ids );
}

function InitAdminJobsGrid( tblid , datas ) {
	var o = $("#"+tblid );

	// set tech timestamps
	var i;
	for (i in datas.ids) {
		var rid = datas.ids[i];
		$("#"+tblid+"_row"+rid).data( "tech_mtstamp" , datas.mtstamps[rid] );
	}
	o.data( "griddata" , datas );
	setTimeout( "AdminJobsGridTimer('"+tblid+"');" , 1000 );
}
