/* Permet d'afficher le contenu du div regroupant toutes les fenetres */
function ViewSourceLayer()
{
	alert(document.getElementById("popupLayer").innerHTML);
}

/* Affiche le contenu complet de l'objet Popup */
function dumpPopup(obj)
{
	d = "\tName : "+obj.name;
	d += "\n\tTitre : "+obj.titre;
	d += "\n\tWidth : "+obj.width;
	d += "\n\tHeight : "+obj.height;
	d += "\n\tTop : "+obj.top;
	d += "\n\tLeft : "+obj.left;
	d += "\n\tView : "+obj.view;
	d += "\n\tMove : "+obj.move;
	d += "\n\tContent : "+obj.content;
	d += "\n\tAlign : "+obj.align;
	d += "\n\tUrl : "+obj.url;
	d += "\n\tAjax : ";
	d += "\n\t\tUrl : "+obj.ajax.url;
	d += "\n\t\tMethod : "+obj.ajax.method;
	d += "\n\t\tData : "+obj.ajax.data;
	return d;
}

function dumpAll()
{
	result = "";
	for(i=0;i<PopupConteneur.length;i++)
	{
		result += "Popup n° "+i+"\n\n";
		result += dumpPopup(PopupConteneur[i]);
		result += "\n\n";
	}
	alert(result);
}



/* ------------------------------------------------------------ */
/* ----------------- Fonctions de déplacement ----------------- */
/* ------------------------------------------------------------ */

var isDragging = false;
var objectToDrag;
var ecartX;
var ecartY;
var curX;
var curY;
var cIndex = 20;

document.onmousemove = getPositionCurseur;

function getPositionCurseur(e)
{
		//ie
		if(document.all)
		{
			curX = event.clientX;
			curY = event.clientY;
		}
	
		//netscape 4
		else if(document.layers)
		{
			curX = e.pageX;
			curY = e.pageY;
		}
	
		//mozilla
		else if(document.getElementById)
		{
			curX = e.clientX;
			curY = e.clientY;
		}
}

function beginDrag(id,e)
{
	isDragging = true;
	objectToDrag = document.getElementById(id);
	cIndex++;
	objectToDrag.style.zIndex = cIndex;
	ecartX = curX - parseInt(objectToDrag.style.left, 10);
	ecartY = curY - parseInt(objectToDrag.style.top, 10);
	drag();
}

function drag()
{
	var newPosX;
	var newPosY;
	if(isDragging)
	{	
		newPosX = curX - ecartX;
		newPosY = curY - ecartY;
		objectToDrag.focus();
		objectToDrag.style.left = (newPosX + "px");
		objectToDrag.style.top = (newPosY + "px");
		setTimeout("drag()",15);
	}
}

function endDrag()
{
	isDragging = false;
	objectToDrag = null;
}


/* ------------------------------------------------------ */
/* ----------------- Création des popup ----------------- */
/* ------------------------------------------------------ */


/* Constructeur de l'objet */
Popup = function(config)
{
	if(typeof config.name != "undefined")
	{
		// Propriétés de base pour l'affichage
		this.statut 	= true;
		this.name		= config.name;
		this.titre		= typeof config.titre == "undefined" ? "" : config.titre;
		this.width		= typeof config.width == "undefined" ? 10 : config.width;
		this.height		= typeof config.height == "undefined" ? 10 : config.height;
		this.top		= typeof config.top == "undefined" ? 10 : config.top;
		this.left		= typeof config.left == "undefined" ? 10 : config.left;
		this.zIndex		= typeof config.zIndex == "undefined" ? 1 : config.zIndex;
		this.className	= typeof config.className == "undefined" ? "tabloPopup" : config.className;
		this.align		= typeof config.align == "undefined" ? "left" : config.align;
		
		// Propriétés spéciales  : 
		this.replace	= typeof config.replace == "undefined" ? false : config.replace;
		this.view		= typeof config.view == "undefined" ? 1 : config.view;
		this.move		= typeof config.move == "undefined" ? 1 : config.move;
		this.content	= typeof config.content == "undefined" ? false : config.content;
		this.url		= typeof config.url == "undefined" ? false : config.url;
		this.ajax		= typeof config.ajax == "undefined" ? false : config.ajax;
		this.startExec	= typeof config.startExec == "undefined" ? "" : config.startExec;
		this.closeExec	= typeof config.closeExec == "undefined" ? "" : config.closeExec;

		// Construit integralement la popup et l'affiche
		this.Show = function () {Popup.Show(this);};
	}
	else
	{
		alert("L'objet doit obligatoirement posséder un nom (name: 'son_nom')");
		this.statut = false;
	}
};

/* Construit le nouveau div */
Popup.MakeDiv = function(obj)
{
	tmp = '<div id="'+obj.name+'"></div>';
	return tmp;
};

