function LFUtils() {}
LFUtils.ns4=(document.layers);
LFUtils.ie4=(document.all&&!document.getElementById);
LFUtils.ie5=(document.all&&document.getElementById);
LFUtils.ns6=(!document.all&&document.getElementById);
LFUtils.getElement = function (DivId){
	if(LFUtils.ie5||LFUtils.ns6) return(document.getElementById(DivId));
	if(LFUtils.ie4) return(document.all[DivId]);
	if(LFUtils.ns4)	return(document.layers[DivId]);
}
LFUtils.getHeight = function (DivId){
	var theid = LFUtils.getElement(DivId)
	return (theid.innerHeight)?theid.innerHeight:theid.clientHeight;
}
LFUtils.getWidth = function(DivId){
	var theid = LFUtils.getElement(DivId)
	return (theid.innerWidth)?theid.innerWidth:theid.clientWidth;
}
LFUtils.getStyle = function (id){
	if (LFUtils.ie5||LFUtils.ns6) return document.getElementById(id).style;
	if(LFUtils.ns4) return document.layers[id];
	if(LFUtils.ie4) return document.all[id].style;
}
LFUtils.getVScroll = function (){
	if(window.pageYOffset)
		return window.pageYOffset;
	else
		return document.body.scrollTop;
}

function LFMenu(_open,_slide,_id,_X,_Y) {
	this.open = _open; this.slide = _slide; this.id = _id;
	this.posX = _X;this.posY = _Y;
	this.initX = _X;this.initY = _Y;
	this.baseX = _X;this.baseY = _Y;
	this.subDeltaX = 0;this.subDeltaY = 0;
	this.children = [];
}
LFMenu.prototype.addMenuItem = function(menuItem){
	menuItem.setDeep(0,this.children.length,this);
	this.children[this.children.length] = menuItem;
}
LFMenu.prototype.initXY = function(tab){
	this.posY= this.initY;
	if (this.slide && !this.open) this.slideSubMenu(tab)
}


LFMenu.prototype.setShowing = function(_showing){}

LFMenu.prototype.openMenu = function(tab){
	this.isOpen = true;
	this.initXY(tab);
	for (var i=0;i<this.children.length;i++){
		this.children[i].setLocation (this.posX,this.posY);	
		this.children[i].setVisible(true);
		this.posY = this.posY+this.children[i].getYMove(tab);
		
		if (this.open || (tab.length > 0 && i == tab[0] ) ) {
			this.posX = this.posX+this.children[i].getXMove(tab);
			this.posY = this.children[i].open(this.posX,this.posY,tab,this.open);
			this.posX = this.posX-this.children[i].getXMove(tab);
		} else {
			this.children[i].close();
		}
	}
}
LFMenu.prototype.slideMenu = function(){
	var X = this.baseX;
	var Y = this.baseY-this.subDeltaY;
	if (LFUtils.getVScroll() > Y) Y = LFUtils.getVScroll();
	if (this.posX != X || this.posY != Y)
		this.setLocation (X,Y);
}
LFMenu.prototype.slideSubMenu = function(tab){
	this.subDeltaX = 0;
	this.subDeltaY = 0;
	for (var i=0;i<tab[0];i++)
		this.subDeltaY += this.children[i].getYMove(tab,this.open);
}
LFMenu.prototype.slideLocation = function(dX,dY){
	this.initX = this.initX+dX;
	this.initY = this.initY+dY;
	this.posX= this.initX;
	this.posY= this.initY;
	for (var i=0;i<this.children.length;i++)
		this.children[i].slideLocation(dX,dY);
}
LFMenu.prototype.setLocation = function(_X,_Y){
	var dX = _X - this.initX;
	var dY = _Y - this.initY;
	var vitesse = 3;
	dX = _X<this.initX?Math.floor(dX/vitesse):Math.ceil(dX/vitesse);
	dY = _Y<this.initY?Math.floor(dY/vitesse):Math.ceil(dY/vitesse);
	if (dX != 0 || dY !=0)
			this.slideLocation(dX,dY);
}
LFMenu.prototype.writeMenu = function(){
	for (var i=0;i<this.children.length;i++)
		this.children[i].writeMenu();
	if (this.slide)
		window.setInterval(this.id+".slideMenu()",100);
	this.slideLocation(0,0);
	this.openMenu([]);
}

