 /*	==========================================================
	Filename		: ft_menubar.js
	Author			: Copyright 2003 ISNPRO - Action 2i
	Last Updated	: jeudi 20 mars 2003
	Comments		: les modifications sont indiquées en commentaires
	==========================================================*/

window.CMenus=[];
/*
object jsMenu

*/
function jsMenu(name, items){

	// properties

	this.bi=new Image();
	this.bi.src=PIXIMG;
	
	if (!window.CMenus) window.CMenus=[];
	window.CMenus[name]=this;
	
	if (!window.CMenuHideTimers) window.CMenuHideTimers=[];
	window.CMenuHideTimers[name]=null;
	
	this.name=name;
	this.root=[];
	this.root.par=null;
	this.root.cd=[];
	this.root.fmt=items[0];
	this.items=[];
	
	// define position of the menu bar
	this.root.frameoff = (items[0].pos)? items[0].pos:[0,0];
	
	
	// define root menu (menubar)
	this.root.lvl=new menuPanel(this, this.root);
	// create  menus items
	for (var i=1;i<items.length;i++) 
		if (!und(items[i])) new menuItem(this, this.root, items[i], und(items[i].format)?items[0]:items[i].format);
	
	
	// ====================== methods ============================

	/*
	function jsMenu.draw
	draw the menu bar and all menus and submenus
	*/
	this.draw=function (){
		for (var i=0;i<this.items.length;i++) d.write (this.items[i].draw());
	}
	
	/*
	hide function
	
	*/
	this.hide=function(){
		if (this.root.fmt.popup) 
			this.root.lvl.vis(0);
		else {
			for (var i=0;i<this.root.cd.length;i++) if (this.root.cd[i].lvl) this.root.cd[i].lvl.vis(0);
			this.root.lvl.a=null;
			this.root.lvl.draw();
			if (this.root.fmt.hidden_top) this.root.lvl.vis(0);
		}
	}
	
	/*
	move popup function
	
	*/
	this.mpopup=function(ev,offX,offY){
		var x=ev.pageX?ev.pageX:(op?ev.clientX:ie?ev.clientX+d.body.scrollLeft:ev.x+d.body.scrollLeft);
		var y=ev.pageY?ev.pageY:(op?ev.clientY:ie?ev.clientY+d.body.scrollTop:ev.y+d.body.scrollTop);
		var po=this.root.fmt.popupoff;
		y += offY?offY:po?po[0]:0;
		x += offX?offX:po?po[1]:0;
		this.popup(x, y);
	}
	
	/*
	menu popup function
	
	*/
	this.popup=function(x,y){
		this.move(x,y);
		this.root.lvl.a=null;
		this.root.lvl.vis(1);
		mEvent(this.name,0,'t');
		mEvent(this.name,0,'0');
	}
	
	/*
	menu move function
	
	*/
	this.move=function(x,y){
		if (!this.root.pos || this.root.pos[0] != x || this.root.pos[1] != y) {
			this.root.pos=[x,y];
			this.root.loff=[0,0];
			this.root.ioff=[0,0];
			for (var i=0;i<this.items.length;i++){
				this.items[i].setPosFromParent();
				this.items[i].move(this.items[i].pos[0],this.items[i].pos[1]);
			}
		}
	}
	
	this.draw();
	if (!this.root.fmt.popup && !this.root.fmt.hidden_top) 
		this.root.lvl.vis(1)
	else
		this.root.lvl.vis(0)
		
		
} // end object jsMenu

/* ========================================================================================
object menuPanel 
create a new menu panel.
could be the main menu bar or a sub menu

menu : menu parent (object)
par : array info for the menu.

*/
function menuPanel(menu, par){
	this.menu=menu;
	this.par=par;
	this.v=0;
	
	/*
	function menuPanel.vis 
	
	*/
	this.vis=function(s){
		var ss=this.v;
		this.v=s;
		var l=this.par.cd.length;
		for (var i=0;i<l;i++){
			var n=this.par.cd[i];
			if ( n.hc() && n.lvl.v && !s ) n.lvl.vis(s);
			n.vis(s);
		}
		if (!s) this.a=null;
		if (this.v!=ss&&this.menu.onlevelshow) this.menu.onlevelshow(this);
	}
	/*
	function menuPanel.setA
	
	*/
	this.setA=function(idx,s){
        var n=this.menu.items[idx];
		if (nn(this.a)&&n.par.lvl!=this.a.par.lvl) return;
		if(s&&n.hc())n.lvl.vis(1);
		if( s && n!= this.a && nn(this.a) && this.a.hc() && this.a.lvl.v ) this.a.lvl.vis(0);
		this.a=n;
		this.draw();
	}
	/*
	function menuPanel.draw
	
	*/
	this.draw=function(){
		if (this.menu.root.lvl==this&&this.menu.root.fmt.hidden_top) return;
		for (var i=0;i<this.par.cd.length;i++)
			if (this.par.cd[i]==this.a)
				this.par.cd[i].setVis('o');
			else
				this.par.cd[i].setVis('n');
	}
	
} // end object menuPanel


