

var inner;
var upload_type;
var current_subs;
var viewing_thread;
var timer 		= setInterval(timerMethod, 120000); // Timer for auto-save. In milliseconds.
var functions_path	= program_url + "/functions";
var js_path		= program_url + "/js";
var current_showing	= current_category_id;


// ---------------------------------------------------------------------------------------
//	Document Ready Functions

$(document).ready(function() { 
        $(document).keyup(function(event) { 
                if (event.keyCode == 13) {
                    if ($('#bd_captcha_input').val()) {
                    	processCaptcha();
                    }
                	else if ($('#login_1').val() && $('#login_2').val()) {
                    	processLogin();
                    }
                    else if ($('#lost_email').val()) {
                    	processLostPass();
                    }
                }
        })
});


// ------------------------------------------
//	Upvote/downvote comment

function vote_comment(id,value) {
	js_path_put = functions_path + "/ajax.php";
	send_data = "action=vote_comment&comment=" + id + "&vote=" + value;
    	$.post(js_path_put, send_data, function(theResponse) {
		var returned = theResponse.split('+++');
		if (returned['0'] == "1") {
			if (value > 0) {
				$('#votedUp' + id).addClass('bd_voted');
				$('#votedDown' + id).removeClass('bd_voted');
			} else {
				$('#votedDown' + id).addClass('bd_voted');
				$('#votedUp' + id).removeClass('bd_voted');
			}
			$('#voteTotal' + id).html(returned['2']);
			closeError();
		} else {
			process_error(returned['1']);
		}
    	});
}

// ------------------------------------------
//	Ban a user

function userBan(username,comment_id) {
	js_path_put = functions_path + "/ajax.php";
	send_data = "action=ban_user&username=" + username + "&comment_id=" + comment_id;
    	$.post(js_path_put, send_data, function(theResponse) {
		var returned = theResponse.split('+++');
		if (returned['0'] == "1") {
			alert('User has been banned.');
			closeError();
		} else {
			process_error(returned['1']);
		}
    	});
}

// ------------------------------------------
//	Edit a comment

function editComment(comment_id) {
	js_path_put = functions_path + "/ajax.php";
	send_data = "action=edit_comment&comment=" + $('#edit' + comment_id).val() + "&comment_id=" + comment_id;
    	$.post(js_path_put, send_data, function(theResponse) {
		var returned = theResponse.split('+++');
		if (returned['0'] == "1") {
		   	$('#showEdit' + comment_id).fadeOut('300', function () {
		   		$('#commentMain' + comment_id).html(returned['1']);
	   			$('#showReplyTop' + comment_id).fadeIn('300');
	   			$('#commentMain' + comment_id).fadeIn('300');
	   		});
			closeError();
		} else {
			process_error(returned['1']);
		}
    	});
}

// ------------------------------------------
//	Delete a comment

function commentDelete(comment_id) {
	if (confirm('Confirm deletion...')) {
		js_path_put = functions_path + "/ajax.php";
		send_data = "action=del_comment&comment_id=" + comment_id;
	    	$.post(js_path_put, send_data, function(theResponse) {
			var returned = theResponse.split('+++');
			if (returned['0'] == "1") {
				if (returned['1'] == "2") {
					$('#comment' + comment_id).fadeOut('300', function () {
						$('#comment' + comment_id).remove();
		   			});
				} else {
					$('#commentMain' + comment_id).html('<span class="deleted_comment">Deleted</span>');
				}
				closeError();
			} else {
				process_error(returned['1']);
			}
	    	});
    	}
}

// ------------------------------------------
//	Approve a comment
//	Only available for users with
//	the correct privileges.

function approveComment(comment_id) {
	js_path_put = functions_path + "/ajax.php";
	send_data = "action=approve_comment&comment_id=" + comment_id;
    	$.post(js_path_put, send_data, function(theResponse) {
		var returned = theResponse.split('+++');
		if (returned['0'] == "1") {
			$('#pending' + comment_id).fadeOut('300');
			closeError();
		} else {
			process_error(returned['1']);
		}
    	});
}

