// Variable definitions
var images_home = "images/";
//----------------------------------------------
var offset; //offset to where write item of tree
var grafik = new Array(); //
var indexes = new Array();//
var expanded = ',';
var start;
var cnt = 0;

var currentLinkInx = 0;

//global array content all added menus independent your level
var ArrAllMenus = new Array();  
//global array content all added menus without parents,when add it explicit
//used for show tree function - showTreeExplicit
var ArrNoParentsMenus = new Array();  

//-----------------------------
// Object definition and Array's initialisation
function Item(text,UOID,parent,slink) {
	//alert(parent);
  this.text = text;
  this.UOID = UOID;
  this.parent = parent;  
  this.link = slink;  
  this.parentMenuIndex = -1;
  this.submenu = null;
  this.expanded = false;
  this.setSubmenu = setSubmenu;
  this.iscurrent = false;
}
//-----------------------------
//Get image name by type (used by item)

//-----------------------------
//item function to hang under level menu
function setSubmenu(menu,parentIndex) {
  this.submenu = menu;
  this.parentMenuIndex=parentIndex;
}
//-----------------------------
//Add item , if no exists menu to hang it menu created 
function AddItemExplicit(text,UOID,p_parent,slink)
{var	parentMenu,item,index,m1;
     tmpArr = null;  
     if (p_parent!=null )
		   tmpArr = GetMenuByUOID(p_parent)
		 	  
		 if ( tmpArr!= null ) //p_parent exists in added until now items
		 { 
         //Submenu items exists
				parentMenu=tmpArr[0];
				item=tmpArr[1];
				index=tmpArr[2];
				
			//alert(item.text);
			 if (item.submenu == null)//item have'nt menu 
					{				 
					 m1 =new Menu(''); 				 
					 item.submenu =m1;
	 				 item.setSubmenu(m1,index);
	         item.submenu.AddItem(text,UOID,p_parent,slink);
					 AddMenuToParent(p_parent,m1);
					}
				else //item have menu,only add item to it
					{
					 item.submenu.AddItem(text,UOID,p_parent,slink);				
					}
		 }
		 else
		 {
		  //alert('Parent with index ' + p_parent  + ' no found in all items')
		  //No submenu items in menu , it is situation when no parent exists
         m1 =new Menu(''); 
         m1.AddItem(text,UOID,p_parent,slink);  
         ArrNoParentsMenus[ArrNoParentsMenus.length] =  m1;       
     }    				    
}   


//-----------------------------
//Set current property to false for each hanged menu
function SetMenuCurrentFalse(lCurrMenuInx)
 {
    if (!ArrAllMenus[lCurrMenuInx]) return;
 		var menu=ArrAllMenus[lCurrMenuInx]; 		 		
	 	for (var i=0;i<menu.items.length;i++)
	 	{
	 	menu.items[i].iscurrent=false;	 	     
	 	}  	
 }	
 

//-----------------------------
//return menu, him hanged items and index as Array
function GetMenuByUOID(lUOID)
 { 
 	var item,menu;	
 	
 	for (var j=0;j<ArrAllMenus.length;j++)
 	{
 		menu=ArrAllMenus[j]; 		
	 	for (var i=0;i<menu.items.length;i++)
	 	{
	 	if (menu.items[i].UOID==lUOID)
	 	     return new Array(menu,menu.items[i],i);
	 	}  
	} 	
 	return null;
 }	
 
//set menu as current
function setCurrentMenu(lUOID)
{ 
 var tMenu=GetMenuByUOID(lUOID);	
 //alert(tMenu[1].iscurrent);
 if (tMenu!=null)
 {
 	 tMenu[1].iscurrent=true; 	
 	// alert(tMenu[1].UOID );
 }	
} 
//-----------------------------
//add menu to parent if parent exists in added list items
function AddMenuToParent(lParent,menu)
{var tmpArr,parentMenu,item,index
	
 	 tmpArr=GetMenuByUOID(lParent);
 	 if (tmpArr!=null) 
 	 {
 	 	parentMenu=tmpArr[0];
 	 	item=tmpArr[1];
 	 	index=tmpArr[2];
 	 	parentMenu.setDownLevel(menu,index);
 	 }	
 	  
}	

//-----------------------------
//Menu function - add item to menu
function AddItem(text,UOID,parent,slink)
 {var item
    item = new Item(text,UOID,parent,slink);
    this.items[this.items.length] = item;    
 }	
