//Innerfade
(function($){$.fn.innerfade=function(options){return this.each(function(){$.innerfade(this,options)})};$.innerfade=function(container,options){var settings={'animationtype':'fade','speed':'normal','type':'sequence','timeout':2000,'containerheight':'auto','runningclass':'innerfade','children':null};if(options)$.extend(settings,options);if(settings.children===null)var elements=$(container).children();else var elements=$(container).children(settings.children);if(elements.length>1){$(container).css('position','relative').css('height',settings.containerheight).addClass(settings.runningclass);for(var i=0;i<elements.length;i++){$(elements[i]).css('z-index',String(elements.length-i)).css('position','absolute').hide()};if(settings.type=="sequence"){setTimeout(function(){$.innerfade.next(elements,settings,1,0)},settings.timeout);$(elements[0]).show()}else if(settings.type=="random"){var last=Math.floor(Math.random()*(elements.length));setTimeout(function(){do{current=Math.floor(Math.random()*(elements.length))}while(last==current);$.innerfade.next(elements,settings,current,last)},settings.timeout);$(elements[last]).show()}else if(settings.type=='random_start'){settings.type='sequence';var current=Math.floor(Math.random()*(elements.length));setTimeout(function(){$.innerfade.next(elements,settings,(current+1)%elements.length,current)},settings.timeout);$(elements[current]).show()}else{alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'')}}};$.innerfade.next=function(elements,settings,current,last){if(settings.animationtype=='slide'){$(elements[last]).slideUp(settings.speed);$(elements[current]).slideDown(settings.speed)}else if(settings.animationtype=='fade'){$(elements[last]).fadeOut(settings.speed);$(elements[current]).fadeIn(settings.speed,function(){removeFilter($(this)[0])})}else alert('Innerfade-animationtype must either be \'slide\' or \'fade\'');if(settings.type=="sequence"){if((current+1)<elements.length){current=current+1;last=current-1}else{current=0;last=elements.length-1}}else if(settings.type=="random"){last=current;while(current==last)current=Math.floor(Math.random()*elements.length)}else alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');setTimeout((function(){$.innerfade.next(elements,settings,current,last)}),settings.timeout)}})(jQuery);function removeFilter(element){if(element.style.removeAttribute){element.style.removeAttribute('filter')}}


//Menú principal
var menu=function(){
	var t=15,z=50,s=6,a;
	function dd(n){this.n=n; this.h=[]; this.c=[]}
	dd.prototype.init=function(p,c){
		a=c; var w=document.getElementById(p), s=w.getElementsByTagName('ul'), l=s.length, i=0;
		for(i;i<l;i++){
			var h=s[i].parentNode; this.h[i]=h; this.c[i]=s[i];
			h.onmouseover=new Function(this.n+'.st('+i+',true)');
			h.onmouseout=new Function(this.n+'.st('+i+')');
		}
	}
	dd.prototype.st=function(x,f){
		var c=this.c[x], h=this.h[x], p=h.getElementsByTagName('a')[0];
		clearInterval(c.t); c.style.overflow='hidden';
		if(f){
			p.className+=' '+a;
			if(!c.mh){c.style.display='block'; c.style.height=''; c.mh=c.offsetHeight; c.style.height=0}
			if(c.mh==c.offsetHeight){c.style.overflow='visible'}
			else{c.style.zIndex=z; z++; c.t=setInterval(function(){sl(c,1)},t)}
		}else{p.className=p.className.replace(a,''); c.t=setInterval(function(){sl(c,-1)},t)}
	}
	function sl(c,f){
		var h=c.offsetHeight;
		if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
			if(f==1){c.style.filter=''; c.style.opacity=1; c.style.overflow='visible'}
			clearInterval(c.t); return
		}
		var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s), o=h/c.mh;
		c.style.opacity=o; c.style.filter='alpha(opacity='+(o*100)+')';
		c.style.height=h+(d*f)+'px'
	}
	return{dd:dd}
}();


//Fade de la portada
function fadeportada()
{
	//Innerfade
	$('ul#portfolio').innerfade({
		speed: 2500,
		timeout: 10000,
		type: 'sequence',
		containerheight: '500px'
	});	
}
	
	
	
//Fade de banners
function fadeimagen()
{
$(".img_change").fadeTo(1000, 1.0);
	$(".img_change").hover(function(){
		$(this).stop().fadeTo(500, 0.0);
		},function(){
		$(this).stop().fadeTo(1500, 1.0);
	});
}


//Idiomas
function combo_idiomas()
{
	try {
		oHandler = $(".mydds").msDropDown().data("dd");
		oHandler.visible(true);
		//alert($.msDropDown.version);
		//$.msDropDown.create("body select");
		$("#ver").html($.msDropDown.version);
		} catch(e) {
			alert("Error: "+e.message);
		}	
}

