compile insert RTI_BBC2HTML

function BBC2HTML(S) {

     if (S===undefined) {
        return "";
     }
     
	var smileyList = {smile: [[':smile:',':-\\)',':\\)'], 'smile'], bigsmile: [[':biggrin:',':-D',':D'],'big grin'], vsad:[[':vsad:',':-\\(\\('],'very sad'], sad:[[':sad:',':-\\('],'sad'], wink:[[':wink:',';-\\)'],'wink'], vshocked:[[':surprised:',':-o'],'shocked'], tongue:[[':tongue:',':-\\!'],'tongue'], cool:[[':cool:'], 'cool!'], mad:[[':mad:'],'mad!'], sconf:[[':confused:'], 'confused'], paranoid:[[':paranoid:'],'paranoid'], question:[[':question:'],'question?'], thumbup:[[':thumbsup:'],'Yes!'], thumbdown:[[':thumbsdown:'], 'No!']};

     S = smileyParse(S);
	if (S.indexOf('[') < 0) return S;
	
	function X(p, f) {return new RegExp(p, f)}
	function D(s) {return rD.exec(s)}
	function R(s) {return s.replace(rB, P)}
	function A(s, p) {for (var i in p) s = s.replace(X(i, 'g'), p[i]); return s;}

		function P($0, $1, $2, $3) {
		  if ($3 && $3.indexOf('[') > -1) $3 = R($3);
		  switch ($1) {
			case 'url':case 'anchor':case 'email': return '<a '+ L[$1] + ($2||$3) +'">'+ $3 +'</a>';
			case 'img': var d = D($2); return '<img src="'+ $3 +'"'+ (d ? ' width="'+ d[1] +'" height="'+ d[2] +'"' : '') +' alt="'+ (d ? '' : $2) +'" />';
			case 'flash':case 'youtube': var d = D($2)||[0, 425, 366]; return '<object type="application/x-shockwave-flash" data="'+ Y[$1] + $3 +'" width="'+ d[1] +'" height="'+ d[2] +'"><param name="movie" value="'+ Y[$1] + $3 +'" /></object>';
			case 'float': return '<span style="float: '+ $2 +'">'+ $3 +'</span>';
			case 'left':case 'right':case 'center':case 'justify': return '<div style="text-align: '+ $1 +'">'+ $3 +'</div>';
			case 'google':case 'wikipedia': return '<a href="'+ G[$1] + $3 +'">'+ $3 +'</a>';
			case 'b':case 'i':case 'u':case 's':case 'sup':case 'sub':case 'h1':case 'h2':case 'h3':case 'h4':case 'h5':case 'h6':case 'table':case 'tr':case 'th':case 'td': return '<'+ $1 +'>'+ $3 +'</'+ $1 +'>';
			case 'row': case 'r':case 'header':case 'head':case 'h':case 'col':case 'c': return '<'+ T[$1] +'>'+ $3 +'</'+ T[$1] +'>';
			case 'acronym':case 'abbr': return '<'+ $1 +' title="'+ $2 +'">'+ $3 +'</'+ $1 +'>';
		  }
		  return '['+ $1 + ($2 ? '='+ $2 : '') +']'+ $3 +'[/'+ $1 +']';
		}

	var rB = X('\\[([a-z][a-z0-9]*)(?:=([^\\]]+))?]((?:.|[\r\n])*?)\\[/\\1]', 'g'), rD = X('^(\\d+)x(\\d+)$');
	var L = {url: 'href="', 'anchor': 'name="', email: 'href="mailto: '};
	var G = {google: 'http://www.google.com/search?q=', wikipedia: 'http://www.wikipedia.org/wiki/'};
	var Y = {youtube: 'http://www.youtube.com/v/', flash: ''};
	var T = {row: 'tr', r: 'tr', header: 'th', head: 'th', h: 'th', col: 'td', c: 'td'};
	//var C = {notag: [{'\\[': '&#91;', ']': '&#93;'}, '', ''], code: [{'<': '&lt;'}, '<code><pre>', '</pre></code>']};
	var C = {notag: [{'\\[': '&#91;', ']': '&#93;'}, '', '']};
	//C.php = [C.code[0], C.code[1]+ '&lt;?php ', '?>'+ C.code[2]];
	var F = {font: 'font-family:$1', size: 'font-size:$1px', color: 'color:$1'};
	var U = {c: 'circle', d: 'disc', s: 'square', '1': 'decimal', a: 'lower-alpha', A: 'upper-alpha', i: 'lower-roman', I: 'upper-roman'};
	var I = {}, B = {};

	function smileyParse(origStr) {
	   var i, j, target, re;
	   // first make sure any 'notag' wrappers are respected
	   origStr = origStr.replace(/\[notag\]((?:.|[\r\n])*?)\[\/notag\]/g, function($0, $1) {var p1 = $1.replace(/\;-\)/g,":wink:"); p1 = p1.replace(/\;\)/g, ":wink:"); p1 = p1.replace(/\:-\)/g, ":smile:"); p1 = p1.replace(/\:\)/g, ":smile:"); p1 = p1.replace(/\:-D/g, ":biggrin:"); p1 = p1.replace(/\:D/g, ":biggrin:"); p1 = p1.replace(/\:-\(/g, ":sad:"); p1 = p1.replace(/\:\(/g, ":sad:"); p1 = p1.replace(/\:-O/g, ":surprised:"); p1 = p1.replace(/\:-o/g, ":surprised:"); p1 = p1.replace(/\:O/g, ":surprised:"); p1 = p1.replace(/\:o/g, ":surprised:"); p1 = p1.replace(/:-\!/g, ":tongue:"); p1 = p1.replace(/:\!/g, ":tongue:"); p1 = p1.replace(/:-\(\(/g, ":vsad:"); p1 = p1.replace(/\:/g, "&#58;"); return "[notag]" + p1 + "[/notag]";});
   	   origStr = origStr.replace(/\[code\]((?:.|[\r\n])*?)\[\/code\]/g, function($0, $1) {var p1 = $1.replace(/\:/g, "&#58;"); return "[code]" + p1 + "[/code]";});
//       for (i in smileyList) {
//	   {
//           for (j in smileyList[i][0]) {
//		      target = smileyList[i][0][j];  
//		      re = new RegExp(target, "g");
//		      origStr = origStr.replace(re, '<img alt="' + smileyList[i][1] + '" src="../images/smilies/' + i + '.gif" border=0 />');
//		   }
//	   }

      re = new RegExp(":smile:", "g");
      origStr = origStr.replace(re, '<img alt="smile" src="../images/smilies/smile.gif" border=0 />');
      re = new RegExp(":-\\)", "g");
      origStr = origStr.replace(re, '<img alt="smile" src="../images/smilies/smile.gif" border=0 />');
      re = new RegExp(":\\)", "g");
      origStr = origStr.replace(re, '<img alt="smile" src="../images/smilies/smile.gif" border=0 />');

      re = new RegExp(":biggrin:", "g");
      origStr = origStr.replace(re, '<img alt="big grin" src="../images/smilies/bigsmile.gif" border=0 />');
      re = new RegExp(":-D", "g");
      origStr = origStr.replace(re, '<img alt="big grin" src="../images/smilies/bigsmile.gif" border=0 />');
      re = new RegExp(":D", "g");
      origStr = origStr.replace(re, '<img alt="big grin" src="../images/smilies/bigsmile.gif" border=0 />');

      re = new RegExp(":vsad:", "g");
      origStr = origStr.replace(re, '<img alt="very sad" src="../images/smilies/vsad.gif" border=0 />');
      re = new RegExp(":-\\(\\(", "g");
      origStr = origStr.replace(re, '<img alt="very sad" src="../images/smilies/vsad.gif" border=0 />');
	  
      re = new RegExp(":sad:", "g");
      origStr = origStr.replace(re, '<img alt="sad" src="../images/smilies/sad.gif" border=0 />');
      re = new RegExp(":-\\(", "g");
      origStr = origStr.replace(re, '<img alt="sad" src="../images/smilies/sad.gif" border=0 />');
      re = new RegExp(":\\(", "g");
      origStr = origStr.replace(re, '<img alt="sad" src="../images/smilies/sad.gif" border=0 />');

      re = new RegExp(":wink:", "g");
      origStr = origStr.replace(re, '<img alt="wink" src="../images/smilies/wink.gif" border=0 />');
      re = new RegExp(";-\\)", "g");
      origStr = origStr.replace(re, '<img alt="wink" src="../images/smilies/wink.gif" border=0 />');
      re = new RegExp(";\\)", "g");
      origStr = origStr.replace(re, '<img alt="wink" src="../images/smilies/wink.gif" border=0 />');

      re = new RegExp(":surprised:", "g");
      origStr = origStr.replace(re, '<img alt="shocked" src="../images/smilies/vshocked.gif" border=0 />');
      re = new RegExp(":-o", "g");
      origStr = origStr.replace(re, '<img alt="shocked" src="../images/smilies/vshocked.gif" border=0 />');
      re = new RegExp(":o", "g");
      origStr = origStr.replace(re, '<img alt="shocked" src="../images/smilies/vshocked.gif" border=0 />');
      re = new RegExp(":-O", "g");
      origStr = origStr.replace(re, '<img alt="shocked" src="../images/smilies/vshocked.gif" border=0 />');
      re = new RegExp(":O", "g");
      origStr = origStr.replace(re, '<img alt="shocked" src="../images/smilies/vshocked.gif" border=0 />');

      re = new RegExp(":tongue:", "g");
      origStr = origStr.replace(re, '<img alt="tongue" src="../images/smilies/tongue.gif" border=0 />');
      re = new RegExp(":-\\!", "g");
      origStr = origStr.replace(re, '<img alt="tongue" src="../images/smilies/tongue.gif" border=0 />');
      re = new RegExp(":\\!", "g");
      origStr = origStr.replace(re, '<img alt="tongue" src="../images/smilies/tongue.gif" border=0 />');

      re = new RegExp(":cool:", "g");
      origStr = origStr.replace(re, '<img alt="cool" src="../images/smilies/cool.gif" border=0 />');

      re = new RegExp(":mad:", "g");
      origStr = origStr.replace(re, '<img alt="mad!" src="../images/smilies/mad.gif" border=0 />');
	  
      re = new RegExp(":confused:", "g");
      origStr = origStr.replace(re, '<img alt="confused" src="../images/smilies/sconf.gif" border=0 />');
	  
      re = new RegExp(":paranoid:", "g");
      origStr = origStr.replace(re, '<img alt="paranoid" src="../images/smilies/paranoid.gif" border=0 />');

      re = new RegExp(":question:", "g");
      origStr = origStr.replace(re, '<img alt="question?" src="../images/smilies/question.gif" border=0 />');

      re = new RegExp(":thumbsup:", "g");
      origStr = origStr.replace(re, '<img alt="Yes!" src="../images/smilies/thumbup.gif" border=0 />');
	  
      re = new RegExp(":thumbsdown:", "g");
      origStr = origStr.replace(re, '<img alt="No!" src="../images/smilies/thumbdown.gif" border=0 />');

	  return origStr;
    }
	
	for (var i in C) I['\\[('+ i +')]((?:.|[\r\n])*?)\\[/\\1]'] = function($0, $1, $2) {var p1 = $1, p2 = $2, rslt; if (p1 == "code") {p2 = p2.replace(/<br>/gi,"\r"); p2 = p2.replace(/<br(.*?)\/>/gi, "\r");} rslt = C[$1][1] + A(p2, C[$1][0]) + C[$1][2]; return rslt;};
	for (var i in F) {B['\\['+ i +'=([^\\]]+)]'] = '<span style="'+ F[i] +'">'; B['\\[/'+ i +']'] = '</span>';}
	B['\\[list]'] = '<ul>'; B['\\[list=(\\w)]'] = function($0, $1) {return '<ul style="list-style-type: '+ (U[$1]||'disc') +'">'}; B['\\[/list]'] = '</ul>'; B['\\[\\*]'] = '<li>';
	B['\\[parentquote(?:=([^\@\\]]+))(?:\@([^\\]]+))?]'] = function($0, $1, $2) {return '<div class="bb-quote" style="border-width:1px;border-style:solid;"><div class="bb-quote bb-quote-author" style="background-color:lightblue;">' + ($2? '<a href="' + $2 + '">':'') + ($1 ? $1 +' wrote': 'Quote') + ($2 ? '</a>':'') + ':</div>'}; B['\\[/parentquote]'] = '</div>';
	B['\\[quote(?:=([^\@\\]]+))(?:\@([^\\]]+))?]'] = function($0, $1, $2) {return '<div class="bb-quote" style="border-width:1px;border-style:solid;"><div class="bb-quote bb-quote-author" style="background-color:lightblue;">' + ($2? '<a href="' + $2 + '">':'') + ($1 ? $1 +' wrote': 'Quote') + ($2 ? '</a>':'') + ':</div><blockquote>'}; B['\\[/quote]'] = '</blockquote></div>';
	B['\\[(hr|br)]'] = '<$1 />'; B['\\[sp]'] = '&nbsp;';
	B['\\[code]'] = '<pre>'; B['\\[/code]'] = '</pre>';
    B['\\[pre]'] = '<pre>'; B['\\[/pre]'] = '</pre>';


	return R(A(A(S, I), B));
}


