
/* LAST MAJ : 27 juin 2008 */

var nb_popUp = 0;
var lastScroll = "";
var tab_objPopUp = new Array();
var selfNvPopUp = null;

/* ####################################################################################### */
// A utiliser pour procéder à la fermeture du dit pop up \\
/* ####################################################################################### */
function close_pseudoPopUp()
{
	

	//document.getElementsByTagName('body').item(0).removeChild(document.getElementById(CONST_div_wrapper))
	//document.getElementsByTagName('body').item(0).removeChild(document.getElementById(CONST_div_overlay))
	
	if (nb_popUp > 1) 
	{
		nb_popUp -= 1;
		window.onscroll = tab_objPopUp[nb_popUp].functionScroll;
		tab_objPopUp.pop();	
		
		document.getElementsByTagName('body').item(0).removeChild(tab_objPopUp[nb_popUp].elmt_divObscure)
	    document.getElementsByTagName('body').item(0).removeChild(tab_objPopUp[nb_popUp].elmt_divAjax)
	}
	else
	{
		nb_popUp -= 1;
		window.onscroll = lastScroll;
		
		document.getElementsByTagName('body').item(0).removeChild(tab_objPopUp[0].elmt_divObscure)
	    document.getElementsByTagName('body').item(0).removeChild(tab_objPopUp[0].elmt_divAjax)
	}
	
	return false;
}