// ------------------------------------------
//	Posts a comment
//	comment_id is for replies

function postComment(article,comment_id) {
	// For "Forum" style threaded comments
	if (viewing_thread !== undefined) {
		comment_id = viewing_thread;
		//replace_box = "showReply" + comment_id;
		text = $('#commentText').val();
	} else {
		if (comment_id) {
			text = $('#reply' + comment_id).val();
			//replace_box = "showReply" + comment_id;
		} else {
			text = $('#commentText').val();
			//replace_box = "bd_comment_box";
		}
	}
	js_path_put = functions_path + "/ajax.php";
	send_data = "action=post_comment&article=" + article + "&comment=" + text + "&comment_id=" + comment_id;
    	$.post(js_path_put, send_data, function(theResponse) {
		var returned = theResponse.split('+++');
		if (returned['0'] == "1") {
			$('#bd_no_comments').hide();
			$('#commentText').val('');
			closeError();
			if (viewing_thread === undefined) {
				if (comment_id) {
					sub_com_put = "showReplyTop" + comment_id;
					$('#' + sub_com_put).append(returned['1']).fadeIn(150).focus();
					cancelReply(comment_id);
					return false;
				} else {
					$('#bd_all_comments').append(returned['1']).hide().fadeIn(150).focus();
					return false;
				}
			} else {
				$('#bd_all_comments').append(returned['1']).hide().fadeIn(150).focus();
				// $('#' + replace_box).html(returned['1']);
				return false;
			}
		} else {
			process_error(returned['1']);
		}
    	});
}

// ------------------------------------------
//	Process login.

function processLogin() {
	js_path_put = functions_path + "/ajax.php";
	send_data = "action=login&username=" + $('#login_1').val() + "&password=" + $('#login_2').val() + "&remember_me=" + $('#login_3').val() + "&c=" + $('#bd_c_field_login');
    	$.post(js_path_put, send_data, function(theResponse) {
		var returned = theResponse.split('+++');
		if (returned['0'] == "1") {
			send_data = "action=get_template&name=logged_in_sidebar&user=" + $('#login_1').val();
		    	$.post(js_path_put, send_data, function(inner) {
				$('#bd_logged_session').fadeOut('600', function () {
					$('#bd_logged_session').html(inner);
					$('#bd_logged_session').fadeIn('600');
					// Re-work the article
					runArticleReProcess('0');
					getManageBar();
				});
		    	});
			closeError();
			closeCaptcha();
		} else {
			process_error(returned['1'],returned['2']);
		}
    	});
}

// ------------------------------------------
//	Renders the management bar.

function getManageBar() {
	js_path_put = functions_path + "/ajax.php";
	send_data = 'action=getManageBar&article=' + current_article_id;
	$.post(js_path_put, send_data, function(theResponse) {
		$('body').fadeIn('300').append(theResponse);
	});
}

// ------------------------------------------
//	Re-process article

function runArticleReProcess(logout) {
      if (current_article_id) {
      	// Now redo the article!
      	js_path_put = functions_path + "/process_article.php";
      	send_dataA = "id=" + current_article_id;
          	$.post(js_path_put, send_dataA, function(innerA) {
          		if (innerA) {
				var returned = innerA.split('|||||');
	      	    		$('#primary_article_holder').fadeOut('600', function () {
	      				$('#primary_article_holder').html(returned['0']);
	      				$('#primary_article_holder').fadeIn('600');
	      	    		});
	      	    		// Comments
	      	    		if (returned['1']) {
		      	    		$('#primary_comment_holder').fadeOut('600', function () {
			      	    		if (logout != "1") {
		      	    				$('#commentText').attr("disabled", false);
		      	    				$('#commentSubmit').attr("disabled", false);
		      	    				$('#commentText').val('');
		      	    			} else {
		      	    				$('#commentText').attr("disabled", true);
		      	    				$('#commentSubmit').attr("disabled", true);
		      	    				$('#commentText').val('Login to comment.');
		      	    			}
		      				$('#primary_comment_holder').html(returned['1']);
		      				$('#primary_comment_holder').fadeIn('600');
		      	    		});
	      	    		}
          		}
          	});
  	}
}

