var Contaner
var TB
var CRIndex = -1;
var CRId = -1;
var Level = 1;
var LayerColor = new Array("#000000", "#0041C6", "#000099", "#0000aa", "#0000bb", "#0000cc")
function Init()
{
	/*
	Contaner = document.getElementById("tblDars")
	CreateTable()
	*/
}
function CreateTable()
{
	var i;
	var Table = document.createElement("TABLE");
	Table.border = 0;
	TB = document.createElement("TBODY");
	Table.appendChild(TB)
	Contaner.appendChild(Table)
	for(i = 0; i < LessonInfo.length; i++)
	{
		if(LessonInfo[i].ParentCode == -1)
		{
			var Row = document.createElement("TR");
			Row.id = LessonInfo[i].Id
			Row.name = "1"
			
			var Cel = document.createElement("TD");
			Cel.innerText = LessonInfo[i].Name;
			Cel.style.fontFamily = "koodak,tahoma";
			Cel.style.fontSize = "14";
			Cel.style.color = LayerColor[0];
			Cel.align = "right";
			Cel.onmouseover = BtnOver;
			Cel.onclick = ShowRows;
			Row.appendChild(Cel)
		}
		TB.appendChild(Row)
	}
}
function BtnOver()
{
	var CE = event.srcElement;
	CE.style.cursor = "hand";
}
function ShowRows()
{
	var Table = TB.parentElement
	if(CRIndex == -1)
	{
		CRIndex = this.parentElement.rowIndex
		CRId = this.parentElement.id
		ShowNewPart()
	}
	else
	{
		if(CRIndex == this.parentElement.rowIndex)
		{
			var MainRow = document.getElementById(this.parentElement.id);
			var NewRow = MainRow.nextSibling;
			if(NewRow)
			{
				if(parseInt(NewRow.name) > parseInt(MainRow.name))
				{
					CRIndex = this.parentElement.rowIndex
					CRId = this.parentElement.id
					var MainRow = document.getElementById(CRId);
					var NewRow = MainRow.nextSibling;
					while(NewRow && parseInt(NewRow.name) > parseInt(MainRow.name))
					{
						Table.deleteRow(CRIndex + 1)
						NewRow = MainRow.nextSibling;
					}
//					HidNewPart()
					CRIndex = MainRow.rowIndex
					CRId = MainRow.id
				}
				else
				{
					CRIndex = this.parentElement.rowIndex
					CRId = this.parentElement.id
					ShowNewPart()
				}
			}
			else
			{
				CRIndex = this.parentElement.rowIndex
				CRId = this.parentElement.id
				ShowNewPart()
			}
			
		}
		else
		{
			if(HasSubTitle(this.parentElement.id))
			{
				var MainRow = document.getElementById(this.parentElement.id);
				var NewRow = MainRow.nextSibling;
				if(NewRow)
				{
					CRIndex = this.parentElement.rowIndex
					CRId = this.parentElement.id
					if(parseInt(NewRow.name) > parseInt(MainRow.name))
					{
						var MainRow = document.getElementById(CRId);
						var NewRow = MainRow.nextSibling;
						while(NewRow && parseInt(NewRow.name) > parseInt(MainRow.name))
						{
							Table.deleteRow(CRIndex + 1)
							NewRow = MainRow.nextSibling;
						}
					}
					else
					{
						ShowNewPart()
					}
				}
				else
				{
						CRIndex = this.parentElement.rowIndex
						CRId = this.parentElement.id
						ShowNewPart()
				}
				
			}
		}
	}
}
function GetFirstElementInALevel(Obj)
{
	alert("Mostafa = "+Obj)
}
function SubTitle(TCode, STCode)
{
	var i;
	for(i = 0; i < LessonInfo.length; i++)
		if(LessonInfo[i].Id == STCode)
			if(LessonInfo[i].ParentCode == TCode)
				return true;
	return false
}
function GetSpace(Layer)
{
	var i
	var Temp = "";
	for(i = 0; i < parseInt(Layer) * 4; i++)
		Temp += "&nbsp;&nbsp;"
	return Temp;
}
function HasSubTitle(TitleCode)
{
	var i;
	for(i = 0; i < LessonInfo.length; i++)
		if(LessonInfo[i].ParentCode == TitleCode)
			return true
	return false
}
function HidNewPart()
{
	var i = 0;
	var MainRow = document.getElementById(CRId);
	var Temp = SubTitleNumber(CRId)
	var Table = TB.parentElement	
	while(i < Temp)
	{
		Table.deleteRow(CRIndex + 1)
		i++
	}
}
function SubTitleNumber(TitleCode)
{
	var i, j = 0;
	for(i = 0; i < LessonInfo.length; i++)
	{
//		alert(LessonInfo[i].ParentCode +" = "+ parseInt(TitleCode))
		if(LessonInfo[i].ParentCode == parseInt(TitleCode))
			j++
	}
	return j
}
function ShowNewPart()
{
	var i;
	var MainRow = document.getElementById(CRId);
	var CRow = MainRow.name
	var Table = TB.parentElement
	for(i = 0; i < LessonInfo.length; i++)
	{
		if(LessonInfo[i].ParentCode == CRId)
		{
			var Row = Table.insertRow(CRIndex + 1)
			Row.id = LessonInfo[i].Id
			Row.name = parseInt(CRow) + 1
			
			Cel = Row.insertCell();
			Cel.innerHTML = GetSpace(parseInt(CRow) + 1) + LessonInfo[i].Name;
			Cel.style.fontFamily = "koodak , tahoma";
			Cel.style.fontSize = "14";
			Cel.style.color = LayerColor[parseInt(Row.name) - 1];
			Cel.align = "right";
			Cel.onmouseover = BtnOver;
			Cel.onclick = ShowRows;
		}
		
	}
}
function Record(NewId, NewName, NewParentCode, NewAddress)
{
	this.Id = NewId;
	this.Name = NewName;
	this.ParentCode = NewParentCode;
	this.Address = NewAddress;
}