// New thread form
function addThreadForm(form) {
	var roomid = form.roomid;
	var title = form.title;
	var text = form.text;
	var icon = $('#form_add_thread :input[name=icon]:checked').val();
	
	// Title field empty
	if(trim(title.value) == "") {
		$(title).css('background', '#FBE3E4').bind('keydown', function(){
			$(this).css('background', '#FFFFFF');
		});
		$('#' + form.id).find('.form_msg').fadeOut('slow', function() {
			$(this).html('Please enter a title.').addClass('error').fadeIn('slow');
		});
		return false;
		
	// Text field empty
	} else if(trim(text.value) == "") {
		$('#' + form.id).find('.form_msg').fadeOut('slow', function() {
			$(this).html('Please enter some content.').addClass('error').fadeIn('slow');
		});
		return false;
	
	// Success!
	} else {
		$.post('/forum/createPost',
			{roomid: roomid.value, title: title.value, text: text.value, icon: icon},
			function(data) {
//				if (data == '') {
					document.location = '/forum/thread/' + data;
//				} else {
//					$('#' + form.id).find('.form_msg').fadeOut('slow', function() {
//						$(this).html(data).removeClass('success').addClass('error').fadeIn('slow');
//					});
//				}
			});
	}

	return false;
}


// Forum thread reply form
function forumThreadReplyForm(form) {
	var roomid = form.roomid;
	var threadid = form.threadid;
	var replyid = form.replyid;
	var comment = form.comment;
	
	// Comment field empty
	if (trim(comment.value) == "") {
		$('#' + form.id).find('.form_msg').fadeOut('slow', function() {
			$(this).html('Please enter a reply.').addClass('error').fadeIn('slow');
		});
		return false;
	
	// Success!
	} else {
		$.post('/forum/replyPost',
			{roomid: roomid.value, threadid: threadid.value, replyid: replyid.value, comment: comment.value},
			function(data) {
//				if (data == '') {
					document.location = '/forum/thread/' + data;
//					$('#' + form.id).find('.form_msg').fadeOut('slow', function() {
//						$(this).html('Thanks for your reply!').removeClass('error').addClass('success').fadeIn('slow');
//					});
//				} else {
//					$('#' + form.id).find('.form_msg').fadeOut('slow', function() {
//						$(this).html(data).removeClass('success').addClass('error').fadeIn('slow');
//					});
//				}
			});
	}
	
	return false;
}

// Edit a forum thread
function editThreadForm(form) {
	var threadid = form.threadid;
	var seourl = form.seourl;
	var text = form.text;
	var icon = $('#form_edit_thread :input[name=icon]:checked').val();
	
	// Text field empty
	if(trim(text.value) == "") {
		$('#' + form.id).find('.form_msg').fadeOut('slow', function() {
			$(this).html('Please enter some content.').addClass('error').fadeIn('slow');
		});
		return false;
	
	// Success!
	} else {
		$.post('/forum/editThreadPost',
			{threadid: threadid.value, text: text.value, icon: icon},
			function(data) {
				document.location = '/forum/thread/' + seourl.value;
			});
	}
	
	return false;
}

// Edit a forum post
function editPostForm(form) {
	var postid = form.postid;
	var seourl = form.seourl;
	var text = form.text;
	
	// Text field empty
	if(trim(text.value) == "") {
		$('#' + form.id).find('.form_msg').fadeOut('slow', function() {
			$(this).html('Please enter some content.').addClass('error').fadeIn('slow');
		});
		return false;
	
	// Success!
	} else {
		$.post('/forum/editPost',
			{postid: postid.value, text: text.value},
			function(data) {
				document.location = '/forum/thread/' + seourl.value;
			});
	}
	
	return false;
}

// Remove a forum post
function forumPostRemove(postid) {
	var answer = confirm('Are you sure you want to remove this post?');
	
	if (answer) {
		$.post('/forum/removePost',
		{postid: postid},
		function(data){
			$('#post-' + postid).fadeOut('slow');
		});
	}
	
	return false;
}

// Reply to a forum post
function forumPostReply(postid) {
	$('#forum_thread_reply :input[name=replyid]').val(postid);
	$('#forum_thread_reply_message')
		.html('Replying to <a href="#' + postid + '" title="View post">Post #' + postid + '</a>... ' + 
			'<button type="button" class="button negative" onclick="clearPostReply()">Nevermind</button>')
		.show();
}

// Clear post reply message
function clearPostReply() {
	$('#forum_thread_reply :input[name=replyid]').val(0);
	$('#forum_thread_reply_message').html('');
}

// Check for news posts on 'thread' pages
function checkForNewPosts(seourl) {
	$.post('/forum/checkForNewPosts',
		{seourl: seourl},
		function(data) {
			if (data['posts'] > 0) {
				$('#new_posts_msg').html(data['posts'] + ' new post' + (data['posts'] > 1 ? 's' : '') + '! Scroll down to load them.').fadeIn('slow');
				$('#new_posts_btn').html("<button type=\"button\" class=\"button positive\" onclick=\"loadNewPosts('" + seourl + "')\">" + data['posts'] + " new post" + (data['posts'] > 1 ? 's' : '') + "! Click to load them.</button>").fadeIn('slow')
			}
			
			// Refresh 'Who is Where?' display
			$('#who_is_where').html(data['users']);
		}, 'json');
	
	// Check again in 20 seconds
	setTimeout("checkForNewPosts('" + seourl + "')", 20000);
}

// Load new posts to 'thread' page
function loadNewPosts(seourl) {
	$('#new_posts_msg').html('').hide();
	$('#new_posts_btn').html('').hide();
	
	$.post('/forum/loadNewPosts',
		{seourl: seourl},
		function(data) {
			$('#forum_post_replies').find('.forum_post_reply:last').after(data);
		});
}

$(document).ready(function() {
	
	// Check for new posts on 'thread' pages
	var url_array = location.pathname.split("/");
	
	if (url_array[2] == 'thread') {		
		setTimeout("checkForNewPosts('" + url_array[3] + "')", 20000);
	}
	
	// jWYSIWYG instance
	$('.wysiwyg').wysiwyg({
		controls: {
			strikeThrough: {visible: true},
			underline: {visible: true},
			separator03: {visible: true},
			undo: {visible: true},
			redo: {visible: true},
			separator04: {visible: true},
			insertOrderedList: {visible: true},
			insertUnorderedList: {visible: true},
			separator06: {visible: true},
			increaseFontSize: {visible: false},
			decreaseFontSize: {visible: false},
			separator08: {visible: false},
			separator09: {visible: false},
			separator10: {visible: false}
		}
	});
});

