// bbcode.js
var bb_nosignature = SS.cookies.read("nosignature") == 1;
var bb_noimg = SS.cookies.read('noimg') == 1;
var bb_nomovie = SS.cookies.read('nomovie') == 1;
var bb_nomusic = SS.cookies.read('nomusic') == 1;
var bb_noflash = SS.cookies.read('noflash') == 1;
function bb_clubcolor(name,dat)
{
	var lines = dat.split(/\n/g);
	var data = '';
	var count = lines.length;
	for (var i = 0; i < count; i ++)
	{
	if (lines[i].indexOf(":#") != -1) {
	var l = lines[i].split(":#");
	lines[i] = ("■").fontcolor(l[1]) + l[0] + ":#" + l[1];
		}
		data += (i?"<br>":'') + lines[i];
	}
	var html = '' +
		'<form action="previewtheme.php" method="post">'+
		ebg('Club color', '配色方案', '配色方案') + ': '+name+'<input type=hidden name=name value="'+name+'" style=width:104>'+
		'<textarea name=theme style="display:none">'+
		dat +
		'</textarea>\n' +
		'<div style="width:160;height:450;font:12px/14px;background-color:white;font-family:宋体;padding: 2px 2px 2px 2px" class=borderk>'+
		'<nobr>'+
		data+
		'</nobr>'+
		'</div>'+
		'<input type="submit" name=preview value="'+ebg('Preview','预览配色','预览配色')+'" class=fi3 style="width:80">'+
		'<input type="submit" name=download value="'+ebg('Download','下载配色','下载配色')+'" class=fi3 style="width:80">'+
		'<script>document.write("<input type=\'hidden\' name=\'charset\''+
		' value=\'"+(document.charset||document.characterSet)+"\' />")</script>\n'+
		'</form>';
	document.write(html);
}
var bb_code_id = 0;
var bb_code_codes = [];
var bb_code_li_1 = '<li style="margin-left:0px;margin-top:0px;margin-bottom:0px">';
var bb_code_li_2 = '</li>';
var bb_code_pre_1 = "<pre style='font-family:\"Courier New\",\"宋体\",\"simsun\";font-size:11px;line-height:120%" +
		";padding-top:5px;margin-top:0px;margin-bottom:0px;'>";
var bb_code_pre_2 = '</pre>';
var bb_code_ul_1 = "<ul style='list-style-type:decimal;margin-top:0px;line-height:120%'>";
var bb_code_ul_2 = "</ul>";

function bb_code(code)
{
	bb_code_id ++;
	bb_code_codes[bb_code_id] = code;
	var _code = code.replace(/\n/g, "\n" + bb_code_li_2 + bb_code_li_1);
	var html = '' +
		'<a href="#bbcode_start'+bb_code_id+'">' + ebg('Code', "絏", "代码") +'</a>: '+
		'<span onmouseover="clearTimeout(this.tmr);bb_code_showbtns('+bb_code_id+',1)" onmouseout="this.tmr=setTimeout(function(){bb_code_showbtns('+bb_code_id+',0)},100)">' +
		'&gt;<span id="bbcode_btns'+bb_code_id+'" style="display:none">' +
		(!window.clipboardData?'':' [<a href="javascript:;" onclick="bb_code_copy('+bb_code_id+');return false">' + ebg('Copy code', "ī", "拷贝") +'</a>]') +
		' [<a href="javascript:;" onclick="bb_code_open('+bb_code_id+',1);return false">' + ebg('New window', "穝怠", "新窗口") +'</a>]' +
		' [<a href="javascript:;" onclick="bb_code_open('+bb_code_id+',0);return false">' + ebg('No color', "礚肅︹", "无颜色") +'</a>]' +
		'</span>' +
		'</span>' +
		"<div id=bbcode_outer"+bb_code_id+" style='width:100%'>" +
		"<div class=code>" +
		"<a name=bbcode_start"+bb_code_id+"></a>" +
		bb_code_pre_1 +
		bb_code_ul_1 + bb_code_li_1 +
		_code +
		bb_code_li_2 + bb_code_ul_2 +
		bb_code_pre_2 + "</div></div>";
	document.write(html);
	setTimeout("bb_coll_div('bbcode_outer"+bb_code_id+"')");
}
function bb_code_open(id, color) {
	var w = openmaxsl('');
	var m = '';
	var metas = document.getElementsByTagName("meta");
	for (i = 0; i < metas.length; i ++) {
		var outerhtml = metas[i].outerHTML;
		if (outerhtml) m += outerhtml;
	}
	var elm = document.getElementById('bbcode_outer'+id);
	w.document.write('<head>'+m+'\n</head>\n<body>\n'+
		bb_code_pre_1 +
		(color ? bb_code_codes[id] : elm.innerText.replace(/>/, "&gt;").replace(/</, "&lt;")) +
		bb_code_pre_2);
}
function bb_code_showbtns(id,show) {
	document.getElementById('bbcode_btns'+id).style.display = show?'':'none';
}
function bb_code_copy(id) {
	var elm = document.getElementById('bbcode_outer'+id);
	window.clipboardData.setData('Text', elm.innerText);
}
function bb_coll_div(id) {
	var o = document.getElementById(id);
	if (o&&o.style) {
		o.style.overflowX = "auto";
		o.style.width = '100%';
		var maxheight = is.ie? document.body.clientHeight : window.innerHeight;
		if (o.offsetHeight > maxheight) {
			o.style.height = maxheight + "px";
			o.style.overflowY = "auto";
		}
		else {
			o.style.height = (o.offsetHeight + 20) + 'px';
		}
	}

}
function bb_pre(pre)
{
	document.write("<pre style='font-family:宋体,simsun;font-size:12px;line-height:12px' class='bbpre'>");
}
function bb_pre2(pre)
{
	document.write("</pre>");
}
///////////// switcher
var bb_sw_id = 0;
function bb_sw()
{
	bb_sw_id ++;
	var html = "<a href='javascript:;'" +
		" onclick='showHide(\"bb_switcher"+bb_sw_id+"\");this.blur();return(false);'>";
	document.write(html);
}
function bb_sw2()
{
	var html = "</a>" +
		"<span id='bb_switcher"+bb_sw_id+"' style=display:none>";
	document.write(html);;
}
function bb_sw3()
{
	document.write("</span>");
}

