// Kitmap.FrontControls.Menu.js
// (C) 2006 Henri Médot
// dernière révision le 18.05.2006
//******************************************************************************
// Classe KM_Menu

// Constructeur
function KM_Menu(id) {
	if (!document.getElementById) return;
	var ul = document.getElementById(id);
	this.table = document.createElement('table');
	this.table.cellSpacing = '0';
	this.table.cellPadding = '0';
	
	var lis = KM_Menu.getChildElementsByTagName(ul, 'li');
	for (var i = 0; i < lis.length; i++) {
		var li = lis[i];
		var cell = this.table.insertRow(-1).insertCell(-1);
		cell.className = li.className;
		KM_Menu.moveChildNodes(li, cell);
		new KM_MenuItem(this, cell);
	}
	ul.parentNode.insertBefore(this.table, ul);
	ul.parentNode.removeChild(ul);
}

// getChildElementsByTagName(elt, name) (statique)
KM_Menu.getChildElementsByTagName = function(elt, name) {
	var arr = [];
	var children = elt.childNodes;
	for (var i = 0; i < children.length; i++) {
		var child = children[i];
		if (child.nodeName.toLowerCase() == name.toLowerCase()) arr.push(child);
	}
	return arr;
};

// moveChildNodes(srcElt, dstElt) (statique)
KM_Menu.moveChildNodes = function(srcElt, dstElt) {
	var children = srcElt.childNodes;
	for (var i = 0; i < children.length; i++) {
		dstElt.appendChild(children[i].cloneNode(true));
	}
};

// getElementLeft(elt) (statique)
KM_Menu.getElementLeft = function(elt) {
	var x = 0;
	do {
		x += elt.offsetLeft;
		elt = elt.offsetParent;
		if ((document.all) && (elt.style.position == 'absolute')) break; 
	} while (elt != document.body);
	return x;
};

// getElementTop(elt) (statique)
KM_Menu.getElementTop = function(elt) {
	var y = 0;
	do {
		y += elt.offsetTop;
		elt = elt.offsetParent;
		if ((document.all) && (elt.style.position == 'absolute')) break;
	} while (elt != document.body);
	return y;
};



//******************************************************************************
// Classe KM_MenuItem

// Constructeur
function KM_MenuItem(owner, cell) {
	this.owner = owner;
	this.cell = cell;
	this.childMenu = null;
	this.link = null;
	var _this = this;
	
	var uls = KM_Menu.getChildElementsByTagName(cell, 'ul');
	if (uls.length > 0) {
		this.childMenu = new KM_ChildMenu(this, uls[0]);
		cell.onmouseover = function(e) {
			_this.childMenu.open();
		};
		cell.onmouseout = function(e) {
			_this.childMenu.close();
		};
	}
	
	var links = KM_Menu.getChildElementsByTagName(cell, 'a');
	if (links.length > 0) {
		this.link = links[0];
		if (this.link.target == '') this.link.target = '_self';
		cell.onclick = function(e) {
			window.open(_this.link.href, _this.link.target);
		};
	}
}



//******************************************************************************
// Classe KM_ChildMenu

// Constructeur
function KM_ChildMenu(owner, ul) {
	this.owner = owner;
	var _this = this;
	
	this.table = document.createElement('table');
	this.table.cellSpacing = '0';
	this.table.cellPadding = '0';
	this.table.style.cssText = 'display:none;position:absolute;visibility:hidden;left:0px;top:0px;';
	
	var lis = KM_Menu.getChildElementsByTagName(ul, 'li');
	for (var i = 0; i < lis.length; i++) {
		var li = lis[i];
		var cell = this.table.insertRow(-1).insertCell(-1);
		cell.className = li.className;
		KM_Menu.moveChildNodes(li, cell);
		new KM_MenuItem(this, cell);
	}
	ul.parentNode.insertBefore(this.table, ul);
	ul.parentNode.removeChild(ul);
}

// open()
KM_ChildMenu.prototype.open = function() {
	var style = this.table.style;
	if (style.visibility == 'visible') return;
	style.display = '';
	var left = KM_Menu.getElementLeft(this.owner.cell) + this.owner.cell.offsetWidth;
	var top = KM_Menu.getElementTop(this.owner.cell);
	var toplimit = document.body.clientHeight - this.table.offsetHeight;
	if (top > toplimit) top = toplimit;
	style.left = left + 'px';
	style.top = top + 'px';
	style.visibility = 'visible';
};

// close()
KM_ChildMenu.prototype.close = function() {
	this.table.style.visibility = 'hidden';
};

