// JavaScript Document

// Pausa la reproducción si el cursor se encuentra sobre uno de los trabajos y vuelve a reproducir si el cursor se quita
function pausar_trabajo(evento, ruta)
{
	if (reproducir == true)
	{
		reproducir = false;
		clearTimeout(accion_1);
		clearTimeout(accion_2);
		clearTimeout(accion_3);
		clearTimeout(accion_4);
		clearTimeout(accion_5);
		clearTimeout(accion_6);
		clearTimeout(accion_7);
		document.getElementById('img_reproducir').src = ruta + 'imgs/play_inactivo.png';
	}
	else
	{
		reproducir = true;
		document.getElementById('img_reproducir').src = ruta + 'imgs/pause_inactivo.png';
		if (trabajo_actual == 3) var nuevo_id = 1; else	var nuevo_id = parseInt(trabajo_actual) + 1;
		cambiar_trabajo(nuevo_id, 5000, ruta);
	}
}

// Realiza las acciones del botón de reproductor de trabajos recientes.
function boton_reproductor(accion, ruta)
{
	if (accion == 'mouseover')
	{
		if (reproducir == true)
		{
			document.getElementById('img_reproducir').src = ruta + 'imgs/pause.png';
		}
		else
		{
			document.getElementById('img_reproducir').src = ruta + 'imgs/play.png';
		}
	}
	
	if (accion == 'mouseout')
	{
		if (reproducir == true)
		{
			document.getElementById('img_reproducir').src = ruta + 'imgs/pause_inactivo.png';
		}
		else
		{
			document.getElementById('img_reproducir').src = ruta + 'imgs/play_inactivo.png';
		}
	}
	
	if (accion == 'click')
	{
		if (reproducir == true)
		{
			reproducir = false;
			clearTimeout(accion_1);
			clearTimeout(accion_2);
			clearTimeout(accion_3);
			clearTimeout(accion_4);
			clearTimeout(accion_5);
			clearTimeout(accion_6);
			clearTimeout(accion_7);
			document.getElementById('img_reproducir').src = ruta + 'imgs/play.png';
		}
		else
		{
			reproducir = true;
			document.getElementById('img_reproducir').src = ruta + 'imgs/pause.png';
			if (trabajo_actual == 3) var nuevo_id = 1; else	var nuevo_id = parseInt(trabajo_actual) + 1;
			cambiar_trabajo(nuevo_id, 5000, ruta);
		}
	}
}

// Realiza las acciones de los botones de trabajos recientes.
function botones_trabajos(id, accion, ruta)
{
	if (accion == 'mouseover')
	{
		if (id != trabajo_actual)
		{
			document.getElementById('img_boton_' + id).src = ruta + 'imgs/boton_' + id + '.png';
		}
	}
	
	if (accion == 'mouseout')
	{
		if (id != trabajo_actual)
		{
			document.getElementById('img_boton_' + id).src = ruta + 'imgs/boton_' + id + '_inactivo.png';
		}
	}
	
	if (accion == 'click')
	{
		reproducir = false;
		clearTimeout(accion_1);
		clearTimeout(accion_2);
		clearTimeout(accion_3);
		clearTimeout(accion_4);
		clearTimeout(accion_5);
		clearTimeout(accion_6);
		clearTimeout(accion_7);
		document.getElementById('img_boton_' + trabajo_actual).src = ruta + 'imgs/boton_' + trabajo_actual + '_inactivo.png';
		document.getElementById('div_trabajo_' + trabajo_actual).style.display = 'none';
		document.getElementById('div_trabajo_' + id).style.display = 'block';
		opacidad('div_trabajo_' + id, 100);
		document.getElementById('img_reproducir').src = ruta + 'imgs/play_inactivo.png';
		document.getElementById('img_boton_' + id).src = ruta + 'imgs/boton_' + id + '_activo.png';
		trabajo_actual = id;
	}
}

