$(document).ready(function() {
	$(".tab").hide();
	$(".tabs").show();
	
	$(".tabs a").click(function() {
		$(".tab").hide();
		$(".tab:eq(" + $(".tabs a").index(this) + ")").fadeIn();
		
		return false;
	});
	
	$("table tbody tr:nth-child(even)").css("backgroundColor", "#EEE");
	
	$(".cancel").live("click", function() {
		$(this).parent().hide().prev().show();
		
		return false;
	});
	
	$(".delete").live("click", function() {
		var _this = this;
		
		$.get($(this).attr("href"), function(ret) {
			if (ret) {
				alert(ret);
			} else {
				$(_this).parents("tr").hide();
			}
		});
		
		return false;
	});
	
	$(".insert").live("click", function() {
		var _this = this;
		
		$.get($(this).attr("href"), function(ret) {
			$("table tbody").prepend(ret);
		});
		
		return false;
	});
	
	$(".submit").live("click", function() {
		var _this = this;
		
		var attr = $(this).prev().attr("name");
		var id = $(this).prev().attr("alt");
		var value = $(this).prev().val();
		
		$.get("?", "ajax=true&attr=" + attr + "&id=" + id + "&update=true&value=" + value, function(ret) {
			if (ret.match('success')) {
				$(_this).parent().hide().prev().text(ret.replace('success', '')).show();
			} else {
				alert(ret);
				$(_this).prev().focus();
			}
		});
		
		return false;
	});
	
	$(".update").live("click", function() {
		$("._update").hide().prev().show();
		
		$(this).hide().next().show().find("input").val($(this).text()).focus();
		
		return false;
	});
});

