

/*	====================================================

	BANANA DANCE by Jon Belelieu
	http://www.doyoubananadance.com/
	http://www.ascadnetworks.com/
	Copyright (C) 2011 Jon Belelieu
	
	File Function: The primary JS file with functions relating
	to almost all JS-related features.
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

====================================================== */


var inner;
var upload_type;
var current_subs;
var timer;
var classifying_comment;
var viewing_thread;
var ed_format_type;
var fb_open;
var expandMenuType	= 'fade'; // fade, slide, instant
var showing_headers	= 0;
var functions_path	= program_url + "/functions";
var js_path		= program_url + "/js";
var current_showing	= current_category_id;
var use_bg_image	= "transparent_white.png"; // Found in templates/html/_imgs
var hold_ori_status 	= current_status;


// ---------------------------------------------------------------------------------------
//	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") {
			process_login_show($('#login_1').val());
			closeError();
			closeCaptcha();
		} else {
			process_error(returned['1'],returned['2']);
		}
    	});
}

function start_fb_session() {
	js_path_put = functions_path + "/fb.php";
	send_data = 'fblogin=1';
    	$.post(js_path_put, send_data, function(theResponse) {
		process_login_show(theResponse);
    	});
}

function process_login_show(user_id) {		
	js_path_put = functions_path + "/ajax.php";
	send_data = "action=get_template&name=logged_in_sidebar&user=" + user_id;
	$.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();
		});
	});
}

// ------------------------------------------
//	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;
			if (together < 0) {
				together = 0;
			}
			$('#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;
			if (together < 0) {
				together = 0;
			}
			$('#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) {
		show_captcha(inner);
	});
}

// ------------------------------------------
//	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") {
      			send_data = "action=get_template&category=" + current_category_id + "&name=logged_out_sidebar";
      		    	$.post(js_path_put, send_data, function(inner) {
		   		$('#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\" /><br /><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\" /><br /><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>";
	show_captcha(html);
}

function addExternalLink() {
	if (ed_format_type == 'cms') {
		if ($('#addLinkName').val()) {
			put = "<a href=\"" + $('#addLinkURL').val() + "\">" + $('#addLinkName').val() + "</a>";
		} else {
			put = "<a href=\"" + $('#addLinkURL').val() + "\"></a>";
		}
	} else {
		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);
      		closeMedia();
      		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();
      					closeMedia();
				}
			}
  		} 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');
}


// Closes the media gallery display.
function closeMedia() {
	screen_open = '0';
	$('#media_box').fadeOut(300);
	$('#media_box').remove();
}

/*
function uploadFile(type) {
	html = "<div id=\"bd_image_display\" style=\"cursor:pointer;position:fixed;top:0;left:0;width:100%;height:100%;background:url('/templates/html/_imgs/" + use_bg_image + "') top left;\">";
	html += '<div id="bd_upload_box" style="width:700px;height:400px;position:relative;top:50%;left:50%;margin-left:-350px;margin-top:-200px;\">';
	html += '<form id="upload_form" onsubmit="alert("asdf");return false;" enctype="multipart/form-data" action="' + functions_path + '/upload_file.php" method="post">';
	html += '<input type="hidden" name="type" value="' + type + '" />';
	html += '<h1>Upload the file</h1>';
	html += '<p>Select the file from your computer.</p>';
	html += '<input type="hidden" name="type" value="image" />';
	html += '<input name="theUploadedFile" id="theUploadedFile" type="file" />';
	html += '<h1>File on Server</h1>';
	html += '<p>Input the full path to the file on your server.</p>';
	html += '<input type="text" name="onserver" style="width:600px;" />';
	html += '<input type="button" onclick="uploadA();return false;" value="Continue" /> <input type="button" value="Cancel" onclick="closeUploadFile();" />';
	html += '</form>';
	html += '</div>';
	html += '</div>';
	$('body').append($(html).hide().fadeIn(200));
}
*/

