// mypages.js

var guestbook = {
	
	do_once:false,
	/* some social setup */
	focused:function() {
		
		$("#guestbook-just-wanted").hide();
		$("#guestbook-message-input").focus();
		
	}, //focused()
	/* unused
	nevamind:function() {
		
		if($("#guestbook-message-input").text()=="") {
			
			$("#guestbook-nevamind").show();
			setTimeout("guestbook.restore()", 2000);
			
		}
		
	}, //nevamind()
	*/
	restore:function() {
		
		if($("#guestbook-message-input").val()=="") {
			
			$("#guestbook-nevamind").hide();
			$("#guestbook-just-wanted").show();
			
		}
		
	}, //restore()
	send:function() {
		
		$(".empty-guestbook-box").remove();
		
	}, //send()
	
	post:function() {
		
		if ($("#guestbook-message-input").val()!="" && guestbook.do_once == false) {
			
			guestbook.do_once = true;
			
			$.ajax({
	
				type:"POST",
	
				url:"/apps/mypages.php",
	
				data:$("#form-add-guestbook-entry").serialize(),
	
				success:function(data) {
					
					$("#guestbook-message-input").val("");
					
					$("#NewEntry").after(data);
					
					$(".empty-guestbook-box").remove();
					
					$.ajax({
			
						type:"POST",
			
						url:"/apps/mypages.php",
			
						data:"activity=count",
			
						success:function(data) {
							
							var image = data.match(/[\d]+/g);
							
							if (parseInt(image[0]) == 1) {
							
								activity.twice();
								
							}
							
							if (parseInt(image[1]) == 1) {
								
								activity.check();
								
							}
							
							guestbook.do_once = false;
											
						}
			
					});
					
				}
	
			});
			
		}
		
	},
	
	deleter:function(id,vic) {
		
		$.ajax({

			type:"POST",

			url:"/apps/mypages.php",

			data:"id="+id+"&vic="+vic,

			success:function() {
				
				$("#entry_"+id).slideUp("fast", function () {
				
					$("#entry_"+id).remove();
				
				});
								
			}

		});
		
	}
	
};

var friends = {
	
	do_once:true,
	
	accept:function(id,vic) {
		
		if (friends.do_once) {
		
			friends.do_once = false;
		
			$.ajax({
	
				type:"POST",
	
				url:"/apps/mypages.php",
	
				data:"accept="+id+"&vic="+vic,
	
				success:function() {
					
					$("#friend_request_"+id).remove();
					
						$.ajax({
				
							type:"POST",
				
							url:"/apps/mypages.php",
				
							data:"activity=count",
				
							success:function(data) {
								
								var image = data.match(/[\d]+/g);
								
								if (parseInt(image[0]) == 1) {
								
									activity.twice();
									
								}
								
								if (parseInt(image[1]) == 1) {
									
									activity.check();
									
								}
								
								friends.do_once = true;
												
							}
				
						});
					
				}
	
			});
			
		}
		
	},
	
	deny:function(id) {
		
		$.ajax({

			type:"POST",

			url:"/apps/mypages.php",

			data:"deny="+id,

			success:function() {
				
				$("#friend_request_"+id).remove();
								
			}

		});
		
	},
	
	remove:function(id) {
		
		$.ajax({

			type:"POST",

			url:"/apps/mypages.php",

			data:"remove="+id,

			success:function() {
				
				$("#friend_row_"+id).remove();
								
			}

		});
		
	},
	
	profile_id:null
		
};

$("#profile_navi_friends_add").live("click", function() {
	$.ajax({
		type:"POST",
		url:"/apps/mypages.php",
		data:"request="+friends.profile_id,
		success:function(data) {
			$("#profile_navi_friends_add").remove();
		}
	});
});

var friends_list = {
	
	victim:false,
	
	generate:function(victim, val, page) {
		
		var hash	=	window.location.hash;
		
		if (!val) {
			if (hash) {
				val		=	hash.match(/[\w]+/i);
				page	=	hash.match(/[\d]+/i);
			} else {
				val		=	"friends";
				page	=	1;
				window.location.hash = "#"+val+"&"+page;
			}
		}
		
		$("#friend_options span").removeClass("friend_option_active");
		$("#friend_options span."+val).addClass("friend_option_active");

		
		friends_list.victim	=	victim;
		
		$("body").css("cursor", "wait");
		
		if (victim) 
			$.ajax({
		
				type:"POST",
		
				url:"/apps/mypages.php",
		
				data:"victim="+victim+"&generate="+val+"&page="+page,
		
				success:function(data) {
					
					$("#friends_js").html(data);
					$("body").css("cursor", "auto");
									
				}
		
			});
		
	}
	
};

$(".delete_friend").live("click", function() {
	var id	=	$(this).attr("value");
	$.ajax({
		type:"POST",
		url:"/apps/mypages.php",
		data:"remove="+id,
		success:function() {
			$("#friend_"+id).remove();
		}
	});
});

$(".friends_previous").live("click", function() {
	$(".friends_previous").remove();
	$(".friends_next").remove();
	var hash	=	window.location.hash;
	window.location.hash = "#"+hash.match(/[\w]+/i)+"&"+($(this).attr("value"));
	friends_list.generate(friends_list.victim);
});

$(".friends_next").live("click", function() {
	$(".friends_previous").remove();
	$(".friends_next").remove();
	var hash	=	window.location.hash;
	window.location.hash = "#"+hash.match(/[\w]+/i)+"&"+(parseInt($(this).attr("value"))+2);
	friends_list.generate(friends_list.victim);
});

$("#friend_options span").live("click", function() {
	window.location.hash = "#"+$(this).attr("value")+"&1";
	friends_list.generate(friends_list.victim);
});
