
/* ********************************************************************************************************** */
/* ********************************************************************************************************** */

function joVMMDataResultClass (id, gui_parent_node_name)
{
	this.id									= id;
	this.gui_type						= "tasks";
	this.gui_title_node				= null;
	this.data								= null;	

	this.data_collection_name	= "";		// ???
	this.data_name						= "";		// ???
	this.data_id							= "";

	this.data_ok							= false;
	
	this.data_entity_id				= "";
	this.data_entity					= null;
	this.prev_data_entity_id		= "";

	this.gui_list_node				= null;
	this.form_node						= null;
	
	this.gui_parent_node_name		= gui_parent_node_name;
	
	this.gui_parent_node			= joFIND (gui_parent_node_name);
	this.gui_container_node		= joFIND (gui_parent_node_name);
	
	this.gui_list_container_node	= null;
	this.detail_node					= null;
	this.detail_info_node			= null;
	
	this.result_node					= null;
	this.result_title_node			= null;
	this.result_info_node			= null;
	
	this.ResetData						= _joVMMDataResult_ResetData;
	this.InitGui							= _joVMMDataResult_InitGui;
	this.ShowData						= _joVMMDataResult_ShowData;
	this.ShowDataEntity				= _joVMMDataResult_ShowDataEntity;
	this.SetActiveEntity			= _joVMMDataResult_SetActiveEntity;
	this.ShowJobDetails				= _joVMMDataResult_ShowJobDetails;

	this.ShowJobTimeStamps			= _joVMMDataResult_ShowJobTimeStamps;
	this.ShowJobResults				= _joVMMDataResult_ShowJobResults;

	this.AddButton						= _joVMMDataResult_AddButton;
	this.ActivateButtons			= _joVMMDataResult_ActivateButtons;
	this.SetButtonState				= _joVMMDataResult_SetButtonState;	

	this.SetAutoFrefresh			= _joVMMDataResult_SetAutoFrefresh;
	this.autorefresher_cycle		= 0;
	this.autorefresher				= null;
	this.start_timestamp			= "";
	
	this.StopOrDeleteJob			=_joVMMDataResult_StopOrDeleteJob;
	this.OnStopOrDeleteJob			=_joVMMDataResult_OnStopOrDeleteJob;
	

	// events
	this.Action							= _joVMMDataResult_Action;
	this.OnEvent		 				= _joVMMDataResult_OnEvent;
	this.OnUpdateFormStatus		= _joVMMDataResult_OnUpdateFormStatus;

	// do now
	this.InitGui();
}

/* ********************************************************************************************************** */

