var lygi = 0;
var pID = new Array();
var arr = new Array();
function saka(id, name, link, parentID)
{
	this.id = id;
	this.name = name;
	this.link = link;
	this.parentID = parentID;
	this.print = 0;
	this.children = false;
	this.childrenCount = 0;
	this.open = 0;
	this.l = 0;
}

function medis(name)
{
	this.medisName = name;
	this.lapai = [];
}

medis.prototype.add = function(id, name, link, parentID)
{	
	leng = this.lapai.length;
	this.lapai[leng] = new saka(id, name, link, parentID);
	if (parentID != 0)
	{
		for (i=0; i<this.lapai.length; i++)
		{
			if (this.lapai[i].id == parentID)
			{
				this.lapai[i].children = true;
				this.lapai[i].childrenCount++;
			}
		}
	}
}

medis.prototype.writeNames = function()
{
	//document.write(this.medisName);
	for (var i=0; i<this.lapai.length; i++)
    {
    	document.write('-- ' + this.lapai[i].l + ' ' + this.lapai[i].name + '<br/>');
    }
}

medis.prototype.writeMenu = function()
{
  
  for (var i=0; i<this.lapai.length; i++)
  {
  	if (this.lapai[i].print == 0)
  	{
  		if (this.lapai[i].link != '')
  		  link = 'javascript:m.openLink('+this.lapai[i].id+')';
  		else
  		  link = 'javascript:m.open('+this.lapai[i].id+')';
  		document.write('<div class="left_link"><a href="'+link+'" id="ref'+this.lapai[i].id+'">'+this.lapai[i].name+'</a></div>');
  	}
  	if (this.lapai[i].children != false)
  	{
  		this.printChildren(this.lapai[i].id, 1);
  		//document.write('i='+i+'<br/>');
  	}
  }
  for (var z=0; z<this.lapai.length; z++)
  {
	value = this.readCookies(this.lapai[z].name);
	if (value)
	{
		this.lapai[z].open = 1;
		this.open(this.lapai[z].id);
	}
  }
}

medis.prototype.closeAll = function()
{
	for (var z=0; z<this.lapai.length; z++)
  {
	value = this.readCookies(this.lapai[z].name);
	if (value)
	{
		this.lapai[z].open = 0;
		this.close(this.lapai[z].id);
	}
  }
}

function margin(count)
{
	var marg = '';
	for (i=0; i<count; i++)
	{
		marg = marg + 'sub';
	}
	if (count > 2)
	{
		marg = 'subsub';
	}
	return marg;
}

medis.prototype.printChildren = function(id, lygis)
{
  for (var j=0; j<this.lapai.length; j++)
  {
  	if (this.lapai[j].parentID == id)
  	{
  		if (this.lapai[j].print == 0)
  		{
  			//document.write(margin(lygis));
  			sub = margin(lygis);
  			cl = 'left_sublink';
  			if (this.lapai[j].link != '')
  			{
  			  //link = this.lapai[j].link;
  			  link = 'javascript:m.openLink('+this.lapai[j].id+')';
  			  //click = 'onclick="javascript:m.openLink('+this.lapai[j].id+')"';
  			}
  			else
  			{
  			  link = 'javascript:m.open('+this.lapai[j].id+')';
  			  //click = 'onclick="javascript:m.open('+this.lapai[j].id+')"';
  			}
  			click = '';
  			document.write('<div onlick="" class="'+cl+'" id="obj'+this.lapai[j].id+'" style="visibility: hidden; position: absolute"><a href="'+link+'" id="ref'+this.lapai[j].id+'" '+click+'>'+this.lapai[j].name+'</a></div>');
  			this.lapai[j].l = lygis;
  			this.lapai[j].print = 1;
  		}
  		if (this.lapai[j].children != false)
  		{
  			sekantis = lygis + 1;
  			this.printChildren(this.lapai[j].id, sekantis);
  		}
  	}
  }
}

medis.prototype.lyg = function(id)
{
	var yra = 0;
	for (var k=0; k<this.lapai.length; k++)
	{
		if (this.lapai[k].id == id)
		{
			lygi++;
			if (this.lapai[k].parentID != 0)
			{
				pID = this.lapai[k].parentID;
				yra = 1;
			}
			else
			{
				yra = 0;
			}
		}
	}
	if (yra == 1)
	{
		this.lyg(pID);
	}

}

