/*

	>> en global

	var monPanneau = new Panel("monPanneau")
	
	>> dans le window.onload
	
	monPanneau.setParameter("HTMLElement","dvwininfo");//defini l'élement HTML représentant le panneau
	
	>> n'importe quand
	
	monPanneau.setParameter("fromWidth",0);
	monPanneau.setParameter("fromHeight",0);
	monPanneau.setParameter("fromLeft",0);		//peut etre "centered" ou un nombre
	monPanneau.setParameter("fromTop",0);		//peut etre "centered" ou un nombre
	monPanneau.setParameter("toWidth",200);
	monPanneau.setParameter("toHeight",200);
	monPanneau.setParameter("toLeft","centered");	//peut etre "centered" ou un nombre
	monPanneau.setParameter("toTop","centered");	//peut etre "centered" ou un nombre

	monPanneau.setParameter("speed",30);
	monPanneau.setParameter("ratio",0.3);
	monPanneau.resize();
*/

//////////////////////////////////////////////////////////////////////////////
// Panneau coulissant
//////////////////////////////////////////////////////////////////////////////

function Panel(sName)
{

	//Déclaration des propriétés
	this.name=sName;
	this.lastdifftop = 0;
	this.lastdiffleft = 0;
	this.lastdiffwidth = 0;
	this.lastdiffheight = 0;

	this.fromleft = null;
	this.fromtop = null;
	this.toleft = null;
	this.totop = null;
	this.fromwidth = null;
	this.fromheight = null;
	this.towidth = null;
	this.toheight = null;
	this.CallBack = null;
	this.DuringCall = null; //Fonction appelée a chaque passage
	
	this.displayStatus = "hidden"; //hidden pour caché, visible pour affiché
	this.toDisplayStatus = "visible";
	this.Status ="";
	this.speed = 30;
	this.ratio = 0.3;
	this.width=0;
	this.height=0;
	this.SubHeight = 0;
	this.SubHeightMethod = "normal";
	this.SizeMethod="";//si = centered, la fenetre est centrée
	
	this.Interval = 0;
	
	this.HTMLElement = null;
	this.HTMLSubElement = null;
	

};



Panel.prototype.initialize = function()
{
	this.lastdifftop = 0;
	this.lastdiffleft = 0;
	this.lastdiffwidth = 0;
	this.lastdiffheight = 0;

	this.fromleft = null;
	this.fromtop = null;
	this.toleft = null;
	this.totop = null;
	this.fromwidth = null;
	this.fromheight = null;
	this.towidth = null;
	this.toheight = null;
	this.CallBack = null;
	this.DuringCall = null;
	this.toDisplayStatus = null;
}

//////////////////////////////////////////////////////////////////////////////
// Methode permettant d'insérer des paramètres
//////////////////////////////////////////////////////////////////////////////


Panel.prototype.setParameter = function(sParam,sValue) {
	
	var nValue = 0;
	sParam = sParam.toLowerCase();
	switch(sParam)
	{
		case "status":
			this.Status=sValue;
			break;
		case "todisplaystatus":
			switch(sValue)
			{
				case "hide":
				case "hidden":
					this.toDisplayStatus = "hidden";
					break;
				default:
					this.toDisplayStatus = "visible";
					break;
			}			
			break;
			
		case "subheightmethod":	// Méthode du redimensionnement du subElement vis à vis de l'Element le contenant
			switch(sValue)
			{
				case "noredim":
				case "normal":
				case "diff":
				case "difference":
					this.SubHeightMethod = sValue;
					break;
				default:
					break;
			
			}
			break;
		case "callback":
			this.CallBack=sValue;
			break;
		case "duringcall":
			this.DuringCall=sValue;
			break;
		case "fromleft":
		case "fromtop":
		case "toleft":
		case "totop":
			if (PanelisNull(sValue))eval("this."+sParam+"=sValue;");
			else
			{
				if (PanelisNumber(sValue))
				{
					eval("this."+sParam+"=sValue;");
				}
				else
				{
					switch(sValue)
					{
						case "centered":
							eval("this."+sParam+"=sValue;");
							break;
						default:
							eval("this."+sParam+"=0;");
							break;
					}
				}
			}
			break;
		case "fromwidth":
		case "fromheight":
		case "towidth":
		case "toheight":
			if (!PanelisNull(sValue))sValue=parseInt(sValue);
			eval("this."+sParam+"=sValue;");
			break;
		case "width":
		case "left":
		case "top":
			nValue=parseInt(sValue);
			this.setGUI(sParam,nValue);
			break;
		case "speed":			
			nValue=parseInt(sValue);
			this.speed=nValue;
			break;
		case "ratio":
			nValue=parseFloat(sValue);
			this.ratio=nValue;
			break;
		case "height":		
			nValue=parseInt(sValue);
			this.setHeight(nValue);
			break;
		case "subheight":
			nValue=parseInt(sValue);
			this.setSubHeight(nValue);
			break;
		case "htmlelement":
			return this.setHTMLElement(sValue);
			break;
		case "sizemethod":
			switch(sValue)
			{
				case "centered":
					this.SizeMethod = sValue;
					break;
				default:
					this.SizeMethod ="";
					break;
			
			}
			break;
	}
}