/* ####################################################################################### */
// besoin d'un pop up ? n'attendez plus ! utilisez cet objet ! \\
/* ####################################################################################### */
function print_pseudoPopUp(URIpath, moreAction, withBackGround, effect, waitMsg, with_closeButton)
{		
	var effect_aggrandissement = 1;
	var effect_fondu = 2;
	
	// définition des variables 
	this.withToAdd_step1 = 4;
	this.withToAdd_step2 = 40;
	this.withToAdd_step3 = 36;
	this.withToAdd_step4 = 18;
	this.withToAdd_step5 = 3;

	// permet de se trouvez a n pixel du haut de l'écran
	var margin_top = 133;

	if (nb_popUp == 0) {lastScroll  = window.onscroll;};
	
	this.widthMax = 0;
	this.heightMax = 0;
	
	// on incrément de 1 le nombre de popUp
	nb_popUp += 1;
	
	// var divId_wrapper = CONST_div_wrapper + "_" + nb_popUp;
	// var divId_content = CONST_div_content + "_" + nb_popUp;


	// création du div qui va masquer la page 
	this.elmt_divObscure = document.createElement("div");
	this.elmt_divObscure.className = "overlay_for_hidden popColor";	// on lui associe ca class CSS
	
	// this.elmt_divObscure.setAttribute("id",CONST_div_overlay + "_" + nb_popUp);	// on lui ajoute une belle Id ;)
	//this.elmt_divObscure.setAttribute("id",CONST_div_overlay);	
	
	
	
	// Création du div qui va contenir l'effet d'appartition désiré, ainsi que l'apparance blobal
	this.elmt_divContent = document.createElement("div");			// création
	
	
	// il existe deux type de pop up, le popUp avec fond, et le popup sans fond mais qui arrive en glissant...
	if (withBackGround)
	{this.elmt_divContent.className = CONST_div_contentBG}
	else
	{this.elmt_divContent.className = CONST_div_content}
	
	// this.elmt_divContent.setAttribute("id", this.divId_content);	// on lui ajoute une belle Id ;)
	this.elmt_divContent.setAttribute("id", "pseudo_popUp_1");	// on lui ajoute une belle Id ;)	
	
	this.closeButton = document.createElement("a");
	this.closeButton.className = "deco"; 
	this.closeButton.innerHTML = "Fermer";
	this.closeButton.setAttribute("href","#")
	this.closeButton.onclick = close_pseudoPopUp;
	
	// création du div qui va contenir la page à charger
	this.elmt_divAjax = document.createElement("div");
	this.elmt_divAjax.className = "overlay_to_write";
	this.elmt_divAjax.style.top  = yMousePos + "px"; 			// puis on le positionne 
	this.elmt_divAjax.appendChild(this.closeButton);
	this.elmt_divAjax.appendChild(this.elmt_divContent);		// importation du div contenant le formulaire dans le div qui grandira ! 
	
	// this.elmt_divAjax.setAttribute("id", this.divId_wrapper); 	// on lui ajoute une belle Id ;) 
	

	// récupération du body 
	this.elmt_Body = document.getElementsByTagName("body").item(0);
	
	
	// récupération de la largeur du bidy
	this.bodyWidth = elmt_Body.offsetWidth;
	
	// récupération des données sur la page
	this.heightAndTop = nv_getPageScroll();
	
	this.leftPosition = 0;
	this.topPosition = 0;
	
	this.positionned = false;
	this.last_yScroll = 0;

 
    
 
	this.curYPosition = yMousePos;
	this.curXPosition = xMousePos;
	this.curTopPosition = yMousePos;
	
	this.curTimer = null;
	
	this.currentOpacity = 0;
	this.currentStepOpacity = 5;
		
		
	this.fondu2 = function()
	{
	   // on verifie si la pop up est déja chargé
		if (this.positionned)
		{ 
			// on recalcule les scroll (en effet, l'utilisatreur peut avior changer la taille de  sa fenetre entre temps
		 	this.heightAndTop = nv_getPageScroll();
 			this.exec_calculWidthAndHeight();
 	
 		 
 			if (this.last_yScroll >= this.heightAndTop[1])
			{
				 if (this.heightAndTop[1] < this.topPosition )
				{this.topPosition = this.heightAndTop[1] + margin_top;}
				
			}
			else if (this.heightAndTop[0] < (this.heightMax + margin_top))
			{ 
				if ( (this.heightAndTop[0] + this.heightAndTop[1]) > (this.topPosition + this.heightMax))
				{this.topPosition =  this.heightAndTop[0] + this.heightAndTop[1] - (this.heightMax + margin_top);} 
			}
			else
			{this.topPosition = this.heightAndTop[1] + margin_top;}
					
		
			this.last_yScroll = this.heightAndTop[1];
			this.elmt_divAjax.style.top = this.topPosition + 'px';
			
		}
	}
	// on associe notre nouvelle fonction de scroll à l'evenement scroll
	window.onscroll = this.fondu2;
	
	this.fondu = function()
	{
		// on purge les timer
		if(this.curTimer){clearTimeout(this.curTimer)}
		
		this.currentOpacity += this.currentStepOpacity;
		this.elmt_divAjax.style.zIndex = 100;

		setOpacity(this.elmt_divAjax, this.currentOpacity)
		
		if (this.currentOpacity >= 90)
		{
		    setOpacity(this.elmt_divAjax, 100);
		    
		    this.positionned = true;
		}
		else if (this.currentOpacity < 100)
		{
		    selfNvPopUp = this;
			selfNvPopUp.curTimer = setTimeout("selfNvPopUp.fondu()",10);
		} 
		else
		{
		this.positionned = true;
		}
	}
	
	
	this.exec_calculWidthAndHeight = function()
	{
		this.heightMax = 0;
		for (i=0; i < this.elmt_divContent.childNodes.length;  i++) 
		{
			if (this.widthMax < this.elmt_divContent.childNodes[i].clientWidth)
			{
				this.widthMax = this.elmt_divContent.childNodes[i].clientWidth;
			}
			
			if (!isNaN(this.elmt_divContent.childNodes[i].clientHeight))
			{
				this.heightMax = this.heightMax + this.elmt_divContent.childNodes[i].clientHeight;
			}
		}
		
		// Patch crade ...
		if (this.widthMax == 0){this.widthMax = this.elmt_divContent.childNodes[0].firstChild.clientWidth;};
	}
	
	
	
	this.calculMaxWidth = function()
	{
		if(this.curTimer){clearTimeout(this.curTimer)}
		
		if (this.elmt_divContent.childNodes.length > 0)
		{
			this.heightMax = 20;
			
			this.exec_calculWidthAndHeight();
			
			// this.elmt_divContent.style.height=  this.heightMax + "px";
			this.elmt_divAjax.style.height=  this.heightMax + "px";
			this.elmt_divContent.style.width=  this.widthMax + "px";
			
			this.leftPosition = (bodyWidth - this.widthMax) / 2 ;
			
			this.topPosition =  (this.heightAndTop[1] + margin_top) ;
			tab_objPopUp[tab_objPopUp.length] = this;
			
			
	        this.elmt_divContent.style.visibility = "visible"; 
		
			if (effect == effect_fondu)
			{
				this.elmt_divAjax.style.overflow= 'auto';
				this.elmt_divAjax.style.border = 'none';
				this.elmt_divAjax.style.left = this.leftPosition + "px";
				this.elmt_divAjax.style.width = "auto";
				this.elmt_divAjax.style.height = "auto";
				this.elmt_divAjax.style.top =  this.topPosition + "px";
				this.fondu();
			}
			else
			{
			    this.sizeForm(xMousePos ,  0)	 
			}
					
			

		}
		else
		{
			selfNvPopUp = this; 
			selfNvPopUp.curTimer = setTimeout("selfNvPopUp.calculMaxWidth()",10);
		}
	}
	
//	if (!document.getElementById(CONST_div_wrapper & "_" & nb_popUp))
//	{

		

    this.elmt_divObscure.onclick = function() 
	{
	    close_pseudoPopUp(); 	
		if(this.curTimer)
		{
		    clearTimeout(this.curTimer)
		}
	};
		
		
	elmt_Body.appendChild(this.elmt_divObscure);

 
	// puis on effectue le tout 
	myXHR = new httpRequest(URIpath, "", "POST");
	myXHR.LoadContentIn(this.elmt_divContent, waitMsg,moreAction);
//alert(this.elmt_divAjax.innerHTML);
	elmt_Body.appendChild(this.elmt_divAjax);// importation du div "grandissant" dans le body 
	
	this.calculMaxWidth();
	
	 
}
/*selfNvPopUp = this;*/
	
	

