
/* ********************************************************************************************************** */
/* List Class */
/* ********************************************************************************************************** */

function joVMMDataListClass (id, gui_parent_node_name, data_collection_name, data_name, source_data_types, destination_data_types)
{
	this.id									= id;

	this.gui_type						= "list";
	this.gui_parent_node_name		= gui_parent_node_name;
	this.gui_parent_node			= joFIND (gui_parent_node_name);
	this.gui_list_node				= null;
	this.InitGui							= _joVMMDataList_InitGui;
	
	this.data_collection_name	= data_collection_name;
	this.data_name						= data_name;
	this.data_id							= "";
	this.data								= null;
	
	this.ActionType					= "";
	
	this.form_node						= null;
	
	this.destination_data_types	= destination_data_types;
	this.source_data_types			= source_data_types;
	
	this.ResetData						= function() {};
	this.ShowData						= _joVMMDataList_ShowData;
	this.ShowDataEntity				= _joVMMDataList_ShowDataEntity;
	this.SetActiveEntity			= _joVMMDataList_SetActiveEntity;
	this.RemoveActiveEntity		= _joVMMDataList_RemoveActiveEntity;
	this.HandleRemoveDlg			= _joVMMDataList_HandleRemoveDlg;
	this.ModelActiveEntity			= _joVMMDataList_ModelActiveEntity;
	this.CheckEntityInUse			= _joVMMDataList_CheckEntityInUse;

	this.AddButton						= _joVMMDataList_AddButton;
	this.ActivateButtons			= _joVMMDataList_ActivateButtons;
	this.SetButtonState				= _joVMMDataList_SetButtonState;
	
	this.Action							= _joVMMDataList_Action;

	// events
	this.OnEvent		 					= _joVMMDataList_OnEvent;
	
	// do now
	this.InitGui();
}

/* ********************************************************************************************************** */

function _joVMMDataList_InitGui()
{
	var button_node;

	this.form_node	 							= joAddHTMLChildNode (this.gui_parent_node, "FORM");
		joAddEvent 								(this.form_node, "submit", function (event) { void(0); });
		joDOM.SetAttribute					(this.form_node, "name", this.id + "_form");
			var list_node						= joAddHTMLChildNode (this.form_node, "DIV", "vmm_list");
				var title_node					= joAddHTMLChildNode (list_node, "DIV", "title");
					joVMMApp.AddTitle 		(title_node, "library_" + this.data_collection_name);
				
				this.gui_list_node			= joAddHTMLChildNode (list_node, "DIV", "content", this.id + "_list");
				var footer_node				= joAddHTMLChildNode (list_node, "DIV", "footer");

					if (this.data_name != "material")
					{
						this.AddButton (footer_node, "model");
					}
					this.AddButton (footer_node, "edit");
					
					if (this.data_name != "device")
					{
						this.AddButton (footer_node, "new");
						this.AddButton (footer_node, "copy");
						this.AddButton (footer_node, "delete");
						this.AddButton (footer_node, "add");
					}
	
	this.ShowData();
	/* new button */
}

/* ********************************************************************************************************** */

var copy_entity_list_obj = null;

function _joVMMDataList_Action (action_type)
{
	copy_entity_list_obj = null;
	this.ActionType = action_type;
	
	switch (action_type)
	{
		case "edit":
		break;
		case "copy":
			copy_entity_list_obj = this;
			joVMM.CopyEntity (this.data_collection_name, this.data_name, this.data_id);
			joVMMApp.OnCloseDialog ("", this.data_name, true);
			joVMMApp.GuiObjectAction ("edit", this);
		break;
		case "new":
		break;
	}

	joVMMApp.GuiObjectAction (action_type, this); 
	return false;
}

/* ********************************************************************************************************** */

function _joVMMDataList_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 _joVMMDataList_OnEvent (event_type, event_data_type, further_infos)
{
	switch (event_type)
	{
		case JO_VMM_GUI_ON_RESIZE:
		break;
		default:
			switch (event_data_type)
			{
				case JO_VMM_DATA_CHANGE_MY_DATA_TYPE:
				{
					var curr_entity_id  = null;
					if (joVMM.LastEditedEntity) curr_entity_id = joVMM.LastEditedEntity.GetAttribute ("id");
					this.ShowData ((further_infos == ("dialog_" + this.data_collection_name) && !curr_entity_id) ? false : true, curr_entity_id);
					joVMM.LastEditedEntity = null;
					joVMMApp.RefreshScenarios (this.data_name);

					break;
				}
				case JO_VMM_DATA_CHANGE_SOURCE_DATA_TYPE:
					if (event_type != JO_VMM_EVENT_DATA_CHANGE_NEW)
					{
						this.ShowData();
					}
				break;
				case JO_VMM_DATA_CHANGE_DESTINATION_DATA_TYPE:
					this.ActivateButtons();
					break;
			}
		break;
	}
}

