
/* 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 if (nb_popUp == 1)
	{
		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)
	}
}


/* ####################################################################################### */
// 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 ;)	
	
	
	// 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.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 = true;
	this.last_yScroll = 0;

 
    
 
	this.curYPosition = yMousePos;
	this.curXPosition = xMousePos;
	this.curTopPosition = yMousePos;
	
	this.curTimer = null;
	
	this.currentOpacity =100;
	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.elmt_divObscure.style.height  && 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";}
			}
			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;
		var divToBoucle = this.elmt_divContent;
		
		
		for (i=0; i < divToBoucle.childNodes.length;  i++) 
		{ 
		     
			    if (this.widthMax < divToBoucle.childNodes[i].clientWidth)
			    {
				    this.widthMax = divToBoucle.childNodes[i].clientWidth;
			    }
    			
			    if (!isNaN(divToBoucle.childNodes[i].clientHeight))
			    {
				    this.heightMax = this.heightMax + divToBoucle.childNodes[i].clientHeight;
			    }
 
		}
		
		// Patch crade ...
		if (this.widthMax == 0){this.widthMax = divToBoucle.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.style.height = this.heightAndTop[2] + "px";	

    this.elmt_divObscure.onclick = function() 
	{
	    close_pseudoPopUp(); 	
		if(this.curTimer)
		{
		    clearTimeout(this.curTimer)
		}
	};
		
		
	elmt_Body.appendChild(this.elmt_divObscure);
	setOpacity(this.elmt_divObscure, 70);

 
	// puis on effectue le tout 
	myXHR = new new_httpRequest(URIpath, "", "POST");
	myXHR.LoadContentIn(this.elmt_divContent, waitMsg,moreAction);
 
	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 nv_getPageScroll() 
{
/*	yScroll = 0;
	maxScroll = 0;
	heightVisible = 0;
	
	if (window.scrollMaxY)
	{maxScroll = window.scrollMaxY;}
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{maxScroll = document.body.scrollHeight - document.body.clientHeight;}
	else
	{maxScroll = document.documentElement.clientHeight;}
		
	if (self.innerHeight) {
		yScroll = self.pageYOffset;
		heightVisible = self.innerHeight;
	} else if (document.documentElement && (document.documentElement.scrollTop !== undefined))
	{	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		heightVisible = document.documentElement.clientHeight;
		
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		heightVisible = document.body.clientHeight;
	}*/
	nv_dimensionAndScroll = new structure_dimensionAndScrol();
	
	var max_height = nv_dimensionAndScroll.max_height;

	if (max_height < nv_dimensionAndScroll.height)
	{
		max_height = nv_dimensionAndScroll.height;
	}
	
	var arrayPageScroll = new Array(nv_dimensionAndScroll.height,nv_dimensionAndScroll.scroll_y, max_height) 
	

	return arrayPageScroll;
}

function structure_dimensionAndScrol()
{
	this.height = 0;
	this.width = 0;
	this.max_height = 0;
	this.max_width = 0;
	this.scroll_x = 0
	this.scroll_y = 0;	
	
	
	this.initHeightAndWidth = function()
	{
		if(window.innerHeight) 
		{
			this.height = window.innerHeight;
			this.width = window.innerWidth - 5;
		} 
		else 
		{
			if(document.documentElement && document.documentElement.clientHeight) 
			{
				this.height = document.documentElement.clientHeight;
				this.width = document.documentElement.clientWidth;
	        }
		    else if(document.body) 
		    {
                this.height = document.body.clientHeight;
                this.width = document.body.clientWidth;
            }
        }
        
        
    }
	
	this.initMaxHeight_andMaxWidth  = function()
	{
		var xScroll, yScroll;
 
		if (window.innerHeight && window.scrollMaxY)
		{   
			this.max_width = this.width + window.scrollMaxX;
            this.max_height = this.height + window.scrollMaxY;
        
        }
         else if (document.body.offsetHeight)
        {
			if (document.body.scrollHeight > document.body.offsetHeight)
			{
				this.max_width = document.body.scrollWidth;
				this.max_height = document.body.scrollHeight; 
			}
			else
			{
				this.max_width = document.body.offsetWidth;
				this.max_height = document.body.offsetHeight;
			}
        }
        else
        {
			this.max_width = this.width;
			this.max_height = this.height;
        }
        
        if (this.max_height < this.height)
        {this.max_height = this.height;}
        
        if (this.max_width < this.width)
        {this.max_width = this.width;}
     
        
	}
	
	this.initPositionScrol = function()
	{
        if(typeof(window.pageYOffset ) == 'number' )
        {
			this.scroll_y = window.pageYOffset;
			this.scroll_x = window.pageXOffset;
        }
        else if(document.body && (document.body.scrollLeft || document.body.scrollTop ))
        {
			this.scroll_y = document.body.scrollTop;
			this.scroll_x = document.body.scrollLeft;
        }
        else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop ))
        {
			this.scroll_y = document.documentElement.scrollTop;
			this.scroll_x = document.documentElement.scrollLeft;
        }
        
	}
	
	this.initHeightAndWidth();
	this.initMaxHeight_andMaxWidth();
	this.initPositionScrol()
	
	
}




