// BetaInterface3 Menu Engine, Przedszkole120 optimized / Copyright (C) 2007-2009 ConsidoNet Solutions / Paweł Gabryelewicz

var currentpopupmenu;
var currentpopupmenubtn;

var currentpopupsubmenus = new Array();
var allowpopupmenuclose = true;

function PopupMenu(id, btnid, x, y) {
  var menu = document.getElementById(id);
  if(!x && !y) var btn = document.getElementById(btnid);
  
  var trs = document.getElementById('m_'+id+'_trs');
  var rs = document.getElementById('m_'+id+'_rs');
  var brs = document.getElementById('m_'+id+'_brs');
  var bs = document.getElementById('m_'+id+'_bs');
  var bls = document.getElementById('m_'+id+'_bls');
  
  if(currentpopupmenu!=menu) ClosePopupMenus();
  ClosePopupSubMenus(0);
  
  allowpopupmenuclose = false;
  
  SetTransparency(menu, 0);
  menu.style.display = 'block';
  
  if(btnid) {
    if(menu.offsetWidth<btn.offsetWidth) {
      menu.style.width=btn.offsetWidth+'px';
    }
  }
  
  if(!x && !y){
    menu.style.left = (GetLeftOffset(btn))+'px';
    //if(document.all || window.opera) menu.style.top = (parseInt(GetTopOffset(btn)))+'px'; 
    if(window.opera) menu.style.top = (parseInt(GetTopOffset(btn))+btn.offsetHeight)-2+'px';
    else menu.style.top = (parseInt(GetTopOffset(btn))+btn.offsetHeight)+'px';
    
  } else {
    var brww, brwh;
    if(typeof(window.innerWidth)=='number') { brwh = window.innerHeight; brww = window.innerWidth; } 
    else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) { brwh = document.documentElement.clientHeight; brww = document.documentElement.clientWidth; } 
    else if(document.body && (document.body.clientWidth || document.body.clientHeight)) { brwh = document.body.clientHeight; brww = document.body.clientWidth; }  
    if((x+menu.offsetWidth)>brww) x-=((x+menu.offsetWidth)-brww)+5;
    if((y+menu.offsetHeight)>brwh) y-=((y+menu.offsetHeight)-brwh)+5;
    menu.style.left = x+'px';
    menu.style.top = y+'px';
  }
    
  trs.style.top='2px';
  trs.style.left=(menu.offsetWidth)+'px';
  rs.style.top='5px';
  rs.style.left=(menu.offsetWidth)+'px';
  rs.style.height = (menu.offsetHeight-5)+'px';
  brs.style.top=(menu.offsetHeight)+'px';
  brs.style.left=(menu.offsetWidth)+'px';
  bs.style.top=(menu.offsetHeight)+'px';
  bs.style.left='5px';
  bs.style.width = (menu.offsetWidth-5)+'px';
  bls.style.top=(menu.offsetHeight)+'px';
  bls.style.left='2px';
    
  trs.style.display = 'block';
  rs.style.display = 'block';
  brs.style.display = 'block';
  bs.style.display = 'block';
  bls.style.display = 'block';
  
  btn.className='active';
  //if(btn && btn.className=='tbbutton') btn.className='tbbuttonactive';
  
  currentpopupmenu = menu;
  if(btn) currentpopupmenubtn = btn; else currentpopupmenubtn = '';
  
  //currentpopupmenu.style.display='block';
  
  for(i=0; i<5; i++) {
    setTimeout("SetTransparency(currentpopupmenu, "+i+"*20)", (parseInt((i*25))));
  } 
  i++;
  setTimeout("SetTransparency(currentpopupmenu, 100)", (parseInt((i*25))));
  setTimeout("currentpopupmenu.style.filter='none'", (parseInt((i*25))));
}