// ------------------------------------------
//	Process CAPTCHA

function processCaptcha() {
	js_path_put = functions_path + "/ajax.php";
	send_data = "action=check_captcha&c=" + $('#bd_captcha_input').val();
    	$.post(js_path_put, send_data, function(theResponse) {
		var returned = theResponse.split('+++');
		if (returned['0'] == "1") {
			$('#bd_logged_session').fadeOut('600');
			closeError();
		} else {
			if (returned['2'] == "2") {
				window.location.reload();
			}
			else if (returned['2'] == "3") {
				closeCaptcha();
			}
			else {
				process_error(returned['1'],'1');
			}
		}
    	});
}

// ------------------------------------------
//	Process lost password.

function processLostPass() {
	js_path_put = functions_path + "/ajax.php";
	send_data = "action=lost_pass&email=" + $('#lost_email').val() + "&c=" + $('#bd_c_field_lost_pass');
    	$.post(js_path_put, send_data, function(theResponse) {
		var returned = theResponse.split('+++');
		if (returned['0'] == "1") {
			inner = "Your new password has been sent. <a href=\"#\" onClick=\"showLogin();return false;\">Click here</a> to login.";
			$('#bd_logged_session').fadeOut('600', function () {
				$('#bd_logged_session').html(inner);
				$('#bd_logged_session').fadeIn('600');
			}
			);
			closeError();
			closeCaptcha();
		} else {
			process_error(returned['1'],returned['2']);
		}
    	});
}


// ------------------------------------------
//	Favorite an article

function addFavorite(article,add) {
	js_path_put = functions_path + "/ajax.php";
	send_data = "action=favorite&article=" + article + "&add=" + add;
    	$.post(js_path_put, send_data, function(theResponse) {
		var returned = theResponse.split('+++');
		if (returned['0'] == "1") {
			showSaved(returned['1']);
			$('#favorite_img').attr('src',returned['2']);
			number = parseInt($('#article_favorites').html());
			returned = parseInt(returned['3']);
			together = number + returned;
			$('#article_favorites').html(together);
			closeError();
		} else {
			process_error(returned['1']);
		}
    	});
}

// ------------------------------------------
//	Follow an article

function addFollow(article,add) {
	js_path_put = functions_path + "/ajax.php";
	send_data = "action=follow&article=" + article + "&add=" + add;
    	$.post(js_path_put, send_data, function(theResponse) {
		var returned = theResponse.split('+++');
		if (returned['0'] == "1") {
			showSaved(returned['1']);
			$('#follow_img').attr('src',returned['2']);
			number = parseInt($('#article_follows').html());
			returned = parseInt(returned['3']);
			together = number + returned;
			$('#article_follows').html(together);
			closeError();
		} else {
			process_error(returned['1']);
		}
    	});
}

// ------------------------------------------
//	E-Mail Suggest Article

function emailArticle(article) {
	js_path_put = functions_path + "/ajax.php";
	send_data = "action=get_template&name=email_article";
	$.post(js_path_put, send_data, function(inner) {
		$('#bd_captcha').html(inner);
		$('#bd_captcha').show('fast');
	});
}

// ------------------------------------------
//	Process registration.

function processEmailFriend() {
	js_path_put = functions_path + "/ajax.php";
	send_data = "article=" + current_article_id + "&" + $('form#email_form').serialize();
    	$.post(js_path_put, send_data, function(theResponse) {
		var returned = theResponse.split('+++');
		if (returned['0'] == "1") {
			closeError();
			closeCaptcha();
			showSaved(returned['1']);
		} else {
			process_error(returned['1']);
		}
    	});
}