function _joVMMDataResult_InitGui()
{
	var list_node, title_node, footer_node, div_node, table_node, tr_node, td_node, select_node, option_node;
	
	this.form_node 													= joAddHTMLChildNode (this.gui_parent_node, "FORM");
		joDOM.SetAttribute 											(this.form_node, "vmm_gui_name", this.id);
		joAddEvent 														(this.form_node, "submit", function (event) { void(0); });
			
		table_node 														= joAddTableChildNode (this.form_node, "results_main");

			// job list
			tr_node														= joAddHTMLChildNode (table_node, "TR");
				td_node													= joAddHTMLChildNode (tr_node, "TD", "jobs");
					joDOM.SetAttribute 								(td_node, "colSpan", 2);
					list_node											= joAddHTMLChildNode (td_node, "DIV", "vmm_jobs");
						title_node										= joAddHTMLChildNode (list_node, "DIV", "title");
							joVMMApp.AddTitle 						(title_node, "jobs_list");
						
						this.gui_list_container_node				= joAddHTMLChildNode (list_node, "DIV", "content", "job_list_container");
						footer_node										= joAddHTMLChildNode (list_node, "DIV", "footer");
						
							this.AddButton 							(footer_node, "stop");
							this.AddButton 							(footer_node, "delete");
							this.AddButton 							(footer_node, "refresh");

							joAddTextChildNode 						(footer_node, "   " + joGUI.GetLocaleText ("auto_refresh"));
							select_node									= joAddHTMLChildNode (footer_node, "SELECT");
								joAddSelectOptionNode 				(select_node, "0", joGUI.GetLocaleText ("deactivated"));
								joAddSelectOptionNode 				(select_node, "1", "1 " + joGUI.GetLocaleText ("min"));
								joAddSelectOptionNode 				(select_node, "5", "5 " + joGUI.GetLocaleText ("min"));
								joAddSelectOptionNode 				(select_node, "20", "20 " + joGUI.GetLocaleText ("min"));
								joAddSelectOptionNode 				(select_node, "60", "1 " + joGUI.GetLocaleText ("hour"));
								select_node.onchange				= function() { joVMMApp.GetGuiObject ("vmm_calc_results").SetAutoFrefresh (this.value); return false; };
						

			// job details
			tr_node														= joAddHTMLChildNode (table_node, "TR");
				this.detail_node										= joAddHTMLChildNode (tr_node, "TD", "jobs");
					this.detail_node.style.visibility			= "hidden";
					list_node											= joAddHTMLChildNode (this.detail_node, "DIV", "vmm_jobs");
						title_node										= joAddHTMLChildNode (list_node, "DIV", "title");
							joVMMApp.AddTitle 						(title_node, "job_details");
							
						this.detail_info_node						= joAddHTMLChildNode (list_node, "DIV", "detail_info", "job_detail_info");
							this.detail_info_node.innerHTML		= "";

				this.result_node										= joAddHTMLChildNode (tr_node, "TD", "jobs");
					this.result_node.style.visibility			= "hidden";
					list_node											= joAddHTMLChildNode (this.result_node, "DIV", "vmm_jobs");
						title_node										= joAddHTMLChildNode (list_node, "DIV", "title");
							this.result_title_node					= joVMMApp.AddTitle (title_node, "job_result");

						this.result_info_node						= joAddHTMLChildNode (list_node, "DIV", "detail_info", "job_result_list");
							this.result_info_node.innerHTML		= "";
					
							

	this.ShowData();
}

/* ********************************************************************************************************** */

function _joVMMDataResult_ResetData()
{
	this.data_entity_id 	= "";
	this.data 					= null;
	this.start_timestamp	= "";
}

/* ********************************************************************************************************** */

function _joVMMDataResult_ShowData()
{
	if (joVMMApp.gui_current_modeller)	
	{
		joDOM.RemoveChildNodes (this.gui_list_container_node);
		this.detail_node.style.visibility	= "hidden";
		this.result_node.style.visibility	= "hidden";

		var table_node, thead_node, tr_node, td_node;
	
		table_node								= joAddHTMLChildNode (this.gui_list_container_node, "TABLE", "jobs", "jobs_list");
			thead_node							= joAddHTMLChildNode (table_node, "THEAD");
				tr_node							= joAddHTMLChildNode (thead_node, "TR");
					td_node						= joAddHTMLChildNode (tr_node, "TD");
						td_node.innerHTML		= joGUI.GetLocaleText ("job_name");
					td_node						= joAddHTMLChildNode (tr_node, "TD");
						td_node.innerHTML		= joGUI.GetLocaleText ("status");
					td_node						= joAddHTMLChildNode (tr_node, "TD");
						td_node.innerHTML		= joGUI.GetLocaleText ("file_date");
	
			this.gui_list_node				= joAddHTMLChildNode (table_node, "TBODY", "scrollingContent");
	
		joVMM.LoadFile ("job_info.txt", ShowJobInfos);	
	}
}

/* ********************************************************************************************************** */

function ShowJobInfos (jobinfos)
{
	if (jobinfos != "")
	{
		var VMMDataResult = joVMMApp.GetGuiObject ("vmm_calc_results");
		VMMDataResult.ResetData();

		var jobs = jobinfos.split ("\n");
		for (var i = 0; i < jobs.length; i++)
		{
			if (jobs[i].replace (/\s*/g, "") != "")
			{
				VMMDataResult.ShowDataEntity (jobs[i]);
			}
		}
	
		if (VMMDataResult.prev_data_entity_id != "")
		{
			VMMDataResult.data_entity_id = VMMDataResult.prev_data_entity_id;
			var active_job = joFIND (VMMDataResult.data_entity_id);
			if (active_job) VMMDataResult.SetActiveEntity (active_job);
		}

		initTableWidget ('jobs_list', GetGUINodeSize ('job_list', 'width'), GetGUINodeSize ('job_list', 'height'), Array('S','S','S'));
		
		VMMDataResult.ActivateButtons();
		
		// ??? this code was moved from last position in ShowData()... maybe buggy here
		if (this.autorefresher_cycle)
		{
			this.autorefresher = window.setInterval ("joVMMApp.GetGuiObject ('vmm_calc_results').ShowData()", this.autorefresher_cycle * DURATION_MINUTE);
		}
		
	}
	VMMDataResult.SetButtonState ('button_refresh', true);
}

