// ----------------------------------------------------------------------------
// markItUp! BBCode Parser
// v 1.0.6
// Dual licensed under the MIT and GPL licenses.
// ----------------------------------------------------------------------------
// Copyright (C) 2009 Jay Salvat
// http://www.jaysalvat.com/
// http://markitup.jaysalvat.com/
// ----------------------------------------------------------------------------
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ----------------------------------------------------------------------------
// Thanks to Arialdo Martini, Mustafa Dindar for feedbacks.
// ----------------------------------------------------------------------------

var text = '';

function BBCode2Html(text) {	
	//prevent jscript injection by encoding <, > and &
	text = text.replace(/&/g, '&amp;');
	text = text.replace(/</g, '&lt;');
	text = text.replace(/>/g, '&gt;');

	//remove start and end whitespace
	text = $.trim(text);
	
	// BBCode to find...
	// /mg means multiple line, global.
	// the \s\S is used to find any characters, '.' does not seem to work with multiline search/replace
	var arr_in = new Array(	/\[b\]([\s\S]*?)\[\/b\]/mg,	
				/\[i\]([\s\S]*?)\[\/i\]/mg,
			 	/\[u\]([\s\S]*?)\[\/u\]/mg,
			 	/\[img\="?([\s\S]*?)(?:\|width\=(.*?))?(?:\|height\=(.*?))?(?:\|position\=(.*?))?"?\]([\s\S]*?)\[\/img\]/mg,
			 	/\[email\="?([\s\S]*?)"?\]([\s\S]*?)\[\/email\]/mg,
			 	/\[url\="?([\s\S]*?)\|newwindow"?\](.*?)\[\/url\]/mg,
			 	/\[url\="?([\s\S]*?)"?\](.*?)\[\/url\]/mg,
			 	/\[popup\="?([\s\S]*?)\|width\=(.*?)\|height\=(.*?)"?\](.*?)\[\/popup\]/mg,
			 	/\[anchorlink\="?([\s\S]*?)"?\](.*?)\[\/anchorlink\]/mg,
				/\[anchor\="?([\s\S]*?)"?\](.*?)\[\/anchor\]/mg,
				/\[size\="?([\s\S]*?)"?\]([\s\S]*?)\[\/size\]/mg,
				/\[color\="?([\s\S]*?)"?\]([\s\S]*?)\[\/color\]/mg,
				/\[quote]([\s\S]*?)\[\/quote\]/mg,
				/\[\*\]\s?([\s\S]*?)(?=\[\*\]|\[\/list\])/mg,
				/\[list\=([\s\S]*?)\]([\s\S]*?)\[\/list\]/mg,
				/\[list\]([\s\S]*?)\[\/list\]/mg
	);

	// And replace them by...
	var arr_out = new Array('<strong>$1</strong>',
			 	'<em>$1</em>',
				'<u>$1</u>',
			 	'<img src="index.php?get_picture=1&visualid=$1" width="$2" height="$3" style="float:$4" alt="$1" />',
			 	'<a href="mailto:$1">$2</a>',
				'<a href="$1" target="_blank">$2</a>',
				'<a href="$1">$2</a>',
				'<a href="$1" onclick="window.open(\'$1\',\'Popup\',\'width=$2,height=$3,scrollbars=no,resizable=yes\');return false;">$4</a>',
				'<a href="#$1">$2</a>',
				'<a name="$1">$2</a>',
				'<span style="font-size:$1%">$2</span>',
				'<span style="color:$1">$2</span>',
				'<blockquote>$1</blockquote>',
				'<li>$1</li>',
				'<ol start="$1">$2</ol>',
				'<ul>$1</ul>'
	);
	
	//do the search replace for every array item
	for(i=0;i<arr_in.length;i++) {
		text = text.replace(arr_in[i], arr_out[i]);
	}
	
	// \r\n to <br/>\r\n
	text = text.replace(/(\r?\n)/gmi, "<br/>\r\n");
	
	// Turn double <br/> into </p><p>
	text = '<p>' + text.replace(/(<br\/>\r\n){2}/gmi, "</p><p>") + '</p>';
	       
	//no br in front of /li
	text = text.replace(/<br\/>\r\n<\/li>/gmi, '</li>');
	
	//no br right after ul/ol start
	text = text.replace(/(<ul>|<ol[\s\S]*?>)<br\/>/gmi, "$1");
	
	//no p tags around ordered list of any kind (with or without starting number)
	text = text.replace(/<p><ol([\s\S]*?)>([\s\S]*?)<\/ol><\/p>/gmi, "<ol$1>$2</ol>");
	
	//no p tags around unordered list
	text = text.replace(/<p><ul>([\s\S]*?)<\/ul><\/p>/gmi, "<ul>$1</ul>");
		
	return text;
}

