$(document).ready(function() { setupForum(); });


function setupForum() {
	//if dashboard exists
	if($('#bsi_forum').length) {
		//give the last <li> in each <ul> a special class
		$('ul > li:last-child').addClass('last');
		
		var sections = $('.bsi_posts');
				
		//add toggle events
		$('.bsi_thread_title').each(function(index, tog) {
			$(tog).unbind("click");
			$(tog).click(function() {
				//collapse others				  
				sections.each(function(i, section) {
					if (index != i) {
						$(section).	slideUp();	
					}
				});
				//do it
				$(sections[index]).slideToggle();	
				//return false;
			});
		});
		
	}
}

function newThread() {
	$.ajaxSetup({
 		global: false,
	  	type: "POST",
	  	ajaxStart: showOverlay('Creating New Thread' + $('#title').val() + '...'),
	  	ajaxComplete: showOverlay('Your thread has been created.')
	});
	
	$.post("bsi_forum_process.php",
	{ 
		action: 'createThread',
		user: $('#user').val(),
		title: $('#title').val(),
		content: tinyMCE.get('elm1').getContent(),
		fid: $('#fid').val()
 	}, 
 	function (data){ 
 		//reset fields
 		$('#thread_box').html(data.thread_output);
		$('#title').val('');
		$('#elm1').val('');
 	},"json");
}

function deleteThread(tid) {
	$.ajaxSetup({
 		global: false,
	  	type: "POST",
	  	ajaxStart: showOverlay('Deleting thread...'),
	  	ajaxComplete: showOverlay('This thread has been deleted.')
	});
	
	$.post("bsi_forum_process.php",
	{ 
		action: 'deleteThread',
		user: $('#user').val(),
		tid: tid,
		fid: $('#fid').val()
 	}, 
 	function (data){ 
 		//reset fields
 		$('#thread_box').html(data.thread_output);
 	},"json");
}

function newPost(tid,ele) {
	$.ajaxSetup({
 		global: false,
	  	type: "POST",
	  	ajaxStart: showOverlay('Creating New Post...'),
	  	ajaxComplete: showOverlay('Your post has been created.')
	});
	
	$.post("bsi_forum_process.php",
	{ 
		action: 'createPost',
		user: $('#user').val(),
		content: tinyMCE.get('elm1').getContent(),
		tid: tid,
		fid: $('#fid').val()
 	}, 
 	function (data){ 
 		//reset fields
 		tinyMCE.execCommand('mceRemoveControl',false,'elm1');
 		$('#'+ele).html(data.post_output);
 		tinyMCE.execCommand('mceAddControl',false,'elm1');
 	},"json");
}

function editPost(pid,ele) {
	$.ajaxSetup({
 		global: false,
	  	type: "POST",
	  	ajaxStart: showOverlay('Editing your post...'),
	  	ajaxComplete: showOverlay('Your post has been edited.')
	});
	
	$.post("bsi_forum_process.php",
	{ 
		action: 'editPost',
		user: $('#user').val(),
		content: tinyMCE.get('elm1').getContent(),
		pid: pid,
		fid: $('#fid').val()
 	}, 
 	function (data){ 
 		//reset fields
 		tinyMCE.execCommand('mceRemoveControl',false,'elm1');
 		$('.edit_fill').html('');
 		$('#'+ele).html(data.output);
 		tinyMCE.execCommand('mceAddControl',false,'elm1');
 	},"json");
}

function deletePost(pid,tid,ele) {
	tinyMCE.execCommand('mceRemoveControl',false,'elm1');
	$('.bsi_posts').html('');
	$.ajaxSetup({
 		global: false,
	  	type: "POST",
	  	ajaxStart: showOverlay('Deleting post...'),
	  	ajaxComplete: showOverlay('This post has been deleted.')
	});
	
	$.post("bsi_forum_process.php",
	{ 
		action: 'deletePost',
		user: $('#user').val(),
		pid: pid,
		tid: tid,
		fid: $('#fid').val()
 	}, 
 	function (data){ 
 		//reset fields
 		$('#'+ele).html(data.post_output);
 		tinyMCE.execCommand('mceAddControl',false,'elm1');
 		setupForum();
 	},"json");
}

function getPosts(tid,ele) {
	tinyMCE.execCommand('mceRemoveControl',false,'elm1');
	$('.bsi_posts').html('');
	$('#new_thread_box').slideUp();
	$('#new_thread_box').html('');
	$.post("bsi_forum_process.php",
	{ 
		action: 'getPosts',
		tid: tid
 	}, 
 	function (data){ 
 		//reset fields
 		$('#'+ele).html(data.post_output);
 		$('#bsi_editor_lite_replace_reply').html($('#bsi_editor_lite').html());
 		tinyMCE.execCommand('mceAddControl',false,'elm1');
 		setupForum();
 	},"json");
}