medis.prototype.lyg2 = function(id)
{
	//window.alert('id='+id);
	var yra = 0;
	var k = 0;
	for (k=0; k<this.lapai.length; k++)
	{
		if (this.lapai[k].id == id)
		{
			lygi++;
			arr[0] = this.lapai[k].parentID;
			//window.alert('pID='+arr[arr.length-1]+' - '+this.lapai[k].parentID+ ' = '+arr.length);
		}
	}
	for (m=0; m<this.lapai.length; m++)
	{
		
		if (this.lapai[m].id == arr[arr.length-1])
		{
			lygi++;
			arr[1] = this.lapai[m].parentID;
			//window.alert('pID='+arr[arr.length-1]+' - '+this.lapai[m].parentID+ ' = '+arr.length);
			if (this.lapai[m].parentID != 0)
			{
				
				yra = 1;
			}
			else
			{
				yra = 0;
			}
		}
	}
	for (n=0; n<this.lapai.length; n++)
	{
		if (this.lapai[n].id == arr[arr.length-1])
		{
			lygi++;
			arr[2] = this.lapai[n].parentID;
			//window.alert('pID='+arr[arr.length-1]+' - '+this.lapai[n].parentID+ ' = '+arr.length);
		}
	}
	if (yra == 1)
	{
	//	this.lyg2(arr[arr.length]);
	}

}
medis.prototype.open = function(id)
{
	var l = 0;
	var now = new Date();

	var tomorow = new Date(now.getTime() + 1000 * 60 * 60 * 24);
	for (var r=0; r<this.lapai.length; r++)
	{
		if (this.lapai[r].id == id)
		{
		//	if (this.lapai[r].open == 1)
		//	{
		//		this.lapai[r].open = 0;
		//		this.close(id);
		//	}
		//	else
		//	{
			var name = 'ref' + id;
	  	    var link = document.getElementById(name);
	  	    link.href="javascript:m.close("+id+")";
	  		
				this.lapai[r].open = 1;
			this.setCookies(this.lapai[r].name, id, tomorow);
		//	}
		}
	}
	for (var i=0; i<this.lapai.length; i++)
	{
	  if (this.lapai[i].parentID == id)
	  {
	  	var name = 'obj' + this.lapai[i].id;
	  	var elem = document.getElementById(name);
	  	elem.style.visibility = 'visible';
	  	elem.style.position = 'static';
	  		mar = (this.lapai[i].l - 1) * 15;
	  		elem.style.padding = '3px '+mar+'px 0 0';
	  }	
	}
}
medis.prototype.close = function(id)
{
	var l = 0;
	var now = new Date();

	var tomorow = new Date(now.getTime() - 1000 * 60 * 60 * 24);
	for (var r=0; r<this.lapai.length; r++)
	{
		if (this.lapai[r].id == id)
		{
			var name = 'ref' + id;
	  	    var link = document.getElementById(name);
	  	    link.href="javascript:m.open("+id+")";
			this.setCookies(this.lapai[r].name, id, tomorow);
		}
	}
	closeAbsolute(id, this.lapai);
	/*for (var i=0; i<this.lapai.length; i++)
	{
	  if (this.lapai[i].parentID == id)
	  {
	  	var name = 'obj' + this.lapai[i].id;
	  	var elem = document.getElementById(name);
	  	elem.style.visibility = 'hidden';
	  	elem.style.position = 'absolute';
	  		mar = (this.lapai[i].l - 1) * 15;
	  		elem.style.padding = '3px '+mar+'px 0 0';
	  }	
	}*/
}

function closeAbsolute(id, lapai)
{
	for (var i=0; i<lapai.length; i++)
	{
	  if (lapai[i].parentID == id)
	  {
	  	var name = 'obj' + lapai[i].id;
	  	var elem = document.getElementById(name);
	  	elem.style.visibility = 'hidden';
	  	elem.style.position = 'absolute';
	  	mar = (lapai[i].l - 1) * 15;
	  	elem.style.padding = '3px '+mar+'px 0 0';
	  	closeAbsolute(lapai[i].id, lapai);
	  	var nameLink = 'ref' + lapai[i].id;
	  	var link = document.getElementById(nameLink);
	  	if (lapai[i].link != '')
	  	{
	  		link.href="javascript:m.openLink("+lapai[i].id+")";
	  	}
	  	else
	  	{
	  	  link.href="javascript:m.open("+lapai[i].id+")";
	  	}
	  }	
	}
}

medis.prototype.writePID = function(te)
{
	var text = '';
	for (i=0; i<te.length; i++)
	{
		text = text + '-' + te[i] + '-';
	}
	window.alert(arr.length);
}

medis.prototype.openLink = function(id)
{

	ids = new Array();
	for (i=0; i<this.lapai.length; i++)
	{

		if (id == this.lapai[i].id)
		  lapoPID = this.lapai[i].parentID;
	}
	this.lyg2(lapoPID);
	//this.writePID(arr);
	ids = pID;
	t = 'kiekis' + arr.length;
    //window.alert(t);
	for (i=0; i<this.lapai.length; i++)
	{
		this.clearCookie(this.lapai[i].name, this.lapai[i].id);
	}
	var now = new Date();

	var yesterday = new Date(now.getTime() + 1000 * 60 * 60 * 24);
	for (j=0; j<arr.length; j++)
	{
		//document.write('veikia');
		for (i=0; i<this.lapai.length; i++)
	    {
	    	if (arr[j] == this.lapai[i].id)
	    	{
		   this.setCookies(this.lapai[i].name, this.lapai[i].id, yesterday);
		   }
		   if (lapoPID == this.lapai[i].id)
		   {
		   	this.setCookies(this.lapai[i].name, this.lapai[i].id, yesterday);
		   }
	    }
	}
	for (l=0; l<pID.length; l++)
	{
		pID.splice(i,'');
	}
	for (i=0; i<this.lapai.length; i++)
	{
		if (id == this.lapai[i].id)
		{
			li = this.lapai[i].link;
		}
	}
	location.href=li;
	for (var z=0; z<this.lapai.length; z++)
  {
	value = this.readCookies(this.lapai[z].name);
	if (value)
	{
		this.lapai[z].open = 1;
		this.open(this.lapai[z].id);
	}
  }
}


medis.prototype.clearCookie = function(cookieName, cookieID) {

	var now = new Date();

	var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);

	this.setCookies(cookieName, cookieID, yesterday);

};

medis.prototype.setCookies = function(cookieName, cookieID, expires)
{
	document.cookie = escape(cookieName) + '=' + escape(cookieID)
	+ (expires ? '; expires=' + expires.toGMTString() : '');
}

medis.prototype.readCookies = function(cookieName)
{
	var cookieValue = '';
	var posName = document.cookie.indexOf(escape(cookieName) + '=');
	if (posName != -1) {
		var posValue = posName + (escape(cookieName) + '=').length;
		var endPos = document.cookie.indexOf(';', posValue);
		if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
		else cookieValue = unescape(document.cookie.substring(posValue));
	}
	return (cookieValue);
}