//-----------------------------
//Menu function  - add downlevel menu to hanged items 
function setDownLevel(menu,parentIndex) 
{
	this.items[parentIndex].setSubmenu(menu,parentIndex);
}
//-----------------------------
//Constructor for Menu
function Menu(text) {
var localArr 
  this.text = text;  
  this.items = new Array();
  this.add = add;
  this.AddItem = AddItem;
  this.setDownLevel = setDownLevel;
  if (ArrAllMenus.length == 0 )
   ArrAllMenus[0] = this; 
  else
  {
  localArr = new Array(this);
  ArrAllMenus =  ArrAllMenus.concat(localArr); 	
  }
}
//-----------------------------
//Menu function  - add item to Menu
function add(item) {
  this.items[this.items.length] = item;
}
//-----------------------------
//Show tree begining with menu without parents(if added explicit)
function showTreeExplicit()
{
	for (var i=0;i<ArrNoParentsMenus.length;i++)
	{
		showTree(ArrNoParentsMenus[i],0,1000);
	}	
}

//-----------------------------
// Show tree structure - main function to show tree
function showTree(menu, begin, end) {
  offset = 0;
  theTree = '';
  if (begin >= menu.items.length) return false;
  if (!end || (end >= menu.items.length)) end = menu.items.length;
  if (begin==0) 
  {
    if (menu.text) 
    {     
      document.write('<TR><TD COLSPAN=2>');
	      document.write(menu.text);
      document.write('</TD></TR>');
    }
    //document.write('<INPUT type=HIDDEN name=expanded value="'+expanded+'">');
  }

  showContent(menu, begin, end);

  if (end == menu.items.length) 
  	{
		//    var end = new Date();
		//    document.write('Generation time CLIENT SIDE(JScript): '+(end-start)+' msec<br>');
  	}
  return false;
}
//-----------------------------
//show inner content of tree 
function showContent(menu, begin, end) {
  for (var i = begin; i < end; i++) {
   indexes[offset] = i;
   showStruct(menu.items[i], (i == menu.items.length - 1));
  }
}

//-----------------------------
//show struct of item
function showStruct(item, last) { 
  var i, img, display;
  document.write('<TR><TD>');
  for (i = 0; i < offset; i++) 
  {
    if (grafik[i])
      document.write('\t<IMG SRC="'+images_home+'icon_stem.gif" class=tico ALIGN=Left>');
    else
      document.write('\t<IMG SRC="'+images_home+'icon_nothing.gif" class=tico ALIGN=Left>');
  }
   
  if (!item.submenu) 
	 {
	    if (item.text.length > 23)
	     document.write( '\t<img src="'+images_home+'icon_item_l' + (last?'last_item':'') + '.gif" class=tico ALIGN=Left>');
	    else	 	
	      document.write( '\t<img src="'+images_home+'icon_' + (last?'last':'') + 'item.gif" class=tico ALIGN=Left>');
	 } 
  else 
 	{
    if (expanded.indexOf(item.UOID) == -1) 
	    {
	      display='style="display:none" ';
	      img = "plus";
    	} 
    else 
    	{
	      display='';
	      img = "minus";
	    }
    if (last) img += "l";
    document.write('\t<IMG NAME=i_'+item.UOID+' SRC="'+images_home+'icon_' + img + '.gif'+
                   '" style="cursor:hand;" onClick="expcol(\''+item.UOID+'\','+last+');" class=tico ALIGN=Left>');
  }  
  var mOut,mOver,sLinkcolor
  if (item.iscurrent)
  { //alert(item.iscurrent);
    currentLinkInx = document.links.length;
  	sLinkcolor="color:white;background:#a9a9a9;";
    mOut =  "this.style.color='white';this.style.backgroundColor='#a9a9a9';";
  }  
  else
  {
    mOut =  "this.style.color='white';this.style.backgroundColor='#FC8E00';";
    sLinkcolor="color:white;background:#FC8E00;";
  }
  mOver = "this.style.color='white';this.style.backgroundColor='#d3d3d3';";
    
   mMytest = "SetOtherCurrent(" + document.links.length + ");"; 
  //mMytest = "alert(document.links[" + document.links.length + "].name)";
  //mMytest = "document.links[0].style.color='green';"
  //mMytest = "alert(document.links[0].style.backgroundColor);"    
  //mMytest = "alert(this.style.backgroundColor);"  
  //"' + item.link + '" 
  //document.write('<a OnClick="'+ mMytest +'"  name="' + 'alink-'+document.links.length + '" target="_self" onmouseover="' + mOver + '" onmouseout="' + mOut + '"  href="' + item.link + '" style="' + sLinkcolor + '" class="tree">'+ item.text + '</a>');
  document.write('<a name="' + 'alink-'+document.links.length + '" target="_self" onmouseover="' + mOver + '" onmouseout="' + mOut + '"  href="' + item.link + '" style="' + sLinkcolor + '" class="tree">'+ item.text + '</a>');
  document.write('</TD><TD align=right nowrap>');
  document.write('</TD></TR>');

  // Write children nodes
  if (item.submenu)  
  {
    if (!last) grafik[offset] = true;
    else grafik[offset] = false;

    offset++;    
    document.write('<TR><TD COLSPAN=2><DIV '+display+'id=d_'+item.UOID+'><TABLE cellPadding=0 cellSpacing=0 width=100%>');
    showContent(item.submenu,0, item.submenu.items.length);
    document.write('</TABLE></DIV></TD></TR>');
    offset--;
  }
  else grafik[i] = false;

  //Open item ny UOID if previously set as current(used instead of Click on item) 
  if (item.iscurrent)
  {
    //expcol(item.parent,false);  
  }  
}

