menuMC.DEBUG = false;


function findPosX(obj){
     var curleft = 0;
     if (obj.offsetParent){
          while (obj.offsetParent){
               curleft += obj.offsetLeft
               obj = obj.offsetParent;
          }
     }
     else if (obj.x)
          curleft += obj.x;
          //curleft += 10;
     return curleft;
}

function findPosY(obj){
     var curtop = 0;
     if (obj.offsetParent){
          while (obj.offsetParent){
               curtop += obj.offsetTop
               //curtop +=10;
               obj = obj.offsetParent;
          }
     }else if (obj.y)
          curtop += obj.y;
     return curtop;
}


function menuMC(pagesurl,InstanceName){
	this.a_menu = new Array;
	
	this.TplMenu = ""

	this.TplsMenuPrefix = ""
	this.TplsMenu = ""
	this.TplsMenuSuffix = ""
	
	this.TplsMenuTpPrefix = ""
	this.TplsMenuTp = ""
	this.TplsMenuTpSuffix = ""

	this.DivsMenu = document.getElementById("sMenu");
	this.DivmenuSCat = document.getElementById("menuSCat");

	this.DivsMenuTP = document.getElementById("sMenuTP");
	this.DivmenuSCatTP = document.getElementById("menuSCatTP");
	
	this.tabSCat = new Array();
	this.tabSCatTP = new Array();
	
	this.tabColor = new Array();
	this.tabColorOver = new Array();
	this.tabPolice = new Array();
	this.tabPoliceOver = new Array();
	
	this.MenuColor = "";
	this.MenuColorOver = "";
	this.MenuPolice = "";
	this.MenuPoliceOver = "";
	
	this.sMenuColor = "";
	this.sMenuColorOver = "";
	this.sMenuTpColor = "";
	this.sMenuTpColorOver = "";
	
	this.sMenuPolice = "";
	this.sMenuPoliceOver = "";
	this.sMenuTpPolice = "";
	this.sMenuTpPoliceOver = "";
	
	this.tabdecal = new Array();

	this.rolled = 0;
	this.sRolled = 0;
	this.sTPRolled = 0;
	this.currentMenu = 0;
	this.currentSMenu = 0;
	
	this.ToHideStack = new Array;
	this.StackJmp = 0;
	
	this.pagesurl = pagesurl;
	this.InstanceName = InstanceName;
	
	this.MenuWidth = 1024;
	
	this.FixedTop = true;
	this.FixedLeft = false;

	this.sMenuPixelTop = 0;
	this.sMenuPixelLeft = 0;
	this.sMenuTpPixelTop = 0;
	this.sMenuTpPixelLeft = 0;
	
	this.sMenuSize = 0;
	this.sMenuTpSize = 0;
	
	this.CustomMenuRoll = false;
	this.CustomsMenuRoll = false;
	this.CustomsMenuTPRoll = false;
	
	if('function' == eval("typeof window." + this.InstanceName + "_MenuRoll"))
		this.CustomMenuRoll = true;
	if('function' == eval("typeof window." + this.InstanceName + "_sMenuRoll"))
		this.CustomsMenuRoll = true;
	if('function' == eval("typeof window." + this.InstanceName + "_sMenuTPRoll"))
		this.CustomsMenuTPRoll = true;

	
	if(menuMC.DEBUG){
		this.DEBUG_AREA = document.getElementById("menuMC_DEBUG");
	}
}




menuMC.MenuKeywords = new Array;
menuMC.MenuKeywords["__MenuOver__"]	= "this.InstanceName+'.menuRoll('+this.a_menu[ii][3]+',1,'+this.a_menu[ii][1]+')'";
menuMC.MenuKeywords["__MenuOut__"]	= "this.InstanceName+'.menuRoll('+this.a_menu[ii][3]+',0,'+this.a_menu[ii][1]+')'";
menuMC.MenuKeywords["__MenuBG__"]	= "((this.MenuColor) ? this.MenuColor : this.tabColor[this.a_menu[ii][3]])";
menuMC.MenuKeywords["__MenuLink__"]	= "this.pagesurl+this.a_menu[ii][4]+'-c-'+this.a_menu[ii][3]+'.htm'";
menuMC.MenuKeywords["__MenuID__"]	= "'menu'+this.a_menu[ii][3]";
menuMC.MenuKeywords["__MenuText__"]	= "this.a_menu[ii][0]";

