//
// 20020204
// javascript menu library by korc@artun.ee
// license: LGPL
//

var menu_focused_item;
var menu_focused_parent;
var menu_last_item;
var menu_closeall_timer;

function getCookie(Name) {
   var search = Name + "="
   if (document.cookie.length > 0) {
      var offset = document.cookie.indexOf(search) 
      if (offset != -1) {
         offset += search.length 
         var end = document.cookie.indexOf(";", offset) 
         if (end == -1) 
            end = document.cookie.length
         return unescape(document.cookie.substring(offset, end))
      } 
   }
}

function menu_toggle() {
  var today=new Date();
  var expiredate=new Date();
  expiredate.setTime(today.getTime() + 1000*60*60*24*365)

  var newval=(getCookie('menu_disabled')=='yes')?'no':'yes';

  document.cookie='menu_disabled='+newval+';path=/;'+' expires='+expiredate.toGMTString();
  window.location.reload()
}

function menu_close(close_item) {
  if(getCookie('menu_disabled')=='yes') return;
  if(!close_item)
    close_item=menu_last_item;
  // do not try close focused item
  if(close_item==menu_focused_item)
    return;

  // let's close children
  if(close_item.menu_children) {
    for(var child_id in close_item.menu_children) {
      xb_setVisibility(close_item.menu_children[child_id],'hidden');
    }
  }

  // this item's parent is not current parent, move up
  if(close_item.menu_parent && close_item.menu_parent!=menu_focused_parent)
    menu_close(close_item.menu_parent);
}

function menu_blur_item() {
  if(menu_closeall_timer)
    window.clearTimeout(menu_closeall_timer);
  menu_last_item=this;
  menu_focused_item=null;
  menu_focused_parent=null;
  menu_closeall_timer=window.setTimeout(menu_close,1000,this);
}

function menu_focus_item() {
  menu_focused_item=this;
  menu_focused_parent=this.menu_parent;

  if(menu_closeall_timer)
    window.clearTimeout(menu_closeall_timer);

  // have previously focused item and it's not our parent
  if(menu_last_item && menu_last_item!=this.menu_parent)
    menu_close(menu_last_item);

  // if we have any children then let's open them
  if(this.menu_children) {
    for(var child_id in this.menu_children) {
      xb_setVisibility(this.menu_children[child_id],'visible');
    }
  }
}

function menu_load(menu_name,x,y,parent) {
  var m;
  var xpos=x;
  var ypos=y;
  var horizontal=(parent)?0:1;

  for (var i=0;m=xb_getElement(menu_name+'_'+i);i++) {
    xb_setEventHandler(m,'mouseover',menu_focus_item);
    xb_setEventHandler(m,'mouseout',menu_blur_item);

    var m_width=xb_getWidth(m);
    var m_height=xb_getHeight(m);

    if(xb_getDocumentWidth()<(xpos+m_width)) {
      return;
    }

    m.menu_left=xpos;
    xb_moveTo(m,xpos,ypos);

    var subx=xpos;
    var suby=ypos;

    if(horizontal) {
      xpos=xpos+m_width;
      suby=y+m_height;
    } else {
      subx=x+m_width;
      ypos=ypos+m_height;
    }

    // load submenu's and set root items visible
    if(parent) {
      if(!parent.menu_children)
        parent.menu_children=new Array();
      parent.menu_children[m.id]=m;
      m.menu_parent=parent;
    } else {
      xb_setVisibility(m,'visible');
    }
    menu_load(m.id,subx,suby,m);
  }
  return i;
}