// ------------------------------------------
//	Process registration.

function processRegistration() {
	if (allow_registration == "1") {
		js_path_put = functions_path + "/ajax.php";
		send_data = $('form#register_form').serialize();
	    	$.post(js_path_put, send_data, function(theResponse) {
			var returned = theResponse.split('+++');
			if (returned['0'] == "1") {
				send_data = "action=get_template&category=" + current_category_id + "&name=logged_in_sidebar&user=" + $('#reg_username').val();
			    	$.post(js_path_put, send_data, function(inner) {
					$('#bd_logged_session').fadeOut('600', function () {
						$('#bd_logged_session').html(inner);
						$('#bd_logged_session').fadeIn('600');
						// Reprocess article
						runArticleReProcess('0');
					});
			    	});
				closeError();
				closeCaptcha();
			} else {
				process_error(returned['1'],returned['2']);
				if (returned['2']) {
					// Highlight fields
					var fields = returned['2'].split('--');
					var len = fields.length;
					for (var i=0; i<len; i++) {
						$('#reg_' + fields[i]).addClass('bd_error_field');
					}
				}
			}
	    	});
	} else {
		process_error('Registration has been disabled.');
    	}
}

function clearText(element,default_text) {
	if (element.value == default_text) {
		element.value = '';
	}
}

// ------------------------------------------
//	Logs a user out.

function logout() {
	js_path_put = functions_path + "/ajax.php";
	send_data = "action=logout";
    	$.post(js_path_put, send_data, function(theResponse) {
		var returned = theResponse.split('+++');
		if (returned['0'] == "1") {
	   		inner = "Welcome guest! (<a href=\"#\" onClick=\"showLogin();return false;\">Login</a> or <a href=\"#\" onClick=\"showRegister();return false;\">Register</a>)";
	   		$('#bd_logged_session').fadeOut('600', function () {
	   			$('#bd_logged_session').html(inner);
	   			$('#bd_logged_session').fadeIn('600');
	   			$('ul.bd_options').fadeOut('300');
	   			$('#bd_manage_notices').fadeOut('300');
      				// Re-work the article
				runArticleReProcess('1');
				closeArticleEdit();
				closePreview();
	   		}
	   		);
   		} else {
			process_error(returned['1']);
   		}
    	});
}

// ---------------------------------------------------------------------------------------
//	Inline article editing

var editingArticle;

function triggerEdit() {
	needToConfirm = true;
}

function triggerNoEdit() {
	needToConfirm = false;
}

function minimizeEdit(article_name) {
	$('#bd_article_inline_edit').fadeOut('fast');
	item = "<li class=\"article\" id=\"minEdit\"><a href=\"#\" onclick=\"maximizeEdit();\"><img src=\"" + theme + "/imgs/icon-maximize.png\" width=\"16\" height=\"16\" border=\"0\" style=\"vertical-align:middle;margin-right:4px;\" />Editing \"" + article_name + "\"</a></li>";
	$('.bd_options').append(item);
	needToConfirm = true;
}

function maximizeEdit() {
	$('#bd_article_inline_edit').fadeIn('fast');
	$('#minEdit').remove();
	needToConfirm = false;
}

