var curFrame;	// the frame to update
var curMenu;	// the selected Menu
var curMenus;	// the actual Menu
var menuGestionFenetre = "";

//Menu in the Menus
function Menu(idButton, url, subMenu){
	this._idButton = idButton;	// button id
	this._url = url;			// default url
	this._subMenu = null;		// subMenu associate
	
	if(subMenu != null && subMenu != ""){
		this._subMenu = new SubMenu(subMenu);
	}
	
	this.init = function(){
		this.setFrame();
	}
	
	this.showSubMenu = function(){
		var myButton = document.getElementById(this._idButton);
		var x = myButton.offsetLeft + 2;
		var y = myButton.offsetTop - 2;
		//whereIs(myButton);
		
		if(this._subMenu != null){
			if(!this._subMenu.visible){
				//this._subMenu.show(myButton.magicPositionX, myButton.magicPositionY)
				this._subMenu.show(x, y)
			}
			else{
				this._subMenu.hide();
			}
		}	
	}
	
	this.show = function(){
		document.getElementById(this._idButton).style.visibility = "visible";
	}
	
	this.hide = function(){
		document.getElementById(this._idButton).style.visibility = "hidden";
		this._subMenu.hide();
	}
	
	this.setFrame = function(){
		document.getElementById(curFrame).setAttribute("src", this._url);
	}
}

//Menus
function Menus(id, idMenu, idSsMenu){
	this._id = id;				//	control id
	this._idMenu = idMenu;		//	tabCellMenuId
	this._idSsMenu = idSsMenu;	//	tabCellSsMenuId
	this._tabs = new Array();	//  in Menu(s)
	
	this.add = function(Menu){
		if(Menu){
			this._tabs.push(Menu);
			if(Menu._subMenu != null){
				Menu._subMenu.hide();
			}
		}
	}
	
	this.hideAll = function(){
		for(var i = 0 ; i < this._tabs.length; i++){
			this.hide(i);
		}
	}
	
	this.hide = function(index){
		this._tabs[index].hide();
	}
	
	//close all sub Menu
	this.closeAllSubMenu = function(index){
		for(var i = 0 ; i < this._tabs.length; i++){
			if(!index || index != i){
				this.closeSubMenu(i);
			}
		}
	}
	
	this.closeSubMenu = function(index){
		if(this._tabs[index]._subMenu != null && this._tabs[index]._subMenu.visible){
			this._tabs[index]._subMenu.hide();
		}
	}
	
	//empty subMenu close each windows
	this.emptyAllSubMenu = function(){
	//--- TO DO ---
	}
	
	//return the index in _tabs knowing idButton
	this.getIndex = function(idButton){
		for(var i =0; i < this._tabs.length; i++){
			if(this._tabs[i]._idButton == idButton){
				return i;
			}
		}
		return -1;
	}
	
	this.getIndexByName = function(buttonName){
		for(var i =0; i < this._tabs.length; i++){
			if(document.getElementById(this._tabs[i]._idButton).value == buttonName){
				return i;
			}
		}
		return -1;
	}
	
	this.unselectTabs = function(){
		for(var i =0; i < this._tabs.length; i++){
			document.getElementById(this._tabs[i]._idButton).className ="btnMenu";
		}
	}
	
	this.selectTab = function(index){
		document.getElementById(this._tabs[index]._idButton).className ="btnMenuSelected";
	}
}

function setActiveMenu(btnName){
	if(curMenus){
		var index = curMenus.getIndexByName(btnName);
		if(index!=-1){
			btn_Menu_onLeftClick(btnName , index);
		}
	}
}

function btn_DataForm_onMouseDown(btnName){
	if(curMenus && curMenu){
		var index = -1;
		try{
			index = curMenus.getIndex(event.srcElement.id.toString());
		}catch(e){
			// because of FireFox who do not use eventElement
			index = curMenus.getIndexByName(btnName);
			btn_Menu_onLeftClick(btnName, index);
			return;
		}
		if(index != -1){
			curMenu = curMenus._tabs[index];
			if(event.which == 1 || event.button == 1){
				// left Click
				btn_Menu_onLeftClick(btnName, index);
			}else if( event.which == 3 || event.button == 2 ){
				// right click
				btn_Menu_onRightClick( btnName, index );
			} else {
				//doNothing
			}
		}
	}
}

