// JavaScript Document
window.addEvent('domready', function() {
	
	// mostramos notificacions (añadir amigo, enviar mensaje) //
	botons = $$('.notificacion');
	botons.each(function(div, i) {

		div.addEvent('click', function(e) {
			new Event(e).stop();
			//alerta//
			ids = this.id.split('-');
			tipo = ids[0];
			if(tipo=='enviar_mensaje')
			{
				$('notificacionalerta').setStyle("width",300);
				$('notificacionalerta').setStyle("height",200);
			}
			else if(tipo=='agregar_amigo')
			{
				$('notificacionalerta').setStyle("width",300);
				$('notificacionalerta').setStyle("height",200);
			}
			else
			{
				$('notificacionalerta').setStyle("width",380);
				$('notificacionalerta').setStyle("height",380);
			}
			
			var h = window.getScrollHeight();
			$('fondoalerta').setStyle("height",h);
			$('fondoalerta').setStyle("display","block");

				//notificacionalerta//
				$('bordealerta').setStyle("height", $('notificacionalerta').getStyle('height').toInt()+20);
				$('bordealerta').setStyle("width", $('notificacionalerta').getStyle('width').toInt()+20);
				$('bordealerta').setStyle("background", "#5586a5");
				
				var oldhborde = $('bordealerta').getStyle('height').toInt();
				
				$('bordealerta').setStyle("height",40);
				$('bordealerta').setStyle("display","block");
				
				var t = window.getScrollTop()+(pageHeight()/2)-(oldhborde/2);
				var l = (pageWidth()/2)-($('bordealerta').getStyle('width').toInt()/2);
				$('bordealerta').setStyle("top",t);
				$('bordealerta').setStyle("left",l);
				
				//notificacion//
				var oldhnotificacion = $('notificacionalerta').getStyle('height').toInt();
				
				$('notificacionalerta').setStyle("height",20);
				
				$('notificacionalerta').setStyle("display","block");
				var t = window.getScrollTop()+(pageHeight()/2)-(oldhnotificacion/2);
				var l = (pageWidth()/2)-($('notificacionalerta').getStyle('width').toInt()/2);
				$('notificacionalerta').setStyle("top",t);
				$('notificacionalerta').setStyle("left",l);
				
				
				
				mostrarNotificacion(this.id, oldhborde, oldhnotificacion);
				//------------------//
			//------//
		
		});

	});
	// -------------------------------------------------------------- //
	
	// cerramos alertas //
	$('fondoalerta').addEvent('click', function() {
		cerrar_alertas();
	});
	// --------------- //
	
	// mostramos errores //
	if($('erroralerta').get('text')!='')
	{
		//alerta//
		var h = window.getScrollHeight();
		$('fondoalerta').setStyle("height",h);
		$('fondoalerta').setStyle("display","block");
			
			//bordealerta//
			
			$('bordealerta').setStyle("height", $('erroralerta').getStyle('height').toInt()+20);
			$('bordealerta').setStyle("width", $('erroralerta').getStyle('width').toInt()+20);
			$('bordealerta').setStyle("background", "#990000");
			
			var oldhborde = $('bordealerta').getStyle('height').toInt();
			
			$('bordealerta').setStyle("display","block");
			
			var t = window.getScrollTop()+(pageHeight()/2)-(oldhborde/2);
			var l = (pageWidth()/2)-($('bordealerta').getStyle('width').toInt()/2);
			$('bordealerta').setStyle("top",t);
			$('bordealerta').setStyle("left",l);
			
			//error//
			var oldherror = $('erroralerta').getStyle('height').toInt();
			
			$('erroralerta').setStyle("display","block");
			var t = window.getScrollTop()+(pageHeight()/2)-(oldherror/2);
			var l = (pageWidth()/2)-($('erroralerta').getStyle('width').toInt()/2);
			$('erroralerta').setStyle("top",t);
			$('erroralerta').setStyle("left",l);
			$('erroralerta').setStyle('background','url(/imagenes/error_bg.jpg) bottom right no-repeat #FFF');
			
			// boton para cerrar alertas //
			botoncerrar = new Element('div', {'class': 'cerrar-alerta-rojo'}).inject($('erroralerta'), 'inside');
			//text.set('html','Imagen subida!');
			
			botoncerrar.addEvent('click', function() {
				cerrar_alertas();
			});
			// //
			
			//------------------//
		//------//
	}
	// ------------------------------------------ //
	
	
	// mostramos confirmacion de datos (todo ok) //
	if($('confirmacionalerta').get('text')!='')
	{
		//alerta//
		var h = window.getScrollHeight();
		$('fondoalerta').setStyle("height",h);
		$('fondoalerta').setStyle("display","block");
			
			//bordealerta//
			
			$('bordealerta').setStyle("height", $('confirmacionalerta').getStyle('height').toInt()+20);
			$('bordealerta').setStyle("width", $('confirmacionalerta').getStyle('width').toInt()+20);
			$('bordealerta').setStyle("background", "#579e57");
			
			var oldhborde = $('bordealerta').getStyle('height').toInt();
			
			$('bordealerta').setStyle("display","block");
			
			var t = window.getScrollTop()+(pageHeight()/2)-(oldhborde/2);
			var l = (pageWidth()/2)-($('bordealerta').getStyle('width').toInt()/2);
			$('bordealerta').setStyle("top",t);
			$('bordealerta').setStyle("left",l);
			
			//error//
			var oldhconfirmacion = $('confirmacionalerta').getStyle('height').toInt();
			
			$('confirmacionalerta').setStyle("display","block");
			var t = window.getScrollTop()+(pageHeight()/2)-(oldhconfirmacion/2);
			var l = (pageWidth()/2)-($('confirmacionalerta').getStyle('width').toInt()/2);
			$('confirmacionalerta').setStyle("top",t);
			$('confirmacionalerta').setStyle("left",l);
			$('confirmacionalerta').setStyle('background','url(/imagenes/confirmacion_bg.jpg) bottom right no-repeat #FFF');
			
			// boton para cerrar alertas //
			botoncerrar = new Element('div', {'class': 'cerrar-alerta-verde'}).inject($('confirmacionalerta'), 'inside');
			//text.set('html','Imagen subida!');
			
			botoncerrar.addEvent('click', function() {
				cerrar_alertas();
			});
			// //
			
			//------------------//
		//------//
	}
	// ------------------------------------------ //
	
	
});

