/**
 * JS
 * (c) 2007 Darío G. Ruellan / D²
 */

 // ** Variables Globales **
var email_filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var flistmove = 0;
var foottime;
var flistwidth;

$(function() {
	beforeShow();
});
window.onload( showAll() );

// ** Funciones para cuando la pagina está oculta **

function beforeShow(){
	
	// Preparamos algunos JS
	$('#busquedarapida').change(function(){
		br_submit(this);
	});
	
	if ( $('div').is('#contactorapido') ) {
		$('#contactorapido form').submit(function(){
			cr_submit();
			return false;
		});
		$('#contactorapido #cre,#contactorapido #crn,#contactorapido #crt').focus(function(){
			text = $(this).val();
			if ( text == "Ingrese su nombre" || text == "Ingrese un email" || text == "¿Qué mensaje desea dejarnos?" ) {
				$(this).val("");			
			}	
		});
	}
	
	// Imagen de Cabecera al Azar
	$('#cab').css("background-image","url(images/main_photo"+Math.floor( Math.random ( ) * 4 + 1 )+".jpg)");
	
	// Sistema de menu
    $("ul.menu li").hover(function(){
		$("ul", this).slideDown("fast");
	}, function() {
		$("ul", this).slideUp("fast");
	});
}

// ** Funciones para cuando la pagina es mostrada **

function afterShow(){
	
	// Si hay una llamada a un ancla, la activamos
	fields = document.location.href.split('#');
	if ( fields[1] ) {
		window.location.hash = fields[1];
	}
	
	// Prepara galería de imágenes (si existe)
	if ( $('div').is('#photogallery_right') ) {
		$("#photogallery_right").jCarouselLite({
			btnNext: ".next",
			btnPrev: ".prev",
			visible: 1
		});
	}
	
	// Comienza animación foot (si existe)
	if ( $('div').is('#footlist') ) {
		flistwidth = $('#footlist_text').width();
		footanim();
		$('#footlist_text').mouseover(function(){
			clearTimeout(foottime);
		});
		$('#footlist_text').mouseout(function(){
			footanim();
		});
	}
}

// ** Muestra la Página **

function showAll() {
	
	$(function(){
		$('#loading').remove();
		$('#mainframe').show().slideDown('slow',function(){
			afterShow();
		});
	});
	return false;
}

// ** Footlist animation **

function footanim () {
	flistmove = flistmove - 2;
	flisttext = $('#footlist_text').html();
	flistwidth = flisttext.length * 1.5; 
	$('#footlist_text').css('left', flistmove);
	if ( flistmove + flistwidth < 0 ) {
		flistmove = 760;
	}
	foottime = setTimeout(function(){
		footanim();
	},50);
}

// ** Submit busqueda rápida **

function br_submit(id) {
	value = $(id).val();
	if ( value !== "" ) {
		$('.bluebox img.loading').css("display", "inline");	
		document.location.href="file.php?isla="+value;
	}
}

// ** Submit contacto rapido **

function cr_submit() {
	email = $("#contactorapido #cre").val();
	name = $("#contactorapido #crn").val();
	text = $("#contactorapido #crt").val();
	email_filter=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if ( email_filter.test(email) ) {
		$("#contactorapido_submit").html('<img src="images/icn_loading_small.gif" alt="loading" /> Enviando...');
		$.post(
			"lib/contacto.ajax.php",
			{ cre: email, crn: name, crt: text },
			function(reply){ swap('#contactorapido form',reply); }
		);
	} else {
		swap("#contactorapido_submit","¡Ingrese una dirección de email!");
		setTimeout(function(){
			swap("#contactorapido_submit",'<input type="submit" value="enviar" class="submit" />');
		},2000);
	}
}

// ** Efectos **

function swap(id,text) {
	$(id).slideUp(function(){
		$(this).html(text);
		$(this).slideDown();
	});
}