$(function(){

	//blob.start();

	$(".view").fancybox();
	
	$(".fancybox").fancybox({
		showCloseButton: false
	});
		
	$("#menu-principal li a").unbind("mouseover").mouseover(function(){
		
		if( $(this).attr("title") == "RSS" )
			$(this).attr("target", "_blank");
		
	});	
	
	$("#searchsubmit, #s").val("");
	
	// search
	$("#s").addClass("out").attr("placeholder", "pesquisar...");

	var s = $('#s');
	
	message = s.attr('placeholder');
	
	s.focus(function() {
		
		s.removeClass("out").val("");
		
		s.animate({ "width":"160px" }, 500);	
		
	}).blur(function() {
		
		if( $.trim( s.val() ) == ''){ 
			s.addClass("out");
			s.animate({ "width":"100px" }, 500, function(){
				s.val( message );
			});
		}
	});
	
	
	$(".respond").fancybox({
		onStart: function(){
			$(".comment-ops").removeClass("comment-ops");
			$("#author, #email, #comment").val("");
		},	
		onComplete: function(){

			window.setTimeout(function(){
				$("#author").focus();
			}, 500);
			
			var commentform = $("#commentform");
			commentform.unbind("submit").bind("submit", function(){
				
				var commentData = commentform.serializeArray();
				
				$(".comment-ops").removeClass("comment-ops");
				
				$.each(commentData, function(i, field){
					
					var it = $("#"+field.name);
					
					if( $.trim( field.value ) == "" )
						it.addClass("comment-ops");
					
				});
				
				var commentOps = $(".comment-ops");
				if( commentOps.length == 0 ){
					
					$("#action").find("p").html("Aguarde enviando coment&aacute;rio...");
					$(".lnk").click();
					
					$.ajax({
						url: commentform.attr("action"),
						type: commentform.attr("method"),
						data: commentData,
						dataType: 'xml',
						success: function( retorno ){

							$("#action").find("p").html("Coment&aacute;rio enviado com sucesso!");
							$(".lnk").click();
						
							window.setTimeout(function(){
								$.fancybox.close();
							}, 3000);
							
						},
						error: function( xhr, ajaxOptions, thrownError ){
						
							$("#action").find("p").html("Coment&aacute;rio enviado com sucesso!");
							$(".lnk").click();
						
							window.setTimeout(function(){
								$.fancybox.close();
							}, 3000);
				
						}
					});
					
				} else {
					commentOps.eq(0).focus();
				}
				
				return false;
				
			});
			
		}
	});
	

});

var blob = {
	
	opt: 0,
	
	x: 0,
	
	w: 0,
	
	over: 0,
	
	def: 0,
	
	start: function(){
		
		var src = $("#access ul li.current_page_item");	
		var it = $("#access ul li a");
		
		if( src.index() == -1 ){
			
			blob.def = -1;
			$("#bar").hide();
			
		} else {
			
			blob.def = src.index();	
			
		}	
			
		blob.opt = blob.def;
		
		blob.set();
		
	},
	
	control: function(){
		
		var it = $("#access ul li a");
		
		it.mouseover(function(){
			
			$("#bar").show();
			blob.opt = $(this).parent().index();
			blob.over = 1;
			blob.set();
		
		}).mouseout(function(){
			
			blob.opt = blob.def;
			blob.set();
			
			if( blob.def == -1  )
				$("#bar").hide();
		
		});	
	
	},
	
	set: function(){
		
		var nav = $("#access");
		var it = $("#access ul li a");
				
		blob.x = it.eq( blob.opt ).position().left + 18;
		blob.w = it.eq( blob.opt ).parent().width() - 20;	
						
		blob.anime();
				
	},
	
	anime: function(){
		
		var bar = $("#bar");
				
		bar.stop().animate({ "left": blob.x + "px", "width": blob.w + "px" }, 400, function(){
			blob.control();
		});
	
	}

}