/* ========================================================================================
object menuItem
create a new menu item.

new menuItem(this, this.root, items[i], und(items[i].format)?items[0]:items[i].format);
menu : menu maître (object)
par : menu parent (object)
item : array of info for this item
format :

*/
function menuItem(menu, par, item, format){
	if (und(item)) return;
	this.lvl=null;this.par=par;
	this.code=item.code;
	this.ocode=item.ocode?item.ocode:item.code;
	this.targ=und(item.target)?"":'target="'+item.target+'" ';
	this.onclickscript=item.onclickscript; // ceci est pour rajouter du javascript dans l'évènement "onClick=..."
	this.url=und(item.url)?"javascript:;":item.url;
	this.fmt=format;
	this.menu=menu;
	this.bw=menu.bw;
	this.cd=[];
	this.divs=[];
	this.index=menu.items.length;
	menu.items[menu.items.length]=this;
	this.pindex=par.cd.length;
	par.cd[par.cd.length]=this;
	this.id="cmi"+this.menu.name+"_"+this.index;
	this.v=0;this.state='n';
	this.diva=["b","s","o","n","e"];
	
	this.hc=function(){return this.cd.length > 0};
	
	/*
	function menuItem.div 
	return a reference to a DIV element
	*/
	this.div=function(n){
		return und(this.divs[n])?this.divs[n]=gE(this.id+n):this.divs[n];
	};
	
	/*
	function menuItem.draw
	draw a menu item
	*/
	this.draw=function (){	
		var b=this.style.border;
		var s=this.style.shadow;
		return  (!this.style.shadow?"":adiv(this.menu.bw, this.id+"s", parseInt(this.z)+1, this.pos[0]+s, this.pos[1]+s, this.size[1], this.size[0], this.style.color.shadow, "", "",""))
				+ (!this.style.border?"":adiv(this.menu.bw, this.id+"b", parseInt(this.z)+2, this.pos[0], this.pos[1], this.size[1], this.size[0], this.style.color.border, "", "",""))
				+ adiv(this.menu.bw, this.id+"o", parseInt(this.z)+3, this.pos[0]+b, this.pos[1]+b, this.size[1]-b*2, this.size[0]-b*2, this.style.color.bgOVER, '<div class="'+this.style.css.OVER+'">'+this.ocode+'</div>', "")
				+ adiv(this.menu.bw, this.id+"n", parseInt(this.z)+4, this.pos[0]+b, this.pos[1]+b, this.size[1]-b*2, this.size[0]-b*2, this.style.color.bgON, '<div class="'+this.style.css.ON+'">'+this.code+'</div>', "")
				+ adiv(this.menu.bw, this.id+"e", parseInt(this.z)+5, this.pos[0]+b, this.pos[1]+b, this.size[1]-b*2, this.size[0]-b*2, "",
					'<a href="'+this.url+'" '+this.targ+'onclick="'+this.onclickscript+';mEvent(\''+this.menu.name+'\','+this.index+',\'c\');"  onmouseover="mEvent(\''
					+this.menu.name+'\','+this.index+',\'o\');" onmouseout="mEvent(\''+this.menu.name+'\','+this.index+',\'t\');">'+'<img src="'
					+this.menu.bi.src+'" width="'+this.size[1]+'" height="'+this.size[0]+'" border="0"></a>', "", '' );
	}
	
	this.vis=function(s){
		if (this.style.shadow) this.visDiv("s",s);
		if (this.style.border) this.visDiv("b",s);
		if (!s) {
			this.visDiv("o",0);
			this.visDiv("n",0);this.state="n";
		}
		else if (this.state=="n") this.visDiv("n",1);
		else this.visDiv("o",1);
		this.visDiv("e",s);
	}
	
	/*
	function set visibility of an element
	update state
	
	*/
	
	this.setVis=function (n){
		if (this.state!=n)
			switch (n){
				case "n":this.visDiv("n",1);this.visDiv("o",0);break;
				case "o":this.visDiv("n",0);this.visDiv("o",1);break;
			}
		this.state=n;
	}
	
	// define methods
	this.visDiv=visDivDom;
	
	/*
	function getf
	
	*/
	this.getf=function(obj, name){
		if (!und(obj) && nn(obj) && !und(obj.fmt)) {
			if (!und(obj.fmt[name]))
				return obj.fmt[name]; 
			if (obj.par!=this.menu.root && obj.par && obj.par.sub && obj.par.sub[0][name]) 
				return obj.par.sub[0][name]; 
			return this.getf(obj.par, name);}
		return;
	}
	
	
	
	this.ioff=this.getf(this, "itemoff");
	this.loff=this.getf(this, "leveloff");
	this.style=this.getf(this, "style");
	this.size=this.getf(this, "size");
	this.prev=this.pindex==0? null : this.par.cd[this.pindex-1];
	
	/*
	function setpos
	
	*/
	this.setPos=function(){
		if (this.prev==null){
			this.z=this.par == this.menu.root? 0: parseInt(this.par.z)+10;
			this.pos=und(this.fmt.pos)?(this.par == this.menu.root? this.menu.root.fmt.pos : this.pos=[this.par.pos[0]+this.loff[1], this.par.pos[1]+this.loff[0]]):this.fmt.pos;
		}else{
			this.prev.next=this;
			this.z=this.prev.z;
			this.pos=[this.prev.pos[0]+this.ioff[1], this.prev.pos[1]+this.ioff[0]];
		}
	}
	
	
	this.setPos();
	
	this.sub=item.sub;
	
	if (!und(this.sub) && !und(this.sub.length)&& this.sub.length>0){
		this.lvl=new menuPanel(menu, this);
		for (var i=1;i<this.sub.length;i++)
			if (!und(this.sub[i])) new menuItem(this.menu, this, this.sub[i], und(this.sub[i].format)?this.sub[0]: this.sub[i].format);
	}
	
	/*
	function setPosFromParent
	 set position of an item from parent position
	
	*/
	this.setPosFromParent=function(){
		if (this.index == 0) {
			this.pos=[this.menu.root.pos[0], this.menu.root.pos[1]]
		} else 
		if (this.prev==null){
			this.pos=[this.par.pos[0]+this.loff[1], this.par.pos[1]+this.loff[0]];
		}else{
			this.pos=[this.prev.pos[0]+this.ioff[1], this.prev.pos[1]+this.ioff[0]];
		}
	}
	
	/*
	function move
	
	
	*/
	this.move=function( x, y ){
		var bl=bt=this.style.border;
		if (this.style.shadow) this.moveTo(x+parseInt(this.style.shadow),y+parseInt(this.style.shadow),"s");
		if (this.style.border) this.moveTo(x,y,"b");
		this.moveTo(x+bl,y+bt,"o");
		this.moveTo(x+bl,y+bt,"n");
		this.moveTo(x+bl,y+bt,"e");
	}
	
	/*
	function moveTo
	
	
	*/
	this.moveTo=function( x, y, b ){
			this.div(b).style.left=x;
			this.div(b).style.top=y;
	}
	return this;
	
} // end object jsMenu


