/**
*Tabletreemanager script
*
* Jukka Mäkihannu
*/
//titleCell object
function titleCell(rowID, cellID, hide, open)
{
	this.rowID = rowID;
	this.cellID = cellID;
	this.childs = new Array();
	this.pCell = null;
	this.hide = hide;
	this.open = open;
};

//bodyRow object
function bodyRow(id, pid, type, group, image, open, openChilds, rowIndex,bottom)
{
	this.id = id;
	this.pid = pid;
	this.pRow = null;
	this.childs = new Array();
	this.type = type;
	this.group = group;
	this.image = image;
	this.open = open;
	this.openChilds = openChilds;
	this.rowID = rowIndex;
	this.bottom = bottom;
};

//table object
function tableTree(objName){
	this.icon = {
		root                : 'images/dtree/base.gif',
        empty               : 'images/navigation/empty18.gif',
        line                : 'images/tTree/line.gif',
        join                : 'images/tTree/join.gif',
        joinBottom  		: 'images/tTree/joinbottom.gif',
        plus                : 'images/tTree/plus.gif',
        plusBottom  		: 'images/tTree/plusbottom.gif',
        minus               : 'images/tTree/minus.gif',
        minusBottom 		: 'images/tTree/minusbottom.gif'
    }
	
	this.cssClass = {
		l 		:	"light", 
		b 		:	"black", 
		sB 		:	"selBlack", 
		sL 		:	"selLight", 
		n 		:	"name", 
		sN 		:	"selName"
	}
	
	//this.name = objName;
	this.tableObj = document.getElementById(objName);
    this.aTitleCell = [];
    this.aBodyRow = [];
	this.titleCO = true;
	this.titleRows = 0;
};

//add title cells to tabletree
tableTree.prototype.addTitle = function(rowID,cellID, pCell, hide, open){
	this.aTitleCell[this.aTitleCell.length] = new titleCell(rowID, cellID, hide, open);
	if(pCell >= 0 && pCell != "")this.setParentCell((this.aTitleCell.length-1),pCell);
};

//set parent cell from upper row... cellID is parentCellID
tableTree.prototype.setParentCell = function(objID,cellID)
{
	for(var i=cellID;i>=0;i--)
	{
		if(this.aTitleCell[i].rowID == (this.aTitleCell[objID].rowID - 1) && this.aTitleCell[i].cellID == cellID)
		{
			//set parent cell to title object
			this.aTitleCell[objID].pCell = this.aTitleCell[i];
			//set title object to parents child array
			this.aTitleCell[i].childs.push(this.aTitleCell[objID]);
		}
	}
};
//add title cells to tabletree
tableTree.prototype.addRow = function(id, pid, type, group, image, open, openChilds, rowIndex,bottom){
	this.aBodyRow[this.aBodyRow.length] = new bodyRow(id, pid, type, group, image, open, openChilds, rowIndex,bottom);
	if(pid != 0)this.setParentRow((this.aBodyRow.length-1), pid);
};


//set parent object to row
tableTree.prototype.setParentRow = function(objID, pid){
	for(var i=(objID -1);i>=0;i--)
	{
		if(this.aBodyRow[i].id == pid)
		{
			//set parent cell to title object
			this.aBodyRow[objID].pRow = this.aBodyRow[i];
			//set title object to parents child array
			this.aBodyRow[i].childs.push(this.aBodyRow[objID]);
		}
	}
}

/*
cell show hide functions
*/
tableTree.prototype.oTitle = function(id)
{
	this.showHideCell(this.aTitleCell[id]);
};


tableTree.prototype.showHideCell = function(cell){
	this.titleCO = cell.open;
	this.cellState(cell);
	if(cell.pCell != null)
		this.titleParent(cell.pCell);
};

//change title cells state.. open or close
tableTree.prototype.cellState = function(cell){
	cell.open = !this.titleCO;
	var divObj = this.tableObj.rows[cell.rowID].cells[cell.cellID].getElementsByTagName('DIV');
	divObj[0].style.display = (this.titleCO)?"none":"";	
	if(cell.childs.length > 1)
	{
		for(var j=0;j<cell.childs.length;j++){
			this.cellState(cell.childs[j]);
		}
	}
	else
	{
		for(var i=(cell.rowID + 1);i<this.tableObj.rows.length;i++){
			divObj = this.tableObj.rows[i].cells[cell.cellID].getElementsByTagName('DIV');
			divObj[0].style.display = (this.titleCO)?"none":"";
		}
	}
};

