/*============================================================================================
 Fichier Javascript
 Fonction de création d'une zone cachée contenant les données de la zone complementaire

 AUTEUR:					Alban BALLIEUX
 CREATION:					29/04/2009
 VARIABLE GLOBALES:			
 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
===============================================================================================*/

/**
 * Affiche la zone complementaire
 * 
 * @param {Object} idsrc
 */
function Affiche_Info_Complementaire(idsrc){
	Zone 			= GetPtId(idsrc);
	ZoneMere 		= Zone.parentNode ;
	ZoneGrandMere	= ZoneMere.offsetParent;
	if (!GetPtId('ZoneCompl')) {
		Div = document.createElement('DIV');
		Div.id = 'ZoneCompl';
		ZoneGrandMere.parentNode.appendChild(Div);
	}
	else {
		Div = GetPtId('ZoneCompl');
		if (Div.style.display != 'none') {
			Close_Zone_Compl();
		}
	}
	Div.innerHTML=Zone.innerHTML;
	Div.setAttribute('ZoneSrc',idsrc);
	Div.style.backgroundColor='white';
	Div.style.border='1pt solid #000000';
	Div.ondblclick=Close_Zone_Compl;
	Div.style.display='block';
	Positionne_Zone_Compl();
	
	IdLien = idsrc.replace('ZoneComplementaire','LinkZc') ;
	PtLien = GetPtId(IdLien);
	PtLien.onclick = function (event){  Close_Zone_Compl(event,idsrc) ;} ;
	
}

/**
 * Place la zone complement sous l'element cliqué
 */
function Positionne_Zone_Compl(){
	Div = GetPtId('ZoneCompl');
	Visi = Div.style.display ;
	LeftIni = Div.offsetLeft ;
	TopIni	= Div.offsetTop;
	Zone = GetPtId(Div.getAttribute('ZoneSrc'));
	ZoneMere 		= Zone.parentNode ;
	ZoneGrandMere	= ZoneMere.offsetParent;
	
	Div.style.position='absolute';
	Left = ZoneMere.offsetLeft + ZoneGrandMere.offsetLeft - ZoneGrandMere.parentNode.scrollLeft  ;
	LeftRel = ZoneMere.offsetLeft - ZoneGrandMere.parentNode.scrollLeft  ; 
	Div.style.left= Left + 'px' ;
	Div.style.width=(ZoneMere.clientWidth) + 'px' ;
	Top = ZoneMere.offsetTop +ZoneGrandMere.offsetTop + ZoneMere.offsetHeight - ZoneGrandMere.parentNode.scrollTop ;
	TopRel = ZoneMere.offsetTop + ZoneMere.offsetHeight - ZoneGrandMere.parentNode.scrollTop ;
	Div.style.top = Top + 'px';
	
	if(TopRel<0 || LeftRel<0){
		Div.style.display='none';
	}else{
		Div.style.display=Visi;
	}
}

/**
 * Ferme la zone complementaire
 * 
 * @param {Object} e		Identifiant de la zone
 */
function Close_Zone_Compl(e,SrcOpt){
	ZComp = GetPtId('ZoneCompl') ;
	Src=ZComp.getAttribute('ZoneSrc') ;
	if(isset(SrcOpt) && SrcOpt!=''){
		Src = SrcOpt ;
	}
	Lien = Src.replace('ZoneComplementaire','LinkZc') ;
	PtLien.onclick = function (){  Affiche_Info_Complementaire(Src) ;} ;
	GetPtId(Src).innerHTML = ZComp.innerHTML ;
	ZComp.style.display='none';
	if(ZComp.textContent==''){
		GetPtId(Lien).style.display='none';
	}
}

/**
 * 
 * 
 * @param {Object} idcell
 * @param {Object} nbenfantsnonzcautorise
 */
function Genere_Zone_Compl_Si_Besoin(idcell, nbenfantsnonzcautorise){
	ZControle = GetPtId(idcell) ;
	nbChild = ZControle.childNodes.length;
	nbenfantsnonzc = 0 ;
	TEnfantsADeplacer=new Array();
	NbEnfantsADeplacer = 0 ;
	IDZC='';
	IDLien='';
	for(numchild=0;numchild<nbChild;numchild++){
		Child=ZControle.childNodes[numchild] ;
		if( (Child.tagName!='A'   || (Child.tagName=='A'   && Child.id.substr(0,6)!='LinkZc' ) ) 
		 && (Child.tagName!='DIV' || (Child.tagName=='DIV' && Child.id.substr(0,18)!='ZoneComplementaire') ) 
		 ){
			nbenfantsnonzc = nbenfantsnonzc + 1 ;
			if(nbenfantsnonzc>nbenfantsnonzcautorise){
				TEnfantsADeplacer.push(numchild);
				NbEnfantsADeplacer = NbEnfantsADeplacer + 1 ;
			}
		}else{
			if(Child.tagName=='DIV'){
				IDZC = Child.id; 
				ZC = Child;
			}
			if(Child.tagName=='A'){
				IDLien = Child.id; 
				Link =  Child; 
			}		
		}
	}
	if(IDZC == '' && NbEnfantsADeplacer>0 ){
		ZC = document.createElement('DIV') ;
		ZC.id='ZoneComplementaireSUPL' + ZControle.parentNode.rowIndex + 'C' + ZControle.cellIndex ;
		ZC.style.display='none' ;
		Link = document.createElement('A') ;
		Link.id='LinkZcSUPL' + ZControle.parentNode.rowIndex + 'C' + ZControle.cellIndex ;
		Link.appendChild(GetPtId('PlusType').firstChild);
		Link.setAttribute('onclick',"Affiche_Info_Complementaire('"+ZC.id+"');");
		ZControle.appendChild(Link);
		ZControle.appendChild(ZC);
	}
	for (i = 0; i < NbEnfantsADeplacer; i++) {
		ZC.appendChild(ZControle.childNodes[TEnfantsADeplacer[i]]);
		ZC.appendChild(document.createElement('BR'));
		if (Link.style.display == 'none') {	
			Link.style.display = 'inline';
		}
	//	ZControle.removeChild(ZControle.childNodes[TEnfantsADeplacer[i]]);
	}
}