/* ********************************************************************************************************** */

function _joVMMDataResult_ShowDataEntity (element)
{
	var element_info = element.split ("\t");

	var tr_node, td_node;
	
	tr_node							= joAddHTMLChildNode (this.gui_list_node, "TR", "", element_info[0]);
		joDOM.SetAttribute 		(tr_node, "vmm_data_obj", this.id);
		joDOM.SetAttribute 		(tr_node, "vmm_status", element_info[1]);
		tr_node.onclick 			= function() { joVMMApp.GetGuiObject (this.getAttribute ("vmm_data_obj")).SetActiveEntity (this); return false; };
		
	
		td_node						= joAddHTMLChildNode (tr_node, "TD");
			td_node.innerHTML		= element_info[0];
		td_node						= joAddHTMLChildNode (tr_node, "TD");
			td_node.innerHTML		= joGUI.GetLocaleText (element_info[1]);
		td_node						= joAddHTMLChildNode (tr_node, "TD");
			td_node.innerHTML		= element_info[2];
}

/* ********************************************************************************************************** */

function _joVMMDataResult_SetActiveEntity (gui_node)
{

	if (this.data_entity_id != "")
	{
		var old_gui_node = joFIND (this.data_entity_id);
		if (old_gui_node)
			old_gui_node.style.backgroundColor = "";
	}
	
	gui_node.style.backgroundColor 	= "#F5F2E1";
	
	this.data_entity						= gui_node;
	this.data_entity_id 				= gui_node.id;
	this.prev_data_entity_id			= gui_node.id;

	// job details
	this.ShowJobDetails (gui_node.id);
		
	this.ActivateButtons();
}

/* ********************************************************************************************************** */

function _joVMMDataResult_OnEvent (event_type, event_data_type)
{
	switch (event_type)
	{
		case JO_VMM_GUI_ON_GET_FOCUS:
		case JO_VMM_GUI_ON_RESIZE:
		{
			if (joVMM.xmlData)
			{
				this.ShowData();
			}
			break;
		}
	}
}

/* ********************************************************************************************************** */

function _joVMMDataResult_AddButton (parent_node, button_type)
{
	button_node						= joAddHTMLChildNode (parent_node, "BUTTON", "joGfxButton");
	button_node.name				= "button_" + button_type;
	button_node.title				= joGUI.GetLocaleText (button_type);
	button_node.onclick			= function() { joVMMApp.GetGuiObject (this.getAttribute ("vmm_data_obj")).Action (this.getAttribute ("name").substr ("button_".length)); return false; };
	joDOM.SetAttribute (button_node, "vmm_data_obj", this.id);
	
	joAddImageChildNode (button_node, 	"images/button_" + button_type + ".gif");
}

/* ********************************************************************************************************** */

function _joVMMDataResult_ActivateButtons()
{
	var active_entity	 = this.data_entity_id == "" ? false : true;
	if (active_entity)
	{
		var status = this.data_entity.getAttribute ("vmm_status");
		switch (status)
		{
			case "finished_ok":
			case "finished_error":
			{
				this.SetButtonState ("button_delete", true);
				this.SetButtonState ("button_stop", false);
				break;
			}
			case "pending":
			case "queued":
			{
				this.SetButtonState ("button_delete", false);
				this.SetButtonState ("button_stop", true);
				break;
			}
		}
	}
	else
	{
		this.SetButtonState ("button_delete", false);
		this.SetButtonState ("button_stop", false);
	}
}

/* ********************************************************************************************************** */