/* ********************************************************************************************************** */

function _joVMMDataList_ShowData (activate_last, active_entity_id)
{
	if (joVMM.xmlData)
	{
		var list_node 	= joVMM.xmlData.FindNode (this.data_collection_name);
		
		if (list_node)
		{
			var elements = list_node.childNodes;
			var element = elements.element_first;
			var element_id;
			
			// init
			joDOM.RemoveChildNodes (this.gui_list_node);
			this.data_id = "";
		
			// fill
			while (element)
			{
				this.ShowDataEntity (element, activate_last && !element.element_next && this.ActionType != "delete");
				element = element.element_next;
			}
		}
		 
		this.ActivateButtons();
		
		if (active_entity_id)
		{
			var gui_node_id = "";
			
			if (this.ActionType == "new")
			{
				var data_collection = joVMM.entities[this.data_name];
			
				if (data_collection && data_collection.childNodes && data_collection.childNodes.element_last)
				{
					gui_node_id = data_collection.childNodes.element_last.GetAttribute ("id");
				}
						
			}
			else
			{
				gui_node_id = active_entity_id;
			}
			
			if (gui_node_id != "")
			{
				gui_node = joFIND (this.id + "_" + gui_node_id);
				if (gui_node) this.SetActiveEntity (gui_node);
			}
		}
		
		if (activate_last || active_entity_id) //......................................auto select and auto edit recently added entity
		{
			joVMMApp.GuiObjectAction ("model", this);
		}
	}
}

/* ********************************************************************************************************** */

function _joVMMDataList_ActivateButtons()
{
	var active_entity	 		= this.data_id == "" ? false : true;
	var active_destination	= false;
	var entity_is_virtual 	= false;
	
	var modeller_obj = joVMMApp.gui_current_modeller;
	if (modeller_obj && modeller_obj.data_id && modeller_obj.data_name != this.data_name) //??? check for modeller form status
	{
		if (joIsBoolean (joDOM.GetAttribute (modeller_obj.entity_form_id, "vmm_ok"))) active_destination = true;
	}
	
	this.SetButtonState ("button_copy", active_entity);
	this.SetButtonState ("button_edit", active_entity);
	this.SetButtonState ("button_delete", active_entity);
	this.SetButtonState ("button_model", active_entity);

	if (joVMM.xmlData) 
	{
		var data_obj		= joVMM.xmlData.FindNode (this.data_collection_name).FindNodeByAttribute ("", "id", this.data_id);
		if (data_obj)
			entity_is_virtual = joNumCheck (data_obj.GetAttribute ("is_virtual"), "boolean");
	}

	//..............................................................set button state for "add to entity" function
	var button_add_state = (!entity_is_virtual && active_entity && modeller_obj && modeller_obj.data_id && modeller_obj.data_id && modeller_obj.data_id != this.data_id);

	if (button_add_state)
	{
		var correct_destination_type = false;
		for (var i = 0; !correct_destination_type && i < modeller_obj.source_data_types.length; i++)
		{
			if (modeller_obj.source_data_types[i] == this.data_name)
			{
				correct_destination_type = true;
			}
		}
		button_add_state = correct_destination_type;
	}

	if (button_add_state)
	{
		if (this.data_name != "material")
		{
			var entity = joVMM.GetEntityByID (this.data_name, this.data_id);
			button_add_state = joVMM.EntityHasBodyEntities (entity);
			
		}
	}
	
	this.SetButtonState ("button_add", button_add_state);
}

/* ********************************************************************************************************** */

