window.onresize = m_repos;
var hm=new head_menu();

function init(sticky_menu_item) {
  hm.fetch_objects();
  hm.sticky(sticky_menu_item);
  hm.position_menus();
}

function m_in() {
  if(hm) {
    hm.activate();
  }
}

function m_out() {
  if(hm) {
    hm.deactivate();
  }
}

function m_lock() {
  if(hm) {
    hm.lock();
  }
}

function m_unlock() {
  if(hm) {
    hm.unlock();
    m_out();
  }
}

function hide_active_menu(t_id) {
  if(hm) {
    hm.do_hide(t_id);
  }
}

function m_show(t_id) {
  if(hm) {
  	hm.show(t_id);
  }
}

function m_repos() {
	if(hm) {
		hm.position_menus();
	}
}


// Menu Object
function head_menu() {
  this.div=Array();
  this.cap_img=Array();
  this.sticky_item="";
  this.last_opened="";
  this.hide_switch=false;
  this.last_timeout_id=0;
  this.lock_switch=false;

  this.position_menus=position_menus;
  this.sticky=sticky;
  this.new_pos=new_pos;
  this.get_real_top=get_real_top;
  this.get_real_left=get_real_left;
  this.show=show_menu;
  this.hide=hide_menu;
  this.get_div=get_menu_div;
  this.fetch_objects=fetch_objects;
  this.activate=activate_menu;
  this.deactivate=deactivate_menu;
  this.do_hide=menu_hide_active;
  this.lock=menu_lock;
  this.unlock=menu_unlock;
}

function fetch_objects() {
  for(var i=0; i<menunames.length; i++) {
    this.div[menunames[i]]=document.getElementById("m_" + menunames[i]);
    this.cap_img[menunames[i]]=document.getElementById("mc_" + menunames[i]);
  }
}

function sticky(item) {
  if(item != null) {
		this.sticky_item = item;
		this.cap_img[item].src = "../../img/" + item + "_over.png";
	}
}

function position_menus() {
  for(var i=0; i<menunames.length; i++) {
	  new_pos(document.getElementById("mi_" + menunames[i]),this.get_div(menunames[i]));
  }
}

function get_menu_div(name) {
  return(this.div[name]);
}
      
function new_pos(source,target) {
  target.style.top=get_real_top(source)+5;
  target.style.left=get_real_left(source)+4;
}

function show_menu(name) {
  if(this.last_opened!="") this.hide(this.last_opened);
	if(this.sticky_item != name) this.cap_img[name].src = "../../img/" + name + "_over.png";
  this.div[name].style.visibility="visible";
  this.last_opened=name;
  this.hide_switch=false;
  this.lock_switch=false;
}

function hide_menu(name) {
  this.div[name].style.visibility="hidden";
	if(this.sticky_item != name) this.cap_img[name].src = "../../img/" + name + ".png";
}

function activate_menu(name) {
  this.hide_switch=false;
}

function deactivate_menu() {
  this.hide_switch=true;
  this.last_timeout_id++;
  if(!this.lock_switch) setTimeout("hide_active_menu(" + this.last_timeout_id + ")",1000);

  if(this.last_timeout_id > 1000) {
    this.last_timeout_id=0;
  }
}

function menu_hide_active(t_id) {
  if(this.hide_switch && (this.last_timeout_id == t_id) && !this.lock_switch) {
    this.hide(this.last_opened);
  	this.hide_switch=false;
  }
}

function menu_lock() {
  this.lock_switch=true;
}

function menu_unlock() {
  this.lock_switch=false;
}

function get_real_top(obj) {
  if(!obj) return(0);
  if(document.layers) {
    var ret=obj.pageY;
  } else {
    var ret=obj.offsetTop;
    if(obj.offsetParent) {
      while(obj.offsetParent.tagName!="BODY") {
        ret += obj.offsetParent.offsetTop;
        obj=obj.offsetParent;
      }
    }
  }
  return(ret);
}

function get_real_left(obj) {
  if(!obj) return(0);
  if(document.layers) {
    var ret=obj.pageX;
  } else {
    var ret=obj.offsetLeft;
    if(obj.offsetParent) {
      while(obj.offsetParent.tagName!="BODY") {
        ret += obj.offsetParent.offsetLeft;
        obj=obj.offsetParent;
      }
    }
  }
  return(ret);
}