function LFMenuItem(_id,_onMouseOver,_openOnClic,_label,_classe,_height,_width) {
	this.id = _id; this.onMouseOver = _onMouseOver; this.openOnClic= _openOnClic;
	this.label = _label; this.classe = _classe;
	this.children = [];	this.posX = 0; this.posY = 0;
	this.height = _height; this.width = _width; this.isOpen = false;
}
LFMenuItem.prototype.getHeight = function (){
	return LFUtils.getHeight(this.id);
}
LFMenuItem.prototype.getWidth = function(){
	return LFUtils.getWidth(this.id);
}
LFMenuItem.prototype.addMenuItem = function ( menuItem ) {
	menuItem.setDeep(this.level+1,this.children.length,this);
	this.children[this.children.length] = menuItem;
}
LFMenuItem.prototype.setDeep = function (_level,_index,_owner){
	this.level = _level; this.index = _index;	this.owner = _owner;
}
LFMenuItem.prototype.slideLocation = function(dX,dY){
	this.setLocation(this.posX+dX,this.posY+dY);
	for (var i=0;i<this.children.length;i++)
		this.children[i].slideLocation(dX,dY);
}
LFMenuItem.prototype.setLocation = function(X,Y){
	this.posX = X; this.posY = Y;
	LFUtils.getStyle(this.id).left = this.posX +"px";
	LFUtils.getStyle(this.id).top = this.posY +"px";
}
LFMenuItem.prototype.setVisible = function(visible){
	if (visible) {
		LFUtils.getStyle(this.id).zIndex = this.level;
		LFUtils.getStyle(this.id).visibility = "visible";
	} else
		LFUtils.getStyle(this.id).visibility = "hidden";
}
LFMenuItem.prototype.getYMove = function(tab,open){
	var res=this.getHeight();
	if (open)
		for (var i=0;i<this.children.length;i++)
			res+= this.children[i].getYMove(tab,open);
	return res;
}
LFMenuItem.prototype.getXMove = function(tab,open){
	//return 10;
	return 0;
}
LFMenuItem.prototype.close = function(){
	for (var i=0;i<this.children.length;i++){
		this.children[i].close();
		this.children[i].setVisible(false);
	}
	this.isOpen = false;
}
LFMenuItem.prototype.open = function(X,Y,tab,open){
	var posx = X; var posy = Y;
	for (var i=0;i<this.children.length;i++){
		this.children[i].setLocation (posx,posy);	
		this.children[i].setVisible(true);
		posy = posy+this.children[i].getYMove(tab);
		if (open || (tab.length > this.children[i].level && i == tab[this.children[i].level] ) ) {
			posx = posx+this.children[i].getXMove(tab);
			posy = this.children[i].open(posx,posy,tab,open);
			posx = posx-this.children[i].getXMove(tab);
		} else
			this.children[i].close();
	}
	this.isOpen = true;
	return posy;
}
LFMenuItem.prototype.show = function(){
	if (!this.isOpen ) {
		var posx = this.posX;
		var posy = this.posY+this.getYMove();
		this.setShowing(true);
		for (var i=0;i<this.owner.children.length;i++)
			if (i != this.index) this.owner.children[i].hide();
		for (var i=0;i<this.children.length;i++){
			posx = posx+this.children[i].getXMove();
			this.children[i].setLocation (posx,posy);		
			this.children[i].setVisible(true);
			LFUtils.getStyle(this.children[i].id).zIndex = this.level+3;
			posy = posy+this.children[i].getYMove();	
			posx = posx-this.children[i].getXMove();
		}
	}
}
LFMenuItem.prototype.hide = function(){
	//TODO : la gestion des menu dep > 3 est incorrecte
	if (!this.isOpen && !this.showing) {
		for (var i=0;i<this.children.length;i++){
			this.children[i].setVisible(false);
			this.children[i].hide();
		}
		this.hideMe();
	} 
}
LFMenuItem.prototype.hideMe = function(){
	if (!this.isOpen && !this.showing)
		for (var i=0;i<this.owner.children.length;i++)
			if (!this.owner.isOpen && !this.owner.showing) this.owner.children[i].setVisible(false);
}
LFMenuItem.prototype.writeMenu = function(){
	document.write("<ul id='"+this.id+"' style='visibility:hidden;position:absolute;width:"+this.width+"px;' class='"+this.classe+"'");
	if (this.onMouseOver)
		document.write(" onmouseover='javascript:"+this.id+".showMenu()' onmouseout='javascript:"+this.id+".hideMenu()'");
	if (this.openOnClic)
		document.write(" onclick='javascript:"+this.id+".openMenu([])'");
	document.write(">");
	document.write(this.label);
	document.write("</ul>");
	for (var i=0;i<this.children.length;i++)
		this.children[i].writeMenu();
}
LFMenuItem.prototype.openMenu = function(tab){
	tab[this.level]=this.index;
	this.owner.openMenu(tab);
}
LFMenuItem.prototype.showMenu = function(){
	this.show();
}
LFMenuItem.prototype.hideMenu = function(){
	this.setShowing(false);
	setTimeout(this.id+".hide()",150);
}
LFMenuItem.prototype.setShowing = function(_showing){
	this.showing = _showing;
	this.owner.setShowing(_showing);
}