function _joVMMDataList_SetButtonState (button_name, state)
{
	var button_node = joDOM.GetFormElement (this.form_node, button_name);
	if (button_node)
	{
		/*
		button_node.style.visibility = state ? "visible" : "hidden";
		*/
		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 _joVMMDataList_ShowDataEntity (element, autoselect)
{
	var element_id, element_node, color_node, info_node, text_node;
	
	element_id									= element.GetAttribute ("id");
	element_is_virtual						= element.GetAttribute ("is_virtual");

	element_node 								= joAddHTMLChildNode (this.gui_list_node, "DIV", "vmm_list_" + this.data_name, this.id + "_" + element_id);
	element_node.ondblclick 				= function() 
														{ 
															var curr_list_obj = joVMMApp.GetGuiObject (this.getAttribute ("vmm_data_obj"));
															if (!joDOM.GetFormElement (curr_list_obj.form_node, "button_add").disabled)
															{
																joVMMApp.gui_current_modeller.AddEntity (joVMMApp.GetGuiObject (this.getAttribute ("vmm_data_obj"))); 
															}
															return false; 
														};
	element_node.onclick 					= function() { joVMMApp.GetGuiObject (this.getAttribute ("vmm_data_obj")).SetActiveEntity (this); return false; };
	element_node.title 						= element.GetAttribute ("description");
	joDOM.SetAttribute (element_node, "vmm_data_obj", this.id);

	color_node 									= joAddHTMLChildNode (element_node, "DIV", "vmm_color_square");
	color_node.innerHTML 					= "<span></span>";
	color_node.style.backgroundColor 	= element.GetAttribute ("gui_color", "#ffffff");
	color_node.style.backgroundImage	= "URL(images/bg_" + this.data_name + ".gif)";
	
	info_node 									= joAddHTMLChildNode (element_node, "DIV", "vmm_info");
	text_node									= joAddHTMLChildNode (info_node, "SPAN", (joIsBoolean (element_is_virtual) ? "virtual" : ""));

	text_node.innerHTML 					= element.GetAttribute ("id");
	
	if (autoselect)
	{
		this.SetActiveEntity (element_node);
	}
}

/* ********************************************************************************************************** */

function _joVMMDataList_SetActiveEntity (gui_node)
{
	if (this.data_id != "")
	{
		var old_gui_node = joFIND (this.id + "_" + this.data_id);
		old_gui_node.getElementsByTagName ("DIV")[0].className = "vmm_color_square";
		old_gui_node.getElementsByTagName ("DIV")[1].className = "vmm_info";
		old_gui_node.style.backgroundColor = "";
	}
	else
	{
	}
	
	gui_node.getElementsByTagName ("DIV")[0].className = "vmm_color_square active_img";
	gui_node.getElementsByTagName ("DIV")[1].className = "vmm_info active_txt";
	
	gui_node.style.backgroundColor = "#DBD8C5";
	
	this.data_id = gui_node.id.substr (this.id.length + 1);
	
	if (this.data_name == "object" && joVMMApp.gui_current_modeller && this.data_name == joVMMApp.gui_current_modeller.data_name)
		joVMMApp.GuiObjectAction ("model", this);
	
	this.ActivateButtons();
}

/* ********************************************************************************************************** */

function _joVMMDataList_ModelActiveEntity ()
{
	joVMMApp.OnChangeDataSelection (this.data_name, this.data_id, true); 
}

/* ********************************************************************************************************** */

function _joVMMDataList_CheckEntityInUse (entity_id, mandatory_in_device)
{
	alert ("change to: joVMM.EntityIsInDevice()");
	return false; // ???
	var dest_objects 			= [];
	var dest_object_ids 	= [];
	var result 					= false;

	for (var i = 0; i < this.destination_data_types.length && !dest_objects.length; i++)
	{
		joVMM.FindDestinationObjects (this.data_name, entity_id, this.destination_data_types[i], dest_objects, dest_object_ids);
	}
	
	var count_objs = dest_objects.length;
	
	if (count_objs) // entity in use
	{
		// if objects are reffered in macros these macros should also be in device - check for input file production
		
		if (mandatory_in_device)
		{
			var macro_list = joVMMApp.GetGuiObject ("vmm_macro_list");
			
			for (var i = 0; i < dest_objects.length; i++)
			{
				if (dest_objects[i].name == "macro")
				{
					if (!macro_list.CheckEntityInUse (dest_objects[i].GetAttribute ("id")))
						count_objs--;
				}
			}
			result = (count_objs ? true : false);
		}
		else
		{
			result = true;
		}
	}	
	return result;
}

/* ********************************************************************************************************** */

function _joVMMDataList_RemoveActiveEntity()
{
	// check if entry object is already in use by one of its potential destination objects (e.g. current material by one or more objects?)

	var entity_used_by = joVMM.EntityIsInUse (this.data_name, this.data_id);
	
	if (entity_used_by != "")
	{
		var msg_str = joGUI.GetLocaleText ("msg_cannot_delete_entity") + "<br />" + entity_used_by;
		joGUI.Alert (msg_str, joGUI_ALERT_TYPE_WARNING, joGUI_ALERT_BUTTON_OK);
	}
	else
	{
		joGUI.Alert (joGUI.GetLocaleText ("joGUI_sure_delete_entity"), joGUI_ALERT_TYPE_NOTE, joGUI_ALERT_BUTTON_OK | joGUI_ALERT_BUTTON_CANCEL, this.HandleRemoveDlg, this);
	}	
}

/* ********************************************************************************************************** */

function _joVMMDataList_HandleRemoveDlg (user_decision, list_obj)
{
	if (user_decision == joGUI_ALERT_BUTTON_OK)
	{
		if (joVMM.RemoveEntity (list_obj.data_collection_name, list_obj.data_name, list_obj.data_id))
		{
			joVMMApp.OnCloseDialog ("", list_obj.data_name, true);
		}
	}
}
