
var TABLELAYOUT = {};

TABLELAYOUT.Table;
TABLELAYOUT.CurrentTool;
TABLELAYOUT.CurrentRadioRow;
TABLELAYOUT.InlineDiagram;
TABLELAYOUT.HideThread;

TABLELAYOUT.Init = function() {
	TABLELAYOUT.CurrentTool = document.getElementById('tabletoolmenu');
	TABLELAYOUT.Table = document.getElementById('financial-table');

	POPUP.InitInternalLinks();

	// add inline diagram
	TABLELAYOUT.InlineDiagram = document.createElement('div');
	TABLELAYOUT.InlineDiagram.id = 'inlinediagram';

	//Hj - fix for notes 230310
	if (!TABLELAYOUT.Table) return;

	for (var i = 2; i < TABLELAYOUT.Table.rows[0].cells.length; i++)
		TABLELAYOUT.InlineDiagram.appendChild(document.createElement('img'));
	document.body.appendChild(TABLELAYOUT.InlineDiagram);

	// add radiobutton for the top cell
	var cell = TABLELAYOUT.Table.rows[0].insertCell(2);
	cell.className = 'radio';
	cell.innerHTML = '<input checked="checked" type="radio" name="inlinegraph" />';
	EVENT.Add(cell.firstChild, 'click', TABLELAYOUT.InlineGraph);
	TABLELAYOUT.CurrentRadioRow = cell.parentNode;

	var countHeaderRows = TABLELAYOUT.Table.getElementsByTagName('thead')[0].rows.length;
	var oldlevel = 1;
	for (var r = countHeaderRows, nn = TABLELAYOUT.Table.rows.length, row = null; r < nn; r++) {

		// add radiobuttons
		cell = TABLELAYOUT.Table.rows[r].insertCell(2);
		cell.className = 'radio';
		if (isNaN(TABLELAYOUT.Table.rows[r].cells[3].innerHTML.replace(/ /g, '')))
			cell.innerHTML = '<br />';
		else {
			cell.innerHTML = '<input type="radio" name="inlinegraph" />';
			EVENT.Add(cell.firstChild, 'click', TABLELAYOUT.InlineGraph);
		}

		// make rows that contains subrows clickable to view additional rows
		var className = TABLELAYOUT.Table.rows[r].className;
		var pos = className.indexOf('lev') + 3;
		var level = className.substring(pos, pos + 1);
		if (level == 2) {
			var cell = TABLELAYOUT.Table.rows[r].cells[0];
			cell.innerHTML = '<span>' + cell.innerHTML + '</span>';
		}
		if (level > oldlevel) {
			var cell = TABLELAYOUT.Table.rows[r - 1].cells[0];
			var tmplevel = level;
			while (tmplevel > 1) {
				tmplevel--;
				cell = cell.childNodes[0];
			}
			cell = cell.parentNode;
			if (!cell.getElementsByTagName('A')[0]) {//## CHECK SO WE DONT APPEND MULTIPLE ITEMS
				a = document.createElement('a');
				a.innerHTML = cell.innerHTML;
				a.href = 'javascript:;';
				EVENT.Add(a, 'click', TABLELAYOUT.SwapRow);
				cell.innerHTML = '';
				cell.appendChild(a);
			}
		}
		oldlevel = level;
	}

};

TABLELAYOUT.InlineGraph = function(e) {
	var input = (e.srcElement) ? e.srcElement : e.target;
	TABLELAYOUT.CurrentRadioRow.className = TABLELAYOUT.CurrentRadioRow.className.replace(' radiochecked', '');
	TABLELAYOUT.CurrentRadioRow = input.parentNode.parentNode;

	// first table will hide the diagram
	if (TABLELAYOUT.CurrentRadioRow.rowIndex == 0) {
		TABLELAYOUT.InlineDiagram.style.display = 'none';
		return;
	}

	TABLELAYOUT.CurrentRadioRow.className += ' radiochecked';

	// clean cell value and deduce max and min
	cellvalue = new Array();
	max = -1000000;
	min = 1000000;
	for (var i = 3; i < TABLELAYOUT.CurrentRadioRow.cells.length; i++) {
		cellvalue[i] = TABLELAYOUT.CurrentRadioRow.cells[i].innerHTML.replace(/ /g, '');
		if (TRANSLATE.Lang == 'en')
			cellvalue[i] = cellvalue[i].replace(',', '');
		else
			cellvalue[i] = cellvalue[i].replace('&nbsp;', '').replace(',', '.');
		cellvalue[i] = parseFloat(cellvalue[i].toString().toString().replace('–', '-'));
		if (cellvalue[i] < min)
			min = cellvalue[i];
		if (cellvalue[i] > max)
			max = cellvalue[i];
		if (isNaN(cellvalue[i]))
			cellvalue[i] = 0;
	}
	scale = 1;
	if (Math.abs(min) > max) {
		scale = -min;
	} else {
		scale = max;
	}

	// apply calculated heights to the diagrambars
	TABLELAYOUT.InlineDiagram.style.top = (TABLELAYOUT.Table.offsetTop + TABLELAYOUT.CurrentRadioRow.offsetTop + 16) + 'px';
	for (var i = 3; i < TABLELAYOUT.CurrentRadioRow.cells.length; i++) {
		height = parseInt((Math.abs(cellvalue[i]) / scale) * 100);

		TABLELAYOUT.InlineDiagram.childNodes[i - 3].style.height = height + 'px';
		if (cellvalue[i] < 0) {
			TABLELAYOUT.InlineDiagram.childNodes[i - 3].className = 'negative';
			TABLELAYOUT.InlineDiagram.childNodes[i - 3].style.top = '0';
			TABLELAYOUT.InlineDiagram.childNodes[i - 3].src = '../../css/graphics/tables/diagram-negative.png';
		}
		else {
			TABLELAYOUT.InlineDiagram.childNodes[i - 3].className = '';
			TABLELAYOUT.InlineDiagram.childNodes[i - 3].style.top = (-height - 1) + 'px';
			TABLELAYOUT.InlineDiagram.childNodes[i - 3].src = '../../css/graphics/tables/diagram-positive.png';
		}
	}
	TABLELAYOUT.InlineDiagram.style.display = 'block';
};