//////////////////////////////////////////////////////////////////////////////
// Methode privée définissant l'élément HTML qui représente le panneau
//
// Entrée : sId : la valeur de l'attribut ID de l'élément HTML
//////////////////////////////////////////////////////////////////////////////

Panel.prototype.setHTMLElement = function(sId) {
	
	var oElement = document.getElementById(sId);

	if (oElement!=undefined && oElement!=null)
	{
		this.HTMLElement = oElement;
	
		//Récupère l'élément contenu
		var oSubElement = oElement.firstChild;
		if (oSubElement!=undefined && oElement!=oSubElement)
		{
			this.HTMLSubElement = oSubElement;
		}
		else
		{
			this.HTMLSubElement = null;
		}
	}
	else
	{
		return false;
	}
	return true;
}

//////////////////////////////////////////////////////////////////////////////
// Methode privée définissant l'élément HTML qui représente le panneau
//
// Entrée : sId : la valeur de l'attribut ID de l'élément HTML
//////////////////////////////////////////////////////////////////////////////

Panel.prototype.setGUI = function(sParam,nValue) {
	
	var oElement = this.HTMLElement;
	var oSubElement = this.HTMLSubElement;

	if (oElement!=undefined && oElement!=null)
	{
		//alert(sParam+" = "+nValue);
		eval("this."+sParam+"=nValue;");
		eval("oElement.style."+sParam+"="+nValue+";");

	}
	else
	{
		return false;
	}
}

//////////////////////////////////////////////////////////////////////////////
// Methode privée définissant l'élément HTML qui représente le panneau
//
// Entrée : sId : la valeur de l'attribut ID de l'élément HTML
//////////////////////////////////////////////////////////////////////////////

Panel.prototype.setHeight = function(nValue) {
	
	var oElement = this.HTMLElement;
	var oSubElement = this.HTMLSubElement;
	var nSubHeight = 0;

	if (oElement!=undefined && oElement!=null)
	{
		//alert(sParam+" = "+nValue);
		eval("this.height=nValue;");
		eval("oElement.style.height='"+nValue+"px';");
		

		
		//Redimensionnement du subElement
		if (oSubElement!=undefined && oSubElement!=null)
		{
			switch(this.SubHeightMethod)
			{
				case "diff": //Redimensionnement par différece
				case "difference":
					
					nSubHeight = nValue - this.SubHeight;
					if (PanelisObject(oSubElement.style) != false)eval("oSubElement.style.height="+nSubHeight+";");
					break;
				case "noredim"://Pas de redimensionnement
					break;
				default://Rediemnsionnement normal (la même hauteur que celle de l'élément conteneur)
					nSubHeight = this.SubHeight;
					if (PanelisObject(oSubElement.style) != false)eval("oSubElement.style.height="+nSubHeight+";");
					break;
			}
		}
	}
	else
	{
		return false;
	}
}

//////////////////////////////////////////////////////////////////////////////
// Methode privée peremttant de redimensionner la hauteur du subElement
//
// Entrée : nValue : un entier définissant soit la hauteur, soit la différence de hauteur avec l'Element
//////////////////////////////////////////////////////////////////////////////

