// JavaScript Document

/*
 * alertachat = new Alerta({
           				divAlerta: $('mostraralerta')
                   	});
 */  


var Alerta = new Class(
{
	Implements: Options,
	
	options: 
	{
		divAlerta: null,		
		anchoBorde: 0,
		margenTop:0,
		margenLeft:0,
		margenBottom:0,
		margenRight:00,
		w: 150,
		h: 300,
		top: null,
		left: null,
		bottom: null,
		align: 'bottomleft',
		textos: null,
		sexo: 'm'
	},
	 
	initialize: function(options)
	{
		this.setOptions(options);
		this.getPosTop();
		this.getPosLeft();
		
		if($$('.contenedor')){
			$$('.contenido').destroy();
		}
		
		texto = this.options.divAlerta.get('html');
		
		if (texto.indexOf("</div>") != -1) {
			this.options.textos = texto.split("</div>");
		}else if(texto.indexOf("</DIV>") != -1){
			this.options.textos = texto.split("</DIV>");
		}else{
			this.options.textos = texto.split("</div>");
		}
		
		element = this;
		var req = new Request({
	        method: 'post',
	        url: '/ajax/micuenta.php',
	        data: {
	            accion: 'tipoayuda'
	        },
	        onSuccess: function(e){
				element.options.sexo = e;
				element.getAlerta();
			}
	    }).send();
		
	},
	
	getAlerta: function()
	{
		texto = this.options.textos.shift();
		texto = texto.replace(/<div>/,""); 
		texto = texto.replace(/<DIV>/,"");
		
		if(texto!='')
		{
			//alert(texto);
						
			contenedorr = new Element('div', {'class': 'contenido'}).inject(document.body, 'inside');
			
			contenedorr.setStyle("height", this.options.h);
			contenedorr.setStyle("width", this.options.w);
			
			idayudas = 'ayudas'+this.options.sexo;
			contenidoo = new Element('div', {'class' : 'ayudas', 'id': idayudas}).inject(contenedorr, 'inside');
			contenidoop = new Element('p', {'id': 'payudas'}).inject(contenidoo, 'inside');
			
			texto = texto.replace('</DIV>',"");
			
			contenidoo.setStyle("opacity", 0);
						
			contenidoop.set('html', texto);
			
			//contenidoo.fade(1);
			var fx = new Fx.Morph(contenidoo, {duration:1200, wait:false});
			fx.start({
				'opacity': 1
			});	
			
			//alert('getAlerta morph');

			if (this.options.textos.length > 1) {
				this.ocultarAlerta.delay(4000, this, contenidoo);
			//this.eliminarAlerta.delay(8000, this);	
			}else{
				this.ocultarAlerta.delay(7000, this, contenidoo);
			}
			
		}
	},
	
	cambiarTexto: function(contenido)
	{
		texto = this.options.textos.shift();
		
		texto = texto.replace(/<div>/,"");
		texto = texto.replace(/<DIV>/,""); 
		
		$('payudas').set('html',texto);
		
		//$('payudas').setStyle("opacity", 0);
		
		/*
		var fx = new Fx.Morph($('payudas'), {duration:1000, wait:false});
			fx.start({
				'opacity': 1
			});
		*/
			
		this.ocultarAlerta.delay(7000, this, contenido);
		
	},
	
	ocultarAlerta: function(contenido)
	{
		if (this.options.textos.length > 1) {
			/*
			var fx = new Fx.Morph($('payudas'), {duration:500, wait:false});
			fx.start({
				'opacity': 0
			});
			*/
			this.cambiarTexto(contenido);
		}else{
			contenido.fade(0);
			this.eliminarAlerta.delay(8000, this);
		}
	},
	
	eliminarAlerta: function()
	{
		$$('.contenedor').destroy();
		idayudas = 'ayudas'+this.options.sexo;
		$(idayudas).destroy();
		
		if (this.options.textos.length > 0) {
			this.getAlerta();
		}

	},
	getPosTop: function()
	{
		if(this.options.align=='bottomleftabsolute')
		{
			this.options.bottom = this.options.margenBottom; //window.getScrollTop()+(window.getHeight()/2) - this.options.margenBottom;
		}
		else if(this.options.align=='bottomleft')
		{
			this.options.top = window.getScrollTop() + window.getHeight() - (this.options.h);
		}
	},
	
	getPosLeft: function()
	{
		if(this.options.align=='bottomleftabsolute')
		{
			this.options.left = (window.getWidth()/2) + this.options.margenLeft;
		}
		else if(this.options.align=='bottomleft')
		{
			this.options.left = ((this.options.w));	
		}
	}	
		
});