function _joVMMDataResult_SetButtonState (button_name, state)
{
	var button_node = joDOM.GetFormElement (this.form_node, button_name);
	if (button_node)
	{
		button_node.style.cursor = state ? "pointer" : "auto";
		button_node.disabled = state ? "" : "disabled";
		button_node.style.backgroundColor = state ? "" : "#ECE9D8";
		
		var image = joDOM.GetSubNode (button_node, "IMG");
		if (image)
		{
			image.style.visibility = state ? "visible" : "hidden";
		}
	}
}

/* ********************************************************************************************************** */

function _joVMMDataResult_Action (action_type)
{
	switch (action_type)
	{
		case "refresh":
			this.SetButtonState ('button_refresh', false);
			this.ShowData();
			break;
		case "stop":
		case "delete":
			joGUI.Alert (joGUI.GetLocaleText ("confirm_job_" + action_type), joGUI_ALERT_TYPE_NOTE, joGUI_ALERT_BUTTON_OK | joGUI_ALERT_BUTTON_CANCEL, StopOrDeleteJob, action_type);
			break;
		default:
			joVMMApp.GuiObjectAction (action_type, this); 
		
	}

	return false;
}

/* ********************************************************************************************************** */

function StopOrDeleteJob (result, action_type)
{
	if (result == joGUI_ALERT_BUTTON_OK)
	{
		joVMMApp.GetGuiObject ('vmm_calc_results').StopOrDeleteJob (action_type);
	}
}

var curr_action_type;

/* ********************************************************************************************************** */

function _joVMMDataResult_StopOrDeleteJob (action_type)
{
	this.prev_data_entity_id = "";
	curr_action_type = action_type;
	
	switch (action_type)
	{
		case "stop":
			var filename = joGUI.GetLocaleText ("file_path") + "/spool/" + joVMM.UserDir + this.data_entity_id + ".kill";
			ajax_upload (joGUI.GetLocaleText('php_path') + '/file_save.php', filename, "johollero! :-)", OnStopOrDeleteJob);
			break;
		case "delete":
			var filename = joGUI.GetLocaleText ("file_path") + "/spool/" + joVMM.UserDir + this.data_entity_id + ".purge";
			ajax_upload (joGUI.GetLocaleText('php_path') + '/file_save.php', filename, "johollero! :-)", OnStopOrDeleteJob);
			break;
	}
}


/* ********************************************************************************************************** */

function _joVMMDataResult_OnUpdateFormStatus (form_node, form_status, field_node) 
{ 
	this.data_ok = form_status;
	this.ActivateButtons();
}

/* ********************************************************************************************************** */

function _joVMMDataResult_ShowJobDetails()
{
	var table_node, tr_node, td_node;
	
	joDOM.RemoveChildNodes (this.detail_info_node);

	this.detail_timestamp_node			= joAddTableChildNode (this.detail_info_node, "job_details", "job_detail_info_table");
		tr_node									= joAddHTMLChildNode (this.detail_timestamp_node, "TR");
			td_node								= joAddHTMLChildNode (tr_node, "TD", "title");
				td_node.innerHTML				= joGUI.GetLocaleText ("job_name");
			td_node								= joAddHTMLChildNode (tr_node, "TD", "data");
				td_node.innerHTML				= this.data_entity_id;
	
	joVMM.LoadFile (this.data_entity_id + ".timestamps.txt", ShowJobDetails);	
	this.detail_node.style.visibility		= "visible";
}

/* ********************************************************************************************************** */

function ShowJobDetails (jobdetails)
{
	if (jobdetails != "")
	{
		var VMMDataResult = joVMMApp.GetGuiObject ("vmm_calc_results");
		VMMDataResult.ShowJobTimeStamps (jobdetails);
	}
}

/* ********************************************************************************************************** */

function ShowJobResults (jobdetails)
{
	if (jobdetails != "")
	{
		var VMMDataResult = joVMMApp.GetGuiObject ("vmm_calc_results");
		VMMDataResult.ShowJobResults (jobdetails);
		joVMMApp.ResizeJobResultNodes();
	}
}

/* ********************************************************************************************************** */

