// JavaScript Document
var timeout_mensaje_sistema;

// Necesario para cargar FB de forma valida
(function($)
{
	$.fn.fakeXFBML = function( fb_tag , params )
	{
		var container = $(this);
		params = params || {};
		fb_tag = $("<fb:" + fb_tag + "></fb:" + fb_tag + ">");
		$.each(params, function( key , value )
		{
			fb_tag.attr( key , value );
		});
		fb_tag.appendTo(container);
	};
})(jQuery);

$(document).ready(function ()
{
	redimensionar();
	window.onresize = function () { redimensionar(); };
	
	$('.slideshow').cycle(
	{
		fx:'scrollHorz',
		easing:'easeOutBounce',
		timeout:'8000'
	});
	Cufon.replace('h2', { hover: 'true' });
	Cufon.replace('h3', { hover: 'true' });
	Cufon.replace('h4', { hover: 'true' });
	Cufon.replace('.cufon', { hover: 'true' });
	Cufon.now();
});

function cerrar_ventana_temporal()
{
	document.body.removeChild(document.getElementById('div_ventana_temporal'));
	document.body.removeChild(document.getElementById('div_cortina_temporal'));
	document.body.removeChild(document.getElementById('div_cerrar_temporal'));
}

function mostrar_formulario_contacto(root)
{
	o3_cortina();

	var div_cerrar_cortina = document.createElement('div');
	div_cerrar_cortina.id = 'div_cerrar_temporal';
	div_cerrar_cortina.style.backgroundImage = 'url(' + root + 'imgs/sprites.png)';
	div_cerrar_cortina.style.backgroundPosition = '0px -133px';
	div_cerrar_cortina.style.width = '40px';
	div_cerrar_cortina.style.height = '40px';
	div_cerrar_cortina.style.position = 'fixed';
	div_cerrar_cortina.style.right = '0px';
	div_cerrar_cortina.style.top = '0px';
	div_cerrar_cortina.style.zIndex = '99';
	div_cerrar_cortina.style.cursor = 'pointer';
	div_cerrar_cortina.onclick = Function('cerrar_ventana_temporal();');
	document.body.appendChild(div_cerrar_cortina);

	div_ventana = document.createElement('div');
	div_ventana.id = 'div_ventana_temporal';
	div_ventana.style.position = 'fixed';
	div_ventana.style.zIndex = 91;
	var contenido_div;
	contenido_div  = '	<table id="table_ventana_temporal" class="formulario_contacto" cellpadding="0" cellspacing="0">';
	contenido_div += '		<tr>';
	contenido_div += '			<td style="width:13px; height:13px; background:url(' + root + 'imgs/sprites.png); background-position:0px 0px;">';
	contenido_div += '			</td>';
	contenido_div += '			<td style="background:url(' + root + 'imgs/sprites_h.png); background-position:0px 0px;">';
	contenido_div += '			</td>';
	contenido_div += '			<td style="background:url(' + root + 'imgs/sprites.png); background-position:-13px 0px;">';
	contenido_div += '			</td>';
	contenido_div += '		</tr>';
	contenido_div += '		<tr>';
	contenido_div += '			<td style="background:url(' + root + 'imgs/sprites_v.png); background-position:0px 0px;">';
	contenido_div += '			</td>';
	contenido_div += '			<td style="background:#FFF; padding:0px 10px;">';
	contenido_div += '				<h3 style="text-align:center; margin:10px 0 5px 0;">Formulario de contacto</h3>';
	contenido_div += '				Nombre';
	contenido_div += '				<br />';
	contenido_div += '				<div class="error" id="div_error_nombre_temporal">';
	contenido_div += '					Debe ingresar su nombre';
	contenido_div += '				</div>';
	contenido_div += '				<input type="text" id="text_nombre_temporal" onkeypress="javascript:document.getElementById(\'div_error_nombre_temporal\').style.display = \'none\';" />';
	contenido_div += '				<br />';
	contenido_div += '				<span style="color:#666;">Tel&eacute;fono</span>';
	contenido_div += '				<br />';
	contenido_div += '				<div class="error" id="div_error_telefono_temporal">';
	contenido_div += '					Debe ingresar un n&uacute;mero telef&oacute;nico';
	contenido_div += '				</div>';
	contenido_div += '				<input type="text" id="text_telefono_temporal" onkeypress="javascript:document.getElementById(\'div_error_telefono_temporal\').style.display = \'none\';" />';
	contenido_div += '				<br />';
	contenido_div += '				E-mail';
	contenido_div += '				<br />';
	contenido_div += '				<div class="error" id="div_error_email_temporal">';
	contenido_div += '					Debe ingresar su e-mail';
	contenido_div += '				</div>';
	contenido_div += '				<input type="text" id="text_email_temporal" onkeypress="javascript:document.getElementById(\'div_error_email_temporal\').style.display = \'none\';" />';
	contenido_div += '				<br />';
	contenido_div += '				Mensaje';
	contenido_div += '				<br />';
	contenido_div += '				<div class="error" id="div_error_consulta_temporal">';
	contenido_div += '					Debe escribir un mensaje';
	contenido_div += '				</div>';
	contenido_div += '				<textarea id="textarea_consulta_temporal" onkeypress="javascript:document.getElementById(\'div_error_consulta_temporal\').style.display = \'none\';"></textarea>';
	contenido_div += '				<div id="div_captcha" style="min-height:100px; margin-top:10px;">';
	contenido_div += '					<div id="recaptcha_image"></div>';
	contenido_div += '					<table cellpadding="0" cellspacing="0">';
	contenido_div += '						<tr>';
	contenido_div += '							<td>';
	contenido_div += '								<div id="div_error_captcha" class="error">El texto ingresado no es correcto</div>';
	contenido_div += '								<span class="recaptcha_only_if_image">Ingrese el texto mostrado arriba</span>';
	contenido_div += '								<span class="recaptcha_only_if_audio">Ingrese los números que escucha</span>';
	contenido_div += '								<br />';
	contenido_div += '								<input type="text" id="recaptcha_response_field" name="recaptcha_response_field" class="textbox" style="width:200px;" onkeydown="javascript:document.getElementById(\'div_error_captcha\').style.display = \'none\';" />';
	contenido_div += '							</td>';
	contenido_div += '							<td style="padding-left:10px;">';
	contenido_div += '								<div><a href="javascript:Recaptcha.reload()"><img src="' + root + 'imgs/recargar.png" alt="Cargar un nuevo CAPTCHA" title="Cargar un nuevo CAPTCHA" width="16" height="16" style="border:none;" /></a></div>';
	contenido_div += '								<div class="recaptcha_only_if_image"><a href="javascript:Recaptcha.switch_type(\'audio\')"><img src="' + root + 'imgs/audio.png" alt="Usar audio" title="Usar audio" width="16" height="16" style="border:none;" /></a></div>';
	contenido_div += '								<div class="recaptcha_only_if_audio"><a href="javascript:Recaptcha.switch_type(\'image\')"><img src="' + root + 'imgs/texto.png" alt="Usar imagen" title="Usar imagen" width="16" height="16" style="border:none;" /></a></div>';
	contenido_div += '								<div><a href="javascript:Recaptcha.showhelp()"><img src="' + root + 'imgs/ayuda.png" alt="Ayuda" title="Ayuda" width="16" height="16" style="border:none;" /></a></div>';
	contenido_div += '							</td>';
	contenido_div += '						</tr>';
	contenido_div += '					</table>';
	contenido_div += '				</div>';
	contenido_div += '				<div style="margin-top:10px;"></div>';
	contenido_div += '				<div id="div_enviar_temporal" style="text-align:right; margin-bottom:5px;">';
	contenido_div += '					<a id="a_enviar" onclick="javascript:enviar_mensaje(\'' + root + '\');" style="font-size:18px;">ENVIAR</a>';
	contenido_div += '				</div>';
	contenido_div += '				<div id="div_enviando_temporal" style="text-align:right; display:none; font-size:10px; margin-bottom:5px;">';
	contenido_div += '					<img src="' + root + 'imgs/cargando_c.gif" alt="Cargando..." title="" style="margin-right:15px;" />Enviando mensaje...';
	contenido_div += '				</div>';
	contenido_div += '			</td>';
	contenido_div += '			<td style="background:url(' + root + 'imgs/sprites_v.png); background-position:-13px 0px;">';
	contenido_div += '			</td>';
	contenido_div += '		</tr>';
	contenido_div += '		<tr>';
	contenido_div += '			<td style="background:url(' + root + 'imgs/sprites.png); background-position:-26px 0px;">';
	contenido_div += '			</td>';
	contenido_div += '			<td style="background:url(' + root + 'imgs/sprites_h.png); background-position:0px -13px;">';
	contenido_div += '			</td>';
	contenido_div += '			<td style="width:13px; height:13px; background:url(' + root + 'imgs/sprites.png); background-position:-39px 0px;">';
	contenido_div += '			</td>';
	contenido_div += '		</tr>';
	contenido_div += '	</table>';
	div_ventana.innerHTML = contenido_div;
	document.body.appendChild(div_ventana);
	Cufon.replace('h3', { hover: 'true' });
	Cufon.replace('#a_enviar', { hover: 'true' });
	Cufon.now();
	Recaptcha.create("6Lerz8MSAAAAAJONnJjBoJ-C4Wf31apwYJNIx7Hv ", "div_captcha",
	{
		theme: "custom",
		custom_translations:
		{
			play_again: "Reproducir nuevamente",
			cant_hear_this: "Descargar en formato MP3"
		},
		lang: 'es'
	});
	o3_centrar('div_ventana_temporal');
	document.getElementById('text_nombre_temporal').focus();
}

