function strip(strText) {
	strText = strText.replace(/^\\t/g,"");
	strText = strText.replace(/ $/g,"");
	return strText;
}

function unBBCode(strPostText) {
	strPostText = strip(strPostText);
	strPostText = strPostText.replace(/\[(\/?)quote\]/g,"<$1blockquote>");
	strPostText = strPostText.replace(/\[(\/?)b\]/g,"<$1strong>");
	strPostText = strPostText.replace(/\[(\/?)i\]/g,"<$1em>");
	strPostText = strPostText.replace(/\[(\/?)(pre|code|strike)\]/g,"<$1$2>");
	strPostText = strPostText.replace(
		/\[url=([[:print:]])\]/g,
		"<a href=\"$1\" target=\"new\">");
	strPostText = strPostText.replace(/\[\/url\]/g,"</a>");

	return strPostText
}

function BBCode(strPostText){
	strPostText = strip(strPostText);
	strPostText = strPostText.replace(/(\r\n|<p>|<\/p>|<\/br>|\t)/g,"");
	//strPostText = strPostText.replace(/<\/(p|br)>/g,"\n");
	strPostText = strPostText.replace(/<(\/?)strong>/g,"[$1b]");
	strPostText = strPostText.replace(/<(\/?)em>/g,"[$1i]");
	strPostText = strPostText.replace(/<(\/?)(pre|code|strike)>/g,"[$1$2]");
	strPostText = strPostText.replace(/<(\/?)blockquote>/g,"[$1quote]");
	strPostText = strPostText.replace(/<img src="[[:print:]]" \/>/g,"[img=$1]");

	// replace hyperlinks, this is ugly, but it works
	strPostText = strPostText.replace(/<a href="/g,"[url=");
	strPostText = strPostText.replace(/" target="new">/g,"]");
	strPostText = strPostText.replace(/<\/a>/g,"[/url]");

	return strPostText;
}

function getSelText(intPostNumber)
{
	var strText = '';

	if (window.getSelection) {
		strText = window.getSelection();
	}
	else if (document.getSelection) {
		strText = document.getSelection();
	}
	else if (document.selection) {
		strText = document.selection.createRange().text;
	}

	if ( strText == '' )
		return $('#PostText'+intPostNumber).html().replace(/\t/g,'');
	else
		return strText	
}



function editPost(intPostNumber) {
	//alert("In edit("+intPostNumber+")");
	var strPostText = $('#PostText'+intPostNumber).html();
	var strTextarea = '<textarea>' + BBCode(strPostText) + '</textarea>';
	var strSaveLink = '<li><a class="saveButton">save</a></li>\n';
	strSaveLink += '<li><a class="cancelButton">cancel</a></li>';

	//$("#edit"+intPostNumber).removeClass();
	$("#edit"+intPostNumber).addClass('hidden');
	$('#PostText'+intPostNumber).html(strTextarea);
	$("#PostInfo"+intPostNumber).append(strSaveLink);
	//$("#PostInfo"+intPostNumber).append(liCancelLink);

	$('.saveButton').click(
		function(){
			alert("Save!");
		});
	$('.cancelButton').click(
		function(){
			$('#PostText'+intPostNumber).html(strPostText);
		});
}

function quotePost(intPostNumber) {
	var strQuoteText = "[quote][b]#"+intPostNumber+" by "+ $( "#Author"+intPostNumber ).text()+"[/b]";
	strQuoteText += BBCode(getSelText(intPostNumber));
	strQuoteText += "[/quote]";

	$("textarea#post_text").val($("textarea#post_text").val() + strQuoteText);
}

$(document).ready(
	function(){
		$( "div.post" ).each(
			function(intIndex){
				var intPostID = $( this ).attr('id');

				//alert(intPostID);

				//$( this ).html(unBBCode($( this ).html()));
				$( "a#quote"+intPostID ).bind(
					"click",
					function(){
						quotePost(intPostID);
					});
				$( "a#edit"+intPostID ).bind(
					"click",
					function(){
						editPost(intPostID);
					});
			});
	});
