/****
 * Popout (to the right) menus
 * Written by Jonathan A. Foster
 * Started January 4th, 2006
 * Copyright JF Possibilities, Inc.
 *
 * 2008-04-29 Major restructure to a more universal format.
 ****/
 


/*** Utilities ***/

function jfpIsIE() {
	/* This could become more complex as I discover browser
		incompatibilities. Duplicated in jfpajax. */
	var b = window.navigator.appName;
	if(b=='Microsoft Internet Explorer')
		return true;
	else
		return false; // Mozilla compatible.
}



/*** Define Pop Out class ***/

function CjfpHtmlMenu() {
	this.showing = new Array();
	this.direction = 0; // 0 = right (default), 1 = down
}



/*** class methods ***/

CjfpHtmlMenu.prototype.show = function(name, event, level) {
	var x, y, l, p_menu_item, menu;
	
	/*** Get menu parent and location ***/
	
	if(this.IE) 
		p_menu_item = event.srcElement;
	else
		p_menu_item = event.target;
	l = new this.loc(p_menu_item);
	
	/*** place new menu ***/
	
	menu = document.getElementById(name);
	if(this.direction==1) {
		menu.style.left = (l.x - 2).toString()+'px';
		menu.style.top = (l.y + p_menu_item.offsetHeight - 2).toString()+'px';
	} else {
		menu.style.left = (l.x + p_menu_item.offsetWidth - 2).toString()+'px';
		menu.style.top = (l.y - 2).toString()+'px';
	}
	
	/*** hide any other menus and show this one ***/
	
	this.hide(level);
	this.showing.length = level + 1;
	this.showing[level] = menu;
	menu.style.visibility = "visible";
}



CjfpHtmlMenu.prototype.hide = function(level) {
	var x;
	var len = this.showing.length;
		
	if(level<len) {
		len--;
		for(x=len; x>=level; x--) 
			this.showing[x].style.visibility="hidden";
		if(level<len) this.showing.length=level;
	}
}



CjfpHtmlMenu.prototype.cleanup = function(event) {
	var x;
	var y;
	if(this.IE) {
		x = event.clientX+document.body.scrollLeft;
		y = event.clientY+document.body.scrollTop;
	} else {
		x = event.pageX;
		y = event.pageY;
	}
	x = this.IsInMenu(x, y);
	if(x<0) this.hide(0);
}
	


CjfpHtmlMenu.prototype.launch = function(url) {
	this.hide(0);
	window.location=url;
}
	


CjfpHtmlMenu.prototype.dump = function(event) {
	var s, x, m;
		
	s='*** MENU DUMP ***\n\n';
	for(x=0; x<this.showing.length; x++) {
		m=this.showing[x];
		s+=x.toString()+': ('+m.offsetLeft.toString()+', '+m.offsetTop.toString()+', '+m.offsetWidth.toString()+', '+m.offsetHeight.toString()+') '+m.id+'\n';
	}
	if(this.IE)
		s+='\n*** Mouse @ ****\n\n'+(event.clientX-document.body.clientLeft).toString()+', '+(event.clientY-document.body.clientTop).toString();
	else
		s+='\n*** Mouse @ ****\n\n'+event.pageX.toString()+', '+event.pageY.toString();
	alert(s);
}


	
CjfpHtmlMenu.prototype.IsInMenu = function(x, y) {
	var z, l;
		
	for(z=0; z<this.showing.length; z++) {
		if(!z)
			l = new this.loc(this.showing[z]);
		else
			l.loc(this.showing[z]);
		if(x>=l.x && x<=l.r && y>=l.y && y<=l.b) return z;
	}
	return -1;
}
	


CjfpHtmlMenu.prototype.loc = function(object) {
	var x, y, p;

	/*** calc x & y of pop up menu ***/

	p=object;
	for(this.x=0, this.y=0; p; p=p.offsetParent) {
		this.x+=p.offsetLeft;
		this.y+=p.offsetTop;
		if(p.offsetParent && p.offsetParent.clientLeft) {
			this.x+=p.offsetParent.clientLeft;
			this.y+=p.offsetParent.clientTop;
		}
	}
	this.r = this.x+object.clientWidth-1;
	this.b = this.y+object.clientHeight-1;
	if(object.clientLeft) {
		this.r+=object.clientLeft*2;
		this.b+=object.clientTop*2;
	}
}



CjfpHtmlMenu.prototype.loc.prototype.loc = CjfpHtmlMenu.prototype.loc;



CjfpHtmlMenu.prototype.loc.prototype.toString = function() {
	return 'CjfpHtmlMenu.loc: '+ this.x.toString() + 'x' + this.y.toString() + ' - ' + this.r.toString() + 'x' + this.b.toString();
}



/*** class constants ***/

CjfpHtmlMenu.prototype.IE = jfpIsIE();