///////////// quote
var bb_quote_id = 0;
function bb_qt()
{
	bb_quote_id ++;
	var html = "<blockquote><b>"
	document.write(html);
	bb_qt2();
}
function bb_qt1()
{
	bb_quote_id ++;
	var html = "<blockquote><a href=\"#bbquote_start"+bb_quote_id+"\">" + ebg('Quote', "まノ", "引用") +"</a><b>"
	document.write(html);
}
function bb_qt2()
{
	var html = "" +
		"</b><div id=bbquote_outer"+bb_quote_id+" style=width:100%>" +
		"<div class=quote>" +
		"<a name=bbquote_start"+bb_quote_id+"></a>" +
		"<div style='font-family:\"Courier New\",\"宋体\",\"simsun\"" +
		";padding-top:5px;margin-top:0px'>";
	document.write(html);
}
function bb_qt3()
{
	var html = "</div></div></div></blockquote>";
	document.write(html);
	setTimeout("bb_coll_div('bbquote_outer"+bb_code_id+"')");
}
var _bb_irc_color_map = [
	"#ffffff",
	"#000000",
	"#00007f",
	"#009300",
	"#ff0000",
	"#7f0000",
	"#9c009c",
	"#fc7f00",
	"#ffff00",
	"#00fc00",
	"#009393",
	"#00ffff",
	"#0000fc",
	"#ff00ff",
	"#7f7f7f",
	"#d2d2d2"
];
var _bb_irc_color_map_output = false;
function bb_irc(ret)
{
	var html = '';
	if (!_bb_irc_color_map_output) {
		_bb_irc_color_map_output = true;
		var ww = /*is.ie55 ? 'word-wrap:break-word' : */'word-break:break-all' ;
		var t = "<style>";
		t += "tt{font-family:FixedSys,SimSun,宋体,Songti;" +
			";line-height:18px;font-size:16px;"+ww+"};\n";
		for(var i in _bb_irc_color_map) {
			var _i=parseInt(i).toString(16);
			t += ".c"+_i+" {color:"+_bb_irc_color_map[i]+"}\n"; // nobg
			for(var j in _bb_irc_color_map) {
				var _j= parseInt(j).toString(16);
				t += ".c"+_i+_j+" {color:"+_bb_irc_color_map[i]+"; background-color:"+_bb_irc_color_map[j]+"}\n";
			}
		}
		t += '</style>';
		html += t;
	}
	html += '<table bgcolor="white" class="bbirc" cellpadding="6"><tr><td><tt>';
	if (ret) return html;
	document.write(html);
}
function bb_irc2(ret)
{
	var html = "</tt></td></tr></table>";
	if (ret) return html;
	document.write(html);
}
var bb_htmlblockid = 0;
function bb_html(html)
{
	var tips = ebg("CTRL+A: Select all; CTRL+C: Copy; You may edit before preview.",
				"CTRL+A 匡絏; CTRL+C ī絏; 钡э絏礛箇凝.",
				"CTRL+A 全选代码; CTRL+C 拷贝代码; 可以直接修改代码然后再预览.");
	var capPreview = ebg("Preview HTML","箇凝 HTML 狦","预览 HTML 效果");
	var capHead = ebg("HTML Code","HTML 絏","HTML 代码");
	bb_htmlblockid ++;
	html = "<div>" +
		"<a href=#bbhtml_body" + bb_htmlblockid + " onclick=bb_html_expand("+bb_htmlblockid+")>" +
		capHead + "</a>:<br>" +
		"<a name=bbhtml_body" + bb_htmlblockid + "></a>" +
		"<textarea id=bbhtmlcode_" + bb_htmlblockid +
		" style=\"width:90%;font-family:'宋体','simsun';font-size:12px;line-height:14px\" rows=8>" +
		html +
		"</textarea><br>" +
		"<input type=button value='"+capPreview+"' class=fi3"+
		" onclick='bb_html_dopreview(\""+bb_htmlblockid+"\")'>" +
		"<span style=font-size:12px> [ "+tips+" ]</span>" +
		"<div class=p1 style=width:1;height:2px>&nbsp;</div>"+
		"</div>";
	document.write(html);
}
function bb_html_expand(blockid)
{
	var o = document.getElementById("bbhtmlcode_"+blockid);
	o.style.width = "100%";
	var docheight = is.ie? document.body.clientHeight : window.innerHeight;
	o.style.height = Math.max(docheight - 40, 50);
}
function bb_html_dopreview(blockid)
{
	var wnd=_open("", "slM");
	if (!wnd) return;
	var doc = wnd.document;
	doc.open("text/html", true);
	doc.write(document.getElementById("bbhtmlcode_"+blockid).value);
	doc.close();
}
function bb_player(params) {
	if (bb_nomusic && bb_nomovie && bb_noflash) return;
	InsertPlayer(params);
}
function bb_music(url, title) {
	if (bb_nomusic) return bb_url(url, title, ec('Music', '音乐'));
	if (bb_checkUrl('music', url)) InsertMusic(url, title);
}
function bb_voice(url, title) {
	if (bb_nomusic) return bb_url(url, title, ec('Voice', '声音'));
	if (bb_checkUrl('voice', url)) InsertMusic(url, title);
}
function bb_movie(url, title) {
	if (bb_nomovie) return bb_url(url, title, ec('Movie', '电影'));
	if (bb_checkUrl('movie', url)) InsertMovie(url, title);
}
function bb_flash(url, w, h ) {
	if (bb_noflash) return bb_url(url, url, ec('Flash', 'Flash'));
	if (bb_checkUrl('flash', url)) InsertFlash(url, w, h );
}
function bb_dcr(url, w, h ) {
	if (bb_noflash) return bb_url(url, url, ec('Director', 'Director'));
	if (bb_checkUrl('dcr', url)) InsertDcr(url, w, h );
}
function bb_checkUrl(codetag, url)
{
	url=url||'';
	if (url.substr(0, 7).toLowerCase() == 'file://'
	 || url.substr(1, 1) == ':'
	) {
		document.write('['+codetag + '] ' +
			ebg('File must upload', '文件必须上传', '文件必须上传').fontcolor('red') +
			': <u>' + url+ '</u>'
			);
		return false;
	}
	if (url.indexOf('://') == -1) {
		document.write('['+codetag + '] ' +
			ebg('URL wrong', '地址错误', '地址错误').fontcolor('red') +
			': <u>' + url+ '</u>'
			);
		return false;
	}
	return true;
}
function bb_url(url, param, name)
{
	document.write((name?name+': ':'')+'<a href="'+url+'" target="_blank">'+(param||url)+'</a>');
}
function bb_img(url, param)
{
	if (bb_noimg) return bb_url(url, param, ec('Image', '图片'));
	if (param&&param.indexOf("'") != -1) return;
	if (bb_checkUrl('img', url))
	{
		var html;
		var lparam = (param||'').toLowerCase();
		if (lparam == 'left' || lparam == 'right' || lparam == 'center') {
			html = "<img align='"+param+"' src=\""+url+"\" class='bbimg' vspace=5 border=0 hspace=10>";
		} else {
			var mystyle = "cursor:move";
			if (param) {
				if (param == '%') {
					mystyle += ";width:100%";
				} else if (param.indexOf("%") != -1) {
					if (is._ie6) {
						mystyle += ";zoom:" + param;
					} else {
						mystyle += ";width:" + param;
					}
				}
			}
			html = "<img src=\""+url+"\" class='bbimg' border=0 " +
				" onmousedown='if(self.startMove)startMove(this,event)'"+
				" onmousewheel='return bb_img_onmousewheel(event,this)'"+
				" style='"+mystyle+"'>";
		}
		document.write(html);
	}
}
function bb_img_onmousewheel(e, o)
{
	var zoom = parseInt(o.style.zoom, 10) || 100;
	zoom += event.wheelDelta / 12;
	if (zoom > 0) o.style.zoom = zoom + '%';
	return false;
}
function bb_appendUrl(value, url) {
	var s = url.split(".");
	var ext = s[s.length-1];
	var code = '';
	switch (ext.toLowerCase())
	{
	case 'gif':
	case 'jpg':
	case 'jpeg':
	case 'png':
		code = '[img]' + url + '[/img]';
		break;
	case 'swf':
		code = '[flash]' + url + '[/flash]';
		break;
	default:
		code = '[url]' + url + '[/url]';
	}
	value = (value||'').toString();
	if (value.indexOf(code) == -1)
	{
		if (value) value += "\n";
		value += code + "\n";
	}
	return value;
}
