$(document).ready(function() {

	$("body").removeClass('no-js').addClass('js');
	
	$(".tip").tipTip();

	/* HIDE ICONS AND SHOW ONLY WHEN HOVER */
		
	//$("#list .actions").hide(); -> MEJOR HACERLO CON CSS
	
	/*
	$("#list .libro").hover(function() {
		if ($(this).hasClass('editable')) {
			$(this).find(".actions").show();
		}
	}, function() {
		$(this).find(".actions").hide();
	});
	*/
	/*
	$("#list .libro").hover(function() {
		$(this).find(".actions").show();
	}, function() {
		$(this).find(".actions").hide();
	});
	
	$("#list .autor").hover(function() {
		$(this).find(".actions").show();
	}, function() {
		$(this).find(".actions").hide();
	});
	
	$("#list .editorial").hover(function() {
		$(this).find(".actions").show();
	}, function() {
		$(this).find(".actions").hide();
	});
	
	$("#list .categoria").hover(function() {
		$(this).find(".actions").show();
	}, function() {
		$(this).find(".actions").hide();
	}); 
	
	$("#list .libreria").hover(function() {
		$(this).find(".actions").show();
	}, function() {
		$(this).find(".actions").hide();
	}); 
	*/
	
	/* SHOW LOGIN FORM AND PERFORM LOGIN */
	
	$("a#login").click(function() {
	
		if ( $("div#login_form").length < 1 ) {
		
			var form_data = {
				ajax: true,
			};
		
			$.ajax({
				url: $(this).attr('href'),
				type: 'POST',
				data: form_data,
				success: function(msg) {
					$("div#menu").before(msg);
					$("div#login_form").hide();
					$("div#login_form").slideDown("slow");
				}
			});

		} else {
			
			$("div#login_form").slideToggle("slow");
		}	
				
		return false;
	});
	
	/* SHOW ADD FORMULAR */
	/*
	$("a#button-add").live('click', function(e) { 
		$("div#list > div#search").slideUp("slow");
		$("div#list > div#add").slideToggle("slow", function() {
			$("div#add #name").focus();
		});
		e.preventDefault();
	});
	*/
	
	/* SHOW FORM FORMULAR */
	/*
	$("a#button-search").live('click', function(e) { 
		$("div#list > div#add").slideUp("slow");
		$("div#list > div#search").slideToggle("slow", function() {
			$("div#search #name").focus();
		});
		e.preventDefault();
	});
	*/

	//spinner = '<div id="spinner"></div>';
	
	/*

	if ($("body").hasClass("85181123185")) {
	

	$("a#add").live('click', function(e) { 
		$("div#list > div#add").slideToggle("slow", function() {
			$("div#add #name").focus();
		});
		e.preventDefault();
	});
	
	$("div#list div#add #submit").live('click', function() {		
		var form_data = {
			name : $("div#add #name").val(),
			ajax: true,
		};
		
		$("div#add #name").attr("value", "");
		
		$.ajax({
			url: $("div#add form#form-add").attr("action"),
			type: 'POST',
			data: form_data,
			success: function(msg) {
				$("div#list > div#add").slideToggle("slow");
				$.ajax({
					url: $("form#form-add #url").val(),
					type: 'GET',
					success: function(msg) {
						$("body").fadeTo('slow', 0.1, function() {
							$(this).append('<div id="response"' + msg + '</div>');
							msg = $("#response").find('#container').children();
							$('#container').html(msg);
							$(this).fadeTo('slow', 1);
							$("#response").remove();
						});
					}
				});				
			}
			
		});
		
		return false;
		
	});

		
		$("div#list #remove").live('click', function() {
			var id = $(this).parent().parent().attr("id");
			
			$.ajax({
				url: $(this).attr("href"),
				type: 'POST',
				success: function(msg) {
					
					$("#"+id).fadeTo('slow', 0.1).slideUp('slow', function() {
						$(this).remove();
					});
				}
			});
				
			return false;		
		});
		
		$("div#list #edit").live('click', function() {
		
			$('.editing').each(function() {
					$(this).removeClass('editing').addClass('libreria');
					$(this).children('form').remove();
					html = $(this).children().html();
					$(this).html(html);
			});
			
			var id = $(this).parent().parent().attr("id"),
			    html = $("#"+id).html(),
			    value = $(this).parent().prev().children().html();
		
			var form_data = {
				ajax: true,
			};

			$.ajax({
				url: $(this).attr("href"),
				type: 'POST',
				data: form_data,
				success: function(msg) {
					$("#"+id).fadeTo('slow', 0.0, function() {
						$(this).removeClass('libreria').addClass('editing');
						$(this).html(msg);
						msg = $(this).find('form');
						$(this).find('#name').val(value);
						$(this).html(msg);
						$(this).append('<div class="' + id + '">' + html + '</div>');
						$("."+id).hide();
						$(this).fadeTo('slow', 1);
						$(this).find('#name').focus();
					});
				}
			});
			return false;	
		});
		
		$("div#list #submit").live('click', function() {
			var id = $('.editing').attr("id");
				url = $("#"+id).children("form").attr("action") + '/' + id;
			
			var form_data = {
				name: $(this).parent().prev().find("#name").val(),
				id: id,
				ajax: true,
				save: true,
			};
			
			$.ajax({
				url: url,
				type: 'POST',
				data: form_data,
				success: function(msg) {
					$("#"+id).fadeTo('slow', 0.0, function() {
						$(this).addClass('libreria').removeClass('editing');
						name = $(this).find("#name").val();
						console.log('name' + name); //TEST
						$(this).find('form').remove();
						html = $(this).find("."+id).html();
						console.log(html); //TEST
						$(this).find("."+id).remove();
						$(this).html(html);
						$(this).find(".name").children().html(name);
						$(this).fadeTo('slow', 1);
					});
				}
			});

			return false;
		});
	} 
	
	*/

});