/* ####################################################################################### */
print_pseudoPopUp.resize_popUp = function()
{
	this.elmt_divContent = document.getElementById(CONST_div_content + "_" + nb_popUp)
					
	// si notre pop up existe, on recalcule la hauteur !
	if (this.elmt_divContent)
	{
		this.elmt_divAjax = document.getElementById(CONST_div_wrapper + "_" + nb_popUp)
		
		var curElmt ;
		
		if (this.elmt_divContent.childNodes.length > 0)
		{
			this.heightMax = 20;
			this.widthMax = 0;
			for (i=0; i < this.elmt_divContent.childNodes.length;  i++) 
			{
				curElmt = this.elmt_divContent.childNodes[i];
	 
				
				if (this.widthMax < curElmt.clientWidth)
				{
					this.widthMax = curElmt.clientWidth;
				}
				
				if (!isNaN(curElmt.clientHeight))
				{
					this.heightMax = this.heightMax + curElmt.clientHeight;
				}
				
			}
			this.elmt_divContent.style.height=  this.heightMax + "px";
			//this.elmt_divContent.style.width=  this.widthMax + "px";
			this.elmt_divContent.style.width=  this.widthMax + "px";
			this.elmt_divAjax.style.height=  "auto";
			this.elmt_divAjax.style.width=  "auto";
			
			this.leftPosition = (document.getElementsByTagName("body").item(0).offsetWidth - this.widthMax) / 2 ;
		 	this.elmt_divAjax.style.left = this.leftPosition + "px";
		}
	}
}




// fonction permettant d'agrandir le bloc
print_pseudoPopUp.sizeForm = function(curLeft, curSize)
{
	var widthAdd = 5;
	var myHeight = (this.heightMax * (curSize / this.widthMax))
	var myWidth = (this.heightMax * (curSize / this.widthMax))
	
	// on purge les timer
	if(this.curTimer){clearTimeout(this.curTimer)}
	
	
	// Calcul de la quantité à ajouter 
	if (curSize > (98 * this.widthMax / 100) ){widthAdd = 1;}
	else if (curSize > (85 * this.widthMax / 100) ){widthAdd = 2 * this.widthMax / 100;}
	else if (curSize > (70 * this.widthMax / 100) ){widthAdd = 3 * this.widthMax / 100;}
	else if (curSize > (50 * this.widthMax / 100) ){widthAdd = 4 * this.widthMax / 100;}
	else {widthAdd = 5 * this.widthMax / 100;}

	
	// on déplace notre div en X 
	if (this.leftPosition < this.curXPosition && (this.leftPosition < (this.curXPosition  - widthAdd )))
	{this.curXPosition = (this.curXPosition - widthAdd );}
	else if (this.leftPosition > this.curXPosition  && (this.leftPosition > (this.curXPosition  + widthAdd )))
	{this.curXPosition = (this.curXPosition + widthAdd);}

	
	// on déplace notre div en Y 
	if (this.topPosition < this.curTopPosition && (this.topPosition < (this.curTopPosition - widthAdd )))
	{this.curTopPosition = this.curTopPosition - widthAdd;}
	else if (this.topPosition > this.curTopPosition && (this.topPosition > (this.curTopPosition + widthAdd)))
	{this.curTopPosition =  this.curTopPosition + widthAdd;}
	
	this.elmt_divAjax.style.left = this.curXPosition + "px";
	this.elmt_divAjax.style.top = this.curTopPosition + "px";
	this.elmt_divAjax.style.width = curSize + "px";
	this.elmt_divAjax.style.height = myHeight + "px";

	// on agit selon la taille 
	if (curSize < this.widthMax)
	{
		selfNvPopUp = this;

		selfNvPopUp.curTimer = setTimeout("selfNvPopUp.sizeForm(" + (curLeft - widthAdd) +", " + (curSize + widthAdd) + ")",10);
	}
	else
	{
		this.elmt_divAjax.style.overflow= 'auto';
		this.elmt_divAjax.style.border = 'none';  
		this.elmt_divAjax.style.left = this.leftPosition + "px";
		this.elmt_divAjax.style.top = this.topPosition + "px";
		this.elmt_divAjax.style.width = "auto";
		this.elmt_divAjax.style.height = "auto";
		
		this.positionForm_left(this.curXPosition ,curSize)
	}
}