menuMC.sMenuKeywords = new Array;
menuMC.sMenuKeywords["__MenuText__"]= "this.a_menu[ii][2][jj][0]";
menuMC.sMenuKeywords["__MenuLink__"]= "this.pagesurl+this.a_menu[ii][2][jj][4]+'-c-'+this.a_menu[ii][2][jj][3]+'.htm'";
menuMC.sMenuKeywords["__MenuOver__"]= "this.InstanceName+'.sMenuRoll('+this.a_menu[ii][2][jj][3]+',1,'+this.a_menu[ii][2][jj][1]+','+this.a_menu[ii][2][jj][2].length+')'";
menuMC.sMenuKeywords["__MenuOut__"]	= "this.InstanceName+'.sMenuRoll('+this.a_menu[ii][2][jj][3]+',0)'";
menuMC.sMenuKeywords["__MenuID__"]	= "'smenu'+this.a_menu[ii][2][jj][3]";

menuMC.sMenuTpKeywords = new Array;
menuMC.sMenuTpKeywords["__MenuText__"]= "this.a_menu[ii][2][jj][2][kk][0]";
menuMC.sMenuTpKeywords["__MenuLink__"]= "this.pagesurl + this.a_menu[ii][2][jj][2][kk][4]+'-c-'+this.a_menu[ii][2][jj][3]+'-tp-'+this.a_menu[ii][2][jj][2][kk][3]+'.htm'";
menuMC.sMenuTpKeywords["__MenuOver__"]= "this.InstanceName+'.sMenuTPRoll('+this.a_menu[ii][2][jj][2][kk][3]+',1)'";
menuMC.sMenuTpKeywords["__MenuOut__"] = "this.InstanceName+'.sMenuTPRoll('+this.a_menu[ii][2][jj][2][kk][3]+',0)'";
menuMC.sMenuTpKeywords["__MenuID__"]  = "'smenutp'+this.a_menu[ii][2][jj][2][kk][3]";


menuMC.prototype.FiltreStr = function(str){
	return str.replace("'", "\\'");
}
menuMC.prototype.Debug = function(str){
	if(!menuMC.DEBUG) return;
	this.DEBUG_AREA.value = str;
	window.status = str;
}

/************** Construction *******************/
menuMC.prototype.AddCat = function(Nom, Count, ID, Libelle, RefID){
	var itemIndex = this.a_menu.length;

	this.a_menu[itemIndex] = new Array;
	this.a_menu[itemIndex][0] = Nom;
	//this.a_menu[itemIndex][1] = Count;
	this.a_menu[itemIndex][1] = itemIndex;
	this.a_menu[itemIndex][2] = new Array;
	this.a_menu[itemIndex][3] = ID;
	this.a_menu[itemIndex][4] = this.FiltreStr(Libelle);
	this.tabSCat[RefID] = "";
}

menuMC.prototype.AddsCat = function(Nom, Count, ID, Libelle, RefID){
	var catIndex = this.a_menu.length-1;
	var itemIndex = this.a_menu[catIndex][2].length;

	this.a_menu[catIndex][2][itemIndex] = new Array;
	this.a_menu[catIndex][2][itemIndex][0] = Nom;
	//this.a_menu[catIndex][2][itemIndex][1] = Count;
	this.a_menu[catIndex][2][itemIndex][1] = itemIndex;
	this.a_menu[catIndex][2][itemIndex][2] = new Array;
	this.a_menu[catIndex][2][itemIndex][3] = ID;
	this.a_menu[catIndex][2][itemIndex][4] = this.FiltreStr(Libelle);
	this.tabSCatTP[RefID] = "";
}

menuMC.prototype.AddsCatTP = function(Nom, Count, ID, Libelle){
	var catIndex = this.a_menu.length-1;
	var scatIndex = this.a_menu[catIndex][2].length-1;
	var TPIndex = this.a_menu[catIndex][2][scatIndex][2].length;

	this.a_menu[catIndex][2][scatIndex][2][TPIndex] = new Array;
	this.a_menu[catIndex][2][scatIndex][2][TPIndex][0] = Nom;
	//this.a_menu[catIndex][2][scatIndex][2][TPIndex][1] = Count;
	this.a_menu[catIndex][2][scatIndex][2][TPIndex][1] = TPIndex;
	this.a_menu[catIndex][2][scatIndex][2][TPIndex][3] = ID;
	this.a_menu[catIndex][2][scatIndex][2][TPIndex][4] = this.FiltreStr(Libelle);
}

