
/* LAST MAJ : 27 juin 2008 */

var nb_popUp = 0;
var lastScroll = "";
var tab_objPopUp = new Array();

/* ####################################################################################### */
// 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 + "_" + nb_popUp))
	document.getElementsByTagName('body').item(0).removeChild(document.getElementById(CONST_div_overlay + "_" + nb_popUp))

//	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();	
	}
	else
	{
		nb_popUp -= 1;
		window.onscroll = lastScroll;
	}
}


/* ####################################################################################### */
// 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 
	var withToAdd_step1 = 4;
	var withToAdd_step2 = 40;
	var withToAdd_step3 = 36;
	var withToAdd_step4 = 18;
	var 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;
	nb_popUp += 1;
	this.divId_wrapper = CONST_div_wrapper + "_" + nb_popUp;
	this.divId_content = CONST_div_content + "_" + nb_popUp;
	//this.divId_wrapper = CONST_div_wrapper;
	// création du div qui va "cacher" un peu tout 
	this.elmt_divObscure = document.createElement("div");
	this.elmt_divObscure.setAttribute("id",CONST_div_overlay + "_" + nb_popUp);	// on lui ajoute une belle Id ;)
	//this.elmt_divObscure.setAttribute("id",CONST_div_overlay);	
	this.elmt_divObscure.className = CONST_div_overlay;	// on lui ajoute une belle Id ;)
	
	
	// Création du div qui va contenir le formulaire 
	this.elmt_divContent = document.createElement("div");			// création
	this.elmt_divContent.setAttribute("id", this.divId_content);	// on lui ajoute une belle Id ;)
	//this.elmt_divContent.setAttribute("id", CONST_div_content);	// on lui ajoute une belle Id ;)	
	// il existe deux type de pop up, le popo avec fond, et le popup sans fond...
	if (withBackGround)
	{this.elmt_divContent.className = CONST_div_contentBG}
	else
	{this.elmt_divContent.className = CONST_div_content}
	
	// création du div qui va "grandir" 
	this.elmt_divAjax = document.createElement("div");			// création 
	this.elmt_divAjax.setAttribute("id", this.divId_wrapper); 	// on lui ajoute une belle Id ;) 
	this.elmt_divAjax.className = CONST_div_wrapper;
	this.elmt_divAjax.style.top  = yMousePos + "px"; 			// puis on le positionne 
	this.elmt_divAjax.appendChild(this.elmt_divContent);		// importation du div contenant le formulaire dans le div qui grandira ! 

	// récupération du body 
	this.elmt_Body = document.getElementsByTagName("body").item(0); // récupération
	
	this.bodyWidth = this.elmt_Body.offsetWidth;
	this.heightAndTop = nv_getPageScroll();
	
	this.leftPosition = 0;
	this.topPosition = 0;
	
	this.positionned = false;
	this.last_yScroll = 0;

	this.functionScroll = function()
	{
		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.elmt_divObscure.style.height > this.elmt_divObscure.style.height.replace(/px/, ''))
 			{
 				this.elmt_divObscure.style.height = this.elmt_divObscure.style.height + "px";
 			}
 		 
 			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.elmt_divObscure.style.height = (this.heightMax + this.topPosition + 40) + "px";	
				// this.topPosition = (this.heightMax + margin_top) - (this.heightAndTop[0] + this.heightAndTop[1]);
				// this.heightAndTop[1] = 0;
				}
			}
			else
			{
				this.topPosition = this.heightAndTop[1] + margin_top;
			}
			// si le pop up est plus grand que le scroll
		
		
			this.last_yScroll = this.heightAndTop[1];
			document.getElementById(this.divId_wrapper).style.top = this.topPosition + 'px';
			
		}
	
	}
	
	
	window.onscroll = this.functionScroll;
	
	this.curYPosition = yMousePos;
	this.curXPosition = xMousePos;
	this.curTopPosition = yMousePos;
	
	this.curTimer;
	
	this.currentOpacity = 0;
	this.currentStepOpacity = 5;
		
	this.fondu = function()
	{
		// on purge les timer
		if(this.curTimer){clearTimeout(this.curTimer)}
		
		this.currentOpacity += this.currentStepOpacity;
		
		setOpacity(this.elmt_divAjax, this.currentOpacity)
		
		if (this.currentOpacity < 100)
		{
			var self = this;
			self.curTimer = setTimeout("self.fondu()",10);
		} 
		else
		{
		this.positionned = true;
		}
	}
	
	
	// fonction permettant d'agrandir le bloc
	this.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)
		{
			var self = this;

			self.curTimer = setTimeout("self.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)
		}
	}
	
	this.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) )
		{
			var self = this;
			self.curTimer = setTimeout("self.positionForm_left(" + (curLeft + widthAdd) + ", " + curSize + ")",10);
		}
		else if (curLeft > (this.leftPosition + 4)) 
		{
			var self = this;
			self.curTimer = setTimeout("self.positionForm_left(" + (curLeft - widthAdd) + ", " + curSize + ")",10);
		}
		else
		{
			this.elmt_divAjax.style.left = this.leftPosition + "px"

			this.positionForm_top(this.curTopPosition);
		}

	}
	
	this.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)) 
		{
			var self = this;
			self.curTimer = setTimeout("self.positionForm_top(" + (curTop + widthAdd) + ")",10);
		}
		else if (curTop > (this.topPosition + 5)) 
		{
			var self = this;
			self.curTimer = setTimeout("self.positionForm_top(" + (curTop - widthAdd) + ")",10);
		}
		else
		{			this.positionned = true;
		this.elmt_divAjax.style.top = this.topPosition + 'px';
		}
	}
	
	
	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)}
		
		this.elmt_divContent.childNodes
			
		
		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 = (this.bodyWidth - this.widthMax) / 2 ;
			
			this.topPosition =  (this.heightAndTop[1] + margin_top) ;
			tab_objPopUp[tab_objPopUp.length] = this;
			
			
		
			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.top = this.topPosition + "px";
				this.elmt_divAjax.style.width = "auto";
				this.elmt_divAjax.style.height = "auto";
			
				this.fondu();
			}
			else
			{
			this.sizeForm(xMousePos ,  0)	
			}
					
			

		}
		else
		{
			var self = this;
			self.curTimer = setTimeout("self.calculMaxWidth()",10);
		}
	}
	
	if (!document.getElementById(CONST_div_wrapper & "_" & nb_popUp))
	{

		
		this.elmt_divObscure.style.height = this.heightAndTop[2] + "px";	

		
		this.elmt_divObscure.onclick = function() 
		{
			close_pseudoPopUp(); 	
			if(this.curTimer)
			{
				clearTimeout(this.curTimer)
			}
		};// on lui ajoute une belle Id ;) 
		
		
		
		this.elmt_Body.appendChild(this.elmt_divObscure)
		this.elmt_Body.appendChild(this.elmt_divAjax)// importation du div "grandissant" dans le body 
 
		// puis on effectue le tout 
		myXHR = new httpRequest(URIpath + '&xhr_id_content=' + this.divId_content, "", "POST");
		myXHR.LoadContent(this.divId_content, waitMsg,moreAction);

		setOpacity(this.elmt_divObscure, 40);
		var self = this;
		self.curTimer = setTimeout("self.calculMaxWidth()",10);
	}
	
	
}


/* ####################################################################################### */
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";
		}
	}
}