/*
 * Menu
 */

function _bm_menu(obj) {
    this.id = _bm_menu_id(obj.id);
    this.obj = obj;
    
    this.shown = false;
    this.hide_id = false;

    obj.bm_menu = this;

    var t = this;
    
    this.obj.onmouseover = function(){t.highlight_item(); t.cancel_hide();};
    this.obj.onmouseout = function(){t.queue_hide();};

    this.hidefunc = function() {t.hide();};
} // _bm_menu


_bm_menu.prototype.moveto=function(p) {
    this.obj.style.left = '' + p[0] + 'px';
    this.obj.style.top = '' + p[1] + 'px';
} // _bm_menu.moveto


_bm_menu.prototype.show=function() {
    if (this.shown) {
	this.cancel_hide();
	return;
    }

    this.obj.style.display = 'block';

    this.shown=true;
} // _bm_menu.show


_bm_menu.prototype.hide=function() {
    this.obj.style.display = 'none';
   
    this.shown=false;
    this.hide_id=false;

    this.parentitem.normalize();
} // _bm_menu.hide


_bm_menu.prototype.highlight_item=function() {
    this.parentitem.highlight();
} // _bm_menu.highlight_item


_bm_menu.prototype.queue_hide=function() {
    if (!this.shown || this.hide_id) return;

    this.hide_id = setTimeout(this.hidefunc, 100);

    if ( this.parentitem && this.parentitem.parentmenu ) {
	this.parentitem.parentmenu.bm_menu.queue_hide();
    }
} // _bm_menu.queue_hide


_bm_menu.prototype.cancel_hide=function() {
    if (!this.hide_id) return; // has nothing to do
    
    clearTimeout(this.hide_id);
    this.hide_id = false;

    // clear parent
    if ( this.parentitem && this.parentitem.parentmenu ) {
	this.parentitem.parentmenu.bm_menu.cancel_hide();
    }
} // _bm_menu.cancel_hide


/*
 * Menu item
 */


function _bm_menu_item(obj, h) {
    this.id = _bm_menu_id(obj.id);
    this.obj = obj;

    // set event ops
    var t = this;
    
    this.obj.onmouseover = function(){t.mouseover();};
    this.obj.onmouseout = function(){t.mouseout();};

    // set object for appropriate menu
    var menu = document.getElementById('bm_menu' + this.id);
   
    if (menu) {
	this.menu = new _bm_menu(menu);
	
	// calculate position
	this.menu.moveto(_bm_get_coord(obj, h));

	// set link to parent item
	this.menu.parentitem = this;
    
    } else {
	this.menu = false;
    }

    // get parent menu
    var parent = this.obj.parentNode;

    while (parent && parent.nodeType == 1) {
	if (parent.id && _bm_menu_id(parent.id)) break;
	parent = parent.parentNode;
    }

    this.parentmenu = (parent && parent.nodeType == 1) ? parent : false;
} // _bm_menu_item


_bm_menu_item.prototype.mouseover=function() {
    if (this.menu) {
	this.menu.show();
    }

    this.highlight();
} // _bm_menu_item.mouseover


_bm_menu_item.prototype.mouseout=function() {
    if (this.menu) {
	this.menu.queue_hide();
    }

    this.normalize();
} // _bm_menu_item.mouseout


_bm_menu_item.prototype.highlight=function() {
    if (!this.parentmenu || this.highlighted) return;
    
    if (this.parentmenu.highlighted_item) this.parentmenu.highlighted_item.normalize();
    
    this.obj.className = this.obj.className + '_over';
    this.highlighted = true;
    this.parentmenu.highlighted_item = this;
} // _bm_menu_item.highlight


_bm_menu_item.prototype.normalize=function() {
    if (!this.parentmenu || !this.highlighted) return;

    var cn = this.obj.className;
    this.obj.className = cn.substr(0, cn.length-5);
    this.highlighted = false;
    this.parentmenu.highlighted_item = false;
} // _bm_menu_item.normalize


function _bm_menu_id(str) {
    if (str.indexOf('bm_menu')==-1 && str.indexOf('bm_item')==-1) return;

    return str.substr(7);
} // _bm_menu_id


function bm_menu_over(n, h) {
    var obj = document.getElementById('bm_item' + n);

    if (!obj) return;
    
    obj.bm_menu_item = new _bm_menu_item(obj, h);
    obj.bm_menu_item.mouseover();
} // bm_menu_over


function _bm_get_coord(obj, h) {
    var x = h ? 0 : obj.offsetWidth;
    var y = h ? obj.offsetHeight : 0;

    do {
	x += obj.offsetLeft;
	y += obj.offsetTop;
	obj = obj.offsetParent;
    } while (obj && obj.nodeType == 1);

    return([x, y]);
} // _bm_get_coord

