/*
	Une palette est une petite fenêtre qui s'affiche au-dessus de toutes les
	autres.
	Elle est ouverte par l'appel:
	
		var palette = new $Palette(client, id);
		
	On dispose de:
		
		palette.Refresh();
		palette.close();
	
	CLIENT doit implémenter:
	
		CLIENT.PaletteRequest(ID, PARAM)
		CLIENT.PaletteEvent(ID, EVT)
*/


window.$Palette = function(CLIENT, PALETTE_ID, PALETTE_TITLE, FOCUS) {
//------=======-----------------------------------------------------

/*
	Construit, affiche, rend sensible une palette vide, et appelle Refresh()
	pour en initialiser le contenu.
*/
	var
		T,
		R,
		C,
		CLIENT_DIV;

	T = $Ce('table');
	if ($Platform('firefox'))
		T.style.position = 'absolute'; // Autrement, problème de drag à cause de "fixed".
	T.className = 'cPALETTE';
	T.id = PALETTE_ID;
	
	R = T.insertRow(-1);
		
	C = R.insertCell(-1);
	C.className = 'cLT';

	C = R.insertCell(-1);
	C.className = 'cT';
	C.innerHTML =
		'<span class="cBUTTON" onclick="$Nca(this, \'cPALETTE\').JS_Obj.Close()">X</span>' +
		'<span>' + PALETTE_TITLE + '</span>';
		
	C = R.insertCell(-1);
	C.className = 'cRT';

	R = T.insertRow(-1);
		
	C = R.insertCell(-1);
	C.className = 'cL';

	C = R.insertCell(-1);
	C.className = 'cC';
	CLIENT_DIV = $Ce('div');
	CLIENT_DIV.className = 'cCLIENT';
	C.appendChild(CLIENT_DIV);
	T.CLIENT_DIV = CLIENT_DIV;
				
	C = R.insertCell(-1);
	C.className = 'cR';
		
	R = T.insertRow(-1);
		
	C = R.insertCell(-1);
	C.className = 'cLB';

	C = R.insertCell(-1);
	C.className = 'cB';
		
	C = R.insertCell(-1);
	C.className = 'cRB';
		
	T.onmousedown =
	T.onmousemove =
	T.onmouseup =
	T.onclick = function(evt) { return T.JS_Obj.ProcessEvent($Event.NrmEvt(evt)); };
	T.JS_Obj = this;
	document.body.appendChild(this.Palette = T);
	this.CLIENT = CLIENT;
	this.Refresh(FOCUS);
};


$Palette.prototype.Close = function() {
//-----------------=====-------------

/*
	Supprime la palette concernée.
*/

	this.Palette.parentNode.removeChild(this.Palette);
};


$Palette.prototype.ToggleSize = function() {
//-----------------==========-------------

/*
	Minimise ou Maximise la palette concernée.
*/

	this.Palette.CLIENT_DIV.style.display = (this.Palette.CLIENT_DIV.style.display == 'none' ? 'block' : 'none');
};


$Palette.prototype.Refresh = function(FOCUS) {
//-----------------=======------------------

	var
		SELF = this,
		REQUEST = this.CLIENT.PaletteRequest(this.Palette.id, FOCUS);
		
	$Ajax.Send(
		REQUEST['URL'],
		REQUEST['PARAMS'],
		function (RESPONSE) { SELF.Palette.CLIENT_DIV.innerHTML = RESPONSE; }
	);
};


$Palette.prototype.Move = function(X, Y) {
//-----------------====-----------------

	this.Palette.style.left = this._LEFT + X + 'px';
	this.Palette.style.top = this._TOP + Y + 'px';
};


$Palette.prototype.ProcessEvent = function(EVT) {
//-----------------============----------------

	var
		SELF = this;
		
	if (EVT.Type == 'mousedown') {
		if (document.body.lastChild != this.Palette)
			document.body.appendChild(this.Palette);
		this._LEFT = this.Palette.offsetLeft;
		this._TOP = this.Palette.offsetTop;
		if ($Hcn($Nta(EVT.Target, 'td'), 'cT') || $Hcn(EVT.Target, 'cLT') || $Hcn(EVT.Target, 'cRT')) {
			$Drag.Track(
				EVT,
				function(X, Y) {
					SELF.Move(X, Y);
				}
			);
		}
	}
	else if (EVT.Type == 'click')
		this.CLIENT.PaletteEvent(this.Palette.id, EVT);
};