Panel.prototype.setSubHeight = function(nValue) {
	
	var oElement = this.HTMLElement;
	var oSubElement = this.HTMLSubElement;
	var nSubHeight = 0;
	this.SubHeight = nValue;

	switch(this.SubHeightMethod)
	{
		case "diff": //Redimensionnement par différece
		case "difference":
			nSubHeight = nValue - this.SubHeight;
			break;
		case "noredim"://Pas de redimensionnement
			break;
		default://Rediemnsionnement normal (la même hauteur que celle de l'élément conteneur)
			nSubHeight = this.SubHeight;
			break;
	}
		


}

//////////////////////////////////////////////////////////////////////////////
// Methode publique permettant de cacher le panneau
//
// Entrée : 
//////////////////////////////////////////////////////////////////////////////

Panel.prototype.hide = function()
{
	var oElement = this.HTMLElement;
	if (PanelisNull(oElement))
	{
		alert("Panel.class.js : L'élément HTML contenant le Panneau n'a pas été défini");
	}

	oElement.style.display="none";
	this.displayStatus="hidden";

}

//////////////////////////////////////////////////////////////////////////////
// Methode publique permettant de montrer le panneau
//
// Entrée : 
//////////////////////////////////////////////////////////////////////////////

Panel.prototype.show = function()
{
	var oElement = this.HTMLElement;
	if (PanelisNull(oElement))
	{
		alert("Panel.class.js : L'élément HTML contenant le Panneau n'a pas été défini");
	}

	oElement.style.display="";
	oElement.style.visibility="visible";
	this.displayStatus="visible";
}



//////////////////////////////////////////////////////////////////////////////
// Methode privée permettant de redimensionner et déplacer le panneau
//
// Entrée : nValue : un entier définissant soit la hauteur, soit la différence de hauteur avec l'Element
//////////////////////////////////////////////////////////////////////////////

Panel.prototype.resize = function() {

	window.clearInterval(this.Interval);
	//Coordonnees de depart
	var oElement = this.HTMLElement;
	if (PanelisNull(oElement))
	{
		alert("Panel.class.js : L'élément HTML contenant le Panneau n'a pas été défini");
	}
	
	if (this.fromwidth!=null)oElement.style.width=this.fromwidth;
	if (this.fromheight!=null)oElement.style.height=this.fromheight;

	if (this.fromleft==null)this.fromleft=parseInt(oElement.style.left);
	if (this.fromtop==null)this.fromtop=parseInt(oElement.style.top);


	if (this.toleft==null)this.toleft=this.fromleft;
	if (this.totop==null)this.totop=this.fromtop;
	if (this.toheight==null)this.toheight=this.fromheight;
	if (this.towidth==null)this.towidth=this.fromwidth;
	if (this.toheight==null)this.toheight=this.fromheight;

	if (this.fromleft!=null)
	{
		var tmpleft=(screen.availWidth/2)-(parseInt(oElement.style.width)/2);
		if (this.fromleft=="centered")oElement.style.left=tmpleft+"px";
		else oElement.style.left=this.fromleft+"px";
	}
	if (this.fromtop!=null)
	{
		var tmptop=(screen.availHeight/2)-(parseInt(oElement.style.height)/2);
		if (this.fromtop=="centered")oElement.style.top=tmptop+"px";
		else oElement.style.top=this.fromtop+"px";
	}
	if (this.fromleft!="centered" && this.toleft=="centered")
	{
		this.toleft=(screen.availWidth/2)-(parseInt(this.towidth)/2);
	}
	if (this.fromtop!="centered" && this.totop=="centered")
	{
		this.totop=(screen.availHeight/2)-(parseInt(this.toheight)/2);
	}
	
	oElement.style.display="";
	oElement.style.visibility="visible";
	this.displayStatus="visible";
	

	this.Interval = window.setInterval(this.name+"._resize('"+this.fromleft+"','"+this.fromtop+"','"+this.toleft+"','"+this.totop+"',"+this.towidth+","+this.toheight+")",this.speed);
}

