var inicio=1;
var hm=0;
$(document).ready(function(){

	 reducir_critica();
	 togle_critica();
	 lazy();
	 div_header();
	 style_buscar();
	 menu_right();
	 inicio=2;
	
	 $("#f_ingreso, #f_registro, #formID, #formB").validationEngine();
	 
	 $('#formB').submit(function(){buskeda();return false;});
});

function lazy()
{
	 $(".img img, .img2 img").lazyload({         
			// placeholder : "{var:IMG}grey.gif",
			  effect : "fadeIn"
		});
}


function style_buscar() {
	$('#box_buscador #buscar').focus(function(){
		$(this).css({'background-image':'none'});
	}).focusout(function(){
		if($(this).val()=='')
			$(this).css({'background-image':'url(img/bg_buscar.jpg)'});	
	});
}


function menu_right() 
{
	$('.box_listado h1').click(function(){
		$('div.listado:visible').not($(this).next('div.listado')).slideToggle('fast');
		$(this).next('div.listado').slideToggle('fast');
		$('.lista2').css('display','none');
	});
	
	$('.box_listado .lista1 li a.enlace1').click(function(){
		
		$('.box_listado .lista1 ul:visible').not($(this).parent().find('ul.lista2')).slideToggle('fast');
		$(this).parent().find('ul.lista2').slideToggle('fast');	
		/*$(this).find("ul").slideToggle('fast').parent().siblings("ul").slideUp("slow"); 
		$(this).siblings();
		*/
	});
	
	$('.box_listado .lista2 li a.enlace2').click(function(){
		
		$('.box_listado .lista2 ul:visible').not($(this).parent().find('ul.lista3')).slideToggle('fast');
		$(this).parent().find('ul.lista3').slideToggle('fast');	

	});



	$('.hbox_listado h1').click(function(){
		$('div.listado:visible').not($(this).next('div.listado')).slideToggle('fast');
		$(this).next('div.listado').slideToggle('fast');
		$('.lista2').css('display','none');
	});
	
	$('.hbox_listado .lista1 li a.enlace1').click(function(){
		
		$('.hbox_listado .lista1 ul:visible').not($(this).parent().find('ul.lista2')).slideToggle('fast');
		$(this).parent().find('ul.lista2').slideToggle('fast');	
		/*$(this).find("ul").slideToggle('fast').parent().siblings("ul").slideUp("slow"); 
		$(this).siblings();
		*/
	});
	
	$('.hbox_listado .lista2 li a.enlace2').click(function(){
		
		$('.hbox_listado .lista2 ul:visible').not($(this).parent().find('ul.lista3')).slideToggle('fast');
		$(this).parent().find('ul.lista3').slideToggle('fast');	

	});


	/*
	jQuery(document).ready(function(){	
		jQuery(".api_papaya_section").click(function(){
			jQuery(this).next("div.api_papaya_section_content").slideToggle(300).siblings("div.api_papaya_section_content").slideUp("slow"); 
			jQuery(this).siblings();	});});*/
}


function acortar_str(texto,t)
{
	var fin;
	for(i=0;i<texto.length;i++)
	{
		if(texto.substring(i,i+1)==t)
		{
			fin=i;	
			break;
		}
	}
	return texto.substring(0,fin);
}

function togle_critica()
{
	$(".leer_mas").click(function(){
		 reducir_critica();
		 $(this).html('');
		$(this).parent().find(".txt_critica").animate({
			'height':$(this).parent().attr('sham_height'),
			'max-height':$(this).parent().attr('sham_height')
				},'fast');
		
	});
}

function reducir_critica()
{
	$(".txt_critica").each(function(){
		
		//$(this).append(acortar_str($(this).css('height'),'p'));
		var h=0;
		var url='';
		h=acortar_str($(this).css('height'),'p');
		if(h>120)
		{
			
			
			if(inicio==1)
			{
				
				$(this).parent().attr({'sham_height':$(this).css('height')}).append('<div class="leer_mas"></div>');		
			}
			url=$(this).parent().parent().parent().parent().parent().find('a.ancla').attr('name');
			$(this).parent().find('.leer_mas').html('<a href="#'+url+'">LEER MAS<a>');
			
			$(this).css({'max-height':'140px','overflow':'hidden'});
			
			
		}
		
	});
}

 function ancla(a) {

	elementClick = $(a).attr("href")
	destination = $(elementClick).offset().top;
	$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100 );
	return false;
		
}


function error_log(text)
{
	$("#nota_login").html(text).show(2000, function(){setTimeout(function(){$("#nota_login:visible").hide(1000).fadeOut();}, 2000);});	
}

function ingresar()
{
	var user = $("#l_usuario").val();
	var clave = $("#l_clave").val();
	var nota = $("#nota_login");
	
	if(user==0 || user<3) error_log('Ingresar un usuario');
	else if(clave==0 || clave<6) error_log('Ingresar una contraseña');
	else xajax_login(user,clave);
	
	return false;
	
}

function galeria(dir)
{
	div='#galeria .carrousel';
	$(div).click(function(){
		//var cant=$(div).index(this);
		//cant++;
		var img=$(this).find('.images2 img').attr('alt');
		//var total=$(div).size();
		
		$('#big_img').html('<img src="'+dir+img+'" />');
	});
}

function url_amigable(txt)
{
	txt = txt.toLowerCase();
	
	txt = txt.replace('á', 'a');
	txt = txt.replace('é', 'e');
	txt = txt.replace('í', 'i');
	txt = txt.replace('ó', 'o');
	txt = txt.replace('ú', 'u');
	
	txt = txt.replace('à', 'a');
	txt = txt.replace('è', 'e');
	txt = txt.replace('ì', 'i');
	txt = txt.replace('ò', 'o');
	txt = txt.replace('ù', 'u');
	txt = txt.replace('ñ', 'n');
	
	txt = txt.replace(' ', '-');
	txt = txt.replace('&', '-');
	txt = txt.replaceAll("\\r\\n", "-");
	txt = txt.replaceAll("\\n", "-");
	txt = txt.replace('+', '-');
	
	txt.replaceAll("/[^a-z0-9\\-<>]/", "");
	txt.replaceAll("/[\\-]+/", "-");
	txt.replaceAll("/<[^>]*>/", "");
	
	return quitando_guion(txt);
}

function quitando_guion(url)
{
	var new_dato = url;
	new_dato=new_dato.substring(0, new_dato.length()-1);
	if(new_dato == "-")
	{
		url=url.substring(0, url.length()-1);
		return url;
	}
	else
	return url;
}

function alert_msj()
{
	$("#save_ok").show(1000, function(){setTimeout(function(){$("#save_ok:visible").hide(2000).fadeOut();}, 5500);});
}