function enviar_mensaje(root)
{
	var nombre = o3_urlencode(document.getElementById('text_nombre_temporal').value);
	var telefono = o3_urlencode(document.getElementById('text_telefono_temporal').value);
	var email = document.getElementById('text_email_temporal').value;
	var consulta = o3_urlencode(document.getElementById('textarea_consulta_temporal').value);
	var recaptcha_challenge_field = Recaptcha.get_challenge();
	var recaptcha_response_field = Recaptcha.get_response();
	var error = false;

	if (recaptcha_response_field == '')
	{
		error = true;
		document.getElementById('div_error_captcha').style.display = 'block';
		document.getElementById('recaptcha_response_field').focus();
	}
	if (consulta == '')
	{
		error = true;
		document.getElementById('div_error_consulta_temporal').style.display = 'block';
		document.getElementById('textarea_consulta_temporal').focus();
	}
	if (email == '')
	{
		error = true;
		document.getElementById('div_error_email_temporal').innerHTML = 'Debe ingresar su e-mail';
		document.getElementById('div_error_email_temporal').style.display = 'block';
		document.getElementById('text_email_temporal').focus();
	}
	else if (!o3_email(email))
	{
		error = true;
		document.getElementById('div_error_email_temporal').innerHTML = 'El e-mail ingresado no es v&aacute;lido';
		document.getElementById('div_error_email_temporal').style.display = 'block';
		document.getElementById('text_email_temporal').focus();
	}
	if (nombre == '')
	{
		error = true;
		document.getElementById('div_error_nombre_temporal').style.display = 'block';
		document.getElementById('text_nombre_temporal').focus();
	}
	if (!error)
	{
		document.getElementById('div_enviar_temporal').style.display = 'none';
		document.getElementById('div_enviando_temporal').style.display = 'block';
		email = o3_urlencode(email);
		o3_ajax(root + 'ajax_contacto.php?nombre=' + nombre + '&telefono=' + telefono + '&email=' + email + '&consulta=' + consulta + '&recaptcha_challenge_field=' + recaptcha_challenge_field + '&recaptcha_response_field=' + recaptcha_response_field + '&root=' + root);
	}
}

