// Nuevo Helptip
var Helptip = new Class({
	Implements: [Options, Events],
	'options': {
			'swOptions': {
					'showNow': false
			},
			'autoFade': true,
			'mostrarImagenEncabezado': true
	},
	initialize:	function(options) {
			// Atributos
			var yo = this;
			this.setOptions(options);
			
			this.SW_options = this.options.swOptions;
			if($defined(this.options.swOptions.relativeTo)) this.elemento = $(this.options.swOptions.relativeTo);
			
			this.options.swOptions.requestOptions = {
					'method'	:	'post',
					'data'		:	"url="+this.options.data
			};
			
			// Controlo la carga de AJAX del StickyWin
			this.options.swOptions.handleResponse = function(html) {
					this.crearVentana(html);
			}.bind(this);
			
			this.HP_stickywin = new StickyWin.Fx.Ajax(this.options.swOptions);
			if(this.options.autoFade)
			{
				this.elemento.addEvent('mouseenter', function() {
						this.show();
				}.bind(this.HP_stickywin));
				
				this.elemento.addEvent('mouseleave', function() {
						this.timeHide();
				}.bind(this));
			}
			
			this.HP_stickywin.update();
			
	},
	
	show: function() {
			if(this.HP_stickywin.win.getStyle('opacity') > 0)
				this.HP_stickywin.win.setStyle('opacity', 0);
			this.HP_stickywin.show();
	},
	
	hide: function() {
			this.HP_stickywin.hide();
	},
	
	timeHide: function() {
			this.timeout = this.hide.delay(250, this);
	},
	crearVentana: function(html) {
			this.datos = eval('('+html+')'); // Leo el JSON
			
			// Creo los elementos de la ventana dinámicamente.
			var contenedor = new Element('div');
			// Div imagen
			if(this.options.mostrarImagenEncabezado)
				contenedor.adopt(new Element('div').setProperty('align', 'center').adopt(new Element('img').setProperties({
							'src'		: this.datos.imagen.src,
							'witdh'		: this.datos.imagen.witdh,
							'height'	: this.datos.imagen.height,
							'alt'		: this.datos.imagen.alt
					})));
			// Div descripcion
			contenedor.adopt(new Element('div').setStyles({'textAling': 'justify', 'marginTop': '5px'}).setProperty('html', this.datos.descripcion
			));
			//alert(this.options);
			for(var i=0; i < this.datos.buttons.length; i++)
			{
				if(this.options.swOptions.uiOptions.buttons)
					this.options.swOptions.uiOptions.buttons.push({text: this.datos.buttons[i].texto, 'properties' : {'class': 'linkHelptip', 'href': this.datos.buttons[i].url}
					});
				else
					this.options.swOptions.uiOptions.buttons = [{text: this.datos.buttons[i].texto, 'properties' : {'class': 'linkHelptip', 'href': this.datos.buttons[i].url}
					}];
			}
			
			// Creo una interfaz de StickyWin basada en el div contenedor que generé
			this.HP_stickywin.html = StickyWin.ui(contenedor, this.options.swOptions.uiOptions);
			
			// Le asigno a la ventana los eventos para el timeout
			this.HP_stickywin.html.addEvent('mouseenter', function() {
					clearTimeout(this.timeout);
			}.bind(this));
			this.HP_stickywin.html.addEvent('mouseleave', function() {
					clearTimeout(this.timeout);
					this.timeout = this.hide.delay(250, this);
			}.bind(this));
			
			// Le agrego los eventos al link del cual está relacionado el StickyWin
			$(this.options.swOptions.relativeTo).addEvent('mouseenter', function() {
					clearTimeout(this.timeout);
			}.bind(this));
			
			// Seteo el contenido del StickyWin con la interfaz creada.
			this.HP_stickywin.setContent(this.HP_stickywin.html);
	}
});