print_pseudoPopUp.positionForm_left = function(curLeft, curSize)
{
	if(this.curTimer){clearTimeout(this.curTimer)}

	this.elmt_divAjax.style.left = curLeft + "px"
	
	widthAdd = 3;
	
	
	// Calcul de la quantité à ajouter 
	
	if (curLeft > (200 * this.leftPosition / 100) ){widthAdd = 15 * this.leftPosition / 100;}
	else if (curLeft > (150 * this.leftPosition / 100) ){widthAdd = 10 * this.leftPosition / 100;}
	else if (curLeft > (115 * this.leftPosition / 100) ){widthAdd = 8 * this.leftPosition / 100;}
	else if (curLeft > (102 * this.leftPosition / 100) ){widthAdd = 6 * this.leftPosition / 100;}
	else if (curLeft > (98 * this.leftPosition / 100) ){widthAdd = 3;}
	else if (curLeft > (85 * this.leftPosition / 100) ){widthAdd = 3 * this.leftPosition / 100;}
	else if (curLeft > (70 * this.leftPosition / 100) ){widthAdd = 4 * this.leftPosition / 100;}
	else if (curLeft > (50 * this.leftPosition / 100) ){widthAdd = 5 * this.leftPosition / 100;}
	else {widthAdd = 15 * this.leftPosition / 100;}


	if (curLeft < (this.leftPosition - 4) )
	{
		selfNvPopUp = this;
		selfNvPopUp.curTimer = setTimeout("selfNvPopUp.positionForm_left(" + (curLeft + widthAdd) + ", " + curSize + ")",10);
	}
	else if (curLeft > (this.leftPosition + 4)) 
	{
		selfNvPopUp = this;
		selfNvPopUp.curTimer = setTimeout("selfNvPopUp.positionForm_left(" + (curLeft - widthAdd) + ", " + curSize + ")",10);
	}
	else
	{
		this.elmt_divAjax.style.left = this.leftPosition + "px"

		this.positionForm_top(this.curTopPosition);
	}

}
	
print_pseudoPopUp.positionForm_top = function(curTop)
{
	if(this.curTimer){clearTimeout(this.curTimer)}

	widthAdd = 4;
	
	
	if (curTop > (200 * this.topPosition / 100) ){widthAdd = 15 * this.topPosition / 100;}
	else if (curTop > (150 * this.topPosition / 100) ){widthAdd = 10 * this.topPosition / 100;}
	else if (curTop > (115 * this.topPosition / 100) ){widthAdd = 8 * this.topPosition / 100;}
	else if (curTop > (102 * this.topPosition / 100) ){widthAdd = 6 * this.topPosition / 100;}
	else if (curTop > (98 * this.topPosition / 100) ){widthAdd = 3;}
	else if (curTop > (85 * this.topPosition / 100) ){widthAdd = 3 * this.topPosition / 100;}
	else if (curTop > (70 * this.topPosition / 100) ){widthAdd = 4 * this.topPosition / 100;}
	else if (curTop > (50 * this.topPosition / 100) ){widthAdd = 5 * this.topPosition / 100;}
	else {widthAdd = 15 * this.topPosition / 100;}
	
	
				
	
	this.elmt_divAjax.style.top = curTop + "px"
	
	if (curTop < (this.topPosition - 5)) 
	{
		selfNvPopUp = this;
		selfNvPopUp.curTimer = setTimeout("selfNvPopUp.positionForm_top(" + (curTop + widthAdd) + ")",10);
	}
	else if (curTop > (this.topPosition + 5)) 
	{
		selfNvPopUp = this;
		selfNvPopUp.curTimer = setTimeout("selfNvPopUp.positionForm_top(" + (curTop - widthAdd) + ")",10);
	}
	else
	{			this.positionned = true;
	this.elmt_divAjax.style.top = this.topPosition + 'px';
	}
}



function showImgInPopup(pathImg){

    alert(pathImg);

}
