// JavaScript Document
window.addEvent('domready', function() {
	// cerramos alertas //
	$('fondoalerta').addEvent('click', function() {
		cerrar_alertas();
		
	});
	// --------------- //
});

function cerrar_alertas()
{
	$('fondoalerta').setStyle("display","none");
	$('bordealerta').setStyle("background", "#000");
	$('bordealerta').setStyle("display","none");
	
	$('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 notificacionflash(nick_usuario, tipo)
{
	if($('flashcontent'))
	{
		$('flashcontent').setStyle("visibility","hidden");
		
		if(tipo=='proximamente_flash')
		{
			$('notificacionalerta').setStyle("width",300);
			$('notificacionalerta').setStyle("height",200);
		}
		else if(tipo=='proximamente_movil_flash')
		{
			$('notificacionalerta').setStyle("width",300);
			$('notificacionalerta').setStyle("height",200);
			pageTracker._trackPageview("/enviar-a-movil");
		}
		else if(tipo=='agregar_amigo_flash')
		{
			$('notificacionalerta').setStyle("width",300);
			$('notificacionalerta').setStyle("height",200);
		}
		else
		{
			$('notificacionalerta').setStyle("width",380);
			$('notificacionalerta').setStyle("height",380);
		}
		
			//alerta//
			var h = window.getScrollHeight();
			$('fondoalerta').setStyle("height",h);
			$('fondoalerta').setStyle("display","block");
			$('bordealerta').setStyle("background", "#5586a5");
				//notificacionalerta//
				
			$('bordealerta').setStyle("height", $('notificacionalerta').getStyle('height').toInt()+20);
			$('bordealerta').setStyle("width", $('notificacionalerta').getStyle('width').toInt()+20);
			
			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);
			mostrarNotificacionFlash(nick_usuario, tipo, oldhborde, oldhnotificacion);
		
	}
}

function mostrarNotificacionFlash(id, tipo, oldhborde, oldhnotificacion)
{
	
	$('notificacionalerta').set('html', 'Cargando...');
	
	url=tipo;
	nick_usuario = id;
	
	var req = new Request(
	{   
		method: 'post',   
		url: '/ajaxflash/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();
			});
			// //
			
			// enviar mensaje flash //
			if($('boton_enviar_mensaje'))
			{
				$('boton_enviar_mensaje').addEvent('click', function(e) {
					new Event(e).stop();
					$('enviar_mensaje').setStyle("display","none");
					enviarMensaje(nick_usuario);
				});
			}
			// agregar amigo flahs //
			if($('boton_agregar_amigo'))
			{
				$('boton_agregar_amigo').addEvent('click', function(e) {
					new Event(e).stop();
					if($('agregar_amigo'))
					{
						tipo = 'agregar';
						$('agregar_amigo').setStyle("display","none");
					}
					if($('confirmar_amigo'))
					{
						tipo = 'confirmar';
						$('confirmar_amigo').setStyle("display","none");
					}
					agregarAmigo(nick_usuario, tipo);
				});
			}
			// denunciar foto //
			if($('boton_denuncia_foto'))
			{
				$('boton_denuncia_foto').addEvent('click', function(e) {
					new Event(e).stop();
					$('denuncia_foto').setStyle("display","none");
					denunciarFoto(nick_usuario);
				});
			}
			
			// voto foto //
			if($('boton_voto_foto'))
			{
				$('boton_voto_foto').addEvent('click', function(e) {
					new Event(e).stop();
					$$('.puntuacion_voto').each(function(el) { 	
							if(el.checked)
							{
								puntos = el.value;
							}
					});
					$('voto_foto').setStyle("display","none");
					votarFoto(nick_usuario, puntos);
				});
			}
			
		}
	}).send();   
}

function enviarMensaje(nick_usuario)
{
	var req = new Request(
	{   
		method: 'post',   
		url: '/ajaxflash/mensaje.php',   
		data: 
		{ 
			accion:'enviar_mensaje',
			asunto_mensaje:$('asunto_mensaje').value,
			mensaje:$('mensaje').value,
			nick_usuario: nick_usuario 
		},   
		onRequest: function() 
		{ 
		},   
		onComplete: function(e) 
		{ 
			$('texto_accion').set('html', e);
		}
	}).send();  
}

function denunciarFoto(nick_usuario)
{
	var req = new Request(
	{   
		method: 'post',   
		url: '/ajaxflash/denuncia_foto.php',   
		data: 
		{ 
			accion:'denuncia_foto',
			motivo_denuncia:$('motivo_denuncia').value,
			id_foto: nick_usuario 
		},   
		onRequest: function() 
		{ 
		},   
		onComplete: function(e) 
		{ 
			$('texto_accion').set('html', e);
		}
	}).send();  
}

function votarFoto(nick_usuario, puntos)
{
	var req = new Request(
	{   
		method: 'post',   
		url: '/ajaxflash/voto.php',   
		data: 
		{ 
			accion:'voto',
			puntuacion_voto: puntos,
			id_foto: nick_usuario 
		},   
		onRequest: function() 
		{ 
		},   
		onComplete: function(e) 
		{ 
			$('texto_accion').set('html', e);
		}
	}).send();  
}

function agregarAmigo(nick_usuario, tipo)
{
	var req = new Request(
	{   
		method: 'post',   
		url: '/ajaxflash/amigo.php',   
		data: 
		{ 
			accion:tipo,
			nick_usuario: nick_usuario 
		},   
		onRequest: function() 
		{ 
		},   
		onComplete: function(e) 
		{ 
			$('texto_accion').set('html', e);
		}
	}).send();  
}
