/* common scripts */

function ubb(s)
{
	s = s.replace(/<input[^>]*?\$ubb\-swf\$:\$(?:http:\/\/)?(.*?)\$end\$[^>]*?>/ig, "<div class=\"swf-object\">$1</div>");
	s = s.replace(/(http:\/\/([\w-]+\.)+[\w-]+(:\d{0,5})?(\/[\w\- \.\/\?%&=\[\]]*)?)/ig, "<a href=\"$1\">$1</a> [<a href=\"$1\" target=\"_blank\">^</a>]")
	s = s.replace(/>(http:\/\/([\w-]+\.)+[\w-]+(:\d{0,5})?(\/[\w\d- .\/?%&=\[\]]*)?\.(png|gif|jpg|jpeg))</ig, "><img src=\"$1\" /><")
	s = s.replace(/\[member\](\w+)\[\/member\]/ig, " <a href=\"/member/$1\" title=\"$1's homepage\">$1</a> ");
	s = s.replace(/(\w)&nbsp;(\w)/g, "$1 $2");

	return s;
}

String.prototype.ubb = function () {
		return ubb(this);
};

(function ($) {
	$.ubb = function (o) {
		$(o).each(function () {
			var o2 = $(this),
				s = o2.html();
			o2.html(s.ubb());

			if (typeof swfobject != "undefined") {
				o2.find("div.swf-object").each(function () {
					page.setWide();
					var swf_src = $(this).html(),
						swf_id = ("swf-object-" + Math.random()).replace(".", ""),
						flashvars = {},
						params = {
							wmode: "opaque"
						},
						attributes = {
							id: swf_id + "-swf",
							name: "swf-object"
						};
					if (swf_src.indexOf("/") != 0)
						swf_src = "http://" + swf_src;
					$(this).attr("id", swf_id);
					swfobject.embedSWF(swf_src, swf_id, "640", "480", "10.0.0", false, flashvars, params, attributes);
				});
			}
		});
	};
})(jQuery);

function CountDownRedirect(second_box, to_url) {
	this.second_box = second_box; // jquery object
	this.to_url = to_url;
	this.n = Math.floor(this.second_box.html().replace(/[^\d]/g, "") || 5);
}

CountDownRedirect.prototype = {
	start: function () {
		this._count();
	},
	_count: function () {
		if (this.n <= 0) {
			location.href = this.to_url;
			return;
		}
		var _this = this;
		this.n --;
		this.second_box.html(this.n);
		setTimeout(function () {
			_this._count();
		}, 1000);
	}
};

var page = {
	is_wide: false,
	setWide: function () {
		if (this.is_wide) return;
		$("#wrapper").css("width", 960);
		$("#body #main").css("width", 780);
		this.is_wide = true;
	}
};

var pageGrid = {
	show: function () {
		var _this = this,
			ob = $("#pageGrid");
		if (ob.length == 0) {
			$("#wrapper").append("<div id=\"pageGrid\"></div>");
			ob = $("#pageGrid");
			ob.click(function () {
				_this.hide();
			});
		}
		var h = $("#wrapper")[0].offsetHeight;
		ob.css("height", h + "px");
		ob.show();
	},
	hide: function () {
		$("#pageGrid").hide();
	}
};

