Imagini.Wall = {
	"init" : function (container, ownerId, userId, postUrl, blocked) {
		Imagini.Wall.container = container;
		Imagini.Wall.posts = container.find('.wallPosts');
		Imagini.Wall.pages = container.find('.wallPages');
		Imagini.Wall.ownerId = ownerId;
		Imagini.Wall.userId = userId;
		Imagini.Wall.postUrl = postUrl;
		Imagini.Wall.blocked = blocked;
		Imagini.Wall.page = 1;
		postButton = container.find('.postButton');
		postButton.click(function() {
			var message = container.find('.wallMessage').val();
			if (message.length == 0)
			{
				Imagini.showMessage('Please enter a message');
				return false;
			}
			Imagini.Wall.container.find('.postBox').busy(true);
			$.post(Imagini.Wall.postUrl, {action: 'post', owner: ownerId, message: message}, function(result) {
				Imagini.Wall.container.find('.postBox').busy(false);
				Imagini.checkResult(result);
				container.find('.wallMessage').val("");
				Imagini.Wall.getPosts();
			}, 'json');
		});
		Imagini.Wall.getPosts();
	}, 
	"showPost" : function(postBlock) {
		postBlock.css('opacity', 0).show();

		postBlock.hover(
				function() {
					$(this).find('.postActions')
							.css('top', $(this).offset().top + 5)
							.css('left', $(this).offset().left + parseInt($(this).width()) - 40)
							.css('height', parseInt($(this).height()))
							.show();
				}, 
				function() {
					$(this).find('.postActions').hide();
				});

		if (postBlock.hasClass('wallPost') && Imagini.Wall.userId && !Imagini.Wall.blocked)
		{
			postBlock.find('.postMessage').click(function() {
				Imagini.Wall.reply($(this).parent().attr('rel'), $(this).parent());
			});
		}

		Imagini.bindEvents(postBlock);
		postBlock.animate({opacity: 1});
	}, 
	"getPosts" : function() {
		Imagini.Wall.posts.busy(true);
		Imagini.Wall.pages.busy(true);
		Imagini.Wall.posts.empty();
		Imagini.Wall.pages.empty();
		$.post(Imagini.Wall.postUrl, {action: 'get', owner: Imagini.Wall.ownerId, page: Imagini.Wall.page}, function(result) {
			Imagini.checkResult(result);
			for (var i in result.posts) {
				var postBlock = $(result.posts[i]);
				Imagini.Wall.posts.append(postBlock);
			}
			Imagini.Wall.posts.find('.wallPost').each(function() {
				Imagini.Wall.showPost($(this));
			});
			Imagini.Wall.posts.find('.postReplies').each(function() {
				$(this).find('.postReply').each(function() {
					Imagini.Wall.showPost($(this));
				});
			});

			Imagini.Wall.posts.busy(false);
			Imagini.Wall.page = result.page;
			if (result.total > 0) {
				if (result.page > 1) Imagini.Wall.pages.append('<a class="prev" href="javascript:;" onclick="return Imagini.Wall.prevPage();">previous</a>');
					else Imagini.Wall.pages.append('<span class="prev">previous</span>');
			}

			Imagini.Wall.maxPage = result.pages;
			fromPage = result.page - 5;
			toPage = result.page + 5;
			if (toPage > Imagini.Wall.maxPage) {
				toPage = Imagini.Wall.maxPage;
				fromPage = toPage - 5;
			}
			if (fromPage < 1)
				fromPage = 1;
			for (var i = fromPage; i <= toPage; i++) {
				Imagini.Wall.pages.append(' <span class="separator">|</span> ');
				if (i == result.page) Imagini.Wall.pages.append('<span>' + i + '</span>');
					else Imagini.Wall.pages.append('<a href="javascript:;" onclick="return Imagini.Wall.setPage(' + i + ');">' + i + '</a>');
			}

			if (result.total > 0) {
				Imagini.Wall.pages.append(' <span class="separator">|</span> ');
				if (result.page < Imagini.Wall.maxPage) Imagini.Wall.pages.append('<a class="next" href="javascript:;" onclick="return Imagini.Wall.nextPage();">next</a>');
					else Imagini.Wall.pages.append('<span class="next">next</span>');
			}

			Imagini.Wall.pages.busy(false);
		}, 'json');
	}, 
	"setPage" : function(page) {
		Imagini.Wall.page = page;
		Imagini.Wall.getPosts();
		return false;
	}, 
	"prevPage" : function() {
		if (Imagini.Wall.page > 1)
			Imagini.Wall.setPage(Imagini.Wall.page - 1);
		return false;
	}, 
	"nextPage" : function() {
		if (Imagini.Wall.page < Imagini.Wall.maxPage)
			Imagini.Wall.setPage(Imagini.Wall.page + 1);
		return false;
	}, 
	"reply" : function (postId, postMessage) { 
		if (postMessage.find('.replyBox').html()) {
			postMessage.find('.replyBox textarea').focus();
			return false;
		} else postMessage.parent().find('.replyBox').remove();
		if (!postId)
			return false;
		replyBox = $('<div class="replyBox"><textarea class="replyMessage"></textarea>'
					+ '<input class="replyButton greenButton" type="button" value="reply" onclick="return Imagini.Wall.postReply(' + postId + ', $(this).parent());" />'
					+ '<input class="replyCancelButton redButton" type="button" value="cancel" onclick="return Imagini.Wall.cancelReply($(this).parent().parent());" />'
					+ '<br style="clear: both;" />'
					+'</div>');
		postMessage.append(replyBox);
		replyBox.find('.replyMessage').focus();
		postMessage.find('.postActions').hide();
	}, 
	"cancelReply" : function(postMessage) {
		postMessage.find('.replyBox').remove();
		postMessage.find('.postActions').hide();
		return false;
	}, 
	"postReply" : function(postId, replyBox) {
		message = replyBox.find('.replyMessage').val()
		if (message.length == 0)
		{
			Imagini.showMessage('Please enter a message');
			return false;
		}
		$.post(Imagini.Wall.postUrl, {action: 'reply', post: postId, message: message}, function(result) {
			Imagini.checkResult(result);
			replyBox.remove();
			Imagini.Wall.getPosts();
		}, 'json');
		return false;
	}, 
	"remove" : function(postId, confirmMessage) {
		if (confirm(confirmMessage)) {
			$.post(Imagini.Wall.postUrl, {action: 'delete', post: postId}, function(result) {
				Imagini.checkResult(result);
				Imagini.Wall.getPosts();
			}, 'json');
		}
		return false;
	}, 
	"block" : function(userId, confirmMessage) {
		Imagini.Wall.container.busy(true);
		if (confirm(confirmMessage)) {
			$.post(Imagini.Wall.postUrl, {action: 'block', user: userId}, function(result) {
				Imagini.checkResult(result);
				document.location.reload();
			}, 'json');
		}
		return false;
	}
}