$(document).ready(function(){

	jQuery('#pages a:last').depagify({
            container:  '#content',
            request:    function(options) {
            },
            effect:     function() {
                jQuery(this).fadeIn('slow');
            }
        });
	
	
	  $(".contingut_post img").lazyload({
	    threshold : 300,
	    placeholder : "img/loading.gif",
	    effect : "fadeIn"  
	  });


	$(".menu2 a.infoextra").hover(function() {
		$().find("em").animate({opacity: "hide", top: "0"}, "fast");
		
			var idblog = $(this).attr("alt");

			var html = $.ajax({
			url: "ajax/bloginfo.php",
			data: "id="+idblog,
			async: false
			}).responseText;


		var hoverText = html;
	    	$(this).find("em").html(hoverText);
		$(this).find("em").animate({ opacity: "show", top: "8"}, "slow");

	}, function() {
		$(this).find("em").animate({opacity: "hide", top: "0"}, "fast");
	});

	$('a.ocultar').click(function(a){

		var atr = $(this).attr('id');
		$('#post'+atr).animate({opacity: "toggle", top: "0"}, "slow");
		// $('a.ocultar#'+atr).text('mostrar');
		$('a.ocultar#'+atr).animate({opacity: "show", top: "0"}, "slow");

	});

	$('.player').click(function(a){


		var id = $(this).attr("alt");
		var lang = $(this).attr("lang");
		var html_resp = $.ajax({
			url: "ajax/playermulti.php",
			data: "id="+id+"&lang="+lang,
			async: false
			}).responseText;

		$('#player-multi-mp3').html(html_resp);
		$('#player-multi-mp3').animate({opacity: "show", top: "0"}, "slow");

	});

	$('#mas_info').click(function(a){
		$('#opciones_avanzadas').toggle();
	});

	$('#options_advance').click(function(a){
		$('#cuerpoprincipal').hide();
		var data = "";
		var num_posts = $('#post_sel_num').val();
		var mode = $('#post_sel_mode').val();

		if( (num_posts<=0) || (num_posts>99)) num_posts = 99;

		var html = $.ajax({
			url: "ajax/lastposts.php",
			data: "num_posts="+num_posts+"&option_mode="+mode,
			async: false,
			beforeSend: function(objeto){
					var hoverText = "<center><img src='img/loading.gif' alt='cargando' /></center>";
					$('#cuerpoprincipal').html(hoverText);
					$('#cuerpoprincipal').show();
			}
			}).responseText;


		var hoverText = html;

	    	$('#cuerpoprincipal').html(hoverText);
		$('#cuerpoprincipal').show(); 
	});

	$('#post_sel_lang').change(function(a){
		$('#cuerpoprincipal').hide();
		
		var html = $.ajax({
			url: "ajax/lastposts.php",
			data: "id="+$(this).val(),
			async: false,
			beforeSend: function(objeto){
					var hoverText = "<center><img src='img/loading.gif' alt='cargando' /></center>";
					$('#cuerpoprincipal').html(hoverText);
					$('#cuerpoprincipal').show();
			}
			}).responseText;


		var hoverText = html;

	    	$('#cuerpoprincipal').html(hoverText);
		$('#cuerpoprincipal').show(); 
	});
});

	// http://johannburkard.de/blog/programming/javascript/6-more-jquery-tips-text-searching-page-load-time-and-others.html
	$.fn.egrep = function(pat) {
		var out = [];

		var textNodes = function(n) {
			if (n.nodeType == Node.TEXT_NODE) {
			var t = typeof pat == 'string' ? n.nodeValue.indexOf(pat) != -1 : pat.test(n.nodeValue);

			if (t) {
			out.push(n.parentNode);
			}
			} else {
				$.each(n.childNodes, function(a, b) {
					textNodes(b);
				});
			}
		};

		this.each(function() {
			textNodes(this);
		});
	return out;
	};


	$.fn.del_hightlight = function() {
		var out = [];

		var textNodes = function(n) {
			if (n.nodeType == Node.TEXT_NODE) {
				out.push(n.parentNode);
			
			} else {
				$.each(n.childNodes, function(a, b) {
					textNodes(b);
				});
			}			
		};

		this.each(function() {
			textNodes(this);
		});
	return out;
	};

	function hightlight(txt) {

		var n = $('body').del_hightlight();

		for (var i = 0; i < n.length; ++i) { void($(n[i]).css('background', '')); }

		var pat = new RegExp(txt, "gi");

		n = $('body').egrep(pat);
		for (var i = 0; i < n.length; ++i) { void($(n[i]).css('background', 'yellow')); }
	}







