/*********************************************************
  création de menu dynamique
  Harrison&Wolf
  version : 1.0
  Date de création : 21/11/03
  Date de modification :21/11/03
*********************************************************/
var menuList = new Array();
var popTimer = 0;
/*********************************************************
  definition des apparences
*********************************************************/
function NAVHAUT_HTML(menuObjName)
{
	//var relativePath='\/static\/fr\/';
	menuObj = getMenuByName(menuObjName);
	if(!menuObj) return "";

	var str ='';
	var CRLF = '\n\r';
		CRLF='';
	
	str+='<div id=\"'+menuObj.name+'\">'+CRLF;
	var tmpSize = menuObj.getSize().x
	str+='<table width=\"'+(tmpSize)+'\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" bgcolor="#FFFFFF">'+CRLF;
	str+='<tr>'+CRLF;
		str+='<td width=\"1\"><img src=\"'+menuObj.urlShim+'\" width=\"1\" height=\"1\"><\/td>'+CRLF;

	str+='<td width=\"10\"><img src=\"'+menuObj.urlShim+'\" name=\"'+menuObj.name+'UL\" id=\"'+menuObj.name+'UL\" width=\"10\" height=\"1\"><\/td>'+CRLF;
	str+='<td width=\"5\"><img src=\"'+menuObj.urlShim+'\" width=\"5\" height=\"1\"><\/td>'+CRLF;
	str+='<td width=\"'+(tmpSize-22)+'\"><img src=\"'+menuObj.urlShim+'\" width=\"'+(tmpSize-22)+'\" height=\"1\"><\/td>'+CRLF;
	str+='<td width=\"5\"><img src=\"'+menuObj.urlShim+'\" width=\"5\" height=\"1\"><\/td>'+CRLF;
	str+='<td width=\"1\"><img src=\"'+menuObj.urlShim+'\" width=\"1\" height=\"1\"><\/td>'+CRLF;

	str+='<\/tr>'+CRLF;
	var nbItems = menuObj.getNumberOfItems();
	for(var i=0;i<nbItems;i++)
	{
		var menuItem = menuObj.itemList[i];
		str+='<tr>'+CRLF;
		str+='<td><img src=\"'+menuObj.urlShim+'\" width=\"1\" height=\"1\"><\/td>'+CRLF;
		str+='<td class=\"layerNavHauteSelection\" align=\"center\" valign=\"top\"><img src=\"'+menuObj.urlShim+'\" name=\"'+"puce"+menuObj.getName()+ndigit(i,2)+'\" width=\"8\" height=\"17\" id=\"'+"puce"+menuObj.getName()+ndigit(i,2)+'\"><\/td>'+CRLF;
		str+='<td class=\"layerNavHauteTexte\" valign=\"top\"><img src=\"'+menuObj.urlShim+'\" width=\"1\" height=\"1\"><\/td>'+CRLF;
		
		var libelle = new String(menuItem.label);
		var re = /&apos;/g;
		libelle = libelle.replace(re, "'");    

		//debut modif LG040526
		if(menuItem.downloadName=='')
		{
			str+='<td class=\"layerNavHauteTexte\"><a href=\"'+menuItem.url+'\" class=\"layerNavHauteTexte\" target=\"'+menuItem.target+'\" onMouseOver=\"mouseoverMenuItem('+"\'"+menuObj.name+"\',"+ i +')\">'+libelle+'<\/a><\/td>'+CRLF;
		}	
		else
		{
			str+='<td class=\"layerNavHauteTexte\"><a href=\"'+menuItem.url+'\" class=\"layerNavHauteTexte\" onClick=\"xt_clic('+"'T','"+menuItem.downloadName+"'"+'\);return true;\" target=\"'+menuItem.target+'\" onMouseOver=\"mouseoverMenuItem('+"\'"+menuObj.name+"\',"+ i +')\">'+libelle+'<\/a><\/td>'+CRLF;
		}
		
		//fin modifs LG040526
		str+='<td class=\"layerNavHauteTexte\"><img src=\"'+menuObj.urlShim+'\" width=\"1\" height=\"1\"><\/td>'+CRLF;
		str+='<td><img src=\"'+menuObj.urlShim+'\" width=\"1\" height=\"1\"><\/td>'+CRLF;

		str+='<\/tr>'+CRLF;

		str+='<tr>'+CRLF;
		str+='<td><img src=\"'+menuObj.urlShim+'\" width=\"1\" height=\"'+((i==nbItems) ? '2' : '1')+'\"><\/td>'+CRLF;
		str+='<td><img src=\"'+menuObj.urlShim+'\" width=\"1\" height=\"1\"><\/td>'+CRLF;
		str+='<td><img src=\"'+menuObj.urlShim+'\" width=\"1\" height=\"1\"><\/td>'+CRLF;
		str+='<td><img src=\"'+menuObj.urlShim+'\" width=\"1\" height=\"1\"><\/td>'+CRLF;
		nameString = ( i==nbItems) ? ' name=\"'+menuObj.name+'BR\" id=\"'+menuObj.name+'BR\"':'';
		str+='<td align="right"><img src=\"'+menuObj.urlShim+'\" '+nameString+' width=\"1\" height=\"1\"><\/td>'+CRLF;
		str+='<td><img src=\"'+menuObj.urlShim+'\" width=\"1\" height=\"1\"><\/td>'+CRLF;
		str+='<\/tr>'+CRLF;
	}
	
	str+='<\/table>'+CRLF;
	str+='<\/div>'+CRLF;

	return str;
}