function _joVMMDataResult_ShowJobTimeStamps (jobdetails)
{
	var details = jobdetails.split ("\n");
	var tr_node, td_node;

	this.start_timestamp = "";

	for (var i = 0; i < details.length; i++)
	{
		if (details[i].replace (/\s*/g, "") != "")
		{
			var statusstr = joGUI.GetLocaleText (details[i++].replace (/#*\s*/g, ""));
			if (statusstr == joGUI.GetLocaleText ("pending")) 
			{
				this.start_timestamp = details[i];
			}

			tr_node									= joAddHTMLChildNode (this.detail_timestamp_node, "TR");
				td_node								= joAddHTMLChildNode (tr_node, "TD", "title");
					td_node.innerHTML				= statusstr;


				var datestr 	= details[i].substr (8, 2) + "." +  details[i].substr (5, 2) + "." + details[i].substr (0, 4);
				var timestr		= details[i].substr (10);

				td_node								= joAddHTMLChildNode (tr_node, "TD", "data");
					td_node.innerHTML				= datestr + " " + timestr;
		 }
	}

	if (this.start_timestamp != "")
	{
		joVMM.LoadFile (this.data_entity_id + ".status.txt", ShowJobResults);	
		this.result_node.style.visibility		= "visible";
	}
	else
	{
		this.result_node.style.visibility		= "hidden";
	}
}

/* ********************************************************************************************************** */

function _joVMMDataResult_ShowJobResults (jobresults)
{
	var results = jobresults.split ("\n");
	var element_node, a_node, span_node, progress_node;

	joDOM.RemoveChildNodes (this.result_info_node);

	if (results.length > 1)
	{

		switch (results[0])
		{
			case "pending":
			{
				var progress_str	= results[1] + "%";
				var starttime 		= "";
				
				var start_year 	= this.start_timestamp.substr (0, 4);
				var start_month 	= this.start_timestamp.substr (5, 2) - 1;
				var start_day 		= this.start_timestamp.substr (8, 2);
				var start_hour		= this.start_timestamp.substr (11, 2);
				var start_min		= this.start_timestamp.substr (14, 2);
				var start_sec		= this.start_timestamp.substr (17, 2);
				var start_time		= new Date (start_year, start_month, start_day, start_hour, start_min, start_sec);
				var curr_time		= new Date();
				var time_done		= curr_time - start_time;
				var time_all		= Math.round (time_done / Math.max (parseFloat (results[1]), 0.1) * 100);
				var time_finish	= new Date (time_all + (start_time - 0));
				var time_rest		= Math.round (time_done / Math.max (parseFloat (results[1]), 0.1) * (100 - parseFloat (results[1])));
				
				var mins_all		= Math.round (time_all / 1000 / 60);
				var hours_all		= Math.floor (mins_all / 60);
				mins_all				= mins_all % 60;
				
				var mins_rest		= Math.round (time_rest / 1000 / 60);
				var hours_rest		= Math.floor (mins_rest / 60);
				mins_rest			= mins_rest % 60;

				this.result_info_node.style.backgroundColor 	= "";
				this.result_info_node.style.height					= "";
				this.result_title_node.innerHTML 					= joGUI.GetLocaleText ("progress");

				table_node													= joAddTableChildNode (this.result_info_node, "job_details job_progress");
					tr_node													= joAddHTMLChildNode (table_node, "TR");
						td_node												= joAddHTMLChildNode (tr_node, "TD", "title");
							td_node.innerHTML								= joGUI.GetLocaleText ("progress");
						td_node												= joAddHTMLChildNode (tr_node, "TD", "data");
							td_node.innerHTML								= progress_str;
						td_node												= joAddHTMLChildNode (tr_node, "TD", "data progress_bar");
							progress_node 									= joAddHTMLChildNode (td_node, "DIV", "job_progress_bar");
								progress_node.style.width				= Math.ceil (results[1]) + "%";
								progress_node.title						= progress_str;
							
					tr_node													= joAddHTMLChildNode (table_node, "TR");
						td_node												= joAddHTMLChildNode (tr_node, "TD", "title");
							td_node.innerHTML								= joGUI.GetLocaleText ("estimated_finish");
						td_node												= joAddHTMLChildNode (tr_node, "TD", "data");
							joDOM.SetAttribute 							(td_node, "colSpan", 2);
							td_node.innerHTML								= joTimeStampStr (time_finish);
							
					tr_node													= joAddHTMLChildNode (table_node, "TR");
						td_node												= joAddHTMLChildNode (tr_node, "TD", "title");
							td_node.innerHTML								= joGUI.GetLocaleText ("estimated_duration");
						td_node												= joAddHTMLChildNode (tr_node, "TD", "data");
							joDOM.SetAttribute 							(td_node, "colSpan", 2);
							td_node.innerHTML								= hours_all + " " + joGUI.GetLocaleText ("hour") + " " + mins_all + " " + joGUI.GetLocaleText ("min");
							
					tr_node													= joAddHTMLChildNode (table_node, "TR");
						td_node												= joAddHTMLChildNode (tr_node, "TD", "title");
							td_node.innerHTML								= joGUI.GetLocaleText ("estimated_togo");
						td_node												= joAddHTMLChildNode (tr_node, "TD", "data");
							joDOM.SetAttribute 							(td_node, "colSpan", 2);
							td_node.innerHTML								= hours_rest + " " + joGUI.GetLocaleText ("hour") + " " + mins_rest + " " + joGUI.GetLocaleText ("min");
							
				break;
			}

			case "finished_error":
			case "finished_ok":
			{
				this.result_title_node.innerHTML 					= joGUI.GetLocaleText (results[0] == "finished_ok" ? "results" : "error_message");
				this.result_info_node.style.height					= "120";
				this.result_info_node.style.backgroundColor 	= "#ffffff";
				for (var i = 1; i < results.length; i++)
				{
					if (results[i].replace (/\s*/g, "") != "")
					{
						var resultinfo						= results[i].split ("\t");
						var filename						= resultinfo[0].substr (resultinfo[0].lastIndexOf ("/") + 1);
						var fileext							= filename.substr (filename.lastIndexOf (".") + 1);
						
						element_node 						= joAddHTMLChildNode (this.result_info_node, "DIV", "vmm_list_progress");
							if (results[0] == "finished_ok")
							{
								a_node								= joAddHTMLChildNode (element_node, "A");
									switch (fileext)
									{
										case "png":
										case "jpg":
										case "gif":
										case "txt":
										{
											joDOM.SetAttribute 		(a_node, "href", "javascript:joOpenResultDialog ('" + resultinfo[0] + "')");
											break;
										}	
										default:
										{
											joDOM.SetAttribute 		(a_node, "href", resultinfo[0]);
											joDOM.SetAttribute 		(a_node, "target", "blank");
										}
											
									}
									a_node.innerHTML			= filename;
								span_node						= joAddHTMLChildNode (element_node, "SPAN");
									span_node.innerHTML		= "(" + resultinfo[1] + "B" + ")";
							}
							else
							{
								element_node.innerHTML	= results[i];
							}
					}
				}
				break;
			}
		}
		
		var help_link_node 	= joFIND ("job_result_link_node");
		if (help_link_node) 
		{
			var help_link_anchor	= "job_result_" + results[0];
			help_link_node.href 	= "javascript:ShowHelp ('" + help_link_anchor + "')";
		}
	}	
}

/* ********************************************************************************************************** */

function _joVMMDataResult_SetAutoFrefresh (value)
{
	this.autorefresher_cycle = parseInt (value);
	
	if (this.autorefresher)
	{
		window.clearInterval (this.autorefresher);
		this.autorefresher = 0;
	}
	
	if (this.autorefresher_cycle)
	{
		this.autorefresher = window.setInterval ("joVMMApp.GetGuiObject ('vmm_calc_results').ShowData()", this.autorefresher_cycle * DURATION_MINUTE);
	}
}

/* ********************************************************************************************************** */

function _joVMMDataResult_OnStopOrDeleteJob()
{
	switch (curr_action_type)
	{
		case "stop":
		{
			this.ShowData();
		}
		break;
		case "delete":
		{
			if (this.data_entity) 
				this.gui_list_node.removeChild (this.data_entity);
				
			this.prev_data_entity_id					= "";
			this.data_entity_id							= "";
			this.data_entity								= null;

			this.detail_node.style.visibility		= "hidden";
			this.result_node.style.visibility		= "hidden";
			this.ActivateButtons();
		}
	}
}

