var	JO_VMM_SCENARIO_DISPLAY_MODE_SCHEMATIC		= 0;
var	JO_VMM_SCENARIO_DISPLAY_MODE_REALISTIC		= 1;

/* ********************************************************************************************************** */
/* Scenario Class */
/* ********************************************************************************************************** */

function joVMMDataScenarioClass (id, gui_parent_node_name, data_collection_name, data_name, source_data_types, list_node, modeller_node)
{
	this.id								= id;
	this.gui_type						= "scenario";
	this.gui_title_node				= null;
	this.gui_list_node				= list_node;
	this.gui_modeller_node			= modeller_node;
	this.data_collection_name		= data_collection_name;
	this.data_collection			= null;
	this.data_name						= data_name;
	this.data_id						= null;		
	this.data							= null;	
	this.source_data_types			= source_data_types;
	this.data_entity_id				= "";
	this.form_node 					= null;

	this.gui_scenario_height		= 0; // ??? (this.data_name == "object") ? 364: 120;
	this.max_entity_height			= 0;
	this.min_entity_width			= JO_VMM_GUI_SCENARIO_OBJECT_WIDTH;
	
	this.landscape						= (this.data_name == "object" ? false : true);
	this.device							= (this.data_name == "device" ? true: false);
	this.display_mode					= JO_VMM_SCENARIO_DISPLAY_MODE_SCHEMATIC;
	
	this.gui_parent_node_name		= gui_parent_node_name;
	this.gui_parent_node			= joFIND (gui_parent_node_name);
	
	this.gui_body_node				= null;
	
	this.ResetData						= _joVMMDataScenario_ResetData;
	this.InitGui						= _joVMMDataScenario_InitGui;
	this.ShowData						= _joVMMDataScenario_ShowData;

	this.ShowDataEntity				= _joVMMDataScenario_ShowDataEntity;
	this.SetActiveEntity			= _joVMMDataScenario_SetActiveEntity;
	this.DrawDataEntity				= _joVMMDataScenario_DrawDataEntity;

	this.ShowSubEntity				= _joVMMDataScenario_ShowSubEntity;

	this.ChangeDisplayMode			= _joVMMDataScenario_ChangeDisplayMode;

	// events
	this.OnEvent		 				= _joVMMDataScenario_OnEvent;

	// do now
	this.InitGui();
}

/* ********************************************************************************************************** */

function _joVMMDataScenario_InitGui()
{
	var scenario_node, table_node, tr_node, tr_node, td_node, title_node, scroll_div_node, footer_node, select_node;
	
	scenario_node														= joAddHTMLChildNode  (this.gui_parent_node, "DIV", "vmm_scenario " + "scenario_" + this.data_name, this.id);
		title_node														= joAddHTMLChildNode  (scenario_node, "DIV", "title");
			this.gui_title_node										= joVMMApp.AddTitle   (title_node, this.data_name + "_" + this.gui_type, joGUI.GetLocaleText (this.gui_type + "_" + this.data_collection_name)); 
		body_node														= joAddHTMLChildNode  (scenario_node, "DIV", "content");
			table_node													= joAddTableChildNode (body_node);
				tr_node													= joAddHTMLChildNode  (table_node, "TR");
					td_node												= joAddHTMLChildNode  (tr_node, "TD");
						scroll_div_node								= joAddHTMLChildNode  (td_node, "DIV", "scenario", this.data_name + "_scenario_resize");
							table_node									= joAddTableChildNode (scroll_div_node);
								tr_node									= joAddHTMLChildNode  (table_node, "TR");
									td_node								= joAddHTMLChildNode  (tr_node, "TD", "vmm_scenario");
										div_node							= joAddHTMLChildNode  (td_node, "DIV", this.data_name + "_scenario");
											table_node					= joAddTableChildNode (div_node);
												this.gui_body_node	= joAddHTMLChildNode  (table_node, "TR");

		if (this.landscape)
		{
			footer_node													= joAddHTMLChildNode  (scenario_node, "DIV", "scenario_footer footer");
				this.form_node	 										= joAddHTMLChildNode  (footer_node, "FORM", "footer");
					joAddEvent 											(this.form_node, "submit", function (event) { void(0); });
					joAddTextChildNode 								(this.form_node, joGUI.GetLocaleText ("display_mode"));
										
					select_node 										= joAddHTMLChildNode (this.form_node, "SELECT", "", "display_mode");
						joDOM.SetAttribute				 			(select_node, "vmm_data_obj", this.id);
						joAddSelectOptionNode 						(select_node, 0, joGUI.GetLocaleText ("display_schematic"));
						joAddSelectOptionNode 						(select_node, 1, joGUI.GetLocaleText ("display_realistic"));
						joAddEvent 										(select_node, "change", function (event) 
																			{ 
																				var event_node 	= (window.event) ? event.srcElement : objSrc = event.target;	// browser incompatibilities
																				var scenario_obj 	= joVMMApp.GetGuiObject (event_node.getAttribute ("vmm_data_obj"));
																				scenario_obj.ChangeDisplayMode (event_node.value); 
																				return false; 
																			});

	
		}	
}

