// JavaScript Document

//Inicio declaraciones
var sound3Embed = null;
var ancho_pag_min = 1000; //Establece el ancho mínimo de página
var chat_nuevo
 //Fin declaraciones

function inicializa_chat(id_chat,id_usu)
	{
	var aleatorio=Math.random();
	//alert($("carga").value)
	$('mensaje').activate()
	$("mens_chat2").scrollTop = $("mens_chat2").scrollHeight;
	chat_nuevo = new Ajax.PeriodicalUpdater('mens_chat', 'chat_lee.asp?i=' + $("carga").value + ' &id_usu=' + id_usu +'&id_chat=' + id_chat +'&nocache=' + aleatorio, {frequency: 2, decay: 1,evalScripts:true,onSuccess: function chat_leido(transport)
							   		{

									aleatorio=Math.random();
									var texto_inicial = $("mens_chat2").innerHTML
									$("mens_chat2").innerHTML = texto_inicial + $("mens_chat").innerHTML
									if ($("mens_chat2").scrollTop + 250 >= $("mens_chat2").scrollHeight) {
										$("mens_chat2").scrollTop = $("mens_chat2").scrollHeight
										}
									}});
	}

function escribe_chat() //Funcion para escribir el contenido en el chat
	{
		if (envia_chat.mensaje.value != ""){
//actualizamos la variable que controla el tiempo de sesion
ahora_mismo = new Date();
var ultima_carga = (ahora_mismo.getTime()/60000);
window.opener.document.getElementById('fecha_actualizacion').value=ultima_carga;
//alert(window.opener.document.getElementById('fecha_actualizacion').value)
//enviamos el mensaje
	$("mens_chat2").innerHTML = $("mens_chat2").innerHTML + '<div ' + envia_chat.estilo_esc.value + ' class="fct"><strong>' + envia_chat.nick_usu.value + ':</strong></div><div ' + envia_chat.estilo_esc.value + ' >' + envia_chat.mensaje.value + '</div>';
	$("mens_chat2").scrollTop = $("mens_chat2").scrollHeight;
	envia_form("envia_chat");
	envia_chat.mensaje.value = "";
	envia_chat.emoticon.value = "";
		}
	}

function escribe_chat2() //Funcion para escribir el contenido en el chat
	{

//actualizamos la variable que controla el tiempo de sesion
ahora_mismo = new Date();
var ultima_carga = (ahora_mismo.getTime()/60000);
window.opener.document.getElementById('fecha_actualizacion').value=ultima_carga;
//alert(window.opener.document.getElementById('fecha_actualizacion').value)
//enviamos el mensaje
$("mens_chat2").innerHTML = $("mens_chat2").innerHTML + '<div' + envia_chat.estilo_esc.value + ' class="fct"><strong>' + envia_chat.nick_usu.value + ':' + '</strong></div><div ' + envia_chat.estilo_esc.value + ' ><img src="images/emoticonos/' + envia_chat.emoticon.value + '" border="0" align="absmiddle" hspace="4" vspace="2"/>' + envia_chat.mensaje.value + '</div>';
	$("mens_chat2").scrollTop = $("mens_chat2").scrollHeight;
	envia_form("envia_chat");
	envia_chat.emoticon.value = "";
	envia_chat.mensaje.value = "";
	}


function envia_form(formulario) //Funcion de envío de formularios
	{
	var accion = document.getElementById(formulario).action;

	new Ajax.Request(accion, {method:'post', 
					 		  postBody: Form.serialize(formulario),
								onSuccess: function(transport) 
									{
									//alert(transport.responseText);
									},
								onFailure: function(failure)
									{
									alert(failure.responseText);
									}
								});
	}
	

function sound3Play() {
	sound3Stop();
	sound3Embed = document.createElement("embed");
	sound3Embed.setAttribute("src", "sonidos/chat-inbound.wav");
	sound3Embed.setAttribute("hidden", true);
	sound3Embed.setAttribute("autostart", true);
	document.body.appendChild(sound3Embed);
}

function sound3Stop() {
	if ( sound3Embed ) {
		document.body.removeChild(sound3Embed);
		sound3Embed = null;
	}
}

function sound3Play2() {
sound3Stop2();
	sound3Embed = document.createElement("embed");
	sound3Embed.setAttribute("src", "sonidos/dirty_drumloop_01.mp3");
	sound3Embed.setAttribute("hidden", true);
	sound3Embed.setAttribute("autostart", true);
	document.body.appendChild(sound3Embed);
}
function sound3Stop2() {
	if ( sound3Embed ) {
		document.body.removeChild(sound3Embed);
		sound3Embed = null;
	}
}