//check is parents all childs closed
tableTree.prototype.titleParent = function(cell){
	bCheck = 0;
	for(i=0;i<cell.childs.length;i++)
	{
		if(!cell.childs[i].open)
			bCheck++;
	}
	if(bCheck == cell.childs.length)
	{
		divObj = this.tableObj.rows[cell.rowID].cells[cell.cellID].getElementsByTagName('DIV');
		divObj[0].style.display = "none";
		cell.open = false;
	}
	else
	{
		divObj = this.tableObj.rows[cell.rowID].cells[cell.cellID].getElementsByTagName('DIV');
		divObj[0].style.display = "";
		cell.open = true;
	}
	
}

tableTree.prototype.closeTitle = function(cell)
{
	var divObj = this.tableObj.rows[cell.rowID].cells[cell.cellID].getElementsByTagName('DIV');
	divObj[0].style.display = "none";
	cell.open = false;
	for(i=(cell.rowID + 1);i<this.tableObj.rows.length;i++){
		for(j=0;j<cell.childs.length;j++){
			divObj = this.tableObj.rows[i].cells[cell.childs[j]].getElementsByTagName('DIV');
			divObj[0].style.display = "none";
		}
	}
};

/*
row show hide functions
*/
tableTree.prototype.oRow = function(id)
{
	tmp = parseInt(id);
	this.showHideRow(this.aBodyRow[tmp]);
	this.resetGroupColor();
	this.changeGroupColor(this.aBodyRow[tmp]);
	
	//update cookie
	this.updateCookie();
	
};

//
tableTree.prototype.showHideRow = function(row){
	this.rowChilds(row);
	for(var i=0;i<row.childs.length;i++)
	{
		if(row.childs[i].openChilds)
			this.showHideRow(row.childs[i]);
			
		this.setVisible(row.childs[i]);
	}
};

//change openChilds and showhide picture
tableTree.prototype.rowChilds = function(row){
	row.openChilds  = !row.openChilds;
	imgObj = this.tableObj.rows[row.rowID].cells[0].getElementsByTagName('IMG');
	for(var i=0;i<(imgObj.length-1);i++)
	{
		if(imgObj[i].src.substr((imgObj[i].src.length - this.icon.empty.length)) != this.icon.empty 
		&& imgObj[i].src.substr((imgObj[i].src.length - this.icon.join.length)) != this.icon.join 
		&& imgObj[i].src.substr((imgObj[i].src.length - this.icon.joinBottom.length)) != this.icon.joinBottom
		&& imgObj[i].src.substr((imgObj[i].src.length - this.icon.line.length)) != this.icon.line)
			var oImg = imgObj[i];
	}
	if(oImg)
	{
		if(row.openChilds)
			oImg.src = (row.bottom)?this.icon.minusBottom:this.icon.minus;
		else
			oImg.src = (row.bottom)?this.icon.plusBottom:this.icon.plus;
	}
};

//change rows display 
tableTree.prototype.setVisible = function(row){
	this.tableObj.rows[row.rowID].style.display = (!row.open)?"":"none";
	row.open = !row.open;
};
	
//change group color
tableTree.prototype.changeGroupColor = function(row){
	var tR = this.tableObj.rows[row.rowID];
	for(var i=0;i<tR.cells.length;i++){
		if(tR.cells[i].className == this.cssClass.l) tR.cells[i].className = this.cssClass.sL;
		if(tR.cells[i].className == this.cssClass.b) tR.cells[i].className = this.cssClass.sB;
		if(tR.cells[i].className == this.cssClass.n) tR.cells[i].className = this.cssClass.sN;
	}
	
	for(var i=0;i<row.childs.length;i++)
	{
		var tR = this.tableObj.rows[row.childs[i].rowID];
		for(var j=0;j<tR.cells.length;j++)
		{
			if(tR.cells[j].className == this.cssClass.l) tR.cells[j].className = this.cssClass.sL;
			if(tR.cells[j].className == this.cssClass.b) tR.cells[j].className = this.cssClass.sB;
			if(tR.cells[j].className == this.cssClass.n) tR.cells[j].className = this.cssClass.sN;
		}
		if(row.childs[i].childs.length > 1)
			this.changeGroupColor(row.childs[i]);
	}
};

//change row colors to default
tableTree.prototype.resetGroupColor = function()
{
	for(i=0;i<this.tableObj.rows.length;i++)
	{
		for(j=0;j<this.tableObj.rows[i].cells.length;j++)
		{
			if(this.tableObj.rows[i].cells[j].className == this.cssClass.sL) this.tableObj.rows[i].cells[j].className = this.cssClass.l;
			if(this.tableObj.rows[i].cells[j].className == this.cssClass.sB) this.tableObj.rows[i].cells[j].className = this.cssClass.b;
			if(this.tableObj.rows[i].cells[j].className == this.cssClass.sN) this.tableObj.rows[i].cells[j].className = this.cssClass.n;
		}
	}
};

