
/* ********************************************************************************************************** */

var rd_titles 	= null;
var rd_data		= null;

function joShowTableRowDiagram (a_node, row_node_id)
{
	var tr_node = null;
	var parent_node = a_node.parentNode;
	
	while (parent_node && !tr_node)
	{
		if (parent_node.nodeName == "TR")
		{
			tr_node = parent_node;
		}
		else parent_node = parent_node.parentNode;
	}
	
	if (tr_node)
	{
		var title_tr_node = document.getElementById (row_node_id);
		if (title_tr_node)
		{
			rd_titles 	= [];
			rd_data		= [];
			joShowTableRowDiagramGetData (tr_node, title_tr_node);
			
			joGUI_ShowDlg ("diagram_popup", 10, 400, 230);
//			alert (escape (rd_data[0]));
			joFIND ("diagram_popup_title").innerHTML = rd_data[0];
			joShowTableRowDiagramFillData();
		}		
	}
}

/* ********************************************************************************************************** */

function joShowTableRowDiagramGetData (tr_node, title_tr_node)
{
	var td_node = title_tr_node.firstChild;
	var classname;
	var str;
	while (td_node)
	{
		classname = td_node.className + "";
		if (classname.indexOf ("title") != -1)
			rd_titles.unshift ((td_node.innerHTML + "").replace (/<[^>]*>/g, ''));
		else if (classname.indexOf ("data") != -1)
			rd_titles.push ((td_node.innerHTML + "").replace (/<[^>]*>/g, ''));
		td_node = td_node.nextSibling;
	}
	
	td_node = tr_node.firstChild;
	while (td_node)
	{
		classname = td_node.className + "";
		if (classname.indexOf ("title") != -1)
		{
			rd_data.unshift ((td_node.innerHTML + "").replace (/<[^>]*>/g, ''));
		}
		else if (classname.indexOf ("data") != -1)
			rd_data.push ((td_node.innerHTML + "").replace (/<[^>]*>/g, ''));
		td_node = td_node.nextSibling;
	}
}

/* ********************************************************************************************************** */

function joShowTableRowDiagramFillData()
{
	var table_node = joFIND ("diagram_table");
	var i, tr_node, td_node, div_node;
	var val;
	
	max_val = 0;
	min_val = 0;
	
	var full_height = joShowTableRowDiagramGetMaxVal();

	if (table_node) 
	{
		joDOM.RemoveChildNodes (table_node);
		
		tr_node = joAddHTMLChildNode (table_node, "TR", "data");
		for (i = 1; i < rd_data.length; i++)
		{
			val											= GetNumber (rd_data[i]);
			td_node 										= joAddHTMLChildNode (tr_node, "TD");
				td_node.style.verticalAlign = "bottom";

				if (val < 0)
				{
					div_node								= joAddHTMLChildNode (td_node, "DIV", "shift");
						div_node.innerHTML 			= "<SPAN></SPAN>";
						div_node.style.height	 	= Math.floor (max_val / full_height * 100) + "%";
				}
				else if (!min_val && val != max_val)
				{
					div_node								= joAddHTMLChildNode (td_node, "DIV", "shift");
						div_node.innerHTML 			= "<SPAN></SPAN>";
						div_node.style.height	 	= (max_val == val ? 100 : Math.ceil ((max_val - val) / full_height * 100)) + "%";
				}

				if (val < 0 && min_val)
				{
					div_node								= joAddHTMLChildNode (td_node, "DIV", "line");
						div_node.innerHTML 			= "<SPAN></SPAN>";
				}
				
				div_node									= joAddHTMLChildNode (td_node, "DIV", "data");
					div_node.innerHTML 				= "<SPAN></SPAN>";
					div_node.style.height 			= Math.floor (Math.abs (val) / full_height * 100) + "%";
					div_node.title						= rd_data[i];

				if (val > 0 && min_val)
				{
					div_node								= joAddHTMLChildNode (td_node, "DIV", "line");
						div_node.innerHTML 			= "<SPAN></SPAN>";
				}

				if (min_val < 0)
				{
					if (val > 0)
					{
						div_node							= joAddHTMLChildNode (td_node, "DIV", "shift");
							div_node.innerHTML 		= "<SPAN></SPAN>";
							div_node.style.height 	= Math.floor (Math.abs (min_val) / full_height * 100) + "%";
					}
					else if (min_val != val)
					{
						div_node							= joAddHTMLChildNode (td_node, "DIV", "shift");
							div_node.innerHTML 		= "<SPAN></SPAN>";
							div_node.style.height 	= Math.floor (Math.abs (min_val - val) / full_height * 100) + "%";
					}
				}
		}

		tr_node = joAddHTMLChildNode (table_node, "TR", "legend");
		for (i = 1; i < rd_titles.length; i++)
		{
			td_node = joAddHTMLChildNode (tr_node, "TD");
			td_node.innerHTML = rd_data[i] + "<br /><b>" + rd_titles[i] + "</b>";
		}

	}
}

/* ********************************************************************************************************** */

max_val = 0;
min_val = 0;

function joShowTableRowDiagramGetMaxVal ()
{
	var result = 0;
	var i;
	var val;
	
	for (i = 1; i < rd_data.length; i++)
	{
		val = GetNumber (rd_data[i]);
		if (val < 0)
		{
			if (val < min_val) min_val = val;
		}
		else
		{
			if (val > max_val) max_val = val;
		}
	}
	
	return max_val - min_val;
}

/* ********************************************************************************************************** */

function GetNumber (num_str)
{
	var result = num_str.replace (/\.|%|\s|\(|\)/g, '');
	
	if (curr_lang == 2) 
			result = result.replace (',', '.');
	else 	result = result.replace (',', '');
	
	result = parseFloat (result);	
	if (isNaN (result)) result = 0;
	
	return result;
}

/* ********************************************************************************************************** */

var orig_table_container 	= null;
var orig_pos_node 			= null;
var float_table_node		= null;

function joShowTablePopup (table_node_id)
{
	var table_container	= document.getElementById ("table_container");
	float_table_node		= document.getElementById (table_node_id);
	
	if (float_table_node && table_container)
	{
		orig_pos_node = document.createElement ("DIV");
		float_table_node.parentNode.insertBefore (orig_pos_node, float_table_node);
	
		var win_height 		= joGetWinInfo ("height");
		var win_width 			= joGetWinInfo ("width");
		var popup_height 		= Math.max (win_height - 100, 100);
		var popup_width		= Math.max (win_width - 100, 100);
		
		joGUI_ShowDlg ("table_popup", 5, popup_width, popup_height);
		table_container.style.width = popup_width - (ie ? 3 : 0);
		table_container.style.height = popup_height - 20 + (ie ? 2 : 0);
		
		table_container.appendChild (float_table_node);
		
	}
}

/* ********************************************************************************************************** */

function joCloseTablePopup()
{
	orig_pos_node.parentNode.insertBefore (float_table_node, orig_pos_node);
	orig_pos_node.parentNode.removeChild (orig_pos_node);
	orig_pos_node = null;
	joGUI_CloseDlg ('table_popup');
}