function sound3Play3() {
sound3Stop3();
	sound3Embed = document.createElement("embed");
	sound3Embed.setAttribute("src", "sonidos/power_down.mp3");
	sound3Embed.setAttribute("hidden", true);
	sound3Embed.setAttribute("autostart", true);
	document.body.appendChild(sound3Embed);
}

function sound3Stop3() {
	if ( sound3Embed ) {
		document.body.removeChild(sound3Embed);
		sound3Embed = null;
	}
}

function sound3Play4() {
sound3Stop4();
	sound3Embed = document.createElement("embed");
	sound3Embed.setAttribute("src", "sonidos/sonar_acuatico.mp3");
	sound3Embed.setAttribute("hidden", true);
	sound3Embed.setAttribute("autostart", true);
	document.body.appendChild(sound3Embed);
}

function sound3Stop4() {
	if ( sound3Embed ) {
		document.body.removeChild(sound3Embed);
		sound3Embed = null;
	}
}


function calcula_posicion() //Función para colocar la tapa en función del tamaño de la ventana
	{
	var arrayPageSize = getPageSize();
	if(arrayPageSize[0]>=2)
		{
		$('tapa_idioma').style.left = ((arrayPageSize[0]/2)-433)+'px';
		}
	else
		{
		$('tapa_idioma').style.left = '0px';
		}
	}


function calcula_posicion3() //Función para colocar la tapa en función del tamaño de la ventana
	{
	var arrayPageSize = getPageSize();
	if(arrayPageSize[0]>=2)
		{
		
		$('tapa_idioma').style.left = ((arrayPageSize[0]/2)-433)+'px';
		$('tapa_alta_int').style.left = ((arrayPageSize[0]/2)-433)+'px';
		$('tapa_alta_int').style.top = ((arrayPageSize[1]/2)-30)+'px';
		}
	else
		{
		$('tapa_idioma').style.left = '0px';
		}
	}

function calcula_posicion2(alto,tipo) //Función para colocar la tapa en función del tamaño de la ventana
	{
	var arrayPageSize2 = getPageSize();
	if(arrayPageSize[0]>=2)
		{
			if (tipo == 'chat')
			{
		$('ventana_alerta').style.left = ((arrayPageSize2[0]-206)/2)+'px';
		$('ventana_alerta').style.top = (((arrayPageSize2[1]-parseInt(alto))/2))+'px';
		$('ventana_alerta2').style.left = (arrayPageSize2[0]-206)+'px';
		$('ventana_alerta2').style.top = (0)+'px';
			}
			else
			{
		$('ventana_alerta').style.left = (arrayPageSize2[0]-206)+'px';
		$('ventana_alerta').style.top = (arrayPageSize2[1]-parseInt(alto))+'px';
		$('ventana_alerta2').style.left = (arrayPageSize2[0]-206)+'px';
		$('ventana_alerta2').style.top = (0)+'px';
			}
		}
	else
		{
		$('ventana_alerta').style.left = '0px';
		}
	}

function calcula_posicion_div(div,alto,tipo) //Función para colocar la tapa en función del tamaño de la ventana
	{
	var arrayPageSize2 = getPageSize();
	if(arrayPageSize[0]>=2)
		{
			if (tipo == 'chat')
			{
		$(div).style.left = ((arrayPageSize2[0]-206)/2)+'px';
		$(div).style.top = ((0))+'px';
			}
			else
			{
		$(div).style.left = (arrayPageSize2[0]-206)+'px';
		$(div).style.top = (0)+'px';
			}
		}
	else
		{
		$('ventana_alerta').style.left = '0px';
		}
	}


function getPageSize() //Función de calculo del tamaño de la ventana
	{
	var xScroll, yScroll, windowWidth, windowHeight;
	
	if (window.innerHeight && window.scrollMaxY) 
		{	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
		}
	else if (document.body.scrollHeight > document.body.offsetHeight)
			{ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
			} 
		else 
			{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
			}
	if (self.innerHeight) 
		{	// all except Explorer
		if(document.documentElement.clientWidth)
			{
			windowWidth = document.documentElement.clientWidth; 
			} 
		else 
			{
			windowWidth = self.innerWidth;
			}
		windowHeight = self.innerHeight;
		} 
	else if (document.documentElement && document.documentElement.clientHeight) 
			{ // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
			} 
		else if (document.body) 
				{ // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
				}	
	if(yScroll < windowHeight)
		{
		pageHeight = windowHeight;
		} 
	else 
		{
		pageHeight = yScroll;
		}
	if(xScroll < windowWidth){	
		pageWidth = xScroll;} 
	else 
		{
		pageWidth = windowWidth;
		}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
	}