function showAddLink(type) {
	html = "<div id=\"bd_addlink\"><p class=\"bd_small\" style=\"float: right;\"><a href=\"#\" onclick=\"showAddLink('internal');return false;\">Internal Link</a><br /><a href=\"#\" onclick=\"showAddLink('external');return false;\">External Link</a></p>";
	if (type == 'internal') {
		html += "<h2>Internal Link</h2><label>Link Display Text<p class=\"bd_small\" style=\"margin: -5px 0 0 0;\">Optional: controls what is displayed to visitors on the page for this link.</p></label><input type=\"text\" name=\"addLinkName\" id=\"addLinkName\" /><label>Webpage Name<p class=\"bd_small\" style=\"margin: -5px 0 0 0;\">Start typing an existing page's name. Select from the returned suggestions to complete the process.</p></label><input type=\"text\" name=\"addLink\" id=\"addLink\" onkeyup=\"suggest('articles',this.value,'name','addLink','id','category,id','linking');\" />";
	} else {
		html += "<h2>External Link</h2><label>Link Display Text<p class=\"bd_small\" style=\"margin: -5px 0 0 0;\">Optional: controls what is displayed to visitors on the page for this link.</p></label><input type=\"text\" name=\"addLinkName\" id=\"addLinkName\" /><label>Link URL</label><input type=\"text\" name=\"addLinkURL\" id=\"addLinkURL\" /><input type=\"button\" value=\"Add Link\" onclick=\"addExternalLink();\" />";
	}
	html += "<p class=\"bd_small bd_center\"><a href=\"#\" onclick=\"closeCaptcha();return false;\">Cancel</a></p></div>";
      	$('#bd_captcha').html(html);
      	$('#bd_captcha').show('fast');
}

function addExternalLink() {
	if ($('#addLinkName').val()) {
		put = "[[" + $('#addLinkURL').val() + "|" + $('#addLinkName').val() + "]]";
	} else {
		put = "[[" + $('#addLinkURL').val() + "]]";
	}
	addCaller('content',put);
	closeCaptcha();
}

function editArticle(id) {
	if ($('#bd_article_inline_edit').is(":visible")) {
      		triggerNoEdit();
		$('#bd_manage_article_edit').css('background-color','');
		$('#bd_manage_article_new').css('background-color','');
		$('#bd_article_inline_edit').fadeOut('250');
      		clearInterval(timer);
      		editingArticle = '';
	}
	else {
		// New article
		js_path_put = functions_path + "/ajax.php";
		send_data = "action=generateEditArticle&id=" + id + "&category=" + current_category_id;
	      	$.post(js_path_put, send_data, function(theResponse) {
			var returned = theResponse.split('+++');
			if (returned['0'] == '1') {
				editingArticle = id;
				// Browser window
				var h = $(window).height();
				var w = $(window).width();
				var difference = h - 29;
				$('#bd_article_inline_edit').css('height',difference);
		  		$('#bd_article_inline_edit').html(returned['1']);
				var text_dif = h - 160;
				var text_dif_w = w - 40;
				$('#content').css('height',text_dif);
				$('#content').css('width',text_dif_w);
		  		$('#bd_article_inline_edit').fadeIn('250');
				var text_difx = h - 120;
				var text_dif_x = w - 50;
	   			$('#bd_article_preview').css('height',text_difx);
	   			$('#bd_article_preview').css('width',text_dif_x);
	   			if (id == 'new') {
	   				$('#bd_manage_article_edit').css('background-color','');
	   				$('#bd_manage_article_new').css('background-color','#000');
	   			} else {
	   				$('#bd_manage_article_edit').css('background-color','#000');
	   				$('#bd_manage_article_new').css('background-color','');
	   			}
	   			$('#minEdit').remove();
	  		} else {
				process_error(returned['1']);
	  		}
	      	});
      	}
}

function previewArticle() {
	if ($('#bd_article_preview').is(":visible")) {
		closePreview();
	} else {
		js_path_put = functions_path + "/ajax.php";
		send_data = "action=previewArticle&id=" + editingArticle + "&" + $('#bd_articleEdit').serialize();
	      	$.post(js_path_put, send_data, function(theResponse) {
			$('#bd_article_preview').html(theResponse);
		   	$('#bd_aie_body').fadeOut('300', function () {
	   			$('#bd_article_preview').fadeIn('300')
				$('#bd_aie_saved').html('<a href="#" onclick="closePreview();return false;">Previewing Article</a>');
				$("#bd_air_preview_button").attr('value', 'Close Preview');
	   		});
	      	});
      	}
}

function closePreview() {
     	$('#bd_article_preview').fadeOut('300', function () {
    		$('#bd_aie_body').fadeIn('300')
  		$('#bd_article_preview').html('');
		$('#bd_aie_saved').html('');
		$("#bd_air_preview_button").attr('value', 'Preview');
    	});
}

