$(document).ready(function() {

	$("span.tools a").click(function(){
		var active = $(this).closest("span.tools").find(".active");
		active.removeClass("active");
		
//		showaj widoczne
//		if(!$(this).hasClass("save"))
		{
			
			$(this).closest("li").find("div.box:visible").each(function(){
				if(!$(this).hasClass("save")) $(this).hide();
			});
		}

		//		pokaz
		var cName = $(this).attr("class");
		if(active.attr("class") != cName ){
			$(this).closest("li").find("div."+cName).show();
			$(this).addClass("active");
		}
		
		//jesli save
		if($(this).hasClass("save")){
			var li = $(this).closest("li.company");
			
			$(this).parent().removeClass("ajaxLink").addClass("flaged");
			$(this).replaceWith("Zapisano");
						
			$.ajax({
				   type: "POST",
				   url: "/user/addToNotes",
				   data: { id: li.attr("rel") }
				   
			});
			
		}
	});
	
	
	$(".closeBox").click(function(){
		$(this).closest("li").find(".active").removeClass("active");
		$(this).closest(".box").hide();
	});
	
	
	//flagi
	$(".box .flagItem").click(function(){
		var li = $(this).closest("li.company");
		var link =	li.find(".active");
		link.parent().removeClass().addClass("flaged");
		link.replaceWith("Oflagowano - " + $(this).html());
		
		$(this).closest(".box").hide();
		
		$.ajax({
			   type: "POST",
			   url: "/user/addFlag",
			   data: { id: li.attr("rel"), flag: $(this).html() }
			   
		});
	});
	
	
	
	//myNotes
	$(".remove").click(function(){
		var li = $(this).closest("li.company");
		var id = li.attr("rel");
		li.remove();
		
		$.ajax({
			   type: "POST",
			   url: "/user/removeFromNotes",
			   data: { id: id }
			   
		});
	});

	$(".minimize").unbind("click").live("click",function(){
		$(this).closest("div").siblings().toggle();
	
	});

/***********************************/
/*MODERATE*/
/***********************************/
    $(".moderate a.ok").click(function(){
	$.ajax({
	    'type':'post',
	    'url':'/admin/AFlags',
	    'data':{'act':'mod','id':$(this).closest("li").attr("rel")}

	});
    });

    $(".moderate a.del").click(function(){
	$.ajax({
	    'type':'post',
	    'url':'/admin/AFlags',
	    'data':{'act':'del','id':$(this).closest("li").attr("rel")}

	});
    });


	
}); 
 