// Cambia trabajo mostrado
function cambiar_trabajo(id, intervalo, ruta)
{
	if (reproducir == true)
	{
		if (id == 1) var id_anterior = 3; else var id_anterior = parseInt(id) - 1;
		if (id == 3) var nuevo_id = 1; else	var nuevo_id = parseInt(id) + 1;
		
		opacidad('div_trabajo_' + id_anterior, 100);
		opacidad('div_trabajo_' + id, 0);
		
		fade('div_trabajo_' + id_anterior, 0, 500);
		accion_1 = setTimeout("document.getElementById('div_trabajo_" + id_anterior + "').style.display = 'none';", 500);
		accion_2 = setTimeout("document.getElementById('img_boton_" + id_anterior + "').src = '" + ruta + "imgs/boton_" + id_anterior + "_inactivo.png';", 500);
		accion_3 = setTimeout("trabajo_actual = " + id + ";", 500);
		accion_4 = setTimeout("document.getElementById('img_boton_" + id + "').src = '" + ruta + "imgs/boton_" + id + "_activo.png';", 500);
		accion_5 = setTimeout("document.getElementById('div_trabajo_" + id + "').style.display = 'block';", 500);
		accion_6 = setTimeout("fade('div_trabajo_" + id + "', 100, 500);", 500);
		accion_7 = setTimeout("cambiar_trabajo(" + nuevo_id + ", " + intervalo + ", '" + ruta + "');", intervalo + 1000);
	}
}

// Oculta la cortina y muestra la página al cargarse.
function pagina_cargada(ruta)
{
	document.getElementById('div_cargando').style.display = 'none';
	document.getElementById('div_pagina_principal').style.display = 'block';
	opacidad('div_cortina', 100);
	fade('div_cortina', 0, 1000);
	setTimeout("document.getElementById('div_cortina').style.display = 'none';", 1000);
	if (ruta == '')
	{
		setTimeout("cambiar_trabajo(2, 5000, '" + ruta + "');", 5000);
	}
}

// Activa el boton al pasar el puntero del ratón sobre él.
function activa_boton(objeto, ruta)
{
	document.getElementById(objeto).style.backgroundImage = 'url(' + ruta + 'imgs/fondo_div_inactivo.png)';
	document.getElementById(objeto).style.borderColor = '#FFF';
	document.getElementById(objeto).style.color = '#036';
}

// Desactiva el boton al pasar el puntero del ratón sobre él.
function desactiva_boton(objeto, ruta)
{
	document.getElementById(objeto).style.backgroundImage = 'url(' + ruta + 'imgs/fondo_div.png)';
	document.getElementById(objeto).style.borderColor = '#036';
	document.getElementById(objeto).style.color = '#FFF';
}

// Acciona el boton al pasar el puntero del ratón sobre él.
function acciona_boton(objeto, ruta)
{
	document.getElementById(objeto).style.backgroundImage = 'url(' + ruta + 'imgs/fondo_div_activo.png)';
	document.getElementById(objeto).style.borderColor = '#FFF';
	document.getElementById(objeto).style.color = '#333';
}

// Publica un nuevo comentario.
function publica_comentario(post)
{
	var nombre = document.getElementById('text_nombre').value;
	var email = document.getElementById('text_email').value;
	var comentario = document.getElementById('textarea_comentario').value.replace(/\n/g,'<br>');

	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	
	if (filter.test(email) == false)
	{
		if (email != '')
		{
			var error_email = true;
		}
		else
		{
			var error_email_vacio = true;
		}
	}

	if (nombre == '')
	{
		alert('El campo "Nombre" es obligatorio.');
	}
	else if (error_email_vacio == true)
	{
		alert('El campo "E-mail" es obligatorio.');
	}
	else if (error_email == true)
	{
		alert('El e-mail ingresado no es v\xe1lido.');
	}
	else if (comentario == '')
	{
		alert('El campo "Comentario" es obligatorio.');
	}
	else
	{
		document.getElementById('div_boton_comentar').style.display = 'none';
		document.getElementById('div_posteando_comentario').style.display = 'block';
		document.getElementById('iframe_comentar').contentWindow.document.getElementById('hidden_nombre').value = nombre;
		document.getElementById('iframe_comentar').contentWindow.document.getElementById('hidden_email').value = email;
		document.getElementById('iframe_comentar').contentWindow.document.getElementById('hidden_comentario').value = comentario;
		document.getElementById('iframe_comentar').contentWindow.document.getElementById('hidden_post').value = post;
		document.getElementById('iframe_comentar').contentWindow.document.getElementById('form_comentar').submit();
	}
}