function getThreads(fid) {
	var tid = self.document.location.hash.substring(1);
	$.post("bsi_forum_process.php",
	{ 
		action: 'getThreads',
		fid: fid,
		tid: tid
 	}, 
 	function (data){ 
 		//reset fields
 		$('#thread_box').html(data.thread_output);
 		setupForum();
 	},"json");
}

function getNewThread() {
	tinyMCE.execCommand('mceRemoveControl',false,'elm1');
	$('.bsi_posts').html('');
	$('.bsi_posts').slideUp();
	$.post("bsi_forum_process.php",
	{ 
		action: 'getNewThread'
 	}, 
 	function (data){ 
 		$('#new_thread_box').html(data.output);
 		$('#bsi_editor_lite_replace_thread').html($('#bsi_editor_lite').html());
 		loadTiny();
 		tinyMCE.execCommand('mceAddControl',false,'elm1');
 	},"json");
}

function addSubscription(tid) {
	$.ajaxSetup({
 		global: false,
	  	type: "POST",
	  	ajaxStart: showOverlay('Creating Subscription...'),
	  	ajaxComplete: showOverlay('You have been subscribed to this thread.')
	});
	
	$.post("bsi_forum_process.php",
	{ 
		action: 'addSubscription',
		user: $('#user').val(),
		tid: tid,
		fid: $('#fid').val()
 	}, 
 	function (data){ 
 	},"json");
	$('#sub_link_'+tid).text('Unsubscribe');
	$('#sub_link_'+tid).attr('title','Unsubscribe to stop notifications of this thread');
	$('#sub_link_'+tid).attr('href','javascript:removeSubscription('+tid+');');
}

function removeSubscription(tid) {
	$.ajaxSetup({
 		global: false,
	  	type: "POST",
	  	ajaxStart: showOverlay('Removing Subscription...'),
	  	ajaxComplete: showOverlay('You have removed your subscription from this thread.')
	});
	
	$.post("bsi_forum_process.php",
	{ 
		action: 'removeSubscription',
		user: $('#user').val(),
		tid: tid,
		fid: $('#fid').val()
 	}, 
 	function (data){ },"json");
	$('#sub_link_'+tid).text('Subscribe');
	$('#sub_link_'+tid).attr('title','Get notified of new post in this thread');
	$('#sub_link_'+tid).attr('href','javascript:addSubscription('+tid+');');
}

function addPin(tid) {
	$.ajaxSetup({
 		global: false,
	  	type: "POST",
	  	ajaxStart: showOverlay('Pinning thread...'),
	  	ajaxComplete: showOverlay('You have pinned this thread.')
	});
	
	$.post("bsi_forum_process.php",
	{ 
		action: 'addPin',
		user: $('#user').val(),
		tid: tid,
		fid: $('#fid').val()
 	}, 
 	function (data){ 
		$('#pin_link_'+tid).text('Unpin');
		$('#pin_link_'+tid).attr('title','Remove the pin from this thread');
		$('#pin_link_'+tid).attr('href','javascript:removePin('+tid+');');
		$('#thread_box').html(data.thread_output);
 	},"json");
}

function removePin(tid) {
	$.ajaxSetup({
 		global: false,
	  	type: "POST",
	  	ajaxStart: showOverlay('Removing Pin from thread...'),
	  	ajaxComplete: showOverlay('You have removed the pin from this thread.')
	});
	
	$.post("bsi_forum_process.php",
	{ 
		action: 'removePin',
		user: $('#user').val(),
		tid: tid,
		fid: $('#fid').val()
 	}, 
 	function (data){ 
		$('#pin_link_'+tid).text('Pin');
		$('#pin_link_'+tid).attr('title','Pin this thread to the top');
		$('#pin_link_'+tid).attr('href','javascript:addPin('+tid+');');
		$('#thread_box').html(data.thread_output);
 	},"json");
}

function getQuote(pid) {
	$.post("bsi_forum_process.php",
	{ 
		action: 'getQuote',
		pid: pid
 	}, 
 	function (data){ 
 		tinyMCE.get('elm1').setContent(data.output);
 	},"json");
}

function getEdit(pid,ele) {
	tinyMCE.execCommand('mceRemoveControl',false,'elm1');
	$('.edit_fill').html('');
	$.post("bsi_forum_process.php",
	{ 
		action: 'getEdit',
		pid: pid
 	}, 
 	function (data){ 
 		$('#'+ele).html(data.output)
 		tinyMCE.execCommand('mceAddControl',false,'elm1');
 	},"json");
}

function cancelEdit(pid) {
	tinyMCE.execCommand('mceRemoveControl',false,'elm1');
	$('.edit_fill').html('');
	tinyMCE.execCommand('mceAddControl',false,'elm1');
}