//open or close all
tableTree.prototype.oAll = function(eve)
{
	
	for(var i=0;i<this.aBodyRow.length;i++)
	{
		if(this.aBodyRow[i].pid != 0)
		{
			this.aBodyRow[i].open = (eve == "open")?true:false;
			this.tableObj.rows[this.aBodyRow[i].rowID].style.display = (eve == "open")?"":"none";
		}
		this.aBodyRow[i].openChilds = (eve == "open")?true:false;
		imgObj = this.tableObj.rows[this.aBodyRow[i].rowID].cells[0].getElementsByTagName('IMG');
		
		for(var j=0;j<(imgObj.length-1);j++)
		{
			if(imgObj[j].src.substr((imgObj[j].src.length - this.icon.empty.length)) != this.icon.empty 
			&& imgObj[j].src.substr((imgObj[j].src.length - this.icon.join.length)) != this.icon.join 
			&& imgObj[j].src.substr((imgObj[j].src.length - this.icon.joinBottom.length)) != this.icon.joinBottom 
			&& imgObj[j].src.substr((imgObj[j].src.length - this.icon.line.length)) != this.icon.line)
				var oImg = imgObj[j];
		}
		if(oImg)
		{
			if(eve == "open")
			{
				if(this.aBodyRow[i].bottom)
					oImg.src = this.icon.minusBottom;
				else
					oImg.src = this.icon.minus;
			}
			else
			{
				if(this.aBodyRow[i].bottom)
					oImg.src = this.icon.plusBottom;
				else
					oImg.src = this.icon.plus;
			}
		}
		
		oImg = null;
			//oImg.src = (eve == "open")?(this.aBodyRow[i].bottom)?this.icon.minusBottom:this.icon.minus:(this.aBodyRow[i].bottom)?this.icon.plusBottom:this.icon.plus;
		//imgObj[0].src = (eve == "open")?this.icon.minus:this.icon.plus;
	}
	
	this.resetGroupColor();
	//update cookie
	this.updateCookie();
};

// cookie functions
tableTree.prototype.updateCookie = function()
{
	var strCookieValue = "";
	for(var i=0;i<this.aBodyRow.length;i++)
	{
		strCookieValue += this.aBodyRow[i].id+"_"+this.aBodyRow[i].open+"_"+this.aBodyRow[i].openChilds+"#"
	}
	strCookieValue += "; path=/";
	document.cookie = "tableTreeCookie="+strCookieValue;
	// alert(document.cookie);
}

tableTree.prototype.readCookie = function()
{	
	var arrCookie = document.cookie.split(";");
	
	for(var i=0;i < arrCookie.length;i++)
	{
		var strCookie = arrCookie[i];

		if(strCookie.indexOf("tableTreeCookie") != -1)
		{	
			var arrStrCookieValue = strCookie.split('=');
			
			return arrStrCookieValue[1];
		}
	}
	return null;
};	

tableTree.prototype.clearCookie = function()
{
	var now = new Date();
    var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
    document.cookie = "tableTreeCookie=cookieValue; expires=" + yesterday +"; path=/";
};

tableTree.prototype.test = function()
{
	strCookieValue = this.readCookie();
	alert(strCookieValue); //ok
}

tableTree.prototype.readCookieToTable = function()
{
	strCookieValue = this.readCookie();

	if(strCookieValue != "cookieValue" && strCookieValue != null)
	{
		if(this.checkRoot(strCookieValue))
		{
			arrNodes = strCookieValue.split("#");
			var cookieEx = false;
			for(var i=0;i<arrNodes.length;i++)
			{	
				// alert(arrNodes[i]);
				arrNodeValues = arrNodes[i].split("_");
				for(var j=0;j<this.aBodyRow.length;j++)
				{
					if(arrNodeValues[0] == this.aBodyRow[j].id)
					{
						// alert(this.aBodyRow[j].rowIndex);
						this.tableObj.rows[this.aBodyRow[j].rowID].style.display = (arrNodeValues[1] == "true")?"":"none";
						this.aBodyRow[j].open = (arrNodeValues[1] == "true")?true:false;
						imgObj = this.tableObj.rows[this.aBodyRow[j].rowID].cells[0].getElementsByTagName('IMG');
							
						for(var k=0;k<(imgObj.length-1);k++)
						{
							if(imgObj[k].src.substr((imgObj[k].src.length - this.icon.empty.length)) != this.icon.empty 
							&& imgObj[k].src.substr((imgObj[k].src.length - this.icon.join.length)) != this.icon.join 
							&& imgObj[k].src.substr((imgObj[k].src.length - this.icon.joinBottom.length)) != this.icon.joinBottom 
							&& imgObj[k].src.substr((imgObj[k].src.length - this.icon.line.length)) != this.icon.line)
								var oImg = imgObj[k];
						}
						
						if(oImg)
						{	
							if(arrNodeValues[2] == "true")
							{
								if(this.aBodyRow[j].bottom)
									oImg.src = this.icon.minusBottom;
								else
									oImg.src = this.icon.minus;
							}
							else
							{
								if(this.aBodyRow[j].bottom)
									oImg.src = this.icon.plusBottom;
								else
									oImg.src = this.icon.plus;
							}
						}
						this.aBodyRow[j].openChilds = (arrNodeValues[2] == "true")?true:false;
						oImg = null;
						j = this.aBodyRow.length;
					}	
				}
			}
		}
	}
};