Panel.prototype._resize = function(froml,fromt,x,y,w,h) {

	var oElement = this.HTMLElement;

	if (oElement != undefined)
	{
		var widthnow = parseInt(oElement.style.width);
		var heightnow = parseInt(oElement.style.height);	
		var topnow = parseInt(oElement.style.top);
		var leftnow = parseInt(oElement.style.left);
	
		//if (!PanelisNumber(widthnow))widthnow=0;
		//if (!PanelisNumber(heightnow))heightnow=0;
		//if (!PanelisNumber(topnow))topnow=0;
		//if (!PanelisNumber(leftnow))leftnow=0;
	
	
		if (w==null) w = widthnow;
		if (h==null) h = heightnow;	
		if (y==null) y = topnow;
		if (x==null) x = leftnow;
	
		if (y!="centered")topnow = parseInt(topnow+((y-topnow)*this.ratio));
		if (x!="centered")leftnow = parseInt(leftnow+((x-leftnow)*this.ratio));
		
		widthnow = parseInt(widthnow+((w-widthnow)*this.ratio));
		heightnow = parseInt(heightnow+((h-heightnow)*this.ratio));
		
		//Verif du width
		diffwidth=widthnow-w;
		if (this.lastdiffwidth==diffwidth)widthnow=w;
		else this.lastdiffwidth=diffwidth;
		
		//Verif du height
		diffheight=heightnow-h;
		if (this.lastdiffheight==diffheight)heightnow=h;
		else this.lastdiffheight=diffheight;
	
		//Verif du left
		if (froml=="centered" && x=="centered")
		{
			oElement.style.left=(screen.availWidth/2)-(widthnow/2)+"px";
			x=parseInt(oElement.style.left);
		}
		else
		{
			diffleft=leftnow-x;
			if (this.lastdiffleft==diffleft)leftnow=x;
			else this.lastdiffleft=diffleft;
			oElement.style.left=parseInt(leftnow)+"px";	
		}
	
		//Verif du top
		if (fromt=="centered" && y=="centered")
		{
			oElement.style.top=(screen.availHeight/2)-(heightnow/2)+"px";	
			y=parseInt(oElement.style.top);	
			
		}
		else
		{
			difftop=topnow-y;
			if (this.lastdifftop==difftop)topnow=y;
			else this.lastdifftop=difftop;
			oElement.style.top=parseInt(topnow)+"px";		
		}
		
	  if (!isNaN(widthnow))oElement.style.width=parseInt(widthnow)+"px";	
	  if (!isNaN(heightnow))this.setHeight(parseInt(heightnow));
	
	  if (isNaN(widthnow))widthnow = 0;		
	  if (isNaN(heightnow))heightnow = 0;		
	  if (isNaN(topnow))topnow = 0;		
	  if (isNaN(leftnow))leftnow = 0;		
	  if (isNaN(y))y = 0;		
	  if (isNaN(x))x = 0;		
	  if (isNaN(w))w = 0;		
	  if (isNaN(h))h = 0;		
	
	
		//document.getElementById("test").innerHTML="top:"+topnow+" / "+y+"<br>left:"+leftnow+" / "+x+"<br>width:"+widthnow+" / "+w+"<br>height:"+heightnow+" / "+h;
	
		
	
		if (parseInt(topnow) == parseInt(y) && parseInt(leftnow)== parseInt(x) && parseInt(widthnow)==parseInt(w) && parseInt(heightnow)==parseInt(h)) 
		{
			//Fin
			window.clearInterval(this.Interval);
			//CallBack
			oElement.focus();
			if (this.CallBack!=null && this.CallBack!="")
			{
				eval(this.CallBack);
			}
			if (this.toDisplayStatus=="hidden")this.hide();
			this.initialize();
		}
		else
		{
			//document.getElementById("test").innerHTML="test:"+this.DuringCall;
			if (this.DuringCall!=null && this.DuringCall!="")
			{
				eval(this.DuringCall);
			}
		}	
	
	}
	else
	{
		window.clearInterval(this.Interval);		
	}
	
}

function PanelisObject(a) {
    return (a && typeof a == 'object') || PanelisFunction(a);
}

function PanelisFunction(a) {
    return typeof a == 'function';
}

function PanelisNumber(a) {
    return typeof a == 'number' && isFinite(a);
}

function PanelisNull(a) {
    return a === null;
}