// Desvanecer por ubicación
$.fn.fadeEachDown = function() {
	var s = this.size();
	return this.each(function(i) {
		var o = 1 - (s == 1 ? 0.5 : 0.85/s*(i+1));
		var e = this.style;
		if (window.ActiveXObject)
			e.filter = "alpha(opacity=" + o*100 + ")";
		e.opacity = o;
		$(this).hover (
			function () { $(this).fadeTo('fast', 1); },
			function () { $(this).fadeTo('fast', o );	}
		);
	});
};

// Tooltips
this.tooltip = function(){	
		xOffset = 15;
		yOffset = 5;		
	$("a.tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<div id='tooltip'><span></span>"+ this.t +"</div>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("right",( (window.innerWidth - e.screenX) + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
   });	
	$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("right",( (window.innerWidth - e.screenX) + yOffset) + "px")
	});	

};


$(function() {

	// Tooltips
	tooltip();
	
	// Mostrar citas en blockquotes
	$("blockquote").each(function(){
		var title = $(this).attr("title");
		var cite = $(this).attr("cite");
		
		if (cite == '' && title != '') {
			$(this).append("<cite class='source'>&mdash; "+title+"</cite>");
		} else if (cite != '' && title == '') {
			$(this).append("<cite class='source'>&mdash; <a href='"+cite+"' title='"+cite+"'>"+cite+"</a></cite>");
		} else if (cite != '' && title != '') {
			$(this).append("<cite class='source'>&mdash; <a href='"+cite+"' title='"+title+"'>"+title+"</a></cite>");
		} else {
		}
		
	});
	
	// Efectos de opacidad en imágenes
  $(".imagenes").children().hover(function() {
    $(".imagenes").children().stop();
    $(this).siblings().fadeTo("fast", 0.2);
    $(this).fadeTo("fast", 1.0);
  },function(){
    $(".imagenes").children().stop();
    $(".imagenes").children().fadeTo("fast", 1.0);
  });

	// Bordecitos
	$("#side h3, .head, .mini h2, a.comentarios, .post .fecha").append("<span></span>");

	// Desvanecer
	$('#twitter li').fadeEachDown();
	$('#musicalizando li').fadeEachDown();
	$('#comentando li').fadeEachDown();

	// Tagcloud
	$(".tagcloud a").each( function (index) {
		var tamano = $(this).css("font-size");
		var opacidad = ((parseFloat(tamano, 10))*2)/100;
		$(this).animate({ opacity: opacidad });
		$(this).hover (
			function () { $(this).animate({ opacity: 1 } ); },
			function () { $(this).animate({ opacity: opacidad } ); }
		);
	});

	// Cajas con código
	$("pre").hover(function() {
		var codeInnerWidth = $("code", this).width() + 10;
		if (codeInnerWidth > 480) {
			$(this).stop(true, false).css({ zIndex: "666", position: "relative" }).animate({ width: codeInnerWidth + "px" },500);
		}
	}, function() {
		$(this).stop(true, false).animate({	width: 480 });
	});


	// Ampersands
	$("*:contains('&'):not(pre, code)", document.body)
		.contents()
		.each(
			function() {
			if( this.nodeType == 3 ) {
				$(this)
				.replaceWith( this
				.nodeValue
				.replace( /&/g, "<span class='amp'>&</span>" )
				);
			}
		}
	);

	// Colorbox
	$(".gallery a, .lightbox").colorbox({maxHeight:'95%',current:"Imágen {current} de {total}"});

  // Mostrar foto de fondo
  $("#verfondo").hover(function() {
  	$("#contenedor").css('visibility','hidden');
  },function(){
  	$("#contenedor").css('visibility','visible');
  });
		
});