tableTree.prototype.checkRoot = function(cookieValue)
{
	arrNodes = cookieValue.split("#");
	arrNodeValues = arrNodes[0].split("_");
	if(arrNodeValues[0] == this.aBodyRow[0].id)
		return true;
	else
	{
		this.clearCookie();
		return false;
	}
};

var tableid = false;
var showExpenses = true;
var expenseFunc = false;

function changeShowExpenses(inputObj)
{
	showExpenses = inputObj.checked;
	hideAll();
}

function showAll(tabid)
{
	divObj = document.getElementById('summary');
	tableObj = divObj.getElementsByTagName('tbody');
	table = tableObj[0];
	length = table.rows.length;
	
	for(i = 0; i < length; i++)
	{
		if(table.rows[i].getAttribute('layer') != 0)
		{	
			table.rows[i].style.display = '';
		}
	}
	var pics = table.getElementsByTagName('img');
	for(i = 0; i < pics.length; i++)
	{
		if(pics[i].src.substr(pics[i].src.length - 8) == 'plus.gif')
		{
			pics[i].src = 'images/navigation/minus.gif';
		}
	}
}

function hideAll()
{
	divObj = document.getElementById('summary');
	tableObj = divObj.getElementsByTagName('tbody');
	table = tableObj[0];
	length = table.rows.length;
	
	for(i = 0; i < length; i++)
	{
		if(table.rows[i].getAttribute('layer') != 0)
		{	
			table.rows[i].style.display = 'none';
		}
	}
	var pics = table.getElementsByTagName('img');
	for(i = 0; i < pics.length; i++)
	{
		if(pics[i].src.substr(pics[i].src.length - 9) == 'minus.gif')
		{
			pics[i].src = 'images/navigation/plus.gif';
		}
	}
}

function showInfo(imgObj)
{	
	closeAllFootnotes();
	var divObj = document.getElementById('summary');
	var divs = divObj.getElementsByTagName('DIV');
		
	for(i=0;i<divs.length;i++)
	{
		
		if(divs[i].className == 'expenseInfo' && divs[i].id == imgObj.id)
		{
			var tmp = findElementCoordinates(imgObj);
			divs[i].style.display = 'block';
			
			imgObj.className = "expInfoImgBlur";
			
			divs[i].style.left = tmp[0] - (divs[i].offsetWidth - 4);
			divs[i].style.top = tmp[1] + 4;
		}
	}
}

function closeInfo(divObj)
{
	var table = document.getElementById('summary');
	var imgs = table.getElementsByTagName('IMG');
	for(i=0;i<imgs.length;i++)
	{
		if(imgs[i].className == 'expInfoImgBlur' && imgs[i].id == divObj.id && imgs[i].src.substr(imgs[i].src.length - 8) == 'info.gif')
		{	
			imgs[i].className = "expInfoImg";
		}
	}
	divObj.style.display = 'none';
}

function closeAllFootnotes()
{
	var table = document.getElementById('summary');
	var divs = table.getElementsByTagName('DIV');
	for(i=0;i<divs.length;i++)
	{
		if(divs[i].className == 'expenseInfo')
		{
			divs[i].style.display = 'none';
		}
	}
	var imgs = table.getElementsByTagName('IMG');
	for(i=0;i<imgs.length;i++)
	{
		if(imgs[i].className == 'expInfoImgBlur' && imgs[i].src.substr(imgs[i].src.length - 8) == 'info.gif')
		{	
			imgs[i].className = "expInfoImg";
		}
	}
}

function findElementCoordinates(objElement)
{
	var arrCoordinates = new Array(2);
	var iXCoordinate = 0;
	var iYCoordinate = 0;
	if (objElement.offsetParent)
	{
		while (objElement.offsetParent)
		{
		iXCoordinate += objElement.offsetLeft;
		iYCoordinate += objElement.offsetTop;
		objElement = objElement.offsetParent;
		}
	}
	else if (objElement.x && objElement.y)
	{
		iXCoordinate += objElement.x;
		iYCoordinate += objElement.y;
	}

	arrCoordinates[0] = iXCoordinate;
	arrCoordinates[1] = iYCoordinate;

	return arrCoordinates;
}

