var ubb_block = "ubb_block";

//------------------------------------------------------

$(document).ready(function(){
	$("."+ubb_block).each(function(){
		this.innerHTML = ubb_parse(this.innerHTML);
	})
});

function ubb_parse(text){ return ubb_parse(text, false); }
function ubb_parse(text, debugmode)
{
	text = text	.replace(/&/g, "&amp;")
				.replace(/</g, "&lt;")
				.replace(/>/g, "&gt;")
				.replace(/\n/g, "<br />");

	ubb_lasterror = "";
	var out = "";
	var regex = /\[(.+?)\b(.*?)\]/g;
	var result = null;
	var oldmatchend = 0;
	var tagStack = [];
	var fatalerror = false;
	var lastindex = 0;
	while (((result = regex.exec(text)) != null) && !fatalerror)
	{
		lastindex = regex.lastIndex;
		out += text.substr(oldmatchend, result["index"]-oldmatchend);
		oldmatchend = result["index"] + result[0].length;
		
		if (result[1] == "/")
		{	// Close tag
			if ((tagStack.length > 0) && (result[2] == tagStack[tagStack.length-1][0]))
			{
				// Correct tag
				out += ubb_parseCloseTag(result[2]);
				tagStack.pop();
			}
			else
			{
				// Incorrect tag, output for easy debugging
				ubb_lasterror = result[0]+" not matching";
				if (debugmode)
				{
					fatalerror = true;
					out += text.substr(result["index"], text.length-result["index"]);
				}
				else
					out += result[0];
			}
		}
		else
		{	// Open tag
			out += ubb_parseOpenTag(result[1], result[2]);
			if (result[1] != "img")
				tagStack.push([result[1], result[2]]);
		}
	}
	
	while (tagStack.length > 0)
	{
		out += ubb_parseCloseTag(tagStack.pop()[0]);
	}
	
	out += text.substr(lastindex);
	
	out += "<div class='clear'></div>"
	
	return out;
}
function ubb_parseOpenTag(tagname, param)
{
	var out = "";
	param = tagname + param + " ";
	switch (tagname)
	{
	case "b":
	case "i":
	case "u":
		out += "<span class='ubb_"+tagname+"'>";
		break;
	case "color":
		var color = ubb_getValFromParam(param, "color");
		if (color !== false)
		{
			if (color.substr(0,1) != "#")
				color = "#" + color;
			color = color.substr(0,7);
			out += "<span style='color: "+color+"'>";
		}
		break;
	case "link":
		var href = ubb_getValFromParam(param, "link");
		if (href !== false)
		{
			if (href.substr(0,7) != "http://")
				href = "http://"+href;
			out += "<a href='"+href+"'>";
		}
		break;
	case "img":
		id = ubb_getValFromParam(param, "");
		if (id !== false)
		{
			width = ubb_getValFromParam(param, "width");
			align = ubb_getValFromParam(param, "align");
			inline = ubb_getValFromParam(param, "inline");
			extraclasses = "ubb_img";
			if (align == "right")
				extraclasses += " ubb_img_right";
			else if (align == "center")
				extraclasses += " ubb_img_center";
			else
				extraclasses += " ubb_img_left";
			out += $.singleImageHtml({image: { name: "", description: "", imageid: id }, width: width, extraclasses: extraclasses});
			if ((inline != 'yes') && (inline != 'ja') && (inline != 'inline'))
				out += "<div class='clear'></div>";
		}
		break;
	}
	return out;
}
function ubb_parseCloseTag(tagname)
{
	var out = "";
	switch (tagname)
	{
	case "b":
	case "i":
	case "u":
	case "color":
		out += "</span>";
		break;
	case "link":
		out += "</a>";
		break;
	case "img":
		out += "</img>";
		break;
	}
	return out;
}
function ubb_getValFromParam(param, name)
{
	var index = param.indexOf(name+"=");
	if (index == -1)
		return false;
	var firstSpace = param.indexOf(" ", index);
	return param.substring(index+name.length+1, firstSpace);
}