
// Redimensionamos coverAll porque no coje todo el tamaño del documento en altura
jQuery(document).ready(function () {
	jQuery("#coverAll").height(jQuery(document).height());
});

/* 
	CoverAll alpha
	Se cargará la capa de COVER si hay imagen de fondo para la Web
 */
if (jQuery("img.maximage").html() != null) {
	jQuery("#coverAll").fadeTo("slow", 0.3);
}

function hideCoverAll ()
{
	// Ocultamos la capa que cubre todo y precarga
	jQuery("#coverAll").fadeOut("slow", function (){
		// Si estamos en la página inicial, habrá logotipo grande: lo mostramos con FADE
		if (jQuery("#logotype").html() != null) {
			jQuery("#logotype").fadeIn("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(/\s*\(.+\)\s*/, "@");
			var email = email.replace("mailto:", ""); // Delete mailto: if exists
			var text = jQuery(this).html().replace(/\s*\(.+\)\s*/, "@"); // 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();
});

/*
	RollOver Index
*/

jQuery(document).ready(function () {
	jQuery("#content.index ul").find("li").each(function() {
		jQuery(this).mouseover(function () {
			jQuery(this).find("a.item_title").each (function () {
				jQuery(this).css("background-position", "bottom center");
			})
		});
			
		jQuery(this).mouseout(function () {
			jQuery(this).find("a.item_title").each (function () {
				jQuery(this).css("background-position", "top center");
			})
		});
	});
});

/*
	Destello del logotipo
*/
jQuery(document).ready(function () {
	if (jQuery("#swf_destello").length > 0) { // Si existe el elemento "swf_destello" 
		jQuery('#swf_destello').flash(
			{
				// test_flashvars.swf is the flash document
				swf: 'http://www.horizontescreativos.net/administracion/azcaray/swf/logotipo_destello.swf',
				width: 75, 
				height: 61, 
				wmode: "transparent", 
				// these arguments will be passed into the flash document
				flashvars: {
					name1: 'jQuery',
					name2: 'SWFObject',
					name3: 'Plugin'
				}
			}
		);

	}
});

/* 
	RollOver menu Productos
*/

var myInterval;
var VAL = false;

jQuery(document).ready(function () {
	jQuery("#productos, #products").mouseover(function () {
		clearInterval(myInterval);
		jQuery(".submenu").show();
		jQuery("#productos a, #products a").css("background-position", "center bottom ");
	}).mouseout(function () {
		VAL = true;
		activeInterval();
	});

	jQuery(".submenu").mouseover(function () {
		clearInterval(myInterval);
		jQuery(".submenu").show();
		jQuery("#productos a, #products a").css("background-position", "bottom center");
	}).mouseout(function () {
		VAL = true;
		activeInterval();
	});
});

function activeInterval(){
	if(VAL==true){
		clearInterval(myInterval);
		myInterval = setInterval(remover, 300);
	}
}

function remover(){
	clearInterval(myInterval);
	VAL = false;
	jQuery(".submenu").hide();
	jQuery("#productos a, #products a").css("background-position", "center top ");
}



/* 
	RollOver Submenu Canal Horeca
*/
jQuery(document).ready(function () {
	var actualItem = jQuery("#image-preview").attr("className");
	if (actualItem != null && actualItem != "") {
		actualItem = actualItem.split("item")[1];
	}
	jQuery("#content.canal-horeca #submenu-container ul").find("li").each(function() {
		jQuery(this).mouseover(function () {
			// cambio de imagen
			var actualID = jQuery(this).attr("id");
			actualID = parseInt(actualID.split("item")[1]);
			var backgrounPositionTop = (actualID - 1) * -300;
			jQuery("#image-preview").stop();
			jQuery("#image-preview").css("background-position", "0px " + backgrounPositionTop + "px")
			
			// Cambio de estilos de enlaces
			jQuery(this).find("a").each(function () {
				jQuery(this).css("className", "a17_FF6735 underline");
			});
		});
		jQuery(this).mouseout(function () {
			// Cambio de imagen
			var theTop = 2000;
			if (actualItem != "") {
				theTop = (parseInt(actualItem) - 1) * -300
			}
			jQuery("#image-preview").css("background-position", "0px " + theTop + "px")
			
			// Cambio de estilos de enlaces
			jQuery(this).find("a").each(function () {
				jQuery(this).css("className", "a17_FFFFFF");
			});
		});
	});
});



/*
	Acciones Submenu Productos
*/
var actualProduct = null;
jQuery(document).ready(function () {
	// Precargamos contenido si esque hay algo en la URL
	var actualURL = document.location.href;
	if (actualURL.indexOf("#de-") !=  -1 || actualURL.indexOf("#of-") !=  -1) {
		
		actualProduct = actualURL.split("#")[1];
		jQuery("#content_" + actualProduct).fadeIn("slow");
		
		// Marcado de enlaces
		jQuery("#" + actualProduct).removeClass()
		jQuery("#" + actualProduct).addClass("a15_FF6735");
		jQuery("#" + actualProduct).addClass("selected");
//		jQuery("#" + actualProduct).css("background-position", "145px -26px")
		
		var thumClass = actualProduct.substr(actualProduct.indexOf("-") + 1, actualProduct.length);
		jQuery("#product_thumb").removeClass()
		jQuery("#product_thumb").addClass(thumClass);
		
		// Enlace de la imagen de muestra
		var thumbImage = jQuery("#product_thumb").css("background-image");
		thumbImage = thumbImage.split("\"")[1].split("\"")[0];
		thumbImage = thumbImage.replace("_thumb", "_big");
		jQuery("#product_thumb a").attr("href", thumbImage);
		// Title ligthbox
		var actualCategoryTitle = jQuery("h3 strong").text();
		var actualProductTitle = jQuery("#" + actualProduct + " strong").text();
		jQuery("#product_thumb a").attr("title", actualCategoryTitle + " - " + actualProductTitle);
	}
	

	jQuery("#submenu-productos li a").click (function () {
		var productId = jQuery(this).attr("href");
		productId = productId.split("#")[1];
		
		function mostrar () {
			actualProduct = productId;
			jQuery("#content_" + actualProduct).fadeIn("slow");
			
			// Marcado de enlaces
			jQuery("#" + actualProduct).removeClass()
			jQuery("#" + actualProduct).addClass("a15_FF6735");
			jQuery("#" + actualProduct).addClass("selected");
//			jQuery("#" + actualProduct).css("background-position", "145px -26px")
			
			// Imagen de muestra
			var thumClass = actualProduct.substr(actualProduct.indexOf("-") + 1, actualProduct.length);
			jQuery("#product_thumb").removeClass()
			jQuery("#product_thumb").addClass(thumClass);
			
			// Enlace de la imagen de muestra
			var thumbImage = jQuery("#product_thumb").css("background-image");
			thumbImage = thumbImage.split("\"")[1].split("\"")[0];
			thumbImage = thumbImage.replace("_thumb", "_big");
			jQuery("#product_thumb a").attr("href", thumbImage);
			// Title ligthbox
			var actualCategoryTitle = jQuery("h3 strong").text();
			var actualProductTitle = jQuery("#" + actualProduct + " strong").text();
			jQuery("#product_thumb a").attr("title", actualCategoryTitle + " - " + actualProductTitle);
		}
		
		if (actualProduct != null) {
			jQuery("#" + actualProduct).removeClass()
			jQuery("#" + actualProduct).addClass("a15_FFFFFF");
//			jQuery("#" + actualProduct).css("background-position", "145px 5px")
			jQuery("#content_" + actualProduct).fadeOut("slow", mostrar);
		} else {
			actualProduct = productId;
			
			jQuery("#content_" + actualProduct).fadeIn("slow");
			// Marcado de enlaces
			jQuery("#" + actualProduct).removeClass()
			jQuery("#" + actualProduct).addClass("a15_FF6735");
			jQuery("#" + actualProduct).addClass("selected");
//			jQuery("#" + actualProduct).css("background-position", "145px -26px")
			
			// Imagen de muestra
			var thumClass = actualProduct.substr(actualProduct.indexOf("-") + 1, actualProduct.length);
			jQuery("#product_thumb").removeClass()
			jQuery("#product_thumb").addClass(thumClass);
			
			// Enlace de la imagen de muestra
			// Enlace de la imagen de muestra
			var thumbImage = jQuery("#product_thumb").css("background-image");
			thumbImage = thumbImage.split("\"")[1].split("\"")[0];
			thumbImage = thumbImage.replace("_thumb", "_big");
			jQuery("#product_thumb a").attr("href", thumbImage);
			// Title ligthbox
			var actualCategoryTitle = jQuery("h3 strong").text();
			var actualProductTitle = jQuery("#" + actualProduct + " strong").text();
			jQuery("#product_thumb a").attr("title", actualCategoryTitle + " - " + actualProductTitle);
		}
	});
});


/*
	Index image rotator
*/

var actualImage = 1;
interval = null;
jQuery(document).ready(function () {
	if (jQuery("#image_rotator").length > 0) { // Si existe el elemento "rotador de imágenes" 
		interval = setInterval('changeImageRotator()',5000);
	}
});


function changeImageRotator ()
{
	if (jQuery("#image_rotator").length > 0) { // Si existe el elemento "rotador de imágenes" 
		// controlamos el final de las imágenes
		var totalImages = jQuery("#image_rotator img").length
		var nextImage = actualImage + 1;
		if (nextImage > totalImages) {
			nextImage = 1;
		}
		
		jQuery("#img" + actualImage).fadeOut(3000, function(){ 
			jQuery("#img" + nextImage).fadeIn(3000);
		});
		actualImage = nextImage;
	}
}


function randomNumber (limitSup, limitInf)
{
	// Generamos un número aleatorio entre 1 y 8 (ambos incluidos) de 1 a 8 son las imágenes de la carpeta images/image_rotator/
	theNumber = limitSup - limitInf;
	theNumber = Math.random() * theNumber;
	theNumber = Math.round(theNumber);
	
	return (theNumber);
}

/*
	contacto
*/

jQuery(document).ready(function () {
	if (jQuery("#showMap").length > 0) { // Si existe el elemento "rotador de imágenes" 
		jQuery("#showMap").click (function () {
			jQuery("#map-preview-container").animate({"height": "400px"}, "slow");
		});
	}
});
