update_conversations = function(){
	var user_id = $(".loggedin").attr("name");
	
	$.post("/ajax/update_conversations", {user_id: user_id}, function(value) {
		var values = eval("(" + value + ")");
		
		$("#conversations a").each(function(e){
			var id = $(this).attr("id").split("_");
			
			for(var i = 0; i<values.length; i++){
				var unread_id = values[i][0].split("_")
				if(unread_id[0] == id[0] && unread_id[1] != id[1]){
					$(this).remove();
					$("#conversations .h").after(values[i][1])
					
				}
				else{
				};
			};
			
		});
		
    });
}

jQuery(function() {
	var intervalId = 0;
	intervalId = setInterval ( "update_conversations()", 60000 );
	
	$("li#make_friend a").click(function(event){
		var target = $("h1#username").parent().attr("id");
		
		$.post("/user/request_fiendship", {target: target}, function(value) {
			$(".user_nav ul li").each(function(){
				$(this).hide();
			});
			
			$("li#remove_friend_request").show();
			$("li#message").show();
		});
		event.preventDefault();
	});
	
	$("li#remove_friend_request a").click(function(event){
		var target = $("h1#username").parent().attr("id");
		
		$.post("/user/cancel_friend_request", {target: target}, function(value) {
			$(".user_nav ul li").each(function(){
				$(this).hide();
			});
			
			$("li#make_friend").show();
			$("li#message").show();
		});
		event.preventDefault();
	});
	
	$("li#break_friendship a").click(function(event){
		var target = $("h1#username").parent().attr("id");
		
		$.post("/ajax/break_friendship", {them: target}, function(value) {
			$(".user_nav ul li").each(function(){
				$(this).hide();
			});
			
			$("li#make_friend").show();
			$("li#message").show();
		});
		event.preventDefault();
	});
	
	$("#my_active_games .game").click(function(event){
		window.location = $(this).attr("name");
	});
	
});