/* ********************************************************************************************************** */

function _joVMMDataScenario_ResetData()
{
	this.data_collection = null;
	this.data = null;
	this.data_id = null;
}

/* ********************************************************************************************************** */

function _joVMMDataScenario_ShowData()
{
	if (joVMM.xmlData)
	{
		if (!this.data_collection)
		{
			this.data_collection = joVMM.xmlData.FindNode (this.data_collection_name);
		}
		if (!this.data)
		{
			this.data = joVMM.entities [this.data_name];
		}
	
		if (this.data)
		{
		
		
			var obj_color;
		
			this.max_entity_height	= joVMM.GetHighestEntityHeight (this.data);
			if (this.landscape)
			{
				this.min_entity_width	= Math.max (Math.floor (joVMM.GetSmallestEntityWidth (this.data)), 1);
			}
			
//			alert (Math.round (100 / this.max_entity_height) * this.gui_scenario_height);


			joDOM.RemoveChildNodes (this.gui_body_node);
		
			var obj = this.data.childNodes.element_first;
			while (obj)
			{
				var coll_name 	= (this.data_collection_name == "device") ? "device" : obj.name;
				var curr_obj	= joVMM.entities[coll_name].FindNodeByAttribute (obj.name, "id", obj.GetAttribute ("id", ""));

				if (this.data_name == "device" && curr_obj)
				{
					var curr_obj_type = curr_obj.GetAttribute ("type");
					if (curr_obj_type != this.data_name) curr_obj = null;
				}

				if (curr_obj)
				{
					obj_color = curr_obj.GetAttribute ("gui_color", "#ffffff");
					this.ShowDataEntity (obj, obj_color);
				}
				obj = obj.element_next;
			}
		}
	}
}

/* ********************************************************************************************************** */

function _joVMMDataScenario_DrawDataEntity (object_name, obj_node, obj_color, obj_body, obj_width)
{
	var subobj;
	var subobj_dimension;
	var subnode;
	var tr_node;
	var draw_rest = 0;
	var draw_dimension;
	
	switch (object_name)
	{
		case "object": //..........................................................object: consisting of materials
		{
			subobj = obj_body.childNodes.element_last;
			while (subobj)
			{
				tr_node												= joAddHTMLChildNode (obj_node, "TR", "");

					data_entity 									= joVMM.entities[subobj.name].FindNodeByAttribute (subobj.name, "id", subobj.GetAttribute ("id", ""));
					subobj_dimension 								= joGetRealPartAsFloat (subobj.GetAttribute ("thickness"));
					draw_dimension									= (subobj_dimension / this.max_entity_height) * this.gui_scenario_height;
					draw_rest										+= draw_dimension - Math.round (draw_dimension);
					draw_dimension									= Math.round (draw_dimension) + (subobj.element_next ? 0 : draw_rest);
					
					if (draw_dimension > 0)
					{
						subnode 										= joAddHTMLChildNode (tr_node, "TD", "material");
							subnode.innerHTML 					= "<SPAN></SPAN>";
					 		subnode.style.backgroundColor 	= data_entity.GetAttribute ("gui_color");
							subnode.style.backgroundImage	= "URL(images/bg_" + subobj.name + ".gif)";
							subnode.style.height 				= draw_dimension;
							subnode.title							= subobj.GetAttribute ("id", "") + " [" + subobj.GetAttribute ("thickness", "") + "]";
					}
				
				subobj = subobj.element_prev;
			}
		}
		break;
		
		//.....................................................device or macro: consisting of objects (and macros)
		case "device":
		case "macro":
		{
			var element_width, element_width_relative, element_height;
			var td_node;
			var subelement_width;
			var data_id;
			
			subobj = obj_body.childNodes.element_first;
			while (subobj)
			{
				if (subobj.name == "macro")
				{
					var count  = subobj.GetAttribute ("count");
					var data_entity = joVMM.entities[subobj.name].FindNodeByAttribute (subobj.name, "id", subobj.GetAttribute ("id", ""));
					if (data_entity)
					{
						for (var i = 0; i < count; i++)
						{
							this.DrawDataEntity (data_entity.name, obj_node, obj_color, data_entity.FindNode ("body"));
						}
					}
				}
				else
				{
					data_id									= subobj.GetAttribute ("id", "");
					data_entity 							= joVMM.entities[subobj.name].FindNodeByAttribute (subobj.name, "id", data_id);
					td_node	 								= joAddHTMLChildNode (obj_node, "TD");
					element_height							= joVMM.GetObjectHeight (data_entity);
					element_width  						= joGetRealPartAsFloat (subobj.GetAttribute ("width"));
					element_width_relative				= Math.round ((element_width / this.min_entity_width) * JO_VMM_GUI_SCENARIO_OBJECT_WIDTH);
					subnode 									= joAddHTMLChildNode (td_node, "DIV", "object");
					subnode.innerHTML 					= "<SPAN></SPAN>";
					subnode.title 							= data_id + " [" + subobj.GetAttribute ("width") + "]";
					if (data_entity) 
					{
						subnode.style.backgroundColor 	= data_entity.GetAttribute ("gui_color");
						subnode.style.backgroundImage	= "URL(images/bg_" + subobj.name + ".gif)";
					}
					subnode.style.height 				= Math.round ((element_height / this.max_entity_height) * this.gui_scenario_height);
					subnode.style.width 				= element_width_relative;
				}

				subobj = subobj.element_next;
			}
		}
		break;
	}
}