function saveArticle(done,autosave) {
	js_path_put = functions_path + "/ajax.php";
	send_data = "action=editArticle&id=" + editingArticle + "&done=" + done + "&" + $('#bd_articleEdit').serialize();
      	$.post(js_path_put, send_data, function(theResponse) {
		var returned = theResponse.split('+++');
		if (returned['0'] == '1') {
			triggerNoEdit();
			if (editingArticle == 'new') {
				window.location = returned['1'];
			} else {
				if (done != '1') {
					if (autosave == '1') {
						showSaved('Auto-saved.');
					} else {
						showSaved('Saved!');
					}
				} else {
					clearInterval(timer);
					editingArticle = '';
	      				// Re-work the article.
					runArticleReProcess('0');
					// Close the edit.
					closeArticleEdit();
				}
			}
  		} else {
			process_error(returned['1']);
  		}
      	});
}

function closeMenus(exclude) {
	if (exclude != 'options') {
		$('#bd_aie_article_options').hide();
		$('#baie_options').css('background-color','');
	}
	if (exclude != 'meta') {
		$('#bd_aie_article_meta').hide();
		$('#baie_meta').css('background-color','');
	}
}

function showArticleOptions() {
	closeMenus('options');
	var offset = $('#baie_options').offset();
	var top = offset.top + 28;
	var left = offset.left - 1;
	$('#bd_aie_article_options').css('top',top);
	$('#bd_aie_article_options').css('left',left);
	$('#bd_aie_article_options').toggle();
	if ($('#bd_aie_article_options').is(":visible")) {
		$('#baie_options').css('background-color','#25292C');
	} else {
		$('#baie_options').css('background-color','');
	}
}

function showArticleMeta() {
	closeMenus('meta');
	var offset = $('#baie_meta').offset();
	var top = offset.top + 28;
	var left = offset.left - 1;
	$('#bd_aie_article_meta').css('top',top);
	$('#bd_aie_article_meta').css('left',left);
	$('#bd_aie_article_meta').toggle();
	if ($('#bd_aie_article_meta').is(":visible")) {
		$('#baie_meta').css('background-color','#25292C');
	} else {
		$('#baie_meta').css('background-color','');
	}
}

function closeArticleEdit() {
	closeMenus();
	$('#bd_article_inline_edit').fadeOut('200');
	$('#bd_article_inline_edit').html();
	editingArticle = '';
}

//	Auto-save
function timerMethod() {
    saveArticle('0','1');
}


// --------------------------------------------
// Show Upload Box
// type 1 = file
// type 2 = image
function showUpload(type,path,article,id) {
	upload_type = type;
	// Upload HTML
	final_html = '<div id="bd_upload_box">';
	final_html += '<form action="' + functions_path + '/upload_file.php" method="post" enctype="multipart/form-data" onsubmit="confirmUpload();">';
	final_html += '<p style=\"float:right;\"><a href="#" onclick="closeDiv(\'bd_upload_box\');return false;">Close</a></p><h1 class="bd_h1">File Not Found</h1><p>We were unable to find this file on your server. Please either upload it using the form provided below or upload it directly to your server using an FTP client.</p>';
	final_html += '<h2 class="bd_h2" style="margin-top:25px;">Select the file</h2><input type="hidden" name="article" value="' + article + '" /><input type="hidden" name="type" value="' + type + '" /><input type="hidden" name="path" id="upload_path" value="' + path + '" /><input type="hidden" name="id" id="upload_id" value="' + id + '" />';
	final_html += '<input type="file" name="file" id="file_input" />';
	if (type == '1') {
		final_html += '<h2 class="bd_h2" style="margin-top:25px;">Additional Options</h2>';
		final_html += '<p><b>Require that users be logged in to download?</b><br /><input type="radio" name="login" value="1" /> Yes <input type="radio" name="login" value="0" checked="checked" /> No<br /><br />';
		final_html += '<b>Limit downloads per person (0 = unlimited)?</b><br /><input type="text" name="limit" value="0" style="width:100px;" /></p>';
	}
	final_html += '<input type="submit" name="submit" value="Upload" /><input type="button" value="Cancel" onclick="closeArticleEdit();" />';
	final_html += '</form>';
	final_html += '</div>';
    	// Browser window
    	var h = $(window).height();
    	var w = $(window).width();
    	var difference = h - 29;
    	$('#bd_article_inline_edit').css('height',difference);
      	$('#bd_article_inline_edit').html(final_html);
      	$('#bd_article_inline_edit').fadeIn('250');
}