// Envia formulario de contacto.
function envia_mensaje()
{
	var nombre = document.getElementById('text_nombre').value;
	var email = document.getElementById('text_email').value;
	var mensaje = document.getElementById('textarea_mensaje').value.replace(/\n/g,'<br>');

	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	
	if (filter.test(email) == false)
	{
		if (email != '')
		{
			var error_email = true;
		}
		else
		{
			var error_email_vacio = true;
		}
	}

	if (nombre == '')
	{
		alert('El campo "Nombre" es obligatorio.');
	}
	else if (error_email_vacio == true)
	{
		alert('El campo "E-mail" es obligatorio.');
	}
	else if (error_email == true)
	{
		alert('El e-mail ingresado no es v\xe1lido.');
	}
	else if (mensaje == '')
	{
		alert('El campo "Mensaje" es obligatorio.');
	}
	else
	{
		document.getElementById('div_formulario_contacto').style.display = 'none';
		document.getElementById('div_enviando_mensaje').style.display = 'block';
		document.getElementById('iframe_contacto').contentWindow.document.getElementById('hidden_nombre').value = nombre;
		document.getElementById('iframe_contacto').contentWindow.document.getElementById('hidden_email').value = email;
		document.getElementById('iframe_contacto').contentWindow.document.getElementById('hidden_mensaje').value = mensaje;
		document.getElementById('iframe_contacto').contentWindow.document.getElementById('form_contacto').submit();
	}
}

// Resetea el formulario de contacto luego de enviado un mensaje y permite enviar un mensaje nuevo.
function mensaje_nuevo()
{
	document.getElementById('text_nombre').value = '';
	document.getElementById('text_email').value = '';
	document.getElementById('textarea_mensaje').value = '';
	document.getElementById('div_mensaje_enviado').style.display = 'none';
	document.getElementById('div_formulario_contacto').style.display = 'block';
	document.getElementById('iframe_contacto').src = 'iframe_contacto.php';
}

// Muestra u oculta el recuadro que resalta la imagen de la sección trabajos anteriores.
function resaltar_img_trabajos_anteriores(resaltar, id, ruta)
{
	if (resaltar == true)
	{
		document.getElementById('td_esquina_superior_izquierda_' + id).style.backgroundImage = 'url(' + ruta + 'imgs/borde_resaltador/esquina_superior_izquierda.png)';
		document.getElementById('td_esquina_superior_derecha_' + id).style.backgroundImage = 'url(' + ruta + 'imgs/borde_resaltador/esquina_superior_derecha.png)';
		document.getElementById('td_esquina_inferior_izquierda_' + id).style.backgroundImage = 'url(' + ruta + 'imgs/borde_resaltador/esquina_inferior_izquierda.png)';
		document.getElementById('td_esquina_inferior_derecha_' + id).style.backgroundImage = 'url(' + ruta + 'imgs/borde_resaltador/esquina_inferior_derecha.png)';
		document.getElementById('td_borde_superior_' + id).style.backgroundImage = 'url(' + ruta + 'imgs/borde_resaltador/borde_superior.png)';
		document.getElementById('td_borde_derecho_' + id).style.backgroundImage = 'url(' + ruta + 'imgs/borde_resaltador/borde_derecho.png)';
		document.getElementById('td_borde_inferior_' + id).style.backgroundImage = 'url(' + ruta + 'imgs/borde_resaltador/borde_inferior.png)';
		document.getElementById('td_borde_izquierdo_' + id).style.backgroundImage = 'url(' + ruta + 'imgs/borde_resaltador/borde_izquierdo.png)';
	}
	else
	{
		document.getElementById('td_esquina_superior_izquierda_' + id).style.background = 'none';
		document.getElementById('td_esquina_superior_derecha_' + id).style.background = 'none';
		document.getElementById('td_esquina_inferior_izquierda_' + id).style.background = 'none';
		document.getElementById('td_esquina_inferior_derecha_' + id).style.background = 'none';
		document.getElementById('td_borde_superior_' + id).style.background = 'none';
		document.getElementById('td_borde_derecho_' + id).style.background = 'none';
		document.getElementById('td_borde_inferior_' + id).style.background = 'none';
		document.getElementById('td_borde_izquierdo_' + id).style.background = 'none';
	}
}

// Cambia la opacidad de un objeto.
function opacidad(objeto, opacidad)
{
	document.getElementById(objeto).style.opacity = opacidad / 100;
	document.getElementById(objeto).style.filter = 'alpha(opacity = ' + opacidad + ')';
}

// Cambia gradualmente la opacidad de un objeto.
function fade(objeto, porcentaje, milisegundos)
{
	var intervalos = milisegundos / 50;
	for (i = 0; i < intervalos; i++)
	{
		var opacidad_inicial = document.getElementById(objeto).style.opacity * 100;
		var nueva_opacidad = opacidad_inicial - ((opacidad_inicial - porcentaje) / intervalos) * (i + 1);
		setTimeout("opacidad('" + objeto + "', " + nueva_opacidad + ");", i * milisegundos / intervalos);
	}
}
