

$(document).ready(function(){
autoRenderBBCodeToHtml();
});

function autoRenderBBCodeToHtml() {
$('.bbcode').each(function(){
	var srcElement=$(this);

	result=convertBBCodeToHtml(srcElement.text());

	srcElement.empty().append(result);
	$(this).removeClass('bbcode');
});
$('.bbcode-autopreview').each(function(){
	var srcElement=$(this);
	var result=convertBBCodeToHtml(srcElement.text());
	srcElement.empty().append(result);
	});
	
	resizeBBCodeImages();
}


function resizeBBCodeImages() {

	$('.bbrender').find('img').load(function(){
		var inX=$(this).attr('width');
		var inY=$(this).attr('height');
		var fitX = 480;
		var fitY = 640;
		var output = new Array();
		var doResize = false;
		
		var ratiox = 0;
		var ratioy = 0;
		var ratio = 0;
		
		if (inX > fitX) {console.log('inx is bigger than fitx');
			doResize = true;
		}
		
		if (inY > fitY) {console.log('iny is bigger than fity');
			doResize = true;
		}
		
		if (doResize) {
			ratiox = fitX / inX;
			ratioy = fitY / inY;
			
			if (ratiox < ratioy) {
				ratio = ratiox;
			} else {
				ratio = ratioy;
			}
			
			$(this).attr('width',inX * ratio); 
			$(this).attr('height',inY * ratio);
		}
	$(this).show();
	});
}

function convertBBCodeToHtml(source)
{
var sourceValue = source.replace(/\r\n/g,"!!-!!");
sourceValue = sourceValue.replace(/\n/g,"!!-!!");
sourceValue = sourceValue.replace(/\r/g,"!!-!!");


search = [
/\\n/ig,
	/\[list\](.*?)\[\/list\]/ig,
	/\[list=(.*?)\](.*?)\[\/list\]/ig,
	/\[\*\](.*?)!!-!!/ig,
	/\[b\](.*?)\[\/b\]/ig,
	/\[i\](.*?)\[\/i\]/ig,
	/\[u\](.*?)\[\/u\]/ig,
	/\[s\](.*?)\[\/s\]/ig,
	/\[sub\](.*?)\[\/sub\]/ig,
	/\[sup\](.*?)\[\/sup\]/ig,
	/\[quote\](.*?)\[\/quote\]/ig,
	/\[url\](.*?)\[\/url\]/ig,
	/\[url=([\w]+?:\/\/[^ \\"\n\r\t<]*?)\](.*?)\[\/url\]/ig,
    /\[img\](.*?)\[\/img\]/ig,
    /\[large\](.*?)\[\/large\]/ig,
    /\[normal\](.*?)\[\/normal\]/ig,
    /\[small\](.*?)\[\/small\]/ig,
    /\[code\](.*?)\[\/code\]/ig,
    /\[color=(.*?)\](.*?)\[\/color\]/ig,
/\[br\]/ig,
    /\[rule\]/ig,
    /\[signature\]/ig,
     /\[osb\]/ig,
      /\[csb\]/ig,
    /\[quote=(.*?)\](.*?)\[\/quote\]/ig,
    /\[video\](.*)\[\/video\]/ig,
    /\[video\](.*?)youtube(.*?)watch\?v=(.*?)\[\/video\]/ig,
    /\[video\](.*?)vimeo(.*?)\/(.*?)\[\/video\]/ig,
	/\[video\].*www.dailymotion.com\/video\/([^_]+).*\[\/video\]/ig
    ];
    
    replace = [		
    "<br />",
    "<ul>$1</ul>",
    "<ol start=\"$1\">$2</ol>",
    "<li>$1</li>",
    "<strong>$1</strong>",
    "<em>$1</em>",
    "<span class=\"underline\">$1</span>",
    "<del>$1</del>",
    "<sub>$1</sub>",
    "<sup>$1</sup>",
    "<div class=\"quote\"><p>$1</p></div>",
    "<a target=\"_blank\" href=\"$1\">$1</a>",
    "<a target=\"_blank\" href=\"$1\">$2</a>",
    "<img class=\"bbimg\" src=\"$1\" />",
    "<span class=\"text_large\">$1</span>",
    "<span class=\"text_normal\">$1</span>",
    "<span class=\"text_small\">$1</span>",
    "<code>$1</code>",
    "<span class=\"textcol_$1\">$2</span>", 
    "<br />", 
    "<hr class='bbmsg' />", 
    "<hr class='bbmsg' />", 
    "!!OSB!!", 
    "!!CSB!!",
    "<div class=\"quote\"><cite>Said by: $1</cite><p>$2</p></div>",
	"<a target=\"_blank\" href=\"$1\">$1</a>",
	"<object width=\"480\" height=\"385\"><param name=\"movie\" value=\"http://www.youtube.com/v/$3\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/$3\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"480\" height=\"385\"></embed></object>",
	"<object width=\"400\" height=\"300\"><param name=\"allowfullscreen\" value=\"true\" /><param name=\"allowscriptaccess\" value=\"always\" /><param name=\"movie\" value=\"http://vimeo.com/moogaloop.swf?clip_id=$3&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1\" /><embed src=\"http://vimeo.com/moogaloop.swf?clip_id=$3&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" allowscriptaccess=\"always\" width=\"400\" height=\"300\"></embed></object>",
	"<object width=\"480\" height=\"270\"><param name=\"movie\" value=\"http://www.dailymotion.com/swf/video/$1\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowScriptAccess\" value=\"always\"></param><embed type=\"application/x-shockwave-flash\" src=\"http://www.dailymotion.com/swf/video/$1\" width=\"480\" height=\"270\" allowfullscreen=\"true\" allowscriptaccess=\"always\"></embed></object>"
    ];
    for(i = search.length-1; i >= 0; i--)
    {
   	 sourceValue = sourceValue.replace(search[i], replace[i]);
    }
    	dest = sourceValue.replace(/!!-!!/g, "<br />");
dest = dest.replace(/!!OSB!!/g, "[");
dest = dest.replace(/!!CSB!!/g, "]");
    	return dest;
    }
    
    
