$(function(){

	// hover del value en inputs de header
	$('#searchbox').each(function(){
		var valor = $(this).val();
		$(this).focus(function(){
			if( $(this).val() == valor ) {
				$(this).val('');
			};
		});
		$(this).blur(function(){
			if( $(this).val() == '' ) {
				$(this).val(valor);
			};
		});
	});
	
	$('.submit').click(function(){
		var valorBuscar = $('#searchbox').val();
		if( valorBuscar == '' || valorBuscar == 'Buscar' ) {
			return false
		} else {
			$('#searchbox').addClass('load');
		}
	});
	
	$('#field-comment, #field-author, #field-email, #field-url').each(function(){
		var fondo = $(this).css('background-image');
		$(this).focus(function(){
			$(this).css({'background-image':'none'})
		});	
		$(this).blur(function(){
			var valor = $(this).val();
			if(valor=='') {
				$(this).css({'background-image':fondo});
			};
		});
	});
/*
	// MOV de menu superior
	$('#header li').each(function(){
		$(this).hover(
			function(){
				$(this).animate({'margin-top':-5},{queue:false, duration:'fast'});
			},
			function(){
				$(this).animate({'margin-top':0},{queue:false, duration:'normal'});
			}
		);

	});
*/
	// TOOLTIP
	$('a[title]').mouseover(function(e) {
		var tip = $(this).attr('title');
		if (tip != '') {
			$(this).attr('title','');
			$(this).append('<div id="tooltip">' + tip + '</div>');
			$('#tooltip').delay(200).fadeTo('fast',0.9);
		}

	}).mousemove(function(e) {
/*		$('#tooltip').css('top', e.pageY + 5 );
		$('#tooltip').css('left', e.pageX + 10 );*/
		var border_top = $(window).scrollTop();
		var my_tooltip = $('#tooltip');
		var border_right = $(window).width();
		var left_pos;
		var top_pos;
		var offset = 15;
		if(border_right - (offset *2) >= my_tooltip.width() + e.pageX){
			left_pos = e.pageX+offset;
		} else{
			left_pos = border_right-my_tooltip.width()-offset;
		}
		if(border_top + (offset *2)>= e.pageY - my_tooltip.height()){
			top_pos = border_top +offset;
		} else{
			top_pos = e.pageY-my_tooltip.height()-offset;
		}	
		my_tooltip.css({left:left_pos, top:top_pos + 45});
		

	}).mouseout(function() {
		$(this).attr('title',$('#tooltip').html());
		$(this).children('div#tooltip').remove();
	});
	
	//Chili Code Highlighter adjustments, thanks to http://thisblog.usejquery.com/2009/03/11/highlight-your-source-code-with-jquery-and-chili/
	 $('.codecss, .codejs, .codehtml, .codeas, .code').each(function(){ 
		var initialWidth = $(this).width();
		$(this).hover(function() {
		    if($(this).height() > $(this).children().height()) {
		      var openSpace = Math.round(($('#container').width() - initialWidth) / 2 - 10); 
		      if($(this).width() == initialWidth) {
		        $(this).animate({ width : initialWidth + openSpace }, 'fast');
		      }
		    }
		  }, function() {
		    $(this).animate({ width : initialWidth }, 'fast');
		  });
	 })

	$(".cuerpo ul li a, .relacionados ul li a").hoverAttribute({
		removeProtocol: true
	});
	
});