//action on left click
function btn_Menu_onLeftClick(btnName, index){
	curMenus.unselectTabs();
	curMenus.closeAllSubMenu(index);
	curMenus.selectTab(index);
	switch(btnName.toString()){
		case MENU.ACCUEIL:
			// show frame
			curMenus._tabs[index].setFrame();
			break;
		case MENU.PRESENTATION_SERVICES:
			curMenus._tabs[index].setFrame();
			break;
		case MENU.FICHES_TECHNIQUES:
			curMenus._tabs[index].setFrame();
			break;
		case MENU.CONTACTS:
			curMenus._tabs[index].setFrame();
			break;
		case MENU.ESPACE_TRAVAIL:
			curMenus._tabs[index].setFrame();
			break;
		case MENU.VUE:
			curMenus._tabs[index].setFrame();
			break;
		case MENU.MON_COMPTE:
			curMenus._tabs[index].setFrame();
			break;
		case MENU.RAPPORTS:
			curMenus._tabs[index].setFrame();
			break;
		case MENU.GESTION_FENETRES:
			//curMenus._tabs[index].showSubMenu();
			curMenus._tabs[index].setFrame();
			break;
		case MENU.ADMINISTRATION:
			curMenus._tabs[index].setFrame();
			break;
		case MENU.RENDEMENT:
			curMenus._tabs[index].setFrame();
			break;
		case MENU.ALERTE:
			curMenus._tabs[index].setFrame();
			break;
		default:
			break;
	}
}

//action on rigth click
function btn_Menu_onRightClick(btnName, index){
	switch(btnName.toString()){
		case MENU.PRESENTATION_SERVICES:
			// do nothing
			//document.getElementById(hiddenFrame).src = URL.EXTERN + PARAMETER.DOWNLOAD + curMenus._tabs[index]._url;
			break;
		default:
			break;
	}
}

// Function used to find the exact position of an element into a document
function whereIs(myNode) 
{ 
	originalNode = myNode; 
	x = 0; 
	y = 0; 
	while (myNode.tagName != 'BODY') 
	{ 
		x += myNode.offsetLeft; 
		y += myNode.offsetTop; 
		myNode = myNode.offsetParent; 
	} 
	originalNode.magicPositionX = x; 
	originalNode.magicPositionY = y; 
}

function openWindowInGestionDesFenetre(description, src, windowParameter, name){
	/*if(curMenus){
		var index = curMenus.getIndex(menuGestionFenetre);
		if(index!=-1){
			var mySubMenu = curMenus._tabs[index]._subMenu;
			if(mySubMenu != null){
				if(description == "Bloc-notes" && mySubMenu.getIndexByDescription(description)!=-1 )
				{
					var ind = mySubMenu.getIndexByDescription(description);
					var myElt = mySubMenu._subMenus[ind];
					if(myElt[1]){
						mySubMenu.del(myElt[1]);
						myElt[1].close();
					}
				}
				var myWindow = window.open(src,"",windowParameter);
				mySubMenu.add(description,myWindow);
			}
		}
	}*/
	var myWindow = window.open(src,"",windowParameter);
}

function deleteWindowFromGestionDesFenetre(theWindow){
	if(curMenus){
		var index = curMenus.getIndex(menuGestionFenetre);
		if(index!=-1){
			var mySubMenu = curMenus._tabs[index]._subMenu;
			if(mySubMenu != null){
				mySubMenu.del(theWindow);
			}
		}
	}
}

function closeAllWindowFromGestionDesFenetre(){
	if(curMenus){
		var index = curMenus.getIndex(menuGestionFenetre);
		if(index!=-1){
			var mySubMenu = curMenus._tabs[index]._subMenu;
			if(mySubMenu != null){
				mySubMenu.end();
			}
		}
	}
}



