/**
Script: TipsForm.js
	Class for creating nice tips to show forms validation errors
	this tip will not follow the mouse, and didn't use mouse events.
	
	Based on Mootools Tips
	
	Original Authors:
		Valerio Proietti
		Christoph Pojer

	Author : 
		Arnaud Guéras
*/

(function(){

var read = function(option, element){
	return (option) ? ($type(option) == 'function' ? option(element) : element.get(option)) : '';
};

this.TipsFix = new Class({

	Implements: [Events, Options],

	options: {
		text: '',
		title : '',
		positionFrom : null, //element sur lequel le tooltip se positionnera, par defaut ça sera l'élément declaré
		className: 'tip-form',
		offset: {x: 0, y: -3},
		fixed: false,
		onShow : function() {
			this.tip.setStyle('visibility','visible');
		},
		onHide : function() {
			this.tip.setStyle('visibility','hidden');
		}
	},

	initialize: function(){
		var params = Array.link(arguments, {options: Object.type, elements: $defined});
		this.element = params.elements;
		this.setOptions(params.options);
		this.positionFrom = this.options.positionFrom !=null ? $(this.options.positionFrom) : $(this.element);
		document.id(this);
	},

	toElement: function(){
		if (this.tip) return this.tip;
		
		this.container = new Element('div', {'class': 'formtip'});
		return this.tip = new Element('div', {
			'class': this.options.className,
			styles: {
				visibility: 'hidden',
				position: 'absolute',
				top: 0,
				left: 0
			}
		}).adopt(
			new Element('div', {'class': 'formtip-top'}),
			this.container,
			new Element('div', {'class': 'formtip-bottom'})
		).inject(document.body);
		
	},

	setText : function(text) {
		this.setValue('text', text);
	},

	setTitle : function(title) {
		this.setValue('title', title);
	},

	setValue : function(name) {
		if (this['formtip'+name] && (!this[name] || this[name] == '')) {
			this['formtip'+name].innerHTML = '';
			return;
		}
		if (!this['formtip'+name]) {
			this['formtip'+name] = new Element('div', {
				"class": 'formtip-' + name
			});
			this['formtip'+name].inject(this.container);
		}
		this.fill(this['formtip'+name], this[name]);
	},

	position: function(){
		if (this.tip.offsetWidth>this.element.offsetWidth) 
			this.tip.setStyle('width',this.element.offsetWidth);
		
		
		var size = window.getSize(), scroll = window.getScroll(),
			tip = {x: this.tip.offsetWidth, y: this.tip.offsetHeight},
			props = {x: 'left', y: 'top'},
			elementPos = $(this.positionFrom).getCoordinates(),
			obj = {};
		
		obj.top = elementPos.top - tip.y + this.options.offset.y;
		obj.left = elementPos.left + ((elementPos.width-tip.x)/2);
		
		this.tip.setStyles(obj);
	},

	fill: function(element, contents){
		if(typeof contents == 'string') element.set('html', contents);
		else element.adopt(contents);
	},

	show: function(element){
		this.setText();
		this.setTitle();
		this.position();
		this.fireEvent('show', [element]);
	},

	hide: function(element){
		this.fireEvent('hide', [element]);
	}

});

})();