function closeUploadFile() {
	
}


// --------------------------------------------
// 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;
}

// ---------------------------------------------------------------------------------------
//	Classify comment

function reClassifyComment(comment_id) {
	classifying_comment = comment_id;
	js_path_put = functions_path + "/ajax.php";
	send_data = "action=get_comment_types&comment_id=" + comment_id;
    	$.post(js_path_put, send_data, function(inner) {
    		var returned = inner.split('+++');
    		if (returned['0'] == '1') {
			show_captcha(returned['1']);
		} else {
			process_error(returned['1']);
		}
    	});
	
}

function sendClassify() {
	js_path_put = functions_path + "/ajax.php";
	send_data = "action=perform_classify&status=" + $('input:radio[name=new_cm_type]:checked').val() + "&id=" + classifying_comment;
    	$.post(js_path_put, send_data, function(inner) {
    		var returned = inner.split('+++');
    		if (returned['0'] == '1') {
			// Get new updated comments
			js_path_put = functions_path + "/ajax.php";
			send_data = "action=reget_comments&article=" + current_article_id ;
		    	$.post(js_path_put, send_data, function(newhtml) {
			   	$('#primary_comment_holder').fadeOut('300', function () {
		   			$('#primary_comment_holder').html(newhtml);
		   			$('#primary_comment_holder').fadeIn('300');
					// Show saved dialogue!
		   			current_status = hold_ori_status;
					closeCaptcha();
					showSaved();
					classifying_comment = '';
		   		});
		    	});
		} else {
			process_error(returned['1']);
		}
    	});
}

// ---------------------------------------------------------------------------------------
//	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') {
			show_captcha(inner);
      		} 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') {
			show_captcha(inner);
      		} 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') {
				show_captcha(inner);
			} 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 show_captcha(inner) {
	closeCaptcha();
	combine = "<div id=\"bd_captcha\" style=\"display:block;\">" + inner + "</div>";
	$('body').append($(combine).hide().fadeIn(50));
}

function closeCaptcha() {
	$('#bd_captcha').hide('fast');
	$('#bd_captcha').remove();
}

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();
		show_captcha(inner);
	} else {
		show_error(error);
	}
}

function show_error(error) {
	closeError();
	combine = "<div id=\"bd_ajax_error\" style=\"z-index:999999999;display:block;cursor:pointer;\"><div class=\"bd_ae_pad\" id=\"bd_ae_pad\" onclick=\"closeError();\">" + error + "</div></div>";
	$('body').append($(combine).hide().fadeIn(50));
}

function closeError(error) {
	$('#bd_ajax_error').hide('fast');
	$('#bd_ajax_error').remove();
	$('#bd_ae_pad').remove();
}

function closeCategory(id) {
	$('#articles' + id).hide();
}

function expandCategory(id) {
	$('#articles' + current_showing).hide();
	if (expandMenuType == 'fade' || expandMenuType === undefined) {
		$('#articles' + id).fadeIn(300);
	}
	else if (expandMenuType == 'slide') {
		$('#articles' + id).slideDown();
	}
	else if (expandMenuType == 'instant') {
		$('#articles' + id).show();
	}
	current_showing = id;
}

function originalMenu(previous) {
	if (! previous) {
		previous = "0";
	}
	$('#articles' + current_showing).hide();
	if (expandMenuType == 'fade' || expandMenuType === undefined) {
		$('#articles' + previous).fadeIn(300);
	}
	else if (expandMenuType == 'slide') {
		$('#articles' + previous).slideDown();
	}
	else if (expandMenuType == 'instant') {
		$('#articles' + previous).show();
	}
	current_showing = previous;
}

function show_status(status) {
	if (! current_status) {
		current_status = '0';
	}
	$('#status' + current_status).hide();
	$('#statusActive' + current_status).removeClass('on');
	$('#status' + status).fadeIn(300);
	$('#statusActive' + status).addClass('on');
	current_status = status;
}