//Enlaces opacidad
function op_enlaces()
{	
	if (navigator.appName != "Microsoft Internet Explorer") //Política de Calidad
	{
		$("li.dos p img").fadeTo("slow", 0.6);
		$("li.dos h3 a").fadeTo("slow", 0.9);	
		$("li.dos p img").hover(function(){
			$(this).stop().fadeTo("slow", 1.0);
			$("li.dos h3 a").stop().fadeTo("slow", 1.0);
			},function(){
			$(this).stop().fadeTo("slow", 0.6);
			$("li.dos h3 a").stop().fadeTo("slow", 0.6);
		});	
		$("li.dos h3 a").hover(function(){
			$(this).stop().fadeTo("slow", 1.0);
			$("li.dos p img").stop().fadeTo("slow", 1.0);
			},function(){
			$(this).stop().fadeTo("slow", 0.6);
			$("li.dos p img").stop().fadeTo("slow", 0.6);
		});
	}
	
	if (navigator.appName != "Microsoft Internet Explorer") //Enlaces Tahe
	{
		$("li.tres div").fadeTo("slow", 0.6);
		$("li.tres div").hover(function(){
			$(this).stop().fadeTo("slow", 1.0);
			},function(){
			$(this).stop().fadeTo("slow", 0.6);
		});
	}
	
	
	$("ul.social_links li a").fadeTo("slow", 0.6); //Social
	$("ul.social_links li a").hover(function(){
		$(this).stop().fadeTo("slow", 1.0); 
		},function(){
		$(this).stop().fadeTo("slow", 0.6); 
	});
}


//Slider de la barra de abajo
function bb_slider(){
	var kwicks_space = 11; //Espacio entre lis
	if (navigator.appName == "Microsoft Internet Explorer") kwicks_space = 16;
	
	$('.kwicks').kwicks({
		max : 385,
		duration: 800,
		sticky: true,
		spacing: kwicks_space
	});
	
	$('.kwicks li').click(function() {		  
		  redirigir($(this).find("a").attr('href'));
		return false;		
	});
}


//Rotación mapa
function rotacion_mapa()
{
	if (navigator.appName != "Microsoft Internet Explorer")
	{
		$(".bb_lateral p a img").rotate({ 
		   bind:
			 { 
				mouseover : function() { 
					$(".t_mapa").rotate({animateTo:360})
				},
				mouseout : function() { 
					$(".t_mapa").rotate({animateTo:0})
				}
			 } 
		   
		});
	}	
}


//Alinear verticalmente
(function ($) {
$.fn.vAlign = function() {
    return this.each(function(i){
        $(this).children().wrapAll('<div class="nitinh-vAlign" style="position:relative;"></div>');
        var div = $(this).children('div.nitinh-vAlign');
        var ph = $(this).innerHeight();
        var dh = div.height();
        var mh = (ph - dh) / 2;
        div.css('top', mh);
	});
};
})(jQuery);


//Funcion para productos
function productos_subcategorias()
{
	$(".titulo h2 a").each(function(i) {
		if ($(this).text().length > 21) {$(this).css("font-size", "33px");} //Categorías
	});	
	
	$("#subcat_titulo div.titulo > h2").css("font-size", "55px").css("margin-top", "-10px"); //Letras título		
	
	if($("#subcat_listado_titulo .cat_listado .titulo p").text().length > 80) {$("#subcat_listado_titulo .cat_listado .titulo p").css("font-size", "140%");} //Titulo listado
	
	$(".subcat_lista li span").each(function(i) { //Imágenes alineadas abajo
		var alto_span = $(this).height();
		var alto_img = $(this).find("img").height();
		var separacion = alto_span-alto_img;		
		var nuevo_alto_span = $(this).height()-separacion;		
		$(this).css("padding-top", separacion).css("height", nuevo_alto_span);
	});	
	
	$('.subcat_lista li h3 a').each(function(i) { //Tamaño en función de la longitud del texto		
		if ($(this).text().length > 21) {
			$(this).css("font-size", "20px");
			$(this).parent().css("margin", "5px 0");
		}
	});
	
	$(".listado_pro li div.img_pro").each(function(i) { //Icono 'Nuevo' colocado según el tamaño de imagen
		var alto_img = $(this).find("img").height();
		nuevo_top = alto_img - 35;		
		$(this).find("span").css("top", nuevo_top);
	});
	
	$("ul.subcategoria li div.busp_img").each(function(i) { //Imágenes de Subcategorías en la búsqueda
		var ancho_img = $(this).find("img").width();
		var alto_img = $(this).find("img").height();		
		if(alto_img < 90 && ancho_img > 125) { $(this).find("img").css("width", "125px"); }
		if(alto_img > 90 && ancho_img > 125) { $(this).find("img").css("height", "90px"); }
		if(alto_img > 90) { $(this).find("img").css("height", "90px"); }
	});
	
	$("ul li div.busp_texto h4").each(function(i) { //Texto largo de la descripción en la búsqueda		
		if ($(this).text().length > 103) {$(this).css("margin-bottom", "10px"); $("ul.subcategoria li div.busp_texto h3").css("margin-bottom", "8px");} //Categorías
	});
	
	$('#busq_pro ul li div.busp_img p').vAlign();
	$('#busq_pro ul li div.busp_img p img').vAlign();
	
}


//Función para noticias
function lista_noticias()
{
	$("#noticias_bg ul li:odd").addClass("par");
	$("#noticias_bg ul li:last-child").css("border-bottom", "none");
	
	$("#noticias_bg ul li").mouseover(function() {
	  $(this).find("h3 a").css("color", "#ee8a20");
	  $(this).find("p a").css("color", "#2e5979");
	  $(this).find("span.fecha_min").css("color", "#333333");
	});
	$("#noticias_bg ul li").mouseout(function() {
	  $(this).find("h3 a").css("color", "#163b58");
	  $(this).find("p a").css("color", "#646464");
	  $(this).find("span.fecha_min").css("color", "#3d6d91");
	});
}



//Redirección
function redirigir(pagina)
{ document.location.href=pagina; }

//Bar Scroll
function deslizar($lugar,$velocidad){
	$('html, body').stop().scrollTo($lugar,$velocidad);
}