menuMC.prototype.DrawMenu = function(){
	var Parsed;
	
	for(ii=0; ii < this.a_menu.length; ii++){
	
		Parsed = this.TplMenu;
		for(var i in menuMC.MenuKeywords){
			var str = eval(menuMC.MenuKeywords[i]);
			if(!str) str = "";
			Parsed = eval("Parsed.replace(/"+i+"/g, str)");
		}
		document.write(Parsed);
		
		this.tabSCat[this.a_menu[ii][3]] = this.TplsMenuPrefix;
		for(jj=0; jj < this.a_menu[ii][2].length; jj++){
				
			Parsed = this.TplsMenu;
			for(var i in menuMC.sMenuKeywords){
				var str = eval(menuMC.sMenuKeywords[i]);
				if(!str) str = "";
				Parsed = eval("Parsed.replace(/"+i+"/g, str)");
			}
				
			this.tabSCat[this.a_menu[ii][3]] += Parsed; 
			this.tabSCatTP[this.a_menu[ii][2][jj][3]] = this.TplsMenuTpPrefix;
			
			for(kk=0; kk < this.a_menu[ii][2][jj][2].length; kk++){
				Parsed = this.TplsMenuTp;
				for(var i in menuMC.sMenuTpKeywords){
					var str = eval(menuMC.sMenuTpKeywords[i]);
					if(!str) str = "";
					Parsed = eval("Parsed.replace(/"+i+"/g, str)");
				}

				this.tabSCatTP[this.a_menu[ii][2][jj][3]] += Parsed;
			}
			
			this.tabSCatTP[this.a_menu[ii][2][jj][3]] += this.TplsMenuTpSuffix;
		}
		this.tabSCat[this.a_menu[ii][3]] += this.TplsMenuSuffix;
	}
	
	//window.onresize = this.sMenuAdjust;
}

menuMC.prototype.HideMenu = function(){
	if(!this.ToHideStack.length) return;
	var MenuID = this.ToHideStack.shift();
	
	if(this.StackJmp == MenuID) return;

	if(this.rolled) return;
	this.DivsMenu.style.visibility = "hidden";
	this.DivsMenuTP.style.visibility = "hidden";
}

menuMC.prototype.ArraySearch = function(a, v){
	for(i=0; i<a.length; i++){
		if(a[i][3] == v) return i;
	}
	return -1;
}

menuMC.prototype.SwitchSelect = function(doc){
	if(!doc) doc = window.document;
	if(doc.location.href.indexOf("https") != -1) return;
	
	AllSelect = doc.getElementsByTagName("select");
	var state = (this.rolled || this.sRolled || this.sTPRolled)? "hidden" : "visible";

	for (var i = 0; i < AllSelect.length; i++) { 
	    AllSelect[i].style.visibility = state;
	}
	
	if(doc.getElementById("frame_recherche_appareil"))
		this.SwitchSelect(doc.getElementById("frame_recherche_appareil").contentWindow.document);
	/*AllFrame = doc.getElementsByTagName("iframe");
	this.Debug("iframe : " + document.getElementById("frame_recherche_appareil"));
	for (var i = 0; i < AllFrame.length; i++) { 
		this.SwitchSelect(AllFrame[i].contentWindow.document)
	}*/
}
 
/************** Modif affichage sur Over/Out *********************/
menuMC.prototype.menuRollStyle = function(numMenu, over){
	var DivMenu = document.getElementById("menu"+numMenu);
	if(over==1) {
			if(this.MenuColorOver) DivMenu.style.background = this.MenuColorOver;
			else DivMenu.style.background = (this.tabColorOver[numMenu])? this.tabColorOver[numMenu] : this.tabColor[numMenu] ;
			if(this.MenuPoliceOver) DivMenu.style.color = this.MenuPoliceOver;
			else DivMenu.style.color = (this.tabPoliceOver[numMenu])? this.tabPoliceOver[numMenu] : this.tabPolice[numMenu];
			if(this.CustomMenuRoll) eval(this.InstanceName + "_MenuRoll(DivMenu, numMenu, over)");
	}else{
			DivMenu.style.color = (this.MenuPolice)? this.MenuPolice : this.tabPolice[numMenu];
			DivMenu.style.background = (this.MenuColor)? this.MenuColor : this.tabColor[numMenu];
			if(this.CustomMenuRoll) eval(this.InstanceName + "_MenuRoll(DivMenu, numMenu, over)");
	}
	this.SwitchSelect();
}