/* ********************************************************************************************************** */

function _joVMMDataScenario_ShowDataEntity (entity_object, obj_color)
{
	var obj_body 		= entity_object.FindNode ("body");
	var obj_node 		= joAddHTMLChildNode  (this.gui_body_node, "TD", this.data_name);
	var obj_name 		= (entity_object.parent.parent.name == "device") ? "device" : entity_object.name;
	var gui_node, table_node, tbody_node, tr_node, td_node, data_entity, subobj_body, subsubobj;
	var count_obj;
	
	//....................................................................................macro or device modeller
	if (this.landscape) 
	{
		if (this.display_mode	== JO_VMM_SCENARIO_DISPLAY_MODE_SCHEMATIC) //......................in schematic mode
		{
			gui_node = obj_node;
		
				table_node 											= joAddTableChildNode (obj_node);
					tr_node 											= joAddHTMLChildNode (table_node, "TR");
						if (obj_body)
						{
							this.DrawDataEntity 				(obj_name, tr_node, obj_color, obj_body);
						}
					tr_node											= joAddHTMLChildNode (table_node, "TR");
						td_node										= joAddHTMLChildNode (tr_node, "TD", "obj_marker_gap");
							td_node.innerHTML 					= "<SPAN></SPAN>";
					tr_node											= joAddHTMLChildNode (table_node, "TR");
						td_node										= joAddHTMLChildNode (tr_node, "TD", "obj_marker");
							td_node.innerHTML 					= "<SPAN></SPAN>";
							td_node.style.backgroundColor 	= obj_color;
							td_node.style.backgroundImage	= "URL(images/bg_" + this.data_name + ".gif)";

							count_obj 								= joVMM.GetCountSubObjects (entity_object);
							if (count_obj > 1)
								td_node.colSpan					= count_obj;
							
							td_node.title 							= entity_object.GetAttribute ("id", "");
		}
		else //...................................................................................in realistic mode
		{
			gui_node = obj_node;
		
				table_node 											= joAddTableChildNode (obj_node);
					tr_node 											= joAddHTMLChildNode (table_node, "TR");
						
						if (obj_body)
						{

							subobj = obj_body.childNodes.element_first;
							while (subobj)
							{
								if (subobj.name == "macro")
								{
									var count  			= subobj.GetAttribute ("count");
									data_entity 		= joVMM.entities[subobj.name].FindNodeByAttribute (subobj.name, "id", subobj.GetAttribute ("id", ""));
									if (data_entity)
									{
										for (var i = 0; i < count; i++)
										{
											subobj_body 	= data_entity.FindNode ("body");
											subsubobj 		= subobj_body.childNodes.element_first;
											while (subsubobj)
											{
												this.ShowSubEntity (subsubobj, tr_node);
												subsubobj = subsubobj.element_next;
											}

										}
									}
								}
								else
								{
									this.ShowSubEntity (subobj, tr_node);
								}
				
								subobj = subobj.element_next;
							}


						}
					tr_node											= joAddHTMLChildNode (table_node, "TR");
						td_node										= joAddHTMLChildNode (tr_node, "TD", "obj_marker_gap");
							td_node.innerHTML 					= "<SPAN></SPAN>";
					tr_node											= joAddHTMLChildNode (table_node, "TR");
						td_node										= joAddHTMLChildNode (tr_node, "TD", "obj_marker");
							td_node.innerHTML 					= "<SPAN></SPAN>";
							td_node.style.backgroundColor 	= obj_color;
							td_node.style.backgroundImage	= "URL(images/bg_" + this.data_name + ".gif)";

							count_obj 								= joVMM.GetCountSubObjects (entity_object);
							if (count_obj > 1)
								td_node.colSpan					= count_obj;
							
							td_node.title 							= entity_object.GetAttribute ("id", "");
		}
	
	}
	else //.......................................................................................object modeller
	{
		gui_node			= joAddHTMLChildNode (obj_node, "TABLE", entity_object.name);
		tbody_node 		= joAddHTMLChildNode (gui_node, "TBODY");
		
		if (obj_body)
		{
			this.DrawDataEntity (obj_name, tbody_node, obj_color, obj_body);
		}
		
		table_node											= joAddTableChildNode (obj_node, "obj_marker");
			tr_node 											= joAddHTMLChildNode (table_node, "TR");
				td_node 										= joAddHTMLChildNode (tr_node, "TD");
					td_node.innerHTML 					= "<SPAN></SPAN>";
					td_node.style.backgroundImage	= "URL(images/bg_" + this.data_name + ".gif)";
					td_node.style.backgroundColor 	= obj_color;
					td_node.title 							= entity_object.GetAttribute ("id", "");
	}
	
	//..................................................................................add entity interactivity
	gui_node.id 		= this.id + "_" + entity_object.GetAttribute ("id");
	
	joDOM.SetAttribute (obj_node, "vmm_data_obj", this.id);
	joDOM.SetAttribute (obj_node, "vmm_data_obj_id", this.id + "_" + entity_object.GetAttribute ("id"));
	obj_node.onclick 	= function() 
	{ 
		joVMMApp.GetGuiObject (this.getAttribute ("vmm_data_obj")).SetActiveEntity (this.getAttribute ("vmm_data_obj_id")); 
		return false; 
	};
	
}

