replaceBBcodes = function (b) {
	b = typeof b != "undefined" ? b: "body";
	$(b).children("div").each(function (d) {
		var e = arguments.callee;
		if ($(this).is(".bbCodeBlock")) {
			for (var a = $("div.codeMessage:first", this).html().replace(/&#(\d+);/g, function (m, o) {
				return String.fromCharCode(o)
			}).replace(/<br\s*\/?>/ig, "\n").replace(/<!--uzc--\>/ig, "").replace(/<!--\/uzc--\>/ig, "").replace(/^ +| +$/img, ""), c, f = "js"; c = a.match(/<(\w+).*?>(.*?)<\/\1>/i);) a = a.replace(c[0], c[2]);
			c = $("td.posttdMessage > div").width();
			$("head").append('<style type="text/css" id="code-fix">td.posttdMessage {width:' + c + "px!important; max-width:" + c + "px}td.posttdMessage .syntaxhighlighter {max-width:768px}</style>");
			if (f = a.match(/^\s*\[(php|html|css|js|html)(-run)?\]/i))
				a = a.replace(f[0], "");
			else
				f = [0, 'js'];
			$(this).replaceWith('<pre class="brush: ' + f[1] + (f[2] == "-run" ? "; run-script:true": '') + '">' + a + "</pre>")
		}
		if ($(this).is("div.bbQuoteName")) this.innerHTML = this.innerHTML.replace(/Quote/i, "Цитата");
		$(this).is("div.quoteMessage") && !(++d % 2) && $(this).addClass("evenQuote");
		$(this).children("div").each(e, [d])
	},
	[0])
};
quote = {
	activeNode: null,
	blocked: false,
	selectedTxt: null,
	getSelectedText: function () {
		if (document.getSelection) return document.getSelection().toString();
		if (window.getSelection) return window.getSelection().toString();
		if (document.selection) return document.selection.createRange().text;
		return ''
	},
	get_selection: function () {
		quote.blocked = true;
		return quote.selectedTxt = quote.getSelectedText()
	},
	insert: function (b, d, e) {
		if (quote.activeNode && quote.selectedTxt) {
			var c = $(quote.activeNode).closest('tr[id^="post"]');
			if (c.length) {
				b = $('td.nameTd', c).text();
				d = $('a.postNumberLink', c).attr('href')
			}
		}
		e = quote.selectedTxt || uz.helper.posts[e];
		if (d) b = b.replace(/\[|\]/g, function (a) {
			return "&#" + a.charCodeAt(0) + ";"
		}) + " [url=" + d + "]&rarr;[/url]";
		paste("[quote" + (b ? "=" + b: '') + "]" + e + "[/quote]", 0);
		quote.blocked = false;
		quote.activeNode = null
	}
};
$(function () {
	$('body').mouseup(function (a) {
		if (!quote.blocked && quote.getSelectedText()) quote.activeNode = a.target
	})
});
document.body && replaceBBcodes();