// Used before the upload is submitted to the server.
// upload_file.php will run some checks to ensure
// that this user can upload this file.
function confirmUpload() {
      	if (editingArticle) {
      		saveArticle('0');
      	}
	js_path_put = functions_path + "/upload_file.php";
	send_data = "action=confirm&id=" + $('#upload_id').val() + "path=" + $('#upload_path').val() + "&type=" + upload_type + "&file=" + $('#file_input').val();
    	$.post(js_path_put, send_data, function(theResponse) {
		var returned = theResponse.split('+++');
		// Continue with submission
		if (returned['0'] == '1') {
			return true;
		}
		// Prevent form submission
		else {
			process_error(returned['1']);
			return false;
		}
    	});
    	return false;
}


// ---------------------------------------------------------------------------------------
//	Display divs

function expandDiscussion(id) {
	   	$('#subcommentsTop' + id).fadeOut('300', function () {
   			$('#subcomments' + id).fadeIn('300')
   		});
}

function commentReply(id) {
	   	$('#showReplyTop' + id).fadeOut('300', function () {
   			$('#showReply' + id).fadeIn('300')
   		});
}

function commentEdit(id) {
	   	$('#showReplyTop' + id).fadeOut('300', function () {
   			$('#showEdit' + id).fadeIn('300')
   		});
}

function cancelReply(id) {
	   	$('#showReply' + id).fadeOut('300', function () {
   			$('#showReplyTop' + id).fadeIn('300')
   		});
}

function cancelEdit(id) {
	   	$('#showEdit' + id).fadeOut('300', function () {
   			$('#showReplyTop' + id).fadeIn('300')
   		});
}

function showLogin() {
	js_path_put = functions_path + "/ajax.php";
	send_data = "action=get_template&name=login_sidebar";
    	$.post(js_path_put, send_data, function(inner) {
        	if (login_type == '1') {
      			$('#bd_captcha').html(inner);
      			$('#bd_captcha').show('fast');
      		} else {
			$('#bd_logged_session').fadeOut('600', function () {
				$('#bd_logged_session').html(inner);
				$('#bd_logged_session').fadeIn('600');
				closeCaptcha();
			});
		}
    	});
}

function showLostPass() {
	js_path_put = functions_path + "/ajax.php";
	send_data = "action=get_template&name=lost_password_sidebar";
    	$.post(js_path_put, send_data, function(inner) {
        	if (login_type == '1') {
      			$('#bd_captcha').html(inner);
      			$('#bd_captcha').show('fast');
      		} else {
			$('#bd_logged_session').fadeOut('600', function () {
				$('#bd_logged_session').html(inner);
				$('#bd_logged_session').fadeIn('600');
				closeCaptcha();
			});
		}
    	});
}


function showRegister() {
	if (allow_registration == "1") {
		js_path_put = functions_path + "/ajax.php";
		send_data = "action=get_template&name=register_sidebar";
	    	$.post(js_path_put, send_data, function(inner) {
	    		if (registration_type == '1') {
				$('#bd_captcha').html(inner);
				$('#bd_captcha').show('fast');
			} else {
				$('#bd_logged_session').fadeOut('600', function () {
					$('#bd_logged_session').html(inner);
					$('#bd_logged_session').fadeIn('600');
					closeCaptcha();
				});
			}
	    	});
    	} else {
		process_error('Registration has been disabled.');
    	}
}

