var ATR_RELOAD_NODE = false;//czy klikniecie na grupe ma powodowac odswiezenie strony
var ATR_TREE_EXPAND_ALL = true;
var globalIDPrev = 0;

function MenuItemAtr(divObj)
{
	this.divObj;
	this.lnkObj;
	this.url;
	this.id;
	this.idunique;
	this.idprev;
	this.imgOpen = "minus3.gif";
	this.imgClose = "plus3.gif";
	this.imgDefault = "last.gif";
	this.entityType = 0;
		
	this.Init = function()
	{		
		this.divObj = divObj;				
		this.divObj.style.display = "block";	
		this.id = this.divObj.id;
		var arr = this.id.split("-");
		
		this.idunique = arr[0].replace("div_","");
		this.entityType = arr[1];
		
		if(this.entityType == 0)
		{
			globalIDPrev = this.idunique;	
			this.idprev = 0;			
		}
		else
		{
			this.idprev = globalIDPrev;
		}
		this.lnkObj = document.getElementById("lnk_"+this.id.replace("div_",""));
		this.url = document.getElementById("hdn_"+this.id.replace("div_","")).value;
	}
	
	this.Visible = function()
	{
		if(this.divObj.style.display == "block")
		{
			this.divObj.style.display = "none";
		}
		else
		{
			this.divObj.style.display = "block";
		}
	}
	
	this.SetImage = function()
	{
		if(this.lnkObj.className == "ie")
		{
			return;			
		}
		if(this.lnkObj.className == "ip")
		{
			this.lnkObj.className = "im"
		}
		else
		{
			this.lnkObj.className = "ip"
		}
	}
	
	this.Load = function(displayMode, imageNo)
	{
		if(displayMode == 1)
		{
		    this.divObj.style.display = "block";
		}
		else
		{
		    this.divObj.style.display = "none";
		}
		
		if(imageNo == 1)
		{
			this.lnkObj.className = "im"
		}
		if(imageNo == 2)
		{
			this.lnkObj.className = "ip"
		}
	}
	
	this.Init();
}

function MenuAtr(idContener,idmark)
{
	this.contener = document.getElementById(idContener);
	this.arrItems = Array();
	this.name = idContener;
	this.externalFunctions = Array();//tablica funkcji zew, ktore maja byc uruchomione przed kliknieciem w node drzewa uruchamiający url (nie rozwijajacy drzewka)
	
	this.Init = function()
	{				
		if(this.contener == null)
		{
			return;
		}
		
		globalIDPrev = 0;
		
		for(var i = 0; i < this.contener.childNodes.length; i++)
		{
			var item = new MenuItemAtr(this.contener.childNodes[i]);
			this.arrItems.push(item);				
		}
		
		for(var i = 0; i < this.arrItems.length; i++)
		{			
			if(this.arrItems[i].idprev == 0)
			{									
				this.arrItems[i].divObj.onclick = new Function(this.name+'.ItemAction('+this.arrItems[i].idunique+',"'+this.arrItems[i].url+'")');			
			}
			else
			{
				this.arrItems[i].Visible();
				this.arrItems[i].divObj.onclick = new Function(this.name+'.GoUrl("'+this.arrItems[i].url+'")');
			}			
			
			this.arrItems[i].divObj.onmouseover = function(){this.className = "dmin"};
			this.arrItems[i].divObj.onmouseout = function(){this.className = "dm"};
						
			if(this.arrItems[i].identity == idmark)	
			{								
				this.arrItems[i].lnkObj.className = "ie asel";
			}
		}
		
		if(ATR_TREE_EXPAND_ALL)
		{
			for(var i = 0; i < this.arrItems.length; i++)
			{
				this.Drill(this.arrItems[i].idunique);
			}
			this.Save();
		}
	}
	
	this.ItemAction = function(idprev,url)
	{
		this.Drill(idprev);
		this.Save();
		if(ATR_RELOAD_NODE)
		{
			this.GoUrl(url);
		}		
	}
	
	this.GoUrl = function(url)
	{		
		this.RunExternalFunctions();
		location.href = url;
	}
	
	this.Drill = function(idprev)
	{
		for(var i = 0; i < this.arrItems.length; i++)
		{
			if(this.arrItems[i].idunique == idprev)
			{
				this.arrItems[i].SetImage();
			}
			
			if(this.arrItems[i].idprev == idprev)
			{
				this.arrItems[i].SetImage();
				this.arrItems[i].Visible();								
			}		
		}
	}
	
	this.Save = function()
	{		
		var tmp="";
		for(var i = 0; i < this.arrItems.length; i++)
		{
			if(this.arrItems[i].divObj.style.display != "block")
			{
				continue;
			}		
			
			tmp += this.arrItems[i].idunique;
			tmp += ",1,";//block
					
			if(this.arrItems[i].lnkObj.className == "im")
			{
				tmp += "1";//minus3.gif
			}
			else
			{							
			    if(this.arrItems[i].lnkObj.className == "ip")
			    {
				    tmp += "2";//plus3.gif
			    }
			    else
			    {
				    tmp += "0";//last.gif
			    }	
			}
			tmp += "|";
		}
		
		tmp = tmp.substring(0,tmp.length-1);
		SetCookie(this.name,tmp);
	}
	
	this.GetItem = function(idunique)
	{
	    for(var i = 0; i < this.arrItems.length; i++)
	    {	       
			if(this.arrItems[i].idunique == idunique)
	        {
				return this.arrItems[i];
	        }
	    }
	    return null;
	}
	
	this.Load = function()
	{	    
		if(GetCookie(this.name) == null)	
	    {
	        return false;
	    }
		
		var tmp = GetCookie(this.name);
		var arr = Array();
		arr = tmp.split("|");
				
		for(var i = 0; i < arr.length; i++)
		{
			var arr2 = Array();
			arr2 = arr[i].split(",");
			try
			{
				this.GetItem(arr2[0]).Load(arr2[1],arr2[2]);				
			}
			catch(e){}
		}
	}
	
	this.SetExternalFunctions = function()
	{
		this.externalFunctions.push("DeleteSearchState()");
	}
	
	this.RunExternalFunctions = function()
	{
	    for(var i = 0; i < this.externalFunctions.length; i++)
	    {			
	    	eval(this.externalFunctions[i]);
	    }		
	}	
		
	this.Init();
	this.Load();
	this.SetExternalFunctions();
}
