function _quickReplyOpen(username, userid, vmid)
{
	$("#reply_link_" + vmid).removeAttr("onclick").unbind('click').bind('click', function(e) { return false;});

	$("#reply_" + vmid).html(" " +
		"<div id=\"reply_comment_" + vmid + "\" style=\"display:none\">" +
			"<strong>This reply will appear on</strong> <span class=\"newscomments\"><strong>" + username + "'s</strong></span> Chalkboard<br />" + 
			"<form name=\"form_reply_" + vmid + "\" action=\"/updateprofile.php\" style=\"padding:0px;margin:0px\">" + 
				"<textarea name=\"message\" rows=\"10\" cols=\"60\" style=\"width:100%; height:50px\"></textarea>" + 
				"<label for=\"qr_quickreply\" style=\"float:left;\"><input type=\"checkbox\" name=\"quotevmid\" value=\"" + vmid + "\" id=\"qr_quickreply\" checked=\"checked\" />Quote <strong>" + username + "'s</strong> message in reply?</label>" + 
				"<div style=\"float:right;padding-top:5px\">" + 
					"<a href=\"javascript:void(0);\" onclick=\"_quickReplySubmit('" + username + "'," + userid + "," + vmid + "); return false;\"><img src=\"/images/button_submit.gif\" border=\"0\"></a> " + 
					"<a href=\"javascript:void(0);\" onclick=\"_hideReply('" + username + "'," + userid + ", " + vmid + "); return false;\"><img src=\"/images/button_cancel.gif\" border=\"0\"></a>" + 
				"</div>" + 
				"<div align=\"center\" id=\"qc_posting_msg_" + vmid + "\" style=\"display:none; margin-top:6px\">" +
					"<img class=\"inlineimg\" src=\"" + IMGDIR_MISC + "/progress.gif\" alt=\" - \" />" + 
					"<!-- <strong>Posting Message - Please Wait</strong> -->" + 
				"</div>" + 
			"</form>" + 
		"</div>" 
	);

	$("form[@name='form_reply_" + vmid + "']").append(" " +
		"<input type=\"hidden\" name=\"fromquickreply\" value=\"1\">" + 
		"<input type=\"hidden\" name=\"s\" value=\"\">" + 
		"<input type=\"hidden\" name=\"do\" value=\"message\">" + 
		"<input type=\"hidden\" name=\"u\" value=\"" + userid + "\">" + 
		"<input type=\"hidden\" name=\"loggedinuser\" value=\"" + USERID + "\">" + 
		"<input type=\"hidden\" name=\"parseurl\" value=\"1\">" + 
		"<input type=\"hidden\" name=\"reply\" value=\"1\">" + 
		"<input type=\"hidden\" name=\"allow_ajax_qc\" value=\"1\">" + 
		"<input type=\"hidden\" name=\"securitytoken\" value=\"" + SECURITYTOKEN + "\">" + 
		"<input type=\"hidden\" name=\"wysiwyg\" value=\"0\">"
	);

	$("#reply_comment_" + vmid).slideDown("slow");
}

function _quickReplySubmit(username, userid, vmid)
{
	validatemessage(stripcode($("form[@name='form_reply_" + vmid + "'] textarea").val(), true), 0, minchars);

	var hiddenform = new vB_Hidden_Form(null);
	hiddenform.add_variables_from_object($("form[@name='form_reply_" + vmid + "']").get(0));

	$.ajax({
		type: "POST",
		url: "/updateprofile.php",
		data: hiddenform.build_query_string(),
		cache: false,
		timeout: vB_Default_Timeout,
		dataType: "text",
		beforeSend: function() {
			$("#qc_posting_msg_" + vmid).css('display', '');
		},
		success: function(html) {
			$("#reply_" + vmid).append(" " + 
				"<span style=\"float:left;border:1px solid #aab4c7; height:14px; width:475px; background-color:#e3eaf2; padding:2px;margin-left:5px\">" + 
					"Your reply has been sent. You can read this message on <a href=\"/" + username + "/\"><strong>" + username + "'s</strong></a> Chalkboard." + 
				"</span>"
			);
			$("#qc_posting_msg_" + vmid).css('display', 'none');
			_hideReply(username, userid, vmid);
		},
		error: function(XMLHttpRequest, textStatus) {
			alert("Error: " + textStatus);
		}
	});
}

function _quickAjaxSubmit(formId, divLoading, appendTo, divPreview, onSuccess)
{
	var hiddenform = new vB_Hidden_Form(null);
	hiddenform.add_variables_from_object($(formId).get(0));

	$.ajax({
		type: "POST",
		url: "/updateprofile.php",
		data: hiddenform.build_query_string() + "&ajax=1" + ((divPreview.length > 0)? '&preview=1' : ''),
		cache: false,
		timeout: vB_Default_Timeout,
		beforeSend: function() {
			$(appendTo).hide();
			$(divLoading).show();
		},
		success: function(html) {
			$(divLoading).hide();
			if (divPreview.length > 0)
			{
				$(divPreview).html(html);
			}
			else
			{
				$(appendTo)
					.html(html)
					.show()
				;
			}
			eval(onSuccess);
		}
	});
}

function _hideReply(username, userid, vmid)
{
	$("#reply_comment_" + vmid).slideUp("slow", function (e) {
		$(this).remove();
		$("#reply_link_" + vmid).bind("click", function(e) {
			_quickReplyOpen(username, userid, vmid);
			return false; 
		});
	});
}

