var agt=navigator.userAgent.toLowerCase(); 
var is_major = parseInt(navigator.appVersion); 
var is_ie   = (agt.indexOf("msie") != -1); 
var is_ie4up  = (is_ie  && (is_major >= 4)); 

var minSize = 100
var maxSize = 160
var charWidth = 8;
menuType = "top";
itemHeight = 17;

var normalColor = '#eeeeee';
var hoverColor = '#ffffff';

function sm(id, cell)
{
	bDefined = eval("typeof(f"+id+")");
	if (bDefined == "undefined")
		return;

	f = eval("f"+id);
	if (typeof(cell) == "string")
		cell = document.all['sc'+f.id];

	if (!cell)
	{
  	mcell = document.all['cl'+f.id];
  	if (mcell) 
  	{
  	 mcell.style.backgroundColor = hoverColor;
     mcell.style.color = '#000000';
  	}

  	mlink = document.all['l'+f.id];
//  	if (mlink) 
//     mlink.className = 'dditemSel';

  	mcell = document.all['cr'+f.id];
  	if (mcell) mcell.style.backgroundColor = hoverColor;
  }	

	if (!is_ie4up || !f || !f.menuId)
		return; 

	f.bind = cell;
	if (!f.bVisible) 
		f.calc(cell);
  f.bVisible = 1;	

  mitem = document.all[f.menuId];
  mitem.style.left=f.px;
  mitem.style.top=f.py;
  mitem.style.visibility='visible';

	if (f.parentId)
		sm(f.parentId);
}

function hm(id)
{
	bDefined = eval("typeof(f"+id+")");
	if (bDefined == "undefined")
		return;

	f = eval("f"+id);

  mcell = document.all['cl'+f.id];
  if (mcell) 
  {
  	mcell.style.backgroundColor = normalColor;
		mcell.style.color = '#ffffff';
	}	

 	mlink = document.all['l'+f.id];
 	if (mlink) 
    mlink.className = 'dditem';

  mcell = document.all['cr'+f.id];
  if (mcell) mcell.style.backgroundColor = normalColor;

	if (!is_ie4up || !f || !f.menuId)
		return; 

	var mitem = document.all[f.menuId];
	mitem.style.visibility='hidden'
  f.bVisible = 0;

	if (f.parentId)
		hm(f.parentId)
}

function convertToHTML(str)
{
  if (!str) return null

  res=''
  realView = "&\"<>-";
  underView = new Array('&amp;', '&quot;', '&lt;', '&gt;', '&nbsp;');

  for (i = 0;  i < str.length;  i++)
  {
    ch = str.charAt(i);
    for (j = 0;  j < realView.length;  j++)
      if (ch == realView.charAt(j))
      {      
       ch = underView[j]
       break;        
      }
    res += ch    
  } 
  return res 
}

function Folder(folderDesc,id)
{ 
 var is;
 eval("is = typeof(f"+id+")")
 if (is == "object") return eval("f"+id);

 this.id = id
 this.desc = convertToHTML(folderDesc)

 this.px = 0
 this.py = 0

 this.mnuVar = "z";
 this.vOffset = 0;
 this.folderWidth = 0;
 this.captionWidth = charWidth * folderDesc.length;

 this.nChildren = 0
 this.Children = 0

 this.firstTime = 1
 this.render = 0;
 this.add = AddNode
 this.calc = CalcPosition
 this.parentId = 0
 this.prev = 0
 this.next = 0
 this.menu = 0
 this.menuId = 0;
 this.menuType = 1
 this.bind = 0;
}

function AddNode(fld)
{
 fld.parentId = this.id

 if (this.folderWidth < fld.captionWidth)
     this.folderWidth = fld.captionWidth;
 
 if (this.folderWidth < minSize)
     this.folderWidth = minSize;

 if (this.folderWidth > maxSize)
     this.folderWidth = maxSize;

 if (!this.Children){
  this.Children = fld
 }
 else
 {
  curFld = this.Children;
  while (curFld.next)
   curFld = curFld.next
  fld.prev = curFld
  curFld.next = fld
 }  
 this.nChildren++
}

function CalcObjCoord(p)
{
	pleft = 0;
	ptop = 0;

	var rp = p.offsetParent;
	if (rp == null)
		return false;

	pleft = p.offsetLeft;
	ptop = p.offsetTop;

	while (true)
	{
		pleft += rp.offsetLeft;
		ptop += rp.offsetTop;
		rp = rp.offsetParent;
		if (rp == null)
			break;
	}	
	return true;
}

function CalcPosition(cell)
{
	if (!cell)
	{
		if (this.parentId)
		{
			eval("p=f"+this.parentId)

      var mcell = document.all['l'+this.id];
      if (mcell && CalcObjCoord(mcell))
      {
      	this.vOffset = ptop;
				this.py = ptop + 3;
      }
      else
				this.py = this.vOffset + 3;

			this.px = p.px;					
			if (this.menuType != 3)
			 this.px += p.folderWidth-3;
	 	}

  }
	else
	{
		if (!cell)
			return false;

		CalcObjCoord(cell);//calc pleft & ptop

		if (this.menuType == 0)//default
		{
			pleft += cell.offsetWidth-1;
		}
		else
		if (this.menuType == 1)//left to right
		{
			pleft += cell.offsetWidth;
		}
		else
		if (this.menuType == 2)//right to left
		{
			pleft -= this.folderWidth;
			pleft+=1;
		}
		else
		if (this.menuType == 3)//top to bottom
		{
			ptop += cell.offsetHeight;
			pleft -= 1;
		}
		else
		if (this.menuType == 4)//bottom to top
		{
			ptop -= cell.offsetHeight;
		}

		this.px = pleft;
		this.py = ptop;
	}	

	if (this.py+this.menu.offsetHeight-document.body.scrollTop > document.body.offsetHeight)
  	this.py -= this.menu.offsetHeight+this.py-document.body.scrollTop-document.body.offsetHeight;

	if (this.py-document.body.scrollTop < 0)
  	this.py = document.body.scrollTop;
  	 	
	this.firstTime = 0;            
	return true;
}
