/**
 * errorbox
 *
 *  *  *  *  * *
 * PARAMETROS  *
 *  *  *  *  * * 
 * inputElement: o elemento com erro
 *
 * mesg: óbvio
 *
 * options:
 	duration : tempo de duração da msg, 5 segundos por defeito
 *
 **/
var ErrorBox = new Class(
{
	options :
	{
		duration:3000,
		autoKill: true,
		msgXPosition : 'e', // e = esquerda, d = dta, m = meio, a = alinhado esquerda
		msgYPosition : 'm' // c = cima, b = baixo, m = meio
	},
	Implements : [Options],
	initialize: function(inputElement,msg,options)
	{
		this.setOptions(options);
		this.element = inputElement;
		this.msg = msg;
		this.label = null;
		this.box = this.createBox();
		this.box.set('morph',{duration:500,'link':'chain'});
		this.box.inject(document.body);
		this.element.addEvent('focus',function()
		{
			this.element.removeEvent('focus');
			this.kill();
		}.bind(this));
	},
	getPosicao : function()
	{
		var r =
		{
			x:0,
			y:0
		};			
		var p = this.element.getCoordinates();//posicao do elemento
		var pm = this.box.getCoordinates();
		switch (this.options.msgXPosition)
		{
			default:
			case 'e':
				r.x = p.left-pm.width-5;
			break;
			case 'd':
				r.x = p.left+p.width+5;
			break;
			case 'm':
				r.x = p.left+(p.width / 2) - (pm.width / 2);
			break;
			case 'a':
				r.x = p.left;
			break;			
		};
		switch (this.options.msgYPosition)
		{
			default:
			case 'c':
				r.y = p.top-pm.height;
			break;
			case 'b':
				r.y = p.top+p.height+2;
			break;
			case 'm':
				r.y = p.top-((pm.height-p.height)/2);
			break;
		};
		if (parseInt(this.options.msgXPosition) > 0)
		{
			r.x = p.left + this.options.msgXPosition * 1;
		}
		if (parseInt(this.options.msgYPosition) > 0)
		{
			r.y = p.top + this.options.msgYPosition * 1;
		}		
		return r;
	},
	show : function(msg)	
	{
		if (this.element.hasFocus()) return true;
		if (msg)
		{
			this.label.set('text',msg);
		}
		var posicao = this.getPosicao();
		this.box.set('styles',
		{
			'left':posicao.x,
			'top':posicao.y
		});
		this.box.morph(
		{
			'opacity':1
		});
		this.kill.delay(this.options.duration,this);
	},
	kill: function()
	{
		this.box.morph(
		{
			'opacity':0
		}).get('morph').chain(function()
		{
			if (this.options.autoKill) this.box.destroy();
		}.bind(this)
		);
	},
	createBox : function()
	{
		var el = new Element('div',{'class':'error-box'});		
		el.set('styles',
		{
			'position':'absolute',
			'left': -5000, //fora de vista
			'top': 0,
			'opacity':0,
			'cursor':'pointer',
			'z-index':1001
			//'padding-right':30 //espaço para a seta			
		});
		el.addEvent('click',function()
		{
			this.kill();
		}.bind(this));
		var lbl = new Element('p');
		lbl.set('text',this.msg);
		lbl.inject(el);
		this.label = lbl;
		return el;
	}
});