/*********************************************************
  wrappers
*********************************************************/

function initializeMenus()
{

	nbMenus = menuList.length;

	for(i=0;i<nbMenus;i++)
	{
		menuList[i].calculateSize();
		//document.write(menuString);
	}
}

// commande de creation des Div de menu
function createMenus(urlShim,urlPuce)
{
	var i;
	var menuString = '';
	nbMenus = menuList.length;
	
	// preload des images
	var shimObj = new Image();
	var puceObj = new Image();
	shimObj.src = urlShim;
	shimObj.src = urlPuce;
	
	for(i=0;i<nbMenus;i++)
	{
		menuList[i].urlShim = urlShim;
		menuList[i].urlPuce = urlPuce;
		menuList[i].writeCSSDefinition();
		menuList[i].writeHTML();
		menuList[i].setChild();
		
		//document.write(menuString);
	}
	
	//document.write(menuString);
	//document.close();

}

function openMenu(menuName)
{
	believeTimer = false;
	clearTimeout(popTimer);

	if(!menuName) return;	

	menuObj = getMenuByName(menuName)
	if(!menuObj) return ;
	hideall(menuName);
	menuObj.display();
}
function closeMenu(menuName) 
{

	believeTimer = true;
	
	if (!menuName)
		//appel venant de la page html suite à un mouseOut
		{ popTimer = setTimeout("hideall()", 500);
		}
	else
		//appel du à l'ouverture d'un autre menu
		{
			hideall(menuName);
		}
}
function hideall(menuName)
{

	if (!believeTimer && (typeof(menuName) != "string") && clientIs.ns && !clientIs.ns6 && !clientIs.ns7) return false;
	
	for(var i=0;i<nbMenus;i++)
	{
		if(menuList[i].name==menuName || !menuList[i].layerObject.displayed) continue;
		menuList[i].layerObject.hide();
	}
}
function addMenu(menuName,menuSize,menuType,menuRefCorner)
{
	// vérification du nom du layer... underscore est incompatible avec Netscape 4.x
	if(menuName.indexOf("_")>=0)
	{
		// debug("Impossible de définir un menu dont le nom contient le caractère '_' (underscore)");
		return ;
	}
	
	var newMenu = new menu(menuName,menuSize,menuType,menuRefCorner);
	
	// ajout des menus
	menuList[menuList.length] = newMenu;
	nbMenus = menuList.length;
}

function addItemToMenu(menuName,ItemLabel,itemURL,itemTarget,xtClicDownloadName,targetMen)
{
	//var tmpArray = new Array(ItemLabel,itemURL,itemTarget,targetMenu);
	
	menuObj = getMenuByName(menuName)
	
	if(!menuObj) 
	{
		//// debug("Menu '"+menuName+"' non défini. Impossible d'ajouter l'item '"+ItemLabel+"'");
		addMenu(menuName,'140','NAVGAUCHE'); //Nicolas
		menuObj = getMenuByName(menuName);
		//if(!menuObj) // debug("ZUT, il est pas créé");
		//return -1;
	}
	menuObj.addItem(ItemLabel,itemURL,itemTarget,xtClicDownloadName,targetMen);
	return 0;
}

