$(document).ready(function() {
	// live chat
	if ($("div#chat").length > 0) {
		var ts = new Date().getTime();
		
		live_chat_refresh(ts);
		setInterval("live_chat_refresh(new Date().getTime())", 6000);
		
		var html = '<div id="chatForm">' +
				   '<form action="/live-chat/#window" method="post">' +
				   '<p>' +
				   '<textarea name="message"></textarea><br />' +
				   '<input type="submit" name="new" class="submit" value="Send" />' +
				   '</p>' +
				   '</form>' +
				   '</div>';
		
		$("div#chat-form").html(html);
	}
	
	wall_set_remove_hover();
	wall_show_comment_box();
	wall_set_remove_click();
	wall_new_post();
	wall_view_all_comments()
	wall_new_comment();
	wall_older_posts();
	
//	var a;
//		$("div#wall div.section div.histories").each(function() {
//			a += $(this).html();
//		});
//	
//	$("div#wall div.section div.history").first().after(a);
});

function wall_set_remove_hover(e)
{
	$(e ? e : "div.main").hover(function() {
		$(this).find("div.remove").show();
	},
	function() {
		$(this).find("div.remove").hide();
	});
}

function wall_show_comment_box(e)
{
	$(e ? e : "span.comment").click(function() {
		$(this).parents("div.post").find("div.comment form").show(1, function() {
			$(this).find("textarea").focus();
		});
	});
	
	var textarea = $(e ? e.parents("div.post").find('div.comment textarea') : 'div.comment textarea');
	
	textarea.val('Write a comment...')
	
	textarea.autoResize({
		animateDuration: 300,
		extraSpace: 16,
		limit: 250
	});
	textarea.focus(function() {
		if ($(this).val() == 'Write a comment...')
			$(this).val('');
		
		$(this).parent().find("input.submit").show();
	});
	textarea.blur(function() {
		if (!$(this).val()) {
			$(this).val('Write a comment...');
			$(this).parent().find("input.submit").hide();
		}
	});
}

function wall_set_remove_click(e)
{
	$(e ? e : "span.remove").click(function() {
		if (confirm("Are you sure you want to delete this post?")) {
			var remove = $(this);
			var info = (remove.attr('id')).split("-");
			
			var type = info[0];
			var id   = info[1];
			
			$.ajax({
				url: "/ajax-remove-post/",
				type: "POST",
				data: ({
					type: type,
					id: id
				}),
				dataType: "json",
				success: function(json){
					if (!json['error']) {
						if (type == 'post')
							var parent = remove.parents("div.post");
						else if (type == 'history')
							var parent = remove.parents("div.history");
						else if (type == 'comment')
							var parent = remove.parents("div.comment div.post");
						
						parent.animate({ height: 0, opacity: 0 }, "slow", function() {
							parent.remove();
						});
					}
					else
						alert(json['error']);
				}
			});
		}
	});
}

function wall_view_all_comments(e)
{
	$(e ? e : "span.view-all").click(function() {
		var view = $(this);
		
		view.unbind("click");
		
		var info = (view.attr('id')).split("-");
		
		var userId = info[0];
		var postId = info[1];
		var num    = info[2];
		
		$.ajax({
			url: "/ajax-view-all-comments/",
			type: "POST",
			data: ({
				userId: userId,
				postId: postId,
				num: num
			}),
			dataType: "json",
			success: function(json){
				if (!json['error']) {
					var comments = view.parent();
					
					view.animate({ height: 0, paddingTop: 0, paddingBottom: 0 }, "slow", function() {
						$(this).remove();
					});
					
					$(comments).prepend(json['post']);
					
					$(comments).find("div.new").animate({ height: 'toggle' }, "slow", function() {
						wall_set_remove_click($(this).find("span.remove"));
						
						$(this).removeClass("new");
					});
				}
				else
					alert(json['error']);
			}
		});
	});
}

function wall_new_comment(e)
{
	$(e ? e : "input.comment").click(function() {
		var button = $(this);
		
		var textarea = $(this).parent().find("textarea");
		
		var userId = $(this).parent().find("input[name=userId]").val();
		var postId = $(this).parent().find("input[name=postId]").val();
		var comment = textarea.val();
		
		if (comment) {
			$(this).attr('disabled', 'disabled');
			$(this).addClass('submitDisabled');
			
			$.ajax({
				url: "/ajax-new-comment/",
				type: "POST",
				data: ({
					userId: userId,
					postId: postId,
					comment: comment
				}),
				dataType: "json",
				success: function(json){
					if (!json['error']) {
						textarea.val('Write a comment...');
						
						var form = textarea.parents("div.comment").find("form");
						
						$(form).before(json['post']);
						
						$("div#posts").find("div.comment div.new").fadeIn("slow", function(){
							wall_set_remove_click($(this).find("span.remove"));
							
							$(this).removeClass("new");
						});
					}
					else
						alert(json['error']);
					
					button.attr('disabled', '').delay(1000);
					button.removeClass('submitDisabled').delay(1000);
				}
			});
		}
	});
}

function wall_new_post()
{
	$("input#share").click(function() {
		var button = $(this);
		
		var textarea = $(this).parent().find("textarea");
		
		var userId = $(this).parent().find("input[name=userId]").val();
		var comment = textarea.val();
		
		if (comment && comment != "Share a message...") {
			$(this).attr('disabled', 'disabled');
			$(this).addClass('submitDisabled');
			
			$.ajax({
				url: "/ajax-new-post/",
				type: "POST",
				data: ({
					userId: userId,
					comment: comment
				}),
				dataType: "json",
				success: function(json){
					if (!json['error']) {
						textarea.val('Share a message...');
						
						$("div#posts").prepend(json['post']);
						
						$("div#posts div.new").fadeIn("slow", function(){
							wall_set_remove_hover($(this).find("div.main"));
							wall_show_comment_box($(this).find("span.comment"));
							wall_set_remove_click($(this).find("span.remove"));
							wall_new_comment($(this).find("input.comment"));
							
							$(this).removeClass("new");
						});
					}
					else
						alert(json['error']);
					
					button.attr('disabled', '').delay(1000);
					button.removeClass('submitDisabled').delay(1000);
				}
			});
		}
	});
	
	var textarea = $('div#wall p textarea');
	
	textarea.val('Share a message...');
	 
	textarea.autoResize({
		animateDuration: 300,
		extraSpace: 16,
		limit: 250
	});
	textarea.focus(function() {
		$(this).val() == 'Share a message...' ? $(this).val('') : '';
	});
	textarea.blur(function() {
		$(this).val() ? '' : $(this).val('Share a message...');
	});
}

function wall_older_posts()
{
	$("span.older-posts").click(function() {
		var older = $(this);
		
		older.unbind("click");
		
		var info = (older.attr('id')).split("-");
		
		var userId = info[0];
		var postId = info[1];
		var histId = info[2];
		
		$.ajax({
			url: "/ajax-older-posts/",
			type: "POST",
			data: ({
				userId: userId,
				postId: postId,
				histId: histId
			}),
			dataType: "json",
			success: function(json){
				if (!json['error']) {
					older.remove();
					
					$("div#posts").append(json['posts']);
					
					$("div#posts div.new").fadeIn("slow", function(){
						wall_set_remove_hover($(this).find("div.main"));
						wall_show_comment_box($(this).find("span.comment"));
						wall_set_remove_click($(this).find("span.remove"));
						wall_new_comment($(this).find("input.comment"));
						wall_view_all_comments($(this).find("span.view-all"))
						
						$(this).removeClass("new");
					});
					
					wall_older_posts();
				}
				else
					alert(json['error']);
			}
		});
	});
}