menuMC.prototype.smenuRollStyle = function(numMenu, over){
	var DivMenu = document.getElementById("smenu"+numMenu);
	if(over==1) {
			if(this.sMenuColorOver){DivMenu.style.background = this.sMenuColorOver;}
			else{ DivMenu.style.background = (this.tabColorOver[this.currentMenu])? this.tabColorOver[this.currentMenu] : this.tabColor[this.currentMenu];}
			
			if(this.sMenuPoliceOver) DivMenu.style.color = this.sMenuPoliceOver;
			else DivMenu.style.color = (this.tabPoliceOver[this.currentMenu])? this.tabPoliceOver[this.currentMenu] : this.tabPolice[this.currentMenu];
			if(this.CustomsMenuRoll) eval(this.InstanceName + "_sMenuRoll(DivMenu, numMenu, over)");
	}else{
			DivMenu.style.color = (this.sMenuPolice)? this.sMenuPolice : this.tabPolice[this.currentMenu];
			DivMenu.style.background = (this.sMenuColor)? this.sMenuColor : this.tabColor[this.currentMenu];
			if(this.CustomsMenuRoll) eval(this.InstanceName + "_sMenuRoll(DivMenu, this.currentSMenu, over)");
	}
}

menuMC.prototype.smenuTPRollStyle = function(numMenu, over){
	var DivMenu = document.getElementById("smenutp"+numMenu);
	if(over==1) {
			if(this.sMenuTpColorOver) DivMenu.style.background = this.sMenuTpColorOver;
			else DivMenu.style.background = (this.tabColorOver[this.currentMenu])? this.tabColorOver[this.currentMenu] : this.tabColor[this.currentMenu];
			if(this.sMenuTpPoliceOver) DivMenu.style.color = this.sMenuTpPoliceOver;
			else DivMenu.style.color = (this.tabPoliceOver[this.currentMenu])? this.tabPoliceOver[this.currentMenu] : this.tabPolice[this.currentMenu];
			if(this.CustomsMenuTPRoll) eval(this.InstanceName + "_sMenuTPRoll(DivMenu, numMenu, over)");
	}else{
			DivMenu.style.color = (this.sMenuTpPolice)? this.sMenuTpPolice : this.tabPolice[this.currentMenu];
			DivMenu.style.background = (this.sMenuTpColor)? this.sMenuTpColor : this.tabColor[this.currentMenu];
			if(this.CustomsMenuTPRoll) eval(this.InstanceName + "_sMenuTPRoll(DivMenu, numMenu, over)");
	}
}



/**************** Mecanique affichage/masquage ****************/
menuMC.prototype.menuRoll = function(numMenu,over,menu_Order) {
	if(numMenu!=0) {
		if(over==1 ) {
			this.rolled = 1;

			if(this.currentMenu == numMenu) this.ToHideStack.shift();
			
			this.menuRollStyle(numMenu, 1);

			this.DivmenuSCat.innerHTML = this.tabSCat[numMenu];
			if(this.tabColor[numMenu]) this.DivmenuSCat.style.background = this.tabColor[numMenu] ;
			if(this.tabPolice[numMenu]) this.DivmenuSCat.style.color = this.tabPolice[numMenu];
			this.DivsMenu.style.visibility = "visible";

			//div pos
			if(this.FixedTop) this.DivsMenu.style.top = this.sMenuPixelTop;
			else{
				this.DivsMenu.style.top = findPosY(document.getElementById("menu"+numMenu)) + this.sMenuPixelTop;
			}
			if(this.FixedLeft) this.DivsMenu.style.left = this.sMenuPixelLeft;
			else{
				var DLeft = findPosX(document.getElementById("menu"+numMenu)) + this.sMenuPixelLeft;
				if(DLeft + this.DivsMenu.offsetWidth > document.body.clientWidth + document.body.scrollLeft) 
					DLeft = document.body.clientWidth - this.DivsMenu.offsetWidth + document.body.scrollLeft
				this.DivsMenu.style.left = DLeft;
			}
			
			this.currentMenu = numMenu;
		} else {
			this.rolled = 0;
			
			this.menuRollStyle(this.currentMenu, 0);

			this.ToHideStack.push(this.currentMenu);
			setTimeout(this.InstanceName + ".HideMenu()",0);
		}
	}
}

