/* ******************************************************** */
/*   					 -= POPUP =-						*/
/*															*/
/*	Création d'un pop-up interne qui permet la gestion		*/
/*	D'écran de saisies sans chargement ou nouvelle page.	*/
/*															*/
/*	create(left:Int, top:Int, h:Int, contenu:String)		*/
/* 	createA(x:Int, y:Int, h:Int, l:Int, contenu:String)		*/
/*	createN(objet: HTML Element, contenu: String)			*/
/*	close()													*/
/*	toString()												*/
/* ******************************************************** */
var popup = function() /* fondOnClick = Fonction ou 0 */
{
		/***********************/
		/** Variables locales **/
		/***********************/
	//if (navigator.appName.substring(0,3) == "Net") document.captureEvents(Event.MOUSEMOVE);
	this.fondOnClick=0
	this.div_fond="";
	this.div="";
	this.exist=0;
	this.ns4=document.layers;
	this.ie4=document.all;
	this.ns6=document.getElementById&&!document.all;
	this.type=0;
	this.fond=1;
	this.className = 'popup'; 
	this.draggable = 1;
	this.calcul=false; 
       
		/***********************/
		/** Fonctions Publics **/
		/***********************/
	
	// Fonction principale : Création du div de fond + du div popup
	this.popup = function()
	{
		var instance=this;
		document.onmousemove = function(event){instance.move(event);}
		this.onMouse(1);
	}
	this.style = function(className)
	{
		this.className = className;
	}
	this.onMouse =function(choice)
	{
		if(choice)
		{
			this.outMouseX = -5;
			this.outMouseY = -5;
			this.plusx=5;
			this.plusy=5;	
		}
		else
		{
			this.outMouseX = 0;
			this.outMouseY = 10;
			this.plusx=10;
			this.plusy=20;
		}
	}
	this.create = function(left, top, h, contenu)
	{
		if(!this.exist)
		{
			this.type="Classic";
			this.exist=1;
			if(this.fond)
				this.create_div_fond();
			this.div = document.createElement("div");
			if(document.all) this.div.className="thisDiv";
			else this.div.setAttribute("class", "thisDiv");
			this.div.style.height=h;
			this.div.style.width="auto";
			this.div.style.display='block';
			var reg = new RegExp('id="', 'g');
			this.div.innerHTML="<div class='"+this.className+"' width:100%;border:1px solid black'>" + contenu.replace(reg, 'id="_') + "</div>";
			if(this.ie4)
			{
				var reg = new RegExp('id=', 'g');
				this.div.innerHTML=contenu.replace(reg, 'id=_');
			}
			this.div.setAttribute("id", "pop_up");
			this.div.style.position="absolute";
			this.centre(this.div, "", top, left);
			document.body.appendChild(this.div);		
			return true;
		}
		else
		{
			return this.close;
		}
	}
	
	// Positionner en absolue
	this.createA = function(x, y, h, l, contenu)
	{
		if(!this.exist)
		{
			this.create('0', '0', h, contenu);
			this.div.style.width=l;
			this.position_popup(this.div,x,y);
			if(this.draggable)
			{
				var span = document.createElement("span");
				if(document.all) span.className="window";
				else span.setAttribute("class", "window");
				
				var span1 = document.createElement("span");
				span1.innerHTML = "<->";
				if(document.all) span1.className="span1";
				else span1.setAttribute("class", "span1");
				span.appendChild(span1);
				
				var span2 = document.createElement("span");
				span2.innerHTML = "X";
				if(document.all) span2.className="span2";
				else span2.setAttribute("class", "span2");
				span.appendChild(span2);
				
				this.div.insertBefore(span, this.div.firstChild);
				var instance = this;
				span1.onmousedown=function(e)
				{
					if (navigator.appName!="Microsoft Internet Explorer") 
					{ 
						instance.outMouseX += parseInt(instance.getPosition(instance.div).x) - parseInt(e.pageX);
					}
					else
					{
						instance.plusx+= parseInt(instance.getPosition(instance.div).x) - event.x;
					}	
					instance.calcul=instance.div;
					return false;
				}
				span1.onmouseup=function(event)
				{
					instance.calcul=false;
					return false;
				}
				span2.onclick=function(event)
				{
					instance.close();
				}
			}
			
			
			this.type="Absolute placement";
			return true
		}
		return false;
	}
	
	// Creer à cotés de l'element 'objet'
	this.createN = function(contenu, width)
	{
		if(!this.exist)
		{				
			this.create('0', '0', 'auto', contenu);
			//this.div.style.margin='auto';
			this.div.style.width = width;
			this.div.style.display='none';
			this.div.style.left ="0px";
			this.div.style.top = "0px";
			window.lastX = 10;
			window.lastY = -10;
			//this.position_popup(this.div, this.getPosition(objet).x,  this.getPosition(objet).y);
			this.calcul=this.div;
			this.type="Near object";
			return true;
		}
		return false;
	}
	this.createIt = function(objet, contenu)
	{
		var instance = this;
		objet.onclick  = function(event)
		{
			instance.create('auto', 'auto', 'auto', contenu);
		}
	}
	this.popupIt = function(objet, contenu)
	{
		var instance = this;
		var back = this.fond;
		objet.onmouseover  = function(event)
		{
			instance.fond=0;
			instance.createN(contenu, '30%');
		}
		objet.onmouseout  = function(event)
		{
			instance.close();
			instance.fond=back;
		}
	}
	/* Fermer le popup */
	this.close = function()
	{
		this.calcul = false;
		if(this.exist)
		{
			if(this.fond)
				this.div_fond.parentNode.removeChild(this.div_fond);
			this.div.parentNode.removeChild(this.div);
			this.exist=0;
			this.type=0;
			return true;
		}
		return false
	}

	this.setFondOnClick = function(fonction)
	{
		if(fonction=="")
			return this.fondOnClick;
		else
			this.fondOnClick = fonction;
		return true;
	}
	// Renvoi l'objet
    this.toString = function()
    {
		object = "[Popup Element]";
		if(this.exist)
			object += " Created"
		else
			object += " Not created";
		object +=" (";
		if(this.type!=0)
			object = object+"Type : " + this.type;
		return object+")";
    }

	
		/***********************/
		/** Fonctions privées **/
		/***********************/
		
	/* Création d'un div grisé avec alpha prenant toute la page */
	this.create_div_fond = function()
	{
		this.div_fond = document.createElement("div");
		this.div_fond.setAttribute("id","div_fond");
		this.div_fond.style.position="absolute";
		this.div_fond.style.height=this.getPageSize()['height'] +"px";
		this.div_fond.style.width='100%';
		if(document.all) this.div_fond.className="div_fond";
		else this.div_fond.setAttribute("class","div_fond");
		this.position_popup(this.div_fond,0,0);
		this.set_opacity(this.div_fond, 20);
		if(this.fondOnClick!=0)
		{
			var instance = this;
			if(this.fondOnClick=="close")
			{
				this.div_fond.onclick = function(event)
				{	
					instance.close();
				}
			}
			else
			{
				if(document.all) this.div_fond.attachEvent("onclick",this.fondOnClick);
				else this.div_fond.addEventListener("click",eval(this.fondOnClick),true);
			}
		}
		document.body.appendChild(this.div_fond);
		return true;
	}
	
	this.getWindowSize = function()
	{
		var d=document;
		db=(!d.documentElement.clientWidth)?document.body:document.documentElement;  
		var windowWidth = (window.innerWidth)? window.innerWidth : db.clientWidth;
		var windowHeight = (window.innerHeight)? window.innerHeight: db.clientHeight;
		return {'width': windowWidth, 'height': windowHeight};
	}
	this.getPageSize = function()
	{
		var windowSize = this.getWindowSize()   
		var xScroll = document.body.scrollWidth;
		var yScroll = (window.innerHeight && window.scrollMaxY)? window.innerHeight + window.scrollMaxY : document.body.scrollHeight;
		var pageWidth = (xScroll < windowSize.width)? windowSize.width : xScroll;  
		var pageHeight = (yScroll < windowSize.height)? windowSize.height : yScroll;
		return {'width': pageWidth, 'height': pageHeight};
	}
	
	/* Récupère la position de l'objet */
	this.getPosition = function(objet)
	{
		position = new Array();
		if(this.ns6)
		{
			position.x=objet.offsetLeft;
			position.y=objet.offsetTop;
		}
		else
		{
			position.x=objet.offsetLeft+"px";
			position.y=objet.offsetTop+"px";
		}
		return position;
	}
	
	/* Récupère la taille */
	
	this.getSize = function(objet)
	{
		taille = new Array();
		if(this.ns6)
		{
			taille.width=objet.offsetWidth+"px";
			taille.height=objet.offsetHeight+"px";
		}
		else
		{
			taille.width=objet.offsetWidth+"px";
			taille.height=objet.offsetHeight+"px";
		}
		return taille;
	}
	
	
	/* Centre l'objet */
	this.centre = function(objet, type, top, left)
	{
		if(type=="x")
			left='auto';
		if(type=="y")
			top='auto';
		if (this.ns4)
			objet.right = parseFloat(left)+"%";
		else
			objet.style.right = parseFloat(left)+"%";		
		return this.position_popup(objet, parseFloat(left)+'%', parseFloat(top)+'%');
	}
	/* Place l'objet */
	this.position_popup = function(objet, x, y)
	{
		if (this.ns4) {
			objet.top =y;
			objet.left =x;
		}
		else if (this.ns6) {
			objet.style.left=x;
			objet.style.top =y;
		}
		else if (this.ie4) {
			objet.style.left=x;
			objet.style.top =y;
		}
		return true;
	}
	
	/* Gère la transparence */
	this.set_opacity = function(el, opacity)
	{
			el.style["filter"] = "alpha(opacity="+opacity+")";
			el.style["-moz-opacity"] = opacity/100;
			el.style["-khtml-opacity"] = opacity/100;
			el.style["opacity"] = opacity/100;
			return true;
	}
	
	this.move = function(e) 
	{
		if(this.calcul) 
		{  
			objet = this.calcul;
			objet.style.display='';
			
			var ie4 = (document.all)? true:false;
			var ns4 = (document.layers)? true:false;
			var ns6 = (document.getElementById)? true:false;
			
			if (ie4) {

			  // Calcul de l'écart de position de la souris
			  var difX=event.clientX-window.lastX;
			  var difY=event.clientY-window.lastY;
			  //Récupération de la position du div et ajout de l'écart de position de la souris
			  var newX1 = parseInt(objet.style.left)+difX;
			  var newY1 = parseInt(objet.style.top)+difY;
			  // Assignation des nouvelles coordonnées au div
			  objet.style.left=newX1+"px";
			  objet.style.top=newY1+"px";
			  //Assignation de l'anciènne position de la souris
			  window.lastX=event.clientX;
			  window.lastY=event.clientY;

		}else if (ns4) {

			  // Calcul de l'écart de position de la souris
			  var difX=e.pageX-window.lastX;
			  var difY=e.pageY-window.lastY;
			  //Récupération de la position du div et ajout de l'écart de position de la souris
			  var newX1 = parseInt(document.layers.objet.left)+difX;
			  var newY1 = parseInt(document.layers.objet.top)+difY;
			  // Assignation des nouvelles coordonnées au div
			  document.layers.objet.left=newX1;
			  document.layers.objet.top=newY1;
			  //Assignation de l'anciènne position de la souris
			  window.lastX=e.pageX;
			  window.lastY=e.pageY;

		}else if (ns6) {
			  // Calcul de l'écart de position de la souris
			  var difX=e.clientX-window.lastX;
			  var difY=e.clientY-window.lastY;
			  //Récupération de la position du div et ajout de l'écart de position de la souris
			  var newX1 = parseInt(objet.style.left)+difX;
			  var newY1 = parseInt(objet.style.top)+difY;
			  // Assignation des nouvelles coordonnées au div
			  objet.style.left=(newX1)+"px";
			  objet.style.top=newY1+"px";
			  //Assignation de l'anciènne position de la souris
			  window.lastX=e.clientX;
			  window.lastY=e.clientY;
			//document.title=objet.style.left+" / " + newX1;
		} 
			
			
		/*
			if (navigator.appName!="Microsoft Internet Explorer") 
			{ // Si on est pas sous IE
				objet.style.left=e.pageX + this.plusx+"px";
				objet.style.top=e.pageY + this.plusy+"px";
			}
			else 
			{
				if(document.documentElement.clientWidth>0) 
				{
					objet.style.left=this.plusx+event.x+document.documentElement.scrollLeft+"px";
					objet.style.top=this.plusy+event.y+document.documentElement.scrollTop+"px";
				} 
				else 
				{
					objet.style.left=this.outMouseX+event.x+document.body.scrollLeft+"px";
					objet.style.top=this.outMouseY+event.y+document.body.scrollTop+"px";
				}
			}
		*/
		}
	}
	this.popup();
}