/* Met en place les différents style que l'on doit appliquer à la popup */
Popup.CreateStyle = function(obj)
{
	document.getElementById(obj.name).style.position = "absolute";
	document.getElementById(obj.name).style.display = (obj.view==1)?"block":"none";
	document.getElementById(obj.name).style.width = obj.width+"px";
	document.getElementById(obj.name).style.height = obj.height+"px";
	if(!obj.replace) document.getElementById(obj.name).style.top = obj.top+"px";
	else document.getElementById(obj.name).style.top = curY+"px";
	document.getElementById(obj.name).style.left = obj.left+"px";
	document.getElementById(obj.name).style.zIndex = obj.zIndex;
};

/* Ecrit le contenu de la popup */
Popup.CreateContent = function(obj)
{
	move = obj.move == 1 ? ' onmousedown="beginDrag(\''+obj.name+'\',event);" onmouseup="endDrag();"' : "";
	tmp = '<table class="'+obj.className+'" id="tabloPopup">';
	if(obj.move == 1)
	{
		tmp += '<tr><td class="DragBar"'+move+'><span class="popup_titre">'+obj.titre+'</span>';
		tmp += '<span class="popup_close" onclick="closePopup(\''+obj.name+'\');"><img src="/commun/images/off.gif" /></span></td></tr>';
	}
	tmp += '<tr><td id="'+obj.name+'_content" class="popup_content" align="'+obj.align+'">';
	tmp += '</td></tr></table>';
	document.getElementById(obj.name).innerHTML = tmp;
	
	if(obj.ajax !== false)
	{
		loadXHR(obj.ajax.url,obj.ajax.method,obj.name+"_content",obj.ajax.data);
	}
	else
	{
		if(obj.content !== false)
		{
			document.getElementById(obj.name+"_content").innerHTML = obj.content;
		}
		else if(obj.url !== false)
		{
			document.getElementById(obj.name+"_content").innerHTML = '<iframe src="'+obj.url+'" width="100%" height="100%" style="border:none;"></iframe>';
		}
	}
};

/* Construit integralement la popup et l'affiche si besoin */
Popup.Show = function(obj)
{
	if(!document.getElementById("popupLayer"))
	{
		alert("Le div 'popupLayer' n'existe pas !\nVeuillez le créer");
	}
	else
	{
		selects = document.getElementsByTagName("select");
		for(i=0;i<selects.length;i++)
		{
			selects[i].style.visibility = "hidden";
		}
		
		if(obj.view == 1)
		{
			Popup.ExecJS(obj.startExec);
		}
		document.getElementById("popupLayer").innerHTML += Popup.MakeDiv(obj);
		Popup.CreateStyle(obj);
		Popup.CreateContent(obj);
	}
};

/* -------------------------------------------------------- */
/* ----------------- Fonctions de gestion ----------------- */
/* -------------------------------------------------------- */


function closePopup(id)
{
	Popup.ExecJS(PopupConteneur[Popup.GetIdPopupByName(id)].closeExec);
	document.getElementById(id).style.display = "none";
	selects = document.getElementsByTagName("select");
	for(i=0;i<selects.length;i++)
	{
		selects[i].style.visibility = "visible";
	}
}

/* Permet de récupérer une popup à l'aide de son ID */
Popup.GetIdPopupByName = function(id)
{
	res = false;
	for(i=0;i<PopupConteneur.length;i++)
	{
		if(PopupConteneur[i]!="undefined" && PopupConteneur[i].name == id)
		{
			res = i;
		}
	}
	return res;
};


Popup.ReloadPopup = function(config)
{
	ind = Popup.GetIdPopupByName(config.name);
	if(ind !== false)
	{
		PopupConteneur[ind] = new Popup(config);
		if(PopupConteneur[ind].statut !== false)
		{
			PopupConteneur[ind].Show();
		}
	}
};

Popup.ExecJS = function(script)
{
	if(Popup.ExecJS.caller !== null)
	{
		eval(script);
	}
};


/* ---------------------------------------------------------- */
/* ----------------- Fonctions Utilisateurs ----------------- */
/* ---------------------------------------------------------- */

PopupConteneur = new Array;

/* Permet de construire une popup suivant les paramètre que l'on souhaite */
Popup.CreateMyPopup = function(config)
{
	if(document.getElementById(config.name))
	{
		/* La popup existe alors on la cache et on la remplace */
		Popup.ReloadPopup(config);
	}
	else
	{
		/* La popup n'existe pas alors on la crée */
		cpt = PopupConteneur.push(new Popup(config));cpt--;
		if(PopupConteneur[cpt].statut !== false)
		{
			PopupConteneur[cpt].Show();
		}
	}
};