/*
function adiv
return a div string

*/
function adiv(bw,name,z,left,top,width,height,bgc,code,otherCSS, otherDIV){
	return '<div id="'+name+'" style="position:absolute;z-index:'+z+';left:'+left+'px;top:'+top+'px;width:'+width+'px;height:'+height+'px;visibility:hidden'+(bgc!=""?';background-color:'+bgc+'':'')+';'+(otherCSS?otherCSS+';':'')+'" '+otherDIV+'>'+code+'</div>';
}

function visDivDom(d,s){
	this.div(d).style.visibility=s?'visible':'hidden';
}

/*
function mEvent

*/
function mEvent(m,node_index,e) {
	if (nn(window.CMenuHideTimers[m])) {
		window.clearTimeout(window.CMenuHideTimers[m]);
		window.CMenuHideTimers[m]=null;
	}
	switch (e){
		case "o": 
			window.CMenus[m].items[node_index].par.lvl.setA(node_index,1);
			if (window.CMenus[m].onmouseover) window.CMenus[m].onmouseover(window.CMenus[m].items[node_index]);
			break;
		case "c":
			if (window.CMenus[m].items[node_index].hc()) 
				window.CMenus[m].items[node_index].lvl.vis(!window.CMenus[m].items[node_index].lvl.v);
			else
				for (var i=0;i<window.CMenus[m].root.cd.length;i++) if (nn(window.CMenus[m].root.cd[i].lvl)) window.CMenus[m].root.cd[i].lvl.vis(0);
			if (window.CMenus[m].onclick) window.CMenus[m].onclick(window.CMenus[m].items[node_index]);
			break;
		case "t":
			window.CMenuHideTimers[m]=setTimeout('window.CMenus["'+m+'"].hide()', und(window.CMenus[m].root.fmt.delay)?600:window.CMenus[m].root.fmt.delay);
			if (window.CMenus[m].onmouseout) window.CMenus[m].onmouseout(window.CMenus[m].items[node_index]);
			break;
	}
	return true;
}


function CMenuPopUp(menu, evn, offX, offY){
	window.CMenus[menu].mpopup(evn, offX, offY);
}

function CMenuPopUpXY(menu,x,y){
	window.CMenus[menu].popup(x,y);
}

function nn(val){return val != null;}
function und(val){return typeof(val) == 'undefined';}


// Return true if the given element currently has the given class name.
function hasClass(el, name) {
  var i, list;
  list = el.className.split(" ");
  for (i = 0; i < list.length; i++)  if (list[i] == name) return true;
  return false;
}
  // Remove the given class name from the element's className property.
function remClass(el, name) {
  var i,cList,nList;
  if (el.className!=null){
  nList = new Array();
  cList = el.className.split(" ");
  for (i=0;i<cList.length;i++) if(cList[i] != name) nList.push(cList[i]);
  el.className = nList.join(" ");
  }

}