//-----------------------------
function SetOtherCurrent(nThisInx)
{
 var oLink
	if (currentLinkInx > -1)
	   if (document.links[currentLinkInx])
	   {
	     //alert(document.links[currentLinkInx].style);
	      document.links[currentLinkInx].style.backgroundColor = "#FC8E00";	     
	      oLink = document.links[currentLinkInx];
	      oLink.onmouseout = "this.style.color='#575656';this.style.backgroundColor='#FC8E00';" ; 
	      oLink.onmouseover = "this.style.color='#132048';this.style.backgroundColor='#f3f3f3';";
	   }   
	   oLink = document.links[nThisInx];
	   //alert( oLink.onmouseover);	   
	oLink.onmouseover = "this.style.color='#132048';this.style.backgroundColor='#a9a9a9';"; 
	oLink.onmouseout = "this.style.color='#FC8E00';this.style.backgroundColor='#a9a9a9';"; 	
	oLink.style.backgroundColor = "#a9a9a9";      
	currentLinkInx = nThisInx;      
}

//-----------------------------
//Set Open status children nodes
function addexp(name) {
 if(expanded.indexOf(','+name,',')==-1) expanded+=(name+',');
}

//-----------------------------
//Set Close status  children nodes
function delexp(name) {
 var i=expanded.indexOf(','+name,',');
 var tmp=expanded;
 if(expanded.indexOf(','+name,',')!=-1) expanded = tmp.substring(0,i) + tmp.substring(i+name.length+1);
}
//-----------------------------
//Hide or Show nodes
function expcol(d,last) {
	//alert("Click"); 
  //var dd=document.all['d_'+d];
  //var img=document.all['i_'+d];
  var dd = document.getElementById('d_'+d)  
  var img = document.getElementById('i_'+d)
  if (!img) img=eval('document.i_'+d);  
  var oexp = document.getElementById('expanded') 
  if (!oexp) oexp=eval('document.expanded');
  //alert(oexp);
  if (dd ==null) return;
  
  if(dd.style.display=='none') {
    dd.style.display='';
    if (last) img.src=images_home+'icon_minusl.gif';
    else img.src=images_home+'icon_minus.gif';
    addexp(d);
  } else {
    dd.style.display='none';
    if (last) img.src=images_home+'icon_plusl.gif';
    else img.src=images_home+'icon_plus.gif';
    delexp(d);
  }
  //document.all['expanded'].value=expanded;
  //oexp.value =expanded; 
}

//-----------------------------
//--Not used in tree
 function GoToId(sAct,nUoiId,nParentId,nType) { 
 var nextUrl

  switch (sAct){   
	case 'del' :  
	           nextUrl='Edit_Protect.asp?delete=1&';
	           break;
	case 'edit' : 
	           nextUrl='Edit_Protect.asp?';   
	           break;
	case 'new' :  
	           nextUrl='Edit_Protect.asp?new=1&';    
	           break;
	case 'newchild'  : 
	           nextUrl='Edit_Protect_child.asp?new=1&';   
	           break;
	default: alert('Default');break;
  }	
   	
 	if (window.parent.Main)
 	{ 
 	//alert(nextUrl + '?UOID=' + nUoiId + '&Parent=' + nParentId);		
   window.parent.Main.document.URL= nextUrl + 'UOID=' + nUoiId + '&Parent=' + nParentId + '&Type=' + nType ;
  }
  return true;
 }
 

//-----------------------------
//--Not used in tree
function setText(text) 
{
  try {
    this.win.document.all("ptext").innerText=text;
  } catch (e) {}
}
//-----------------------------
//--Not used in tree
function close() 
{
  try {
    this.win.close();
  } catch (e) {
}

}
