var menusource=new Array()
var menus=new Array()
var root=2      // A menustruktura gyokere
var MenuDiv, LocDiv, LocTD, LocDivTable

function toggleLoc(rows) {
  if (LocDiv==null) {
    alert("Hiba!")
    return
  }
//  LocDiv.style.height=parseInt(rows)*14+6-parseInt(LocDiv.style.height)
//  LocDiv.style.height=parseInt(rows)*16+30-parseInt(LocDiv.style.height)
  LocDiv.style.height=parseInt(LocDivTable.clientHeight)+30-parseInt(LocDiv.style.height)
  LocDiv.style.clip="rect(0,164,"+LocDiv.style.height+",0)"
  LocDiv.style.visibility=parseInt(LocDiv.style.height)?"visible":"hidden"
  LocTD.style.height=parseInt(LocDiv.style.height)
  document.locdivnext.src="/images/link_"+(parseInt(LocDiv.style.height)?"y":"x")+"_end.gif"
}

// A menupont objektuma
function Menu(id,parent,name,desc,type,level) {
   this.id = id
   this.name = name
   this.parent = parent
   this.desc = desc
   this.type = type
   this.level = level
   this.child = new Array()
   this.mwrite=mwrite
//alert(id+","+parent+","+name+","+desc+","+type+","+level)   
   
/*   
   // Ez alabbi lehet, hogy nem jo itt, akkor a feltolteshez kell tenni
   if (parent>0) {
     if (!menus[parent]) menus[parent]=new Menu(parent,0,'','',0,0)
     menus[parent].child.push(this)
   }
*/
}

   function mwrite(id,recursive) {
if (debug || (document.jstest&&document.jstest.debug.checked)) alert("i: "+i+"\nmwrite: "+id+","+this.id+","+recursive)     
     var tmp=''
     if (recursive) {
       for (var i=0; i<this.child.length; i++) {
         tmp+=this.child[i].mwrite(id,this.id!=id)
       }
     }
     if (this.id==root) return tmp
     if (tmp!='' || this.id==id || !recursive) {
       switch (this.type) {
         case 1:
           url='<a href="/textpage.html?'+setlink+'menu_id='+this.id+'">'+this.name+'</a>'
           break
         case 5:
           url='<a href="/dview.html?'+setlink+'menu_id='+this.id+'">'+this.name+'</a>'
           break
         default: 
           url='<a href="javascript: void(null)" onClick="updateMenu('+this.id+')">'+this.name+'</a>'
       }
       if (this.id==id) url='<b>'+this.name+'</b>'
       return '<tr><td class="menu32" nowrap>'+str_repeat('&nbsp;&nbsp;',this.level-1)+url+'</td></tr>\n'+tmp
     }
     return ''
   }


function updateMenu(id) {
//    for (i=0;i<menus.length;i++) {
//      if (menus[i]) alert(i+","+menus[i].id)
//    }  
//  alert(root+": "+menus[root].id)

  MenuDiv.innerHTML='<table border=0 cellspacing=0 cellpadding=0 width=185><tr><td class="menu1">&nbsp;<a href="javascript:void(null)" onClick="updateMenu(2)" class="menu1">'+menutext+'</a></td><td><img name="locdivnext" src="/images/link_x_end.gif"></td></tr></table>';


  tmp=menus[root].mwrite(id,true)   //+'<tr><td class="menu2">Azé valami lögyön</td></tr>\n'
  if (tmp) {
    MenuDiv.innerHTML+='<table cellpadding=0 cellspacing=0 border=0><tr><td width="185"><img src="/images/link_shadow_orange_all.gif"></td></tr>\n'+tmp+'</table>'
//    sorokszama=tmp.split('<tr><td class=').length-1
//    alert(sorokszama)
    MenuDiv.style.height=14*(tmp.split('<tr><td class=').length)-8+24
    document.menudivnext.src="/images/link_y_end.gif"
    MenuDiv.style.backgroundimage="url('/images/link_submenu_all_nagy2_bg.gif')"
  } else {
    MenuDiv.innerHTML+=''
    MenuDiv.style.height="24px"
    document.menudivnext.src="/images/link_x_end.gif"
    MenuDiv.style.backgroundimage=""
  }

}

function str_repeat(str,num) {
  if (num==0) return ''
  if (num==1) return str
  return str+str_repeat(str,parseInt(num)-1)
}

function init() {
  MenuDiv=(document.getElementById)?document.getElementById('MenuDiv'):((document.all)?document.all['MenuDiv']:((document.layers)?document.layers['MenuDiv']:null))
  LocDiv=(document.getElementById)?document.getElementById('LocDiv'):((document.all)?document.all['LocDiv']:((document.layers)?document.layers['LocDiv']:null))
  LocDivTable=(document.getElementById)?document.getElementById('LocDivTable'):((document.all)?document.all['LocDivTable']:((document.layers)?document.layers['LocDivTable']:null))
  LocTD=(document.getElementById)?document.getElementById('LocTD'):((document.all)?document.all['LocTD']:((document.layers)?document.layers['LocTD']:null))
  
  // Menupontok feltoltese a php altal generalt tombbol
//  alert(menusource.length)
  for (var i=0; i<menusource.length; i++) {
    m=menusource[i]
//    alert("m[0]="+m[0])
    // indexek: 0:id, 1:parent, 2:name, 3:desc, 4:type, 5:level
    if (menus[m[0]]) {
      menus[m[0]].parent=m[1]
      menus[m[0]].name=m[2]
      menus[m[0]].desc=m[3]
      menus[m[0]].type=m[4]
      menus[m[0]].level=m[5]
//      alert("menus["+m[0]+"]=regi Menu("+m[0]+","+m[1]+","+m[2]+","+m[3]+","+m[4]+","+m[5]+")")
    } else {
      menus[m[0]]=new Menu(m[0],m[1],m[2],m[3],m[4],m[5])
//      alert("menus["+m[0]+"]=new Menu("+m[0]+","+m[1]+","+m[2]+","+m[3]+","+m[4]+","+m[5]+")")
    }
    
    if (!menus[m[1]]) {
      menus[m[1]]=new Menu(m[1],0,'','',0,0)
//      alert("menus["+m[1]+"]=new parentMenu("+m[1]+",0,'','',0,0)")
    } else {
      menus[m[1]].child.push(menus[m[0]])
//      alert("menus["+m[1]+"]=regi parentMenu("+m[1]+").child.push(menus["+m[0]+"])")
    }

    // Ha a menuobjektum generalas vegen nem muxik, akkor ez kell, 
    // de meg lehet, hogy egy parent meglet-ellenorzes, letrehozas sem artana ele:
//    menus[m[1]].child.push(menus[m[0]])
  }
  updateMenu(menu_id)
}

