﻿/* **************************************************
* SWT - Sansi.Org's Website Toolkit
* based on jQuery
* 
* Sansi.Org, 2008
* by oldj.wu@gmail.com 
* ^_^
************************************************** */

//从左边截取n个字符，如果包含汉字，则汉字按两个字符计算
String.prototype.left = function (n){
	return this.slice(0, n - this.slice(0, n).replace(/[\x00-\xff]/g, "").length);
};
//从右边截取n个字符，如果包含汉字，则汉字按两个字符计算
String.prototype.right = function (n){
	return this.slice(this.slice(-n).replace(/[\x00-\xff]/g, "").length - n);
};
//字符串长度，如果包含汉字，则汉字按两个字符计算
String.prototype.len2 = function () {
	return "中".length == 1 ? (this.length + this.replace(/[\x00-\xff]/g, "").length) : this.length;
};

//类似于Python的partition
String.prototype.partition = function (sp) {
	var i = this.indexOf(sp);
	return i != -1 ? [this.substring(0, i), sp, this.substring(i + 1)] : [this, sp, ""];
};

//去掉字符串两边的空格
String.prototype.trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");
};

//将字符串进行html编码
String.prototype.htmlEncode2 = function () {
	return this.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\r\n/g, "<br />").replace(/\n/g, "<br />").replace(/\r/g, "<br />").replace(/〈\/textarea〉/ig, "&lt;/textarea&gt;");
};

//将字符串进行html解码
String.prototype.htmlDecode2 = function () {
	return this.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/<br \/>/ig, "\n").replace(/<br>/ig, "\n").replace(/<\/textarea>/ig, "〈/textarea〉");
};

var swt = {
	getLeft: function (e){
		var offset = e.offsetLeft;
		if (e.offsetParent != null) offset += swt.getLeft(e.offsetParent);
		return offset;
	},
	getTop: function (e){
		var offset = e.offsetTop;
		if (e.offsetParent != null) offset += swt.getTop(e.offsetParent);
		return offset;
	},
	sInform: {
		//小提示
		show: function (e, s) {
			var si = $("#swt_sInform");
			if (si.length == 0) {
				$("<div id=\"swt_sInform\"><div id=\"swt_sInform_msg\"></div><div id=\"swt_sInform_arrow\"></div></div>").appendTo($("body"));
				si = $("#swt_sInform");
			}
			//alert(swt.getLeft(e) + ", " + swt.getTop(e) + "\n" + s);
			if ($.browser.msie && $.browser.version == "6.0")
				si.width((s.len2() / 2 + 2) + "em");
			si.css({
				top: (swt.getTop(e) - 25) + "px",
				left: (swt.getLeft(e) - 10 + e.offsetWidth / 4) + "px"
			}).show().find("#swt_sInform_msg").html(s);
		},
		hide: function () {
			$("#swt_sInform").hide();
		}
	},
	overlay: function (show) {
		if (show) {
			$("<div id=\"swt_overlay\"></div>").appendTo($("body"));
			$("#wrapper select").css("visibility", "hidden");
			var h = Math.max(document.documentElement.clientHeight, document.documentElement.scrollHeight);
			$("#swt_overlay").height(h + "px").css("opacity", "0.5").fadeIn("normal");
			$(window).resize(function () {
				var h = Math.max(document.documentElement.clientHeight, document.documentElement.scrollHeight);
				$("#swt_overlay").height(h + "px");
			});
		} else {
			$("#swt_overlay").fadeOut("normal", function () {$(this).remove();});
			$("#wrapper select").css("visibility", "inherit");
		}
	},
	window: {
		show: function (ob) {
			//ob title: 标题, html: 内部显示内容, width: 宽, height: 高, onshow: 显示后执行的函数, onclose: 关闭后执行的函数
			swt.overlay(true);
			if (!ob.width) ob.width = 400;
			if (!ob.height) ob.height = 225;
			var s = "" +
			"<div class=\"swt_win\"><table border=\"0\" class=\"swt_win_table\" cellpadding=\"0\" cellspacing=\"0\">" +
			"<tr><td class=\"swt_win_tl op\"></td><td class=\"op\"></td><td class=\"swt_win_tr op\"></td></tr>" +
			"<tr><td class=\"op\"></td><td class=\"swt_win_td55\">" +
			"<div class=\"swt_win_toolBar\"><img src=\"/style/img/close.gif\" class=\"close\" alt=\"关闭\" /><span class=\"swt_win_title\">" + ob.title + "</span></div>" + 
			"<div class=\"swt_win_body\">" + (ob.html || "<span class=\"swt_loading\">加载中...</span>") + "</div>" +
			"</td><td class=\"op\"></td></tr>" +
			"<tr><td class=\"swt_win_bl op\"></td><td class=\"op\"></td><td class=\"swt_win_br op\"></td></tr>" +
			"</table></div>";
			$("body").append($(s));
			var keepInCenter = function () {
				var _w = document.documentElement.clientWidth,
					_h = document.documentElement.clientHeight,
					_t = document.documentElement.scrollTop;
				var _l = (_w - ob.width) / 2, _t2 = (_h - ob.height) / 2 + _t;
				$(".swt_win").css({"left": ((_l >= 0) ? _l : 0) + "px",
					"top": ((_t2 >= 0) ? _t2 : 0) + "px"});
			};
			keepInCenter();
			$(".swt_win").css({"width": (ob.width + 20) + "px",
				"height": (ob.height + 20) + "px"
			}).fadeIn("normal", function () {
				$(this).focus();
			});
			if ($.browser.msie && $.browser.version == "6.0")
				$(".swt_win .op").css("opacity", "0.75");
			$(".swt_win .swt_win_body").height(((ob.height && !isNaN(ob.height) && ob.height > 84) ? ob.height - 84 : 0) + "px" || "auto");
			$(".swt_win .swt_win_body").width((ob.width - 94) + "px");
			$(".swt_win .close").click(function () {
				if (typeof(ob.onclose) == "function")
					ob.onclose();
				$(".swt_win").fadeOut("normal", function () {$(this).remove();});
				$(".swt_win_back").fadeOut("normal", function () {$(this).remove();});
				swt.overlay(false);
			});
			$(window).resize(keepInCenter);
			$(window).scroll(keepInCenter);
			
			if (typeof(ob.onshow) == "function")
				ob.onshow();
		},
		set: function (ob) {
			if (ob.title)
				$(".swt_win .swt_win_title").html(ob.title);
			if (ob.body)
				$(".swt_win .swt_win_body").html(ob.body);
		},
		close: function () {
			$(".swt_win .close").click();
		}
	}
};

var safecode = {
	reload: function () {
		$("img#img_safecode").attr("src", "/sc/" + Math.random());
	},
	val: function () {
		return $("input#safecode").val();
	}
};

$(document).ready(function () {
	$("img#img_safecode").click(function () {
		$(this).attr("src", "/sc/" + Math.random());
	});
});