menuMC.prototype.sMenuRoll = function(numMenu,over,menu_Order,ChildCount) {
	if(numMenu && this.currentMenu){
		this.DivcurrentMenu = document.getElementById("smenu"+numMenu);
	}
	
	if(over==1){
		this.sRolled = 1;

		this.StackJmp = this.currentMenu
		
		if(numMenu && this.currentMenu){
			this.smenuRollStyle(numMenu, 1);		
			this.menuRollStyle(this.currentMenu, 1);
		}
		
		this.DivsMenu.style.visibility = "visible";
		
		if(numMenu!=0) {
			this.currentSMenu = numMenu;
			
			if(!ChildCount){
				this.DivsMenuTP.style.visibility = "hidden";
				if(this.tabColor.length) this.DivmenuSCatTP.style.background = this.tabColor[this.currentMenu] ;
				return;
			}

			this.DivsMenuTP.style.visibility = "visible";
			
			this.DivmenuSCatTP.innerHTML = this.tabSCatTP[this.currentSMenu];
			if(this.tabColor.length) this.DivmenuSCatTP.style.background = this.tabColor[this.currentMenu] ;
			
			//div pos
			if(this.FixedTop) this.DivsMenuTP.style.top = this.sMenuTpPixelTop;
			else this.DivsMenuTP.style.top = findPosY(document.getElementById("smenu"+this.currentSMenu)) + this.sMenuTpPixelTop;
			if(this.FixedLeft) this.DivsMenuTP.style.left = this.sMenuTpPixelLeft;
			else{
				var DLeft = findPosX(document.getElementById("smenu"+this.currentSMenu)) + document.getElementById("smenu"+this.currentSMenu).scrollWidth + this.sMenuTpPixelLeft;
				var TotalClientWidth = parseInt(document.body.clientWidth) + parseInt(document.body.scrollLeft)
				if(DLeft + this.DivsMenuTP.offsetWidth > parseInt(document.body.clientWidth) + parseInt(document.body.scrollLeft))
					DLeft = findPosX(document.getElementById("smenu"+this.currentSMenu)) - this.DivsMenuTP.offsetWidth
				this.DivsMenuTP.style.left = DLeft;
			}
		} else {
			var MenuIndex = this.ArraySearch(this.a_menu, this.currentMenu);
			if(MenuIndex == -1) return;
			var sMenuIndex = this.ArraySearch(this.a_menu[MenuIndex][2], this.currentSMenu);
			if(sMenuIndex == -1) return;

			if(!this.a_menu[MenuIndex][2][sMenuIndex][2].length) return;

			this.DivsMenuTP.style.visibility = "visible";
			this.DivmenuSCatTP.innerHTML = this.tabSCatTP[this.currentSMenu];
			if(this.tabColor.length) this.DivmenuSCatTP.style.background = this.tabColor[this.currentMenu] ;
		}
	} else {
		this.sRolled = 0;

		this.StackJmp = 0;

		if(numMenu && this.currentMenu){
			this.smenuRollStyle(this.currentSMenu, 0);
		}
		this.DivsMenu.style.visibility = "hidden";
		this.DivsMenuTP.style.visibility = "hidden";
		
		//rollover parent
		this.menuRollStyle(this.currentMenu, 0);
	}
}

menuMC.prototype.sMenuTPRoll = function(numMenu,sOver) {
	if(numMenu){
		this.DivcurrentMenu = document.getElementById("smenutp"+numMenu);
	}
		
	if(sOver==1) {
		this.sTPRolled = 1;

		if(numMenu){
			this.menuRollStyle(this.currentMenu, 1);		
			this.smenuRollStyle(this.currentSMenu, 1);
			this.smenuTPRollStyle(numMenu,1)
		}

		this.DivsMenu.style.visibility = "visible";
		this.DivsMenuTP.style.visibility = "visible";

	} else {
		this.sTPRolled = 0;

		if(numMenu){
			this.smenuTPRollStyle(numMenu,0)		
		}

		this.DivsMenu.style.visibility = "hidden";
		this.DivsMenuTP.style.visibility = "hidden";
		
		//rollover parent
		this.menuRollStyle(this.currentMenu, 0);		
		this.smenuRollStyle(this.currentSMenu, 0);
	}
}

function menuMCDebugMove(){
	var div = document.getElementById('menuMCDebug');
	div.style.top = document.body.scrollTop;
	div.style.left = document.body.scrollLeft;
}
if(menuMC.DEBUG){
	document.write("<div style='position:absolute' id='menuMCDebug'><input type=text id='menuMC_DEBUG' size=50></div>");
	document.onscroll = menuMCDebugMove;
}

