/* EVENT Manager */
Yke.UI.EventManager = function()
{
	this._handlers = new Array();
}
/* + void */ Yke.UI.EventManager.prototype.add = function(handler)
{
	this._handlers.push(handler);
	return this._handlers.length - 1;
}

/* + void */ Yke.UI.EventManager.prototype.remove = function(id)
{
	this._handlers[i] = null;
}

/* + void */ Yke.UI.EventManager.prototype.fire = function(sender, e)
{
	var ret = true;
	for(var i=0; i<this._handlers.length; i++)
	{
		if(this._handlers[i] != null)
		{
			try
			{
				var r = this._handlers[i](sender, e);
				if(r == false) ret = false;
			}
			catch(e)
			{
			}
		}
	}
	return ret;
}

/* CONTROL MANAGER */

Yke.UI.ControlManager = {};
Yke.UI.ControlManager._controls = {};

Yke.UI.ControlManager.contains = function (name)
{
	var exists = false;
	if(Yke.UI.ControlManager._controls[name] != null) exists = true;
	return exists;
}

Yke.UI.ControlManager.getControl = function (name)
{
	return Yke.UI.ControlManager._controls[name];
}

Yke.UI.ControlManager.getControlValue = function (name)
{
	try
	{
		var ctrl = Yke.UI.ControlManager.getControl(name);
		return ctrl.getValue();
	}
	catch(e)
	{
		$YDEBUG("CONTROLNOTFOUND", "getControlValue failed because the control '" + name + "' does not exists", name, 2);
		return null;
	}
}

Yke.UI.ControlManager.setControlValue = function (name, value)
{
	try
	{
		var ctrl = Yke.UI.ControlManager.getControl(name).setValue(value);
	}
	catch(e)
	{
		$YDEBUG("CONTROLNOTFOUND", "setControlValue failed because the control '" + name + "' does not exists", name, 2);
	}
}





/* CONTROL GENERIQUE */

Yke.UI.Control = function()
{
	/* # */ this._controlName = "Abstract Control";
	
	/* # */ this._value = null;

	/* # */ this._id = null;
	/* # */ this._container = null;
	/* # */ this._domControl = null;
	
	/* # */ this._needControls = null;
	/* # */ this._needFormatters = null;
	
	/* # */ this._visible = true;
	
	/* # */ this._rendered = false;
	
	/* # */ this._readOnly = false;
		
	/* # */ this._highlight = false;//if(this._highlight==true)
}
	
	
/* + void */ Yke.UI.Control.prototype.render = function(HTMLContainer, id)
{
	this._id = id;
	this._container = $(HTMLContainer);
	this.repaint();
	if(this._value != null) 
	{
		this.updateControlValue();
	}
}


/* + bool */ Yke.UI.Control.prototype.repaint = function()
{
	this.makeDomControl();
	this._container.innerHTML = '';
	this._container.appendChild(this._domControl);
	this._rendered = true;
}

/* # virtual */ Yke.UI.Control.prototype.makeDomControl = function ()
{
	this._domControl = document.createElement('span');
	this._domControl.appendChild( document.createTextNode('abstract control') );	
}


/* + void */ Yke.UI.Control.prototype.setVisible = function(b)
{
	this._visible = b;
	if (b == true) this._domControl.style.display = 'none';
	else this._domControl.style.display = '';
}
/* + bool */ Yke.UI.Control.prototype.getVisible = function()
{
	return this._visible;
}

/* + str */ Yke.UI.Control.prototype.getID = function()
{
	return this._id;
}

/* CONTROL GENERIQUE contenant une valeur */

Yke.UI.ValueControl = function () {};
ykeCopyPrototype(Yke.UI.ValueControl, Yke.UI.Control);

Yke.UI.ValueControl.prototype._formatter = null;
Yke.UI.ValueControl.prototype._readOnly = false;


/* + str */ Yke.UI.ValueControl.prototype.getValue = function()
{
	if(this._rendered) this.getValueFromControl();
	var ret = this._value;
	return ret;
}

/* + void */ Yke.UI.ValueControl.prototype.setValue = function(val)
{
	if(this.checkValue(val))
	{
		var v = val;
		this._value = v;
		if(this._rendered) this.updateControlValue();
	}
	else
	{
		$YDEBUG(this._controlName + " : Invalid value", "impossible to set the value of a the " + this._controlName + " Control with the following object", val, 2);
	}
}

/* # bool */ Yke.UI.ValueControl.prototype.checkValue = function(val)
{
	return true;
}

/* + void */ Yke.UI.ValueControl.prototype.getValueFromControl = function()
{
	/* va chercher la valeur dans le controle */
}

/* + void */ Yke.UI.ValueControl.prototype.updateControlValue = function()
{
	/* écrit la valeur dans le controle */
}

/* + void */ Yke.UI.ValueControl.prototype.focus = function()
{
	alert("not implemented");
}

/* + void */ Yke.UI.ValueControl.prototype.setReadOnly = function(b)
{
	this._readOnly = b;
}
/* + bool */ Yke.UI.ValueControl.prototype.getReadOnly = function()
{
	return this._readOnly;
}

