//****************************************************************************
// XLI																		 *
//****************************************************************************


// Columnas con igual altura

function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}
$(function()
{
	if($.jcarousel) jQuery('#mycarousel').jcarousel();
});
$(window).load(function()
{
	equalHeight($(".home_descripcion"));
	equalHeight($(".home_altura"));
	equalHeight($(".altura2"));
});


// Overlay
$(function() {
	try
	{
		$("a.zoom[rel]").overlay();
	}
	catch (E)
	{
	}
});



$(function() { setTimeout(function() { $(".sari, .sari_bold, .sari_normal").show(); }, 100); });


function catalogoImgPqAGd(anch)
{
	var imgUrl = anch.getAttribute("rel");
	var aGd = document.getElementById("imgGd");
	aGd.setAttribute("href", imgUrl);
	aGd.getElementsByTagName("img")[0].src = imgUrl+"?w=500&h=379";
	return false;
}

function mostrarDescargas(toOn)
{
	var D = $(document.getElementById("ventanaDescargas"));
	if(!toOn) D.fadeOut();
	else D.css({
			left: (document.body.scrollWidth - D.width()) / 2,
			top: document.body.scrollTop - D.height()/1.5 + document.body.clientHeight/2
		})
		.fadeIn()
	;
	return false;
}


function abrirURL(url,target){
	window.open(url, target, 'width=525,height=350');
	return false;
}

Cufon.set('fontFamily', 'sari_bold').replace('.sari_bold');
Cufon.set('fontFamily', 'sari_normal').replace('.sari_normal');
Cufon.set('fontFamily', 'sari_bold').replace('body.paisajes-culturales h2');


$(function()
{
	$(".widget dd").hide(0);
	$(".widget dt a").click(function()
	{
		$(this).parent().siblings("dd").slideUp(300);
		$(this).parent().next("dd").eq(0).slideDown(300);
		return false;
	});

	$("div.columna_de").each(function()
	{
		if(!$.trim($(this).html()).length)
		{
			$(this).remove();
			$("div.columna_iz, div.direccion").width('100%');
		}
	});
});


//
// Persianas del menœ
//

$(document).ready(function() {
//On Hover Over
function megaHoverOver(){
    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
    (function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth  = $(this).width(); //Add each ul's width together
            });
        };
    })(jQuery);

    if ( $(this).find(".row").length > 0 ) { //If row exists...

        var biggestRow = 0;

        $(this).find(".row").each(function() {	//for each row...
            $(this).calcSubWidth(); //Call function to calculate width of all ul's
            //Find biggest row
            if(rowWidth > biggestRow) {
                biggestRow = rowWidth;
            }
        });

        $(this).find(".sub").css({'width' :biggestRow}); //Set width
        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

    } else { //If row does not exist...

        $(this).calcSubWidth();  //Call function to calculate width of all ul's
        $(this).find(".sub").css({'width' : rowWidth}); //Set Width

    }
}
//On Hover Out
function megaHoverOut(){
  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
      $(this).hide();  //after fading, hide it
  });
}


//Set custom configurations
var config = {
     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
     interval: 100, // number = milliseconds for onMouseOver polling interval
     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
     timeout: 500, // number = milliseconds delay before onMouseOut
     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
};

$("ul#topnav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
$("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations

});

// menu_secundario
$(function()
{
	var hayCambios = false;
	$("div.menu_secundario ul li a").each(function()
	{
		var aUrl = $(this).attr("href");
		var dUrl = document.location.toString();
		var mtch = (dUrl.substr(dUrl.length - aUrl.length, aUrl.length).toLowerCase() == aUrl.toLowerCase());
		$(this).parents("li").removeClass(mtch ? "sari_normal" : "sari_bold").addClass(mtch ? "sari_bold" : "sari_normal");
		hayCambios |= mtch;
	});
	if(!hayCambios) $("div.menu_secundario ul li").eq(0).removeClass("sari_normal").addClass("sari_bold");
});


// boletin
$(function()
{
	$("form.formBoletin").submit(function()
	{
		var to = 200;
		var form = $(this);
		var btn = $("input[type=submit]", form);
		var divOk = form.siblings("div.boletin_ok");
		var divErr = form.siblings("div.boletin_ko");

		btn.fadeOut(to);
		divOk.slideUp(to);
		divErr.slideUp(to);

		$.ajax({
			type		: "POST",
			url			: "/boletin.php",
			dataType	: "json",
			data		: {
				ajax: 1,
				email: $("input[type=text]", form).val()
			},
			error		: function(request, textStatus, errorThrow)
			{
				btn.fadeIn(to);
				divErr.slideDown(to);
			},
			success		: function(data, textStatus)
			{
				if(data.error) return this.error();
				form.slideUp(to);
				divOk.slideDown(to);
			}
		});
		return false;
	});
});

