function debug(text)
{
  x=document.getElementById('debug');
  x.innerHTML+=text;
}


function windowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
 return [myWidth,myHeight];
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

var timershow=Array();
var timerhide=Array();

function menushow(name)
{
  clearTimeout(timerhide[name]);
  timershow[name]=setTimeout(function() { bg_menushow(name);},250);
}

function menuhide(name)
{
  timerhide[name]=setTimeout(function() { bg_menuhide(name);},500);
}

function bg_menushow(name)
{
  menuobj=document.getElementById("menu");
  menupos=findPos(menuobj);

  m=document.getElementById(name);
  
  m.style.display="block";
  m.style.zIndex="1000";
  
  mpos=findPos(m);

  ch=windowSize();
  
  if(m.clientHeight>ch[1]-menupos[1]+10) 
    rearrange(name,ch[1]-menupos[1]+10); 

  s=getScrollXY();  

  if((mpos[1]-s[1])+m.clientHeight>ch[1])
  {    
    repos=ch[1]-((mpos[1]-s[1])+m.clientHeight);
    m.style.top=repos+"px";
  }
}

function bg_menuhide(name)
{
  m=document.getElementById(name);
  m.style.display="none";
  m.style.zIndex="0";
  m.style.top="0px";
}

function rearrange(name,ws)
{
  m=document.getElementById(name);
  

  mpos=findPos(m);
  
  //ennyi menuelem van, de ez nem igaz
  //ic=Math.ceil(m.clientHeight/20);

  //ennyi menuelem fer el a screenen ez sem igaz
  //sc=Math.floor(ws/20)-1;
  i=1;

  var a=Array();		
  var ac=0;

  //menu container szelesseget beallitjuk a megfelelo oszlopszam szerint
  //m.style.width=(Math.ceil(ic/sc)*150)+"px";

  for(cn=0;m.childNodes[cn];cn++)
  {
	if(m.childNodes[cn].id!=undefined)
	{
	  a[ac]=m.childNodes[cn].id;
	  ac++;
	}
  }
  
  var col=1;
  var colh=0;
  var maxcolh=0;
  var allcol=0;

  for(var j=0;j<ac;j++)
  {
    x=document.getElementById(a[j]);
	if(colh+x.clientHeight>ws)
	{
      allcol+=colh;//colhs[col]=colh;
      col++;
	  m.style.width=(col*150)+"px";
	  if(colh>maxcolh) maxcolh=colh;
	  colh=0;
	}
    x.style.left=((col-1)*150)+"px";
	x.style.top=(-allcol)+"px";
	x.style.display="block";
    colh+=x.clientHeight;
  }
  if(colh>maxcolh) maxcolh=colh;
  m.style.height=maxcolh+"px";
  
/*  alert(ic+" : "+ac);
  x=document.getElementById(name+"_"+(sc+i)+"root");
  col=1;
  while(x!=undefined)
  {
    x.style.left=(col*150)+"px";
    x.style.top=(-(sc*col)*20)+"px";
    x.style.display="block";
    i++;
    if((sc+i)%sc==1) col++;
    x=document.getElementById(name+"_"+(sc+i)+"root");
  }

  a=m.clientHeight-((i-1)*20);
  m.style.height=a+"px";
  //m.style.overflow="hidden";
*/

}