TABLELAYOUT.SwapRow = function(e) {
	if (document.addEventListener) {
		e.preventDefault();
		a = e.target;
		if (a.className != 'expanded') {
			a.className = 'expanded';
			viewtype = 'table-row';
		} else {
			a.className = '';
			viewtype = 'none';
		}
	} else {
		e.cancelBubble = true;
		a = e.srcElement;
		if (a.className != 'expanded') {
			a.className = 'expanded';
			viewtype = 'block';
		} else {
			a.className = '';
			viewtype = 'none';
		}
	}
	tr = a.parentNode;
	while (tr.tagName != 'TR') {
		tr = tr.parentNode;
	}
	if (a.className != 'expanded') {
		tr.className = tr.className.replace(' expanded', '');
	} else {
		tr.className += ' expanded';
	}
	var textindex = tr.className.indexOf('lev');
	startlevel = parseInt(tr.className.substring(textindex + 3, textindex + 4));
	level = startlevel + 1;
	var rowindex = tr.rowIndex;
	tr = tr.parentNode.rows[rowindex - 1];
	while (startlevel < level) {
		if (startlevel == (level - 1) || viewtype == 'none') {
			if (a.parentNode.parentNode != tr) tr.style.display = viewtype;
		}
		if (tr.parentNode.rows.length <= rowindex + 1) break;
		rowindex++;
		tr = tr.parentNode.rows[rowindex - 1];
		textindex = tr.className.indexOf('lev');
		level = parseInt(tr.className.substring(textindex + 3, textindex + 4));
	}

	// hide the inline graphs if the corresponding row was hidden
	if (TABLELAYOUT.CurrentRadioRow.style.display == 'none')
		TABLELAYOUT.InlineDiagram.style.display = 'none';
	else if (TABLELAYOUT.CurrentRadioRow.rowIndex != 0 && TABLELAYOUT.Table.className.indexOf('multiyear') > -1)
		TABLELAYOUT.InlineDiagram.style.display = 'block';

	// adjust the top position of the inline graphs
	TABLELAYOUT.InlineDiagram.style.top = (TABLELAYOUT.Table.offsetTop + TABLELAYOUT.CurrentRadioRow.offsetTop + 16) + 'px';

	return false;
};

TABLELAYOUT.SwitchTool = function(e) {
	var anchor;
	anchor = (e.srcElement) ? e.srcElement : e.target;

	var oldTool = TABLELAYOUT.CurrentTool;

	// if this is a column page, there is no default tool, and the tools can be hidden
	if (document.body.className.indexOf('tablepage') < 0) {
		document.getElementById('tabletoolarea').className = 'visible';
		if (oldTool) {
			if (oldTool == anchor) { // a click on the same tool will hide the toolarea
				oldTool.className = '';
				document.getElementById('tabletoolarea').className = '';
				document.getElementById(oldTool.id + 'Area').style.display = 'none';
				TABLELAYOUT.CurrentTool = null;
				return;
			}
		}
		else {
			oldTool = anchor;
		}
	}

	oldTool.className = '';
	TABLELAYOUT.CurrentTool = anchor;
	TABLELAYOUT.CurrentTool.className = 'selected';
	TABLELAYOUT.InlineDiagram.style.display = 'none';

	if (oldTool.id != 'tabletoolmultiyear')
		document.getElementById(oldTool.id + 'Area').style.display = 'none';
	else {
		TABLELAYOUT.Table.className = 'financial';
		TABLELAYOUT.Table.rows[0].cells[5].style.display = 'none';
	}

	if (TABLELAYOUT.CurrentTool.id != 'tabletoolmultiyear')
		document.getElementById(TABLELAYOUT.CurrentTool.id + 'Area').style.display = 'block';
	else {
		if (TABLELAYOUT.CurrentRadioRow.style.display != 'none' && TABLELAYOUT.CurrentRadioRow.rowIndex != 0)
			TABLELAYOUT.InlineDiagram.style.display = 'block';
		if (isIE) {
			TABLELAYOUT.Table.className = 'financial multiyear';
			TABLELAYOUT.Table.rows[0].cells[5].style.display = 'block';
		}
		else {
			TABLELAYOUT.Table.className = 'financial multiyearNonIE';
			TABLELAYOUT.Table.rows[0].cells[5].style.display = 'table-cell';
		}
	}
};