function _deleteMsg(vmid)
{
	if ($("#comment_entry_" + vmid).length && confirm("Are you sure you wish to delete this message?"))
	{
		$.ajax({
			type: "POST",
			url: "/updateprofile.php",
			data: "do=deletemessage&vmid=" + vmid + "&securitytoken=" + SECURITYTOKEN + "&ajax=1",
			cache: false,
			timeout: vB_Default_Timeout,
			beforeSend: function() {
				$("#qc_deleting_msg_" + vmid).css('display', '');
			},
			success: function(html) {
				$("#comment_entry_" + vmid).slideUp("slow", function (e) {
					$(this).remove();
				});
			}
		});
	}
}

function showOverlay() 
{
	if ($('facebox_overlay').length == 0) 
		$("body").append('<div id="facebox_overlay" class="facebox_hide"></div>')

	$('#facebox_overlay').hide().addClass("facebox_overlayBG")
		.css('opacity', 0)
		//.click(function() { $(document).trigger('close.facebox') })
		.fadeIn(200)

	return false
}

function hideOverlay() 
{

	$('#facebox_overlay').fadeOut(200, function()
	{
		$("#facebox_overlay").removeClass("facebox_overlayBG")
		$("#facebox_overlay").addClass("facebox_hide") 
		$("#facebox_overlay").remove()
	})

	return false
}

function getPageScroll() 
{
	var xScroll, yScroll;
	if (self.pageYOffset) 
	{
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} 
	else if (document.documentElement && document.documentElement.scrollTop) 
	{	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} 
	else if (document.body) 
	{// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	return new Array(xScroll, yScroll) 
}

  // Adapted from getPageSize() by quirksmode.com
function getPageHeight() 
{
	var windowHeight
	if (self.innerHeight) 
	{	// all except Explorer
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowHeight = document.body.clientHeight;
	}	
	return windowHeight
}

function init_facebox()
{
	if (inited) 
		return true
	else 
		inited = true

	//makeCompatible()
	$('body').append('\
    <div id="facebox" style="display:none;"> \
      <div class="popup"> \
        <table> \
          <tbody> \
            <tr> \
              <td class="tl"/><td class="b"/><td class="tr"/> \
            </tr> \
            <tr> \
              <td class="b"/> \
              <td class="body"> \
                <div class="content"> \
                </div> \
                <div class="footer"> \
                  <a href="#" class="close"> \
                    <img src="/facebox/closelabel.gif" title="close" class="close_image" /> \
                  </a> \
                </div> \
              </td> \
              <td class="b"/> \
            </tr> \
            <tr> \
              <td class="bl"/><td class="b"/><td class="br"/> \
            </tr> \
          </tbody> \
        </table> \
      </div> \
    </div>');
}

function showLoading()
{
	init_facebox();
	showOverlay();
	$('#facebox .content').empty()
	$('#facebox .body').children()
		.hide()
		.end()
		.append('<div class="loading"><img src="/facebox/loading.gif"/></div>')

	$('#facebox').css({
		top:	getPageScroll()[1] + (getPageHeight() / 2),
		left:	0
	}).show()
}

function hideLoading()
{
	$('#facebox').fadeOut(function() 
	{
		$('#facebox .content').removeClass().addClass('content')
		hideOverlay()
		$('#facebox .loading').remove()
	})
}

function _loadTab(tabname, thisobj)
{
	if (!$("#maincontent").length)
	{
		window.location = thisobj.href;
		return;
	}

	$.ajax({
		type: "GET",
		url: "/updateprofile.php",
		data: "do=get" + tabname + "block&u=" + PROFILE_USERID + "&securitytoken=" + SECURITYTOKEN + "&ajax=1",
		cache: false,
		timeout: vB_Default_Timeout,
		beforeSend: function() {
			showLoading();
		},
		success: function(html) {
			hideLoading();
			$("#right").show();
			$("#maincontent").css("width", "640px");
			$("#maincontent").html(html);

			$("#tabsK *").each(function () {
				$(this).removeClass("active");
				$(this)
			});

			$(thisobj).parent("li").addClass("active");
		}
	});
}

function _loadBlock(blockname, norightbar)
{
	$.ajax({
		type: "GET",
		url: "/updateprofile.php",
		data: "do=get" + blockname + "block&u=" + PROFILE_USERID + "&securitytoken=" + SECURITYTOKEN + "&ajax=1",
		cache: false,
		timeout: vB_Default_Timeout,
		beforeSend: function() {
			showLoading();
		},
		success: function(html) {
			hideLoading();
			if (norightbar)
			{
				$("#right").hide();
				$("#maincontent").css("width", "950px");
			}
			$("#maincontent").html(html);
		}
	});
}

function _loadBlockCP(blockname, urlParams)
{
	$.ajax({
		type: "GET",
		url: "/updateprofile.php",
		data: "do=getcp" + blockname + "block&u=" + PROFILE_USERID + "&securitytoken=" + SECURITYTOKEN + "&ajax=1" + urlParams,
		cache: false,
		timeout: vB_Default_Timeout,
		beforeSend: function() {
			showLoading();
		},
		success: function(html) {
			hideLoading();
			$("#maincontent").html(html);
		}
	});
}