function PopupSubMenu(id, btn) {
  var menu = document.getElementById(id);
  
  currentpopupsubmenus.push(id);
  
  
  var trs = document.getElementById('m_'+id+'_trs');
  var rs = document.getElementById('m_'+id+'_rs');
  var brs = document.getElementById('m_'+id+'_brs');
  var bs = document.getElementById('m_'+id+'_bs');
  var bls = document.getElementById('m_'+id+'_bls');
  
  SetTransparency(menu, 0);
  menu.style.display = 'block';
  
  var x = GetLeftOffset(btn)+btn.offsetWidth;
  var y = (parseInt(GetTopOffset(btn))-1);
  
  var brww, brwh;
  if(typeof(window.innerWidth)=='number') { brwh = window.innerHeight; brww = window.innerWidth; } 
  else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) { brwh = document.documentElement.clientHeight; brww = document.documentElement.clientWidth; } 
  else if(document.body && (document.body.clientWidth || document.body.clientHeight)) { brwh = document.body.clientHeight; brww = document.body.clientWidth; }  
  
  if((x+menu.offsetWidth)>brww) x=GetLeftOffset(btn)-menu.offsetWidth;
  if((y+menu.offsetHeight)>brwh) y-=((y+menu.offsetHeight)-brwh)+5;
  
  menu.style.left = x+'px';
  menu.style.top = y+'px';
  
    
  trs.style.top='2px';
  trs.style.left=(menu.offsetWidth)+'px';
  rs.style.top='5px';
  rs.style.left=(menu.offsetWidth)+'px';
  rs.style.height = (menu.offsetHeight-5)+'px';
  brs.style.top=(menu.offsetHeight)+'px';
  brs.style.left=(menu.offsetWidth)+'px';
  bs.style.top=(menu.offsetHeight)+'px';
  bs.style.left='5px';
  bs.style.width = (menu.offsetWidth-5)+'px';
  bls.style.top=(menu.offsetHeight)+'px';
  bls.style.left='2px';
    
  trs.style.display = 'block';
  rs.style.display = 'block';
  brs.style.display = 'block';
  bs.style.display = 'block';
  bls.style.display = 'block';
  
  
  //currentpopupmenu = menu;
  //currentpopupmenubtn = btn;
  
  //menu.style.display='block';
  
  for(i=0; i<5; i++) {
    setTimeout("SetTransparency(document.getElementById('"+menu.id+"'), "+i+"*20)", (parseInt((i*25))));
  } 
  i++;
  setTimeout("SetTransparency(document.getElementById('"+menu.id+"'), 100)", (parseInt((i*25))));
  setTimeout("document.getElementById('"+menu.id+"').style.filter='none'", (parseInt((i*25))));
}

function ClosePopupMenus() {
  if(!currentpopupmenu) return false;
  if(!allowpopupmenuclose) { allowpopupmenuclose = true; return false; }
  
  if(currentpopupmenubtn) currentpopupmenubtn.className='';
  
  var menuid = currentpopupmenu.id;
  for(i=0; i<5; i++) {
    setTimeout("SetTransparency(document.getElementById('"+menuid+"'), 100-("+i+"*20))", (parseInt((i*25))));
  } 
  i++;
  setTimeout("document.getElementById('"+menuid+"').style.display='none'",(parseInt((i*25))));
  i++;
  setTimeout("SetTransparency(document.getElementById('"+menuid+"'), 0)", (parseInt((i*25))));
  
  currentpopupmenu = '';
  currentpopupmenubtn = '';
  
  ClosePopupSubMenus(0);
}

function ClosePopupSubMenus(depth) {
  var t = currentpopupsubmenus.length;
  for(var i = depth; i<t; i++) {
    var menuid = currentpopupsubmenus.pop();
    for(var j=0; j<5; j++) setTimeout("SetTransparency(document.getElementById('"+menuid+"'), 100-("+j+"*20))", (parseInt((j*25))));
    j++; setTimeout("document.getElementById('"+menuid+"').style.display='none'",(parseInt((j*25))));
    j++; setTimeout("SetTransparency(document.getElementById('"+menuid+"'), 0)", (parseInt((j*25))));
  }
  if(depth==0) { delete currentpopupsubmenus; currentpopupsubmenus = new Array(); }
}