/* ********************************************************************************************************** */

function _joVMMDataScenario_ShowSubEntity (subobj, parent_node)
{
	var td_node, table_node, tbody_node;

	var subobj_width	= joGetRealPartAsFloat (subobj.GetAttribute ("width"))
	var data_entity 	= joVMM.entities[subobj.name].FindNodeByAttribute (subobj.name, "id", subobj.GetAttribute ("id", ""));
	
	if (data_entity)
	{
		var subobj_body 							= data_entity.FindNode ("body");
		if (subobj_body)
		{
			td_node 									= joAddHTMLChildNode (parent_node, "TD");
				table_node							= joAddHTMLChildNode (td_node, "TABLE");
					joDOM.SetAttribute 			(table_node, "width", Math.max (1, Math.round (subobj_width / this.max_entity_height * this.gui_scenario_height)));
					tbody_node 						= joAddHTMLChildNode (table_node, "TBODY");
						this.DrawDataEntity 	("object", tbody_node, data_entity.GetAttribute ("gui_color", "#000000"), subobj_body, subobj_width);
		}
	}
}


/* ********************************************************************************************************** */

function _joVMMDataScenario_SetActiveEntity (gui_id, triggered_from_list)
{
	var i = 0;
	var gui_node;

	if (this.data_entity_id != "")
	{
		gui_node = joFIND (this.id + "_" + this.data_entity_id);
		if (gui_node) gui_node.className = this.data_name;
	}
	
	gui_node = joFIND (gui_id);
	if (gui_node)
	{
		gui_node.className = this.data_name + "_active";
	
		this.data_entity_id	 	= gui_id.substr (this.id.length + 1);
		var data_body				= this.data.FindNode("body");
		this.data_entity 			= (data_body ? data_body.childNodes.GetElement (this.data_entity_id) : null);
		
		if (!triggered_from_list) 
		{
			this.gui_list_node.SetActiveEntity (joFIND (this.gui_list_node.id + "_" + this.data_entity_id));
			this.gui_list_node.Action ("model");
		}
	}
}

/* ********************************************************************************************************** */

function _joVMMDataScenario_OnEvent (event_type, event_data_type)
{
	switch (event_type)
	{
		case JO_VMM_GUI_ON_RESIZE:
			this.gui_scenario_height = joGetDivInfo (joFIND (this.data_name + "_scenario_resize"), "height") - 50;
			this.ShowData();
		break;
	}
}

/* ********************************************************************************************************** */

function _joVMMDataScenario_ChangeDisplayMode (display_mode)
{
	this.display_mode = parseInt (display_mode);

	this.ShowData();	
	
	if (this.data_entity_id)
	{
		this.SetActiveEntity (this.id + "_" + this.data_entity_id, false)
	}
	

}

