/*============================================================================================
 Fichier Javascript
 Fonction de manipulation des menu contextuelle

 AUTEUR:					Alban BALLIEUX
 CREATION:					06/04/2009
 VARIABLE GLOBALES:			cptclick
 MODIFICATIONS:
===============================================================================================
	copyright 2009 Alban BALLIEUX - ballieuxa@laon.noirot
	http://www.phpmyportal.info

	This file is part of phpMyPortal.

    phpMyPortal is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or
    any later version.

    phpMyPortal is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with phpMyPortal; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===============================================================================================*/


//---------------------------------------------------------------------------------------
// Fonctions Propres Au menus de contexte
//---------------------------------------------------------------------------------------

/**
 * Fonction appelÃ©e par le callback du foreach de InitMenuContext
 * @param {Object} element
 * @param {Object} index
 * @param {Object} array
 */
function Genere_Menu_Type(element, index, array){
	attributType = element[0] ;
	CurContext = attributType ;

//Je n'ai plus qu'a generer mon tableau.
	nblignes = element.length;
	Table = document.createElement('TABLE') ;
	for(i=1;i<nblignes;i++){
		if (element[i] == 'SEP') {
			Sep = document.createElement('HR');
			Sep.style.width = '98%';
			Ligne = document.createElement('TR');
			Cell = document.createElement('TD');
			Cell.setAttribute('colspan', 2);
			
			Cell.appendChild(Sep);
			Ligne.appendChild(Cell);
			Table.appendChild(Ligne);
		}
		else {
			Ligne = document.createElement('TR');
			if (element[i][1] != '') {
				Ligne.className = 'CLCONTEXT';
				Ligne.setAttribute('onmousedown', element[i][1]);
			}else{
				Ligne.className = 'CLCONTEXTINACT';
			}
			
			Cell = document.createElement('TD') ;
			Cell.innerHTML = element[i][0];
			Cell.style.whiteSpace='nowrap';
			
			CellVide=document.createElement('TD') ;
			CellVide.innerHTML = '   ';
			CellVide.style.minWidth='5px';
			
			Ligne.appendChild(CellVide);
			Ligne.appendChild(Cell);
			Table.appendChild(Ligne) ;
		}
	}
//reste Ã  creer la Div.
	Div = document.createElement('DIV');
	Div.id = 'menutype' + attributType ;
	Div.style.display='none';
	Div.appendChild(Table);
	
	document.body.appendChild(Div);
	CurContext = '' ;
	
}

var cptclick=0;
/**
 * Fonction qui fait l'initialisation des menus contextuels 
 * et du onclick. 
 */
function InitMenuContext(){
	CurContext = '' ;
	if (isFunction(TMenuContext.forEach)) {
		TMenuContext.forEach(Genere_Menu_Type);
	}else{
		Taille = TMenuContext.length;
		for(index=0;index<Taille;index++){
			Genere_Menu_Type(TMenuContext[index],index,TMenuContext) ;
		}
	}
	AjouteEvent('onmousedown',ClickContext);
	document.oncontextmenu=desactiver;
}

function desactiver(){
	return false;
}

/**
 * Fonction Evenementielle du sur le click context.
 * 
 * @param {Object} e		L'evenement du click
 */
function ClickContext(e){
	if (!e) var e = window.event;
	if (e.button == 2){
		Target = ETarget(e);
		TargetInit = Target ;
		while (Target.tagName != 'HTML' && !isset(Target.getAttribute('contextmenu'))) {
			Target = Target.parentNode;
		}
		
		if (isset(Target.getAttribute('contextmenu'))) {
			PosX = e.clientX + 'px';
			PosY = e.clientY + 'px';
			attributType = Target.getAttribute('contextmenu');
			Menu = GetPtId('menutype' + attributType);
			Contenu = Menu.innerHTML;
			Menu.style.overflow='hidden';
			Menu.style.width='1px';
			Menu.style.display='block';
			Larg = (Menu.firstChild.offsetWidth + 5) + 'px' ;	
			Haut = (Menu.firstChild.offsetHeight + 5) + 'px' ;
			Menu.style.display='none';
			Menu.style.overflow='';
			Menu.style.width='';
			Contenu = Macro(Contenu,TargetInit) ;
			AfficheContextMenu(PosX, PosY, Contenu, Larg,Haut);
		}
		else {
			if (GetPtId('CONTEXT')) {
				GetPtId('CONTEXT').style.display = 'none';
			}
		}
	}else{
		if (GetPtId('CONTEXT')) {
			GetPtId('CONTEXT').style.display = 'none';
		}
	}
}

/**
 * Fonction qui fait l'affichage des contexts menus.
 * 
 * @param {Object} PosX
 * @param {Object} PosY
 * @param {Object} Contenu
 * @param {Object} Larg
 * @param {Object} Haut
 * @param {Object} Transp
 * @param {Object} Titre
 */
function AfficheContextMenu(PosX, PosY, Contenu, Larg, Haut, Transp, Titre){
	// Initialisation des parametres si vides.
	if(!isset(Titre)){
		Titre = '';
	}
	// Je regarde si la zone n'existe pas
	PtBO=GetPtId('CONTEXT');
	if (PtBO == false){
	// Je crée la zone
		PtBO=document.createElement('DIV');
		PtBO.id='CONTEXT';
		PtBO.style.position='absolute';
		PtBO.style.display='none';
		PtBO.name='CONTEXT';
		DivDefil= document.createElement('DIV');
		DivDefil.id='CONTEXTDEF';
		DivDefil.innerHTML="<table><tr><th id=\"CONTEXTT\">Titre</th></tr><tr><td id=\"CONTEXTC\">Corp</td></tr></table>";
		DivDefil.className='CLCONTEXT';
		PtBO.appendChild(DivDefil);
		document.body.appendChild(PtBO);
	}
	DivDefil=GetPtId('CONTEXTDEF');
	PtBOC=GetPtId('CONTEXTC');
	PtBOT=GetPtId('CONTEXTT');

// Je positionne la fenetre dans la page et j'initialise le contenu de la DIV
	PtBO.style.top=PosY;
	PtBO.style.left=PosX;
	PtBO.style.position='absolute';
	PtBO.style.overflow='hidden';
	
	PtBOC.innerHTML = Contenu;
	PtBOT.innerHTML = Titre;
	
	if (isSize(Larg))	PtBO.style.width=Larg;
	if (isSize(Haut)) 	PtBO.style.height=Haut;
	if (!isNumber(Transp)) Transp=100;
	
	DivDefil.style.position='relative';
	Ecart=parseInt(Haut.replace('px','')) ;
	DivDefil.style.top=(Ecart * -1) + 'px';
	
	ChangeOpac('CONTEXT', Transp);
	GetPtId('CONTEXT').style.display='block';
	Descend_Div('CONTEXTDEF',(Ecart * -1),0,5);
			
}