function mensaje_sistema(texto, root)
{
	clearTimeout(timeout_mensaje_sistema);
	var div_mensaje_sistema = document.createElement('div');
	div_mensaje_sistema.id = 'div_mensaje_sistema';
	div_mensaje_sistema.innerHTML = '<table cellpadding="0" cellspacing="0"><tr><td style="width:15px; height:30px; font-size:1px; background-image:url(' + root + 'imgs/sprites.png); background-position:0px -173px;">&nbsp;</td><td style="vertical-align:middle; background-image:url(imgs/fondo_control.png);">' + texto + '</td><td style="width:15px; font-size:1px; background-image:url(imgs/sprites.png); background-position:-15px -173px;">&nbsp;</td></tr></table>';
	div_mensaje_sistema.style.position = 'fixed';
	div_mensaje_sistema.style.height = '30px';
	div_mensaje_sistema.style.overflow = 'hidden';
	div_mensaje_sistema.style.color = '#333';
	div_mensaje_sistema.style.zIndex = '1000';
	document.body.appendChild(div_mensaje_sistema);
	o3_centrar('div_mensaje_sistema');
	document.getElementById('div_mensaje_sistema').style.top = '10px';
	o3_opacity('div_mensaje_sistema', 90);
	timeout_mensaje_sistema = setTimeout("document.body.removeChild(document.getElementById('div_mensaje_sistema'));", 5000);
}

function redimensionar()
{
	if (document.getElementById('div_ventana_temporal'))
	{
		o3_centrar('div_ventana_temporal');
		var ancho_ventana = document.documentElement.clientWidth;
		var alto_ventana = document.documentElement.clientHeight;
		document.getElementById('div_cortina_temporal').style.width = ancho_ventana + 'px';
		document.getElementById('div_cortina_temporal').style.height = alto_ventana + 'px';
	}
	o3_centrar('div_redes_sociales');
	document.getElementById('div_redes_sociales').style.left = '';
	document.getElementById('div_redes_sociales').style.right = '0px';
}