function CreatePopupMenu(menuarray, menuid, depth) {
  var pdiv = document.getElementById('popupmenus');
  var submenus = 0;
  if(depth===undefined) depth = 0;
  var innerhtml = '<div class="popupmenu" oncontextmenu="return false;" style="z-index: '+(50000+depth)+';" id="'+menuid+'"><div class="popupmenuinner">';
  for(var i=0; i<menuarray.length; i++) {
    if(menuarray[i][0]!='-') {
      if(menuarray[i][2]) var icontext = ' style="background-image: url(\''+menuarray[i][2]+'\');"'; else var icontext = '';
      if(menuarray[i][3]) var idtext = ' id="'+menuarray[i][3]+'"'; else var idtext = '';
      if(typeof(menuarray[i][1])=='object' && menuarray[i][1].constructor==Array) {
        innerhtml+='<a style="background-position: right center; background-repeat: no-repeat; background-image: url(\'./bincms/styles/gfx/menuarrowhv.gif\'); padding-right: 15px; " href="#" onmouseover="javascript:  ClosePopupSubMenus('+depth+'); PopupSubMenu(\''+menuid+'_p'+submenus+'\', this, true); return false;">'+icontext+menuarray[i][0]+'</a>';
        CreatePopupMenu(menuarray[i][1], menuid+'_p'+submenus, (depth+1));
        submenus++;
      } else innerhtml+='<a'+icontext+idtext+' onmouseover="javascript: ClosePopupSubMenus('+depth+');" href="#" onclick="javascript: '+menuarray[i][1]+' this.blur(); return false;">'+menuarray[i][0]+'</a>';
    } else innerhtml+='<div onmouseover="javascript: ClosePopupSubMenus('+depth+');" class="separator"></div>';
  }
  innerhtml+='</div><img src="./gfx/shadow/trs.png" id="m_'+menuid+'_trs" class="png m_trs" alt="" /><img src="./gfx/shadow/rs.png" id="m_'+menuid+'_rs" class="png m_rs" alt="" /><img src="./gfx/shadow/brs.png" id="m_'+menuid+'_brs" class="png m_brs" alt="" /><img src="./gfx/shadow/bs.png" id="m_'+menuid+'_bs" class="png m_bs" alt="" /><img src="./gfx/shadow/bls.png" id="m_'+menuid+'_bls" class="png m_bls" alt="" /></div>';
  pdiv.innerHTML+=innerhtml;
}

function OverwritePopupMenu(menuarray, menuid, depth) {
  var menudiv = document.getElementById(menuid);
  var submenus = 0;
  if(depth===undefined) depth = 0;
  var innerhtml = '<div class="popupmenuinner">';
  for(var i=0; i<menuarray.length; i++) {
    if(menuarray[i][0]!='-') {
      if(menuarray[i][2]) var icontext = ' style="background-image: url(\''+menuarray[i][2]+'\');"'; else var icontext = '';
      if(menuarray[i][3]) var idtext = ' id="'+menuarray[i][3]+'"'; else var idtext = '';
      if(typeof(menuarray[i][1])=='object' && menuarray[i][1].constructor==Array) {
        innerhtml+='<a style="background-position: right center; background-repeat: no-repeat; background-image: url(\'./bincms/styles/gfx/menuarrowhv.gif\'); padding-right: 15px; " href="#" onmouseover="javascript: ClosePopupSubMenus('+depth+'); PopupSubMenu(\''+menuid+'_p'+submenus+'\', this, true); return false;">'+menuarray[i][0]+'</a>';
        OverwritePopupMenu(menuarray[i][1], menuid+'_p'+submenus, (depth+1));
        submenus++;
      } else innerhtml+='<a'+icontext+idtext+' onmouseover="javascript: ClosePopupSubMenus('+depth+');" href="#" onclick="javascript: '+menuarray[i][1]+' this.blur(); return false;">'+menuarray[i][0]+'</a>';
    } else innerhtml+='<div onmouseover="javascript: ClosePopupSubMenus('+depth+');" class="separator"></div>';
  }
  innerhtml+='</div><img src="./bincms/gfx/shadow/trs.png" id="m_'+menuid+'_trs" class="png m_trs" alt="" /><img src="./bincms/gfx/shadow/rs.png" id="m_'+menuid+'_rs" class="png m_rs" alt="" /><img src="./bincms/gfx/shadow/brs.png" id="m_'+menuid+'_brs" class="png m_brs" alt="" /><img src="./bincms/gfx/shadow/bs.png" id="m_'+menuid+'_bs" class="png m_bs" alt="" /><img src="./bincms/gfx/shadow/bls.png" id="m_'+menuid+'_bls" class="png m_bls" alt="" />';
  menudiv.innerHTML=innerhtml;
}

function FadeIn(objid) {
  for(var i=1; i<=10; i++) setTimeout("if (document.getElementById('"+objid+"')) SetTransparency(document.getElementById('"+objid+"'), "+i+"*10)", (parseInt(i*25)));
}

