// *********************************
// Francisco Javier Martínez
// francisco_javier_martinez@hotmail.com
// www.javiscript.com
// *********************************

// Preparamos la capa COVER ALL con opacidad
jQuery("#coverAll").height(jQuery(document).height());


function showFormSendStatus () {
	jQuery("#coverAll").css({ opacity: 0.65 });
	jQuery("#coverAll").fadeIn("slow");
	
	jQuery(".send_header").fadeIn("slow");
}

if (document.location.href.indexOf("galeria") != -1) {
//	jQuery("#coverAll").css({ opacity: 0.65 });
//	jQuery("#coverAll").fadeIn("slow");
	
	// Preparamos la capa de PRECARGA de imágenes
//	jQuery(".message_container").height(jQuery(document).height());
}



/* Setting the default values for FORM */
jQuery(document).ready(function() {
	//Assign default value to form field #1
	jQuery("#Asunto").DefaultValue("Asunto");
	jQuery("#Nombre").DefaultValue("Nombre");
	jQuery("#Correo_electronico").DefaultValue("Correo electrónico");
	jQuery("#Telefono").DefaultValue("Teléfono");
	jQuery("#Comentario").DefaultValue("Comentario");
});


// Cerar ventanas emergentes
jQuery(document).ready(function() {
	jQuery(".message_close").click (function () {
		jQuery(".message_container").fadeOut("slow");
		jQuery("#coverAll").fadeOut("slow");
	});
});


jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ((jQuery(window).height() - this.outerHeight()) / 2) + jQuery(window).scrollTop() + "px");
    this.css("left", ((jQuery(window).width() - this.outerWidth()) / 2) + jQuery(window).scrollLeft() + "px");
    return this;
}

// Preloader Gallery images
var preload_images_array = new Array();
var image_preload_string = "";
jQuery(document).ready(function() {
	jQuery(document).find(".gallery_item").each (function () {
		// Get the elementy Id
		var element_id = jQuery(this).attr("id");
		element_id = element_id.split("_")[1];
		for (contImages=0;contImages<3;contImages++) {
			preload_images_array[preload_images_array.length] = "images/galeria/" + element_id + "_" + contImages + "_big.jpg";
		}
	});
	
	for (cont=0;cont<preload_images_array.length;cont++) {
		image_preload_string+= "'" + preload_images_array[cont] + "'";
		if (cont < (preload_images_array.length - 1)) {
			image_preload_string+= ",";
		}
	}
	eval("precarga(" + image_preload_string + ")");
});


function changeThumb (thumbURL) {
	jQuery("#image_big").attr("src", thumbURL);
}

jQuery(document).ready(function () {
	// Si existe la capa GALERIA, es que estamos en la sección de galería y asignamos el evento de CLIC a las fotospara amplairlas.
	if (jQuery(".galeria").length){
	
		jQuery("#content ul li a").click (function () {
			
			// Obtenemos TITULO de este elemento
			var element_title = jQuery(this).find("span").get(0);
			element_title = jQuery(element_title).html();
			var element_description = jQuery(this).parent().find("div").get(0);
			element_description = jQuery(element_description).html();
			
			// Ponemos en la capa de ampliación el título y el texto obtenidos
			jQuery("#maximize_title").html(element_title);
			jQuery("#maximize_description").html(element_description);
			
			// Eliminamos los saltos de línea que tenemos puestos en los títulos
			var elementBR = jQuery("#maximize_title").find("br").get(0);
			jQuery(elementBR).remove();
			
			// Ponemos las imágenes de este elemento
			var elementId = jQuery(this).parent().attr("id");
			elementId = elementId.split("_")[1];
			jQuery("#image_big").attr("src", "images/galeria/" + elementId + "_0_big.jpg");
			
			for (contThumbs=0;contThumbs<3;contThumbs++) {
				jQuery("#image_thumb_" + contThumbs).attr("src", "images/galeria/" + elementId + "_" + contThumbs + "_thumb.jpg");

				jQuery("#image_thumb_" + contThumbs).parent().attr("href", "javascript:changeThumb('images/galeria/" + elementId + "_" + contThumbs + "_big.jpg')");
			}
			
			// Centramos previamente la capa que maximiza la información
			jQuery("#gallery_maximize").center();

			// Lanzamos capa de ampliacion de galeria			
			jQuery("#coverAll").css({ opacity: 0.65 });
			jQuery("#coverAll").fadeIn("slow");
			jQuery("#gallery_maximize").fadeIn("slow");
			
		});
		
		jQuery("#close").click (function () {
			jQuery("#gallery_maximize").fadeOut("slow");
			jQuery("#coverAll").fadeOut("slow");
		});
	}
});

/* Hack para que los enlaces con la clase EXTERNAL carguen en ventana nueva */
jQuery(document).ready(function () {
	jQuery("a.external").click(function(){
		this.target = "_blank";
	});
});

// Hack para reemplazar la dirección de correo escrita (cuenta [AT] dominio [dot] ext) por la dirección real
jQuery.fn.mailto = function() {
	return this.each(function(){
		if (jQuery(this).attr("href").indexOf("(at)") != -1)
		{
			var email = jQuery(this).attr("href").replace("(at)", "@");
			email = email.replace("(dot)", ".");
			var email = email.replace("mailto:", ""); // Delete mailto: if exists
			var text = jQuery(this).html().replace("(at)", "@"); // Replace (at) on text if exists
			text = text.replace("(dot)", "."); // Replace (at) on text if exists
			var classNames = jQuery(this).attr("class");
			jQuery(this).before('<a href="mailto:' + email + '" rel="nofollow" class="' + classNames + '" title="Email ' + email + '">' + text + '</a>').remove();
		}
	});
};

jQuery(document).ready(function () {
	jQuery(".email").mailto();
});


var total = 0;
var actual = 0;
var ims = new Array();
function precarga() {
	var imagenes = precarga.arguments
	total = imagenes.length
	for (i = 0; i < total; i ++) {
		ims[i] = new Image();
		ims[i].src = imagenes[i];
		ims[i].onload = function() {
			actual++; 
			// Si se han precargado todas.... CERRRAMOS VENTANITA
			if (total == actual) {
//				jQuery(".message_container").fadeOut("slow");
//				jQuery("#coverAll").fadeOut("slow");
			}
		}
	}
}