function showHiddenHeaders() {
	if (showing_headers == 1) {
		showing_headers = 0;
		$('li.hide').hide();
	} else {
		showing_headers = 1;
		$('li.hide').show();
	}
}


function openHelpBubble(id) {
	div = "help_" + id;
	// Get the location relative
	// to the page and the element
	// holding the image.
	var offset = $('#' + id).position();
	topy = offset.top;
	leftx = offset.left;
	var offset_true = $('#' + id).offset();
	true_left = offset_true.left;
	// Make sure this isn't going
	// off the page.
	user_width = $(window).width();
	bubble_width = $('#' + div).outerWidth();
	total_right = true_left + bubble_width + 25;
	if (total_right > user_width) {
		add_offset = leftx - bubble_width;
	} else {
		add_offset = leftx + 25;
	}
	// Display the help bubble
	$('#' + div).css('top',topy);
	$('#' + div).css('left',add_offset);
	$('#' + div).fadeIn('150');
}

function closeHelpBubble(id) {
	div = "help_" + id;
	$('#'+div).fadeOut('150');
}

function showSubcomments(id) {
	sub_coms = "subcomments" + id;
	main_coms = "comment" + id;
      	size = $('.bd_a_main_comment').size();
      	if (size <= 1) {
      		run_subcom_hide(id,main_coms,sub_coms);
      	} else {
      	    	$('.bd_a_main_comment').not('#' + main_coms).hide(function () {
      	    		run_subcom_hide(id,main_coms,sub_coms);
      	    	});
      	}
}

function run_subcom_hide(id,main_coms,sub_coms) {
      	$('#' + main_coms).show();
      	$('#' + 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\" style=\"z-index:9999999998;cursor:pointer;\" onclick=\"closeSaved();\">" + text + "</div>";
	$('body').append($(element).hide().fadeIn(200));
	setTimeout(function () { closeSaved() }, 2000);
}

function closeSaved() {
	$("#bd_saved").fadeOut(50);
	$("#bd_saved").remove();
}



/*	--------------- Images --------------------- */


function viewFullImage(path,caption,title,full_width,full_height) {
	window_x = $(window).width();
	window_y = $(window).height();
	
	final_full_width = full_width;
	final_full_height = full_height;
	
	// Make sure image fits on screen
	if (full_width > window_x) {
		new_x = window_x - 60;
		scaling = new_x / full_width;
		new_y = Math.round(full_height * scaling);
		final_full_width = new_x;
		final_full_height = new_y;
	} else {
		final_full_width = full_width;
	}
	
	if (full_height > window_y) {
		new_y = window_y - 60;
		scaling = new_y / full_height;
		new_x = Math.round(full_width * scaling);
		final_full_width = new_x;
		final_full_height = new_y;
	} else {
		final_full_height = full_height;
	}
	
	margx = Math.round((window_x - final_full_width) / 2);
	margy = Math.round((window_y - final_full_height) / 2);
	from_top = window_y - final_full_height;
	
	element = "<div id=\"bd_image_display\" onclick=\"closeImage();return false;\" style=\"cursor:pointer; position:fixed; top:0; left:0; width:100%; height:100%; background:url('" + program_url + "/templates/html/_imgs/" + use_bg_image + "') top left;\">";
	element += "<div id=\"bd_img_inner\" style=\"width:" + final_full_width + "px;margin:" + margy + "px auto 0 auto;text-align:center;\"><img src=\"" + path + "\" width=\"" + final_full_width + "\" height=\"" + final_full_height + "\" border=\"0\" alt=\"" + title + "\" title=\"" + title + "\" class=\"bd_gallery_img\" />";
		if (caption) {
			element += "<div class=\"bd_gallery_caption\">" + caption + "</div>";
		}
	element += "</div></div>";
	$('body').append($(element).hide().fadeIn(200));
}

function closeImage() {
	$('#bd_image_display').hide();
	$('#bd_image_display').remove();
}