function mouseoverMenuItem(menuName,numero)
{
	// on efface les puces précédemment affichées
	menuObj = getMenuByName(menuName);
	if(!menuObj) return null;

	menuObj.mouseoverMenuItem(numero);

}
/***********************************************************************************
* 
***********************************************************************************/
function getMenuByName(menuName)
{
	var menuObj ;
	var nbLayers = menuList.length
	var i;
	for(i=0;i<nbLayers;i++)
		if(menuList[i].name==menuName)
			return menuList[i];

	return null;
}


/*********************************************************
  	class menu
	modif LG040605
*********************************************************/
function menu(menuName,menuSize,menuType,menuRefCorner)
{
	// vérification du nom du layer... underscore est incompatible avec Netscape 4.x
	if(menuName.indexOf("_")>=0)
	{
		// debug("Impossible de définir un menu dont le nom contient le caractère '_' (underscore)");
		return ;
	}
	
	
	//propriétés
	this.name = menuName;
	this.type = menuType;
	//this.width = menuSize;
	
	if(!menuRefCorner) {
	     this.refCorner=0;
		 }
		 else
		 {
		 	if(menuRefCorner=="lt") this.refCorner=0;  // left top
			else
				if(menuRefCorner=="rt") this.refCorner=1; // right top
				else
					if(menuRefCorner=="rb") this.refCorner=2; // right bottom
					else
						if(menuRefCorner=="lb") this.refCorner=3; // left bottom
						else
							if(menuRefCorner=="ct") this.refCorner=4; // center top
							else
								if(menuRefCorner=="cb") this.refCorner=5; // center bottom
		} ; // 0= haut gauche ( 1= haut droite; 2 = bas droite; 3; bas gauche)
	
	this.urlShim = '';
	this.urlPuce = '';
	this.height = 0;
	this.itemList = new Array();
	this.nbItems = 0;
	this.left = 0;
	this.top = 0;
	this.parent = null;
	this.size = new point()
	this.size.x = menuSize;
	this.layerObject = new HWLAYER(this.name);
	this.position = new point();
	
	this.getName = function()
	{
		return this.name;
	}
	this.getSize = function()
	{
		return this.size;
	}
	// methodes
	this.addItem = function(ItemLabel,itemURL,itemTarget,xtClicDownloadName,targetMen) {
		if(!xtClicDownloadName) xtClicDownloadName ='';
		xtClicDownloadName = xtClicDownloadName.replace(/ +/g,'_')
		xtClicDownloadName = xtClicDownloadName.replace(/[àâä]/g,'a');
		xtClicDownloadName = xtClicDownloadName.replace(/[éèêë]/g,'e');
		xtClicDownloadName = xtClicDownloadName.replace(/[îïì]/g,'i');
		xtClicDownloadName = xtClicDownloadName.replace(/[ôöò]/g,'o');
		xtClicDownloadName = xtClicDownloadName.replace(/[ûüù]/g,'u');
		
		var newItem = new menuItem(this,ItemLabel,itemURL,itemTarget,xtClicDownloadName,targetMen);
		
		this.itemList[this.nbItems] = newItem;
		
		newItem.setRollImage("puce"+this.getName()+ndigit(this.nbItems,2));
		this.nbItems = this.itemList.length;
		return newItem;
		}
	this.getItemByIndex = function(itemIndex)
		{
			
			return (itemIndex>this.nbItems) ? null : this.itemList[itemIndex];
		}
	this.getNumberOfItems = function()
		{
			return this.nbItems;
		}
	this.setChild = function() {
			for(var i=0;i<this.nbItems;i++)
			{
				var obj;
				if(!this.itemList[i].targetMenu) continue;
				obj = getMenuByName(this.itemList[i].targetMenu);
				if(!obj) continue;
				obj.parent = this;
			}
		}
	this.isParent = function(layerName) {
	
		var parentArray = this.getParentArray();
			//// debug("is parent"+parentArray[0]);
		for(var i=0;i<parentArray.length;i++)
		{
			//// debug("parent name="+parentArray[i].name);
			if(parentArray[i].name==layerName) return true;
		}
		return false;
	}
	this.getParentArray = function(){
			var parentArray = new Array();
			tmpParent = this;
			
			while(tmpParent!=null)
			{		
				parentArray[parentArray.length]=tmpParent.name;				
				tmpParent = tmpParent.parent;		
			}
			
			return parentArray;
		}
	this.calculateSize = function(){	
		var a=getElementPosition(this.name+'UL');
		var b=getElementPosition(this.name+'BR');
		if(!this.size.x) this.size.x = b.x - a.x;
		if(!this.size.y) this.size.y = b.y - a.y;
		}
	this.writeCSSDefinition = function(){
		var CRLF = '\n\r';
		CRLF='';
		var CSSDefinitionString ='<style type=\"text\/css\">';
		CSSDefinitionString += '#'+this.name+'{position:absolute; visibility:hidden; z-index:2; left: '+this.left+'px; top: '+this.top+'px}';
		CSSDefinitionString +='<\/style>';
		document.write(CSSDefinitionString);
		document.close();
		return CSSDefinitionString;
		}
	this.writeHTML = function(){
	//// debug(this.type);
		HTMLString = eval(this.type+"_HTML('"+this.getName()+"')");
		
		document.write(HTMLString);
		document.close();
		return HTMLString;
		}
	this.mouseoverMenuItem = function(numero)
	{
		this.hide();

		menuItemObj=this.getItemByIndex(numero);
		if(!menuItemObj) return;
		
		menuItemObj.setRollImageSrc(this.urlPuce);

	}
	this.hide = function()
	{
		//this.layerObject.hide();
		for (i=0;i<=this.nbItems;i++)
		{
			menuItemObj=this.getItemByIndex(i);
			if(!menuItemObj) continue;
			menuItemObj.setRollImageSrc(this.urlShim);
		}
		
	}
	this.setPosition = function(x,y)
	{
		this.layerObject.setPosition(x,y);
	}
	this.getPosition = function()
	{
		return this.layerObject.getPosition();
	}
	this.display = function()
	{
	
		//modifié le 11/12/03
		var targetMenuObj;
		var targetMenuStyle;
		// on vérifie que le menu est déclaré. si non, on le déclare
		//if(getArrayPos(lay,menuName)<0) lay[lay.length]=menuName;
	
	
		//pour netscape
 		;
		if(clientIs.ns && !clientIs.ns6 && !clientIs.ns7)
		{
			setTimeout("believeTimer = false;", 200);
			//if (typeof(menuName) != "string") return false;
		}

		// on recherche le layer et son style pour pouvoir le manipuler
		/* targetMenuObj = getLayerObject(menuName);
		if(!targetMenuObj) return;
		targetMenuStyle = getLayerObject(menuName,true);
		if(!targetMenuStyle) return;
	*/
	/*
		if(!this.layerObject) this.layerObject = this.getLayerObject();
		targetMenuObj = this.layerObject;
		targetMenuStyle = this.getLayerObject(true);
		*/
		//on efface tous les menu qui était visible
		//hideall(this.name);
		this.layerObject.display();
		//on rend visible le menu à ouvrir
		/*
		targetMenuObj.onmouseout =closeMenu;
		targetMenuObj.onmouseover=openMenu;
	
		targetMenuStyle.visibility = ((clientIs.ie || clientIs.ns6 || clientIs.ns7) ? ("visible") : "show" );
		*/
	}
	
	/*this.getLayerObject = function(returnStyle)
	{

		if(clientIs.ns && !clientIs.ns6 && !clientIs.ns7) return document[this.name];
	
		if(!document.getElementById(this.name)) return null;
		if(!returnStyle) return document.getElementById(this.name);
		return document.getElementById(this.name).style;
	}
	*/
}
/*********************************************************
  class menuItem
*********************************************************/
function menuItem(par,ItemLabel,itemURL,itemTarget,xtClicDownloadName,targetMen)
{
	if(!par) return null;
	this.parent = par;
	this.downloadName = xtClicDownloadName;
	this.targetMenu = targetMen;
	this.label = ItemLabel;
	this.url = itemURL;
	this.target = itemTarget;
	this.image = null;
	if(!xtClicDownloadName) this.downloadName = '';
	
	//if(!targetMen) this.targetMenu = targetMen;

	//if(!targetMen) this.targetMenu = '';
	this.setRollImage = function(imageName)
	{
		
		this.image = new point(imageName);
		return this.image;
	}
	this.setRollImageSrc = function(imageURL)
	{
		var imageRef = this.image.getRef();
		if(!imageRef) return null;

		imageRef.src = imageURL;
		return imageRef.src;
	}

}