// cerramos alertas //
function cerrar_alertas()
{
	$('fondoalerta').setStyle("display","none");
	$('bordealerta').setStyle("background", "#000");
	$('bordealerta').setStyle("display","none");
	$('bordealerta').setStyle('background','#FFF');
	
	$('notificacionalerta').setStyle("display","none");
	$('notificacionalerta').innerHTML='';
	
	$('erroralerta').setStyle("display","none");
	$('erroralerta').setStyle('background','#FFF');
	$('erroralerta').innerHTML='';
	
	$('confirmacionalerta').setStyle("display","none");
	$('confirmacionalerta').setStyle('background','#FFF');
	$('confirmacionalerta').innerHTML='';
	
	if($('flashcontent'))
	{
		$('flashcontent').setStyle("visibility","visible");
	}
}
// calculate the current window width //
function pageWidth() {
  return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

// calculate the current window height //
function pageHeight() {
  return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}


function mostrarNotificacion(id, oldhborde, oldhnotificacion)
{
	//$('notificacionalerta').setHTML('<img src="/imagenes/ajax-loader.gif" />');
	
	$('notificacionalerta').set('html', 'Cargando...');
	
	ids = id.split('-');
	
	var i=1;
	var nick_usuario="";
	
	while(i<ids.length)
	{
		nick_usuario += ids[i];
		if(i!=ids.length-1)
		{
			nick_usuario += '-';
		}
		i++;
	}
	//alert(nick_usuario);
	//nick_usuario = trim(nick_usuario);
	//alert(nick_usuario);
	
	url = ids[0];
	//nick_usuario = ids[1];
	if(url == 'agregar_amigo')
	{
		oldhborde = 220;
		oldhnotificacion = 200;
	}
	
	var req = new Request(
	{   
		method: 'post',   
		url: '/ajax/formulariosnotificacion/'+url+'.php',   
		data: 
		{ 
			accion:'nuevo_amigo',
			nick_usuario: nick_usuario 
		},   
		onRequest: function() 
		{ 
		},   
		onComplete: function(e) 
		{ 
			var fx = new Fx.Morph($('bordealerta'), {duration:200, wait:false});
			fx.start({
				'height': oldhborde
			});
			var fx = new Fx.Morph($('notificacionalerta'), {duration:200, wait:false});
			fx.start({
				'height': oldhnotificacion
			});
					
			$('notificacionalerta').setStyle('background','url(/imagenes/notificaion_bg.jpg) bottom right no-repeat #FFF');
			$('notificacionalerta').set('html',e);
			
			// boton para cerrar alertas //
			botoncerrar = new Element('div', {'class': 'cerrar-alerta-azul'}).inject($('notificacionalerta'), 'inside');
			//text.set('html','Imagen subida!');
			
			botoncerrar.addEvent('click', function() {
				cerrar_alertas();
			});
			// //

		}
	}).send();   
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