function showComment(id) {
	$('#hiddenText' + id).hide();
	$('#hidden' + id).fadeIn('300');
}

function closeCaptcha() {
	$('#bd_captcha').empty();
	$('#bd_captcha').hide('fast');
}

function closeDiv(id) {
	$('#' + id).fadeOut('100');
      	$('#bd_article_inline_edit').hide();
}

function process_error(error,captcha) {
	if (captcha == "1") {
		inner = '<form id="captcha_form" onSubmit="return false;"><center>';
		inner += '<img src="' + program_url + '/functions/captcha.php" class="bd_img_border" style="margin-bottom:10px;" />';
		inner += '<p class=\"bd_normal_text\" style=\"color:red;\">' + error + '</p>';
		inner += '<label>Confirmation Required</label>';
		inner += '<input type="text" name="bd_captcha_input" id="bd_captcha_input" style="width:180px;" />';
		inner += '<p class="bd_field_desc">Please confirm that you are human by inputting the text above into this textbox.</p>';
		inner += '<input type="button" onClick="processCaptcha();return false;" value="Confirm" />';
		inner += '</center></form>';
		closeError();
		$('#bd_captcha').html(inner);
		$('#bd_captcha').show('fast');
	} else {
		$('#bd_ae_pad').html(error);
		$('#bd_ajax_error').show('fast');
	}
}

function closeError(error) {
	$('#bd_ajax_error').hide('fast');
	$('#bd_ae_pad').html('');
}

function closeCategory(id) {
	$('#articles' + id).hide();
}

function expandCategory(id) {
	$('#articles' + current_showing).hide();
	$('#articles' + id).fadeIn(300);
	current_showing = id;
}

function originalMenu(previous) {
	if (! previous) {
		previous = "0";
	}
	$('#articles' + current_showing).hide();
	$('#articles' + previous).fadeIn(300);
	current_showing = previous;
}

function show_status(status) {
	$('#status' + current_status).hide();
	$('#statusActive' + current_status).removeClass('on');
	$('#status' + status).fadeIn(300);
	$('#statusActive' + status).addClass('on');
	current_status = status;
}


function openHelpBubble(id) {
	div = "help_" + id;
	var offset = $('#'+id).offset();
	top = offset.top;
	left = offset.left;
	$('#'+div).css('top',top);
	$('#'+div).css('left',left);
	$('#'+div).fadeIn('150');
}

function closeHelpBubble(id) {
	div = "help_" + id;
	$('#'+div).fadeOut('150');
}

function showSubcomments(id) {
	sub_coms = "subcomments" + id;
	main_coms = "comment" + id;
	if ($('#' + sub_coms).length == '0') {
		// Nothing there, don't do anything.
	} else {
	    	$('.bd_a_main_comment').not('#' + main_coms).fadeOut('300', function () {
	    		$('#' + sub_coms).fadeIn('300');
	    		$('#' + main_coms).addClass('bd_thread_active');
	    		$('#bd_subcom_return').show();
	    		current_subs = sub_coms;
	    		viewing_thread = id;
	    	});
    	}
}

function hideSubcomments(id) {
	sub_coms = "subcomments" + id;
	main_coms = "comment" + id;
    	$('#' + current_subs).fadeOut('300', function () {
    		$('.bd_a_main_comment').fadeIn('300');
    		$('#bd_subcom_return').hide();
    		current_subs = '';
    		viewing_thread = '';
	    	$('.bd_a_main_comment').removeClass('bd_thread_active');
    	});
}

function showSaved(text) {
	closeError();
	element = "<div id=\"bd_saved\" class=\"bd_shadow_white\">" + text + "</div>";
	$('body').append($(element).hide().fadeIn(200));
	setTimeout(function () { $("#bd_saved").fadeOut('300') }, 2000);
}
