
var g_str = '';
var g_uid = 0;
var g_org = '';

function Alert(){alert("alert");};
function Init_g_str(){g_str='';};
function Set_g_str(s){g_str=s;};
function Init_g_uid(){g_uid=0;};
function Set_g_uid(u){g_uid=u;};
function Init_g_org(){g_org='';};
function Set_g_org(s){g_org=s;};

var im = (function ($) {
	return {
		CallC:function(str){
			window.CallC.postMessage(str);
		},
		Ready:function(){
			var jv = {};
			jv.cmd = "Ready";
			var str = JSON.stringify(jv);
			this.CallC(str);
		},
		HtmlEncode: function (str) {
			return $("<div/>").text(str).html();
			//return $("<textarea />").text(str).html();
		},
		HtmlDecode: function (str) {
			return $("<div/>").html(str).text();
			//return $("<textarea />").html(str).text();
		},
		IntToRgb(c){
			const b=(c& 0xff0000)>>16;
			const g=(c& 0xff00)>>8;
			const r=(c& 0xff);
			return `rgb(${r},${g},${b})`;
		},
		_week_day: ["[日]", "[一]", "[二]", "[三]", "[四]", "[五]", "[六]"],
		WeekNum: function (ts) {
			var dt = new Date(ts * 1000);
			var b = new Date(dt.getFullYear(), 0, 1);
			var days = Math.floor((dt - b) / (24 * 60 * 60 * 1000));
			return Math.ceil((dt.getDay() + 1 + days) / 7);
		},
		TimeStr: function (ts) {
			var cur = new Date();
			var dt = new Date(ts * 1000);
			var stime = (dt.getHours() < 10) ? ("0" + dt.getHours().toString()) : dt.getHours().toString();
			stime += ":";
			stime += (dt.getMinutes() < 10) ? ("0" + dt.getMinutes().toString()) : dt.getMinutes().toString();
			var sdate = (dt.getMonth() < 10) ? ("0" + dt.getMonth().toString()) : dt.getMonth().toString();
			sdate += "-";
			sdate += (dt.getDate() < 10) ? ("0" + dt.getDate().toString()) : dt.getDate().toString();
			if (cur.getFullYear() == dt.getFullYear()) {
				if (cur.getMonth() == dt.getMonth()) {
					if (cur.getDate() == dt.getDate()) return stime;

					var wn = this.WeekNum(ts);
					if (this.WeekNum(cur.getTime() / 1000) == wn) {
						return this._week_day[dt.getDay()] + " " + stime;
					}
				}
				return sdate + this._week_day[dt.getDay()] + " " + stime;
			}
			return dt.getFullYear() + "-" + sdate + this._week_day[dt.getDay()] + " " + stime;
		},
		SizeStr: function (sz) {
			if (sz < 1024) return sz.toString() + "B";

			if (sz < 1024 * 1024) {
				return (sz / 1024).toFixed(2).toString() + "K";
			}
			else if (sz < 1024 * 1024 * 1024) {
				return (sz / (1024 * 1024)).toFixed(2).toString() + "M";
			}
			else if (sz < 1024 * 1024 * 1024 * 1024) {
				return (sz / (1024 * 1024 * 1024)).toFixed(2).toString() + "G";
			}
			return (sz / (1024 * 1024 * 1024 * 1024)).toFixed(2).toString() + "T";
		},
		TransStatus: function (ts) {
			if (ts == 0) return "等待";
			else if (ts == 1) return "传送中";
			else if (ts == 2) return "正在重试";
			else if (ts == 3) return "完成";
			else if (ts == 4) return "已失败";
			else if (ts == 5) return "已取消";
		},
		SetClick: function (json) {
			var jr = jQuery.parseJSON(json);
			if (!(jr instanceof Array)) return;
			for (var i = 0; i<jr.length; i++) {
				var f=jQuery("selector")[jr[i].f];//eval(jr[i].f);//window[jr[i].f];
				if(f==null)continue;
				var v=$(jr[i].i);
				if(v==null || v.length<=0)continue;
				v.off("click");
				v.click(function(){f($(this));});
			}
		},
		MSG_TYPE_TEXT:1,
		MSG_TYPE_ALARM:2,
		MSG_TYPE_FILE:7,
		MSG_TYPE_OFFLINE:8,
		MSG_TYPE_DIR:9,
		MSG_TYPE_PIC:10,
		MSG_TYPE_SOUND:11,
		MSG_TYPE_VIDEO:12,
		MSG_TYPE_FILE_FINISHED:15,
		MSG_TYPE_AT:17,
	}

})(jQuery);

