if (!BLT) var BLT={};

BLT.Tip=new Class({
	Implements: [Options, Events],
	
	options: {
		margintop: 5,
		marginleft: 0
	},
	
	initialize: function(text, element) {
		this.element=this.createHtml(text);
		this.element.fade('hide');
		this.element.inject($(document.body));

		if (element) this.setPositionBy(element);
	},
	
	createHtml: function(text) {
		var e=new Element('div', {
			html: text,
			'class': 'blutoo-tip'
		});

		var close=new Element('div', {'html': 'x', 'class': 'blutoo-tip-close'}).inject(e);
		var arrow=new Element('div', {'class': 'blutoo-tip-arrow'}).inject(e);
		
		arrow.setStyles({
			left: 20,
			bottom: -5
		});
		
		e.addEvent('click', function(el) {
			this.close();
		}.bind(this));

		return e;
	},
	
	setPositionBy: function(element) {
		var pos=element.getPosition($(document.body));
		var size=element.getSize();
		
		var tsize=this.element.getSize();
		
		this.setPosition(pos.x+size.x/2-25+this.options.marginleft, pos.y-tsize.y+this.options.margintop);
	},
	
	setPosition: function(x, y) {
		this.element.setStyles({
			left: x,
			top: y
		});
	},
	
	show: function() {
		this.element.fade(0.7);
		this.fireEvent('show');
	},
	
	close: function() {
		if (!this.isclosing) {
			this.isclosing=true;
			
			this.element.fade('out').get('tween').chain(function() {
				if (this.element) this.destroy();
			}.bind(this));
		}
	},
	
	destroy: function() {
		this.fireEvent('destroy');
		this.element.destroy();
		this.element=null;
	}
});
