// init main object
globalMenu = new GlobalMenu('globalMenu');

/* 	global object
	contain all menu */
function GlobalMenu ( name ) {
	/* const vars */
	this.hiding_default_timeout = 100;

	/* vars */
	this.name = name;
	this.menu = new Array();
	this.zIndex = 1000;
	this.nav = ( navigator.appName=="Microsoft Internet Explorer"? "IE" : "N" );
	
	/* methods */
	this.init = GlobalMenu_init;
	this.getZindex = GlobalMenu_getZindex;
	this.registerMenu = GlobalMenu_registerMenu;
}

/* init menus */
function GlobalMenu_init( ) {
	for ( var i in this.menu ) {
		this.menu[i].init();	
	}
}

/* send and update zIndex */
function GlobalMenu_getZindex ( ) {
	return ++this.zIndex;
}

/* add a menu to global object */
function GlobalMenu_registerMenu ( menu ) {
	i = this.menu.length
	this.menu[menu.elmtName] = menu;
	this.menu[menu.elmtName].globalMenu = this.name;
}

/* menu object */
function Menu ( elmtName, parentMenu, parentElmtName, location ) {
	/* conf */
	this.elmtName = elmtName;
	this.parentMenu = parentMenu;
	this.parentElmtName = parentElmtName;
	this.location = location;
	this.location_adjustx = 0;
	this.location_adjusty = 0;
	this.globalMenu = null;
	this.listIndex = null;
	this.enabled = true;

	/* vars */
	this.selected = false;
	this.displayed = false;
	this.displayedchild = false;
	/* methods*/
	this.init = Menu_init;
	this.select = Menu_select;
	this.unselect = Menu_unselect;
	this.displayMe = Menu_displayMe;
	this.hideMe = Menu_hideMe;
	this.setposition = Menu_setposition;
	this.setposition_right = Menu_setposition_right;
	this.setposition_left = Menu_setposition_left;
	this.setposition_bottom = Menu_setposition_bottom;
	this.setposition_top = Menu_setposition_top;
	this.setposition_over = Menu_setposition_over;
	this.setposition_mouse = Menu_setposition_mouse;
	this.setposition_none = Menu_setposition_none;
}

function Menu_init ( ) {
	this.elmt = document.getElementById( this.elmtName );
	this.parentElmt = document.getElementById( this.parentElmtName );
}

/* select menu */
function Menu_select ( ) {
	//alert ( this.elmtName );

	if ( this.selected || !this.enabled ) {
		return;
	}
	this.selected = true;
	if ( this.displayed == false ) {
		this.displayMe();
	}
	
	if ( this.parentMenu != null ) {
		eval( this.globalMenu+'.menu["'+this.parentMenu+'"].select()');
	}
}

/* unselect menu */
function Menu_unselect ( timeout ) {
	if ( !this.selected ) {
		return;
	}
	this.selected = false;
	if ( ! ( timeout > 0 ) ) {
		timeout = eval( ''+this.globalMenu+'.hiding_default_timeout');
	}
	setTimeout(this.globalMenu+'.menu["'+this.elmtName+'"].hideMe();', timeout );
	if ( this.parentMenu != null ) {
		eval( ''+this.globalMenu+'.menu["'+this.parentMenu+'"].unselect()');
	}
}

/* set menu to given position */
function Menu_setposition ( ) {
	switch ( this.location ) {
		case 'bottom':
			this.setposition_bottom();
			break;
		case 'left':
			this.setposition_left();
			break;
		case 'right':
			this.setposition_right();
			break;
		case 'top':
			this.setposition_top();
			break;
		case 'over' :
			this.setposition_over();
			break;
		case 'mouse':
			this.setposition_mouse();
			break;
		case 'none':
		default :
			this.setposition_none();
			break;

	}
}

/* display menu */
function Menu_displayMe( ) {

	// disable all element which have the same parent	
	for ( var i in eval(this.globalMenu).menu ) {
		if ( this.elmtName != eval(this.globalMenu).menu[i].elmtName && 
			this.parentMenu == eval(this.globalMenu).menu[i].parentMenu && 
			this.parentMenu != null ) {
			eval(this.globalMenu).menu[i].unselect(0);
		}
	}
	
	// display menu
	this.displayed = true;
	this.setposition();
	this.elmt.style.zIndex = eval(this.globalMenu).getZindex();
	this.elmt.style.visibility = 'visible';
	this.elmt.style.overflow = 'visible';
}

/* hide menu */
function Menu_hideMe ( ) {
	if ( this.selected == false ) {
		this.elmt.style.visibility = 'hidden';
		this.elmt.style.overflow = 'hidden';	
		this.displayed = false;
	}
}

/*	position : none
	set menu div to position 0,0 */
function Menu_setposition_none () {
	this.elmt.style.left = this.location_adjustx;
	this.elmt.style.top = this.location_adjusty;
}

/*	position : bottom
	set menu div to position parentElmtName.x , parentElmtName.y + parentElmtName.h */
function Menu_setposition_bottom () {
	this.elmt.style.left = getPosX ( this.parentElmt ) + this.location_adjustx;
	this.elmt.style.top  = getPosY ( this.parentElmt ) + this.parentElmt.offsetHeight + this.location_adjusty;
}

/*	position : right
	set menu div to position parentElmtName.x + parentElmtName.w , parentElmtName.y */
function Menu_setposition_right () {
	this.elmt.style.left = getPosX ( this.parentElmt ) + this.parentElmt.offsetWidth + this.location_adjustx;
	this.elmt.style.top  = getPosY ( this.parentElmt ) + this.location_adjusty;
}

/*	position : left
	set menu div to position parentElmtName.x - this.w , parentElmtName.y */
function Menu_setposition_left () {
	this.elmt.style.left = getPosX ( this.parentElmt ) - this.elmt.offsetWidth + this.location_adjustx;
	this.elmt.style.top  = getPosY ( this.parentElmt ) + this.location_adjusty;
}

/*	position : over
	set menu div to position parentElmtName.x , parentElmtName.y */
function Menu_setposition_over () {
	this.elmt.style.left = getPosX ( this.parentElmt ) + this.location_adjustx;
	this.elmt.style.top  = getPosY ( this.parentElmt ) + this.location_adjusty;
}

/*	position : top
	set menu div to position parentElmtName.x , parentElmtName.y - this.h */
function Menu_setposition_top () {
	this.elmt.style.left = getPosX ( this.parentElmt ) + this.location_adjustx;
	this.elmt.style.top  = getPosY ( this.parentElmt ) - this.elmt.offsetHeight + this.location_adjusty;
}

/*	position : mouse
	set menu div to mouse position */
function Menu_setposition_mouse () {

}

/**************** TOOL FUNCTIONS ***************/

// get x position of given object
function getPosX(object) {
	var left = 0;
	if (object.offsetParent){
		// get recursive position of successive parents
		while (object.offsetParent){
			left += object.offsetLeft
			object = object.offsetParent;
		}
	}
	return left;
}

// get y position of given object
function getPosY(object) {
	var top = 0;
	if (object.offsetParent) {
		// get recursive position of successive parents
		while (object.offsetParent) {
			top += object.offsetTop
			object = object.offsetParent;
		}
	}
	return top;
}

function debug( t ) {
	document.getElementById( 'debug' ).innerHTML = t;
}

/**************** DISPLAY FUNCTIONS ****************/
// update rollover on menu item
function updtMenuItem ( menu, item, selected, css ) {
	document.getElementById ( menu + "-" + item ).className = css;
}