var popWin = {
	_ob: null, // a jQuery object
	_ob_overlay: null,
	_ob_title: null,
	_ob_content: null,
	_w: 480,
	_h: 360,
	_isShow: false,
	_init2: function () {
		this._ob = $("#popWin");
		this._ob_overlay = $("#popWin_overlay");
		this._ob_title = $("#popWin_title");
		this._ob_content = $("#popWin_content");
	},
	init: function () {
		if ($("#popWin").length > 0) {
			this._init2();
			return;
		}
		var _this = this,
			html = '<div id="popWin_overlay"></div>'
			+ '<div id="popWin"><div id="popWin_wrapper">'
			+ '<div id="popWin_shade"></div>'
			+ '<div id="popWin_main">'
				+ '<div id="popWin_head">'
				+ '<a href="#" class="close">关闭</a>'
				+ '<strong id="popWin_title"></strong>'
				+ '</div>'

				+ '<div id="popWin_body">'
				+ '<div id="popWin_content"></div>'
				+ '</div>'
			+ '</div>'
			+ '</div></div>';
		$("body").append(html);
		this._init2();
		this._ob.find("a.close").click(function () {
				_this.hide();
				return false;
			});
		$(window).scroll(function () {
				if (_this._isShow)
					_this.locate();
			});
		$(window).resize(function () {
				if (_this._isShow)
					_this.locate();
			});
	},
	show: function (param) {
		if (this._ob == null)
			this.init();
		var _this = this,
			w0 = param.width || 480,
			h0 = param.height || 360;
		this._w = w0 >= 200 ? w0 : 200;
		this._h = h0 >= 100 ? h0 : 100;
		this.setSize();
		this.locate();
		this.setTitle(param.title);
		this.setContent(param.content)
		this._ob_overlay.show();
		this._ob.show();
		this._isShow = true;
		this._onHide = param.onHide || null;

		if (param.action) {
			(param.method != "post" ? $.get : $.post)(param.action,
					param.params ? param.params : {},
					function (r) {
						_this.setContent(r);
						if (typeof param.afterHandler == "function")
							param.afterHandler.call(param.afterHandler, r);
					}
				);
		}
	},
	hide: function () {
		if (typeof(this._onHide) == "function")
			this._onHide();
		this._ob_overlay.hide();
		this._ob.hide();
		this._isShow = false;
	},
	setSize: function () {
		this._ob_overlay.css({
				width: document.documentElement.scrollWidth,
				height: document.documentElement.scrollHeight
			});
		this._ob.css({
				width: this._w,
				height: this._h
			});
		$("#popWin_shade").css({
				width: this._w - 5,
				height: this._h - 5
			});
		$("#popWin_main").css({
				width: this._w - 7,
				height: this._h - 7
			});
		$("#popWin_body").css("height", this._h - 47);
	},
	reSetWidth: function (w) {
		this._w = w;
		this.setSize();
		this.locate();
	},
	reSetHeight: function (h) {
		this._h = h;
		this.setSize();
		this.locate();
	},
	locate: function () {
		var w = document.documentElement.clientWidth,
			h = document.documentElement.clientHeight,
			t = document.documentElement.scrollTop + document.body.scrollTop,
			t2 = t + (h - this._h) / 2;
		this._ob.css({
				top: t2 > 0 ? t2 : 0,
				left: (w - this._w) / 2,
				width: this._w,
				height: this._h
			});
	},
	setTitle: function (val) {
		this._ob_title.html(val || "加载中...");
	},
	setContent: function (val) {
		this._ob_content.html(val || "<div class=\"loading\">加载中，请稍候...</div>");
	}
};

function SmallHint(baseObj, msg, id) {
	this.id = id || "smallhint-" + cf.rndStr();
	this.base = baseObj;
	this.msg = msg;

	this.init();
}

SmallHint.prototype = {
	init: function () {
		var _this = this,
			html = "<div class=\"smallhint\" id=\"" + this.id + "\">"
				+ "<span>" + this.msg + "</span>"
				+ "</div>";
		this.base.append(html)
		this.obj = $("#" + this.id);
		this.span = this.obj.find("span");
		this.obj.click(function () {
			_this.hide();
		});
	},
	show: function (msg, x, y) {
		this.msg = msg || this.msg;
		this.span.html(msg);
		this.obj.css({
			"left": x,
			"top": y
		});
		this.obj.show();
	},
	hide: function () {
		this.obj.hide();
	}
};

SmallHint.items = {};

var cf = { // common functions
	stars: function () {
		$("span.stars").each(function () {
			var v = this.innerHTML.replace(/(^\s+|\s+$)/g, "");
			if (v.match(/^\d+$/)) {
				v = Math.floor(v);
				if (0 <= v && v <= 5) {
					var s = "";
					for (var i = 0, j = 0; i < 5; i ++) {
						j = i >= v ? 1 : 2;
						s += "<img src=\"/css/img/star_" + j + ".gif\" class=\"s_" + (i + 1) + "\" />";
					}
					this.innerHTML = s;
				}
				$(this).addClass("val_" + v);
			}
		});
	},
	rndStr: function (len) {
		var s0 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
			n = len || 16;
		for (var i = 0, s = ""; i < n; i ++)
			s += s0.substr(Math.floor(Math.random() * n), 1);
		return s;
	},
	updateSafecode: function (ipt, wrapper) {
		var cid = "img_safecode_" + this.rndStr();
		ipt.val("");
		wrapper.html("<img src=\"/safecode/" + cf.rndStr() + "/\" class=\"img_safecode\" id=\"" + cid + "\" />");
	}
};

$(document).ready(function () {
	//pageGrid.show();
	$("p.ubb-fail").hide();
	$("div.pagination select.gotoPageOptions").change(function () {
		location.href = $(this).val();
	});
});

