/*动态加载，备用http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml
function loadjscssfile(filename, filetype){
 	if (filetype=="js"){ //if filename is a external JavaScript file
  		var fileref=document.createElement('script')
  		fileref.setAttribute("type","text/javascript")
  		fileref.setAttribute("src", filename)
 	}
 	else if (filetype=="css"){ //if filename is an external CSS file
  		var fileref=document.createElement("link")
  		fileref.setAttribute("rel", "stylesheet")
  		fileref.setAttribute("type", "text/css")
  		fileref.setAttribute("href", filename)
 	}
 	if (typeof fileref!="undefined"){
  		document.getElementsByTagName("head")[0].appendChild(fileref)
 	}
}
//loadjscssfile("/media/js/jquery.dialog.js", "js") //dynamically load and add this .js file
*/

//通用去除html标签函数
function html(text) {
	text = text.replace(/&/g, "&amp;");
	text = text.replace(/"/g, "&quot;");
	text = text.replace(/</g, "&lt;");
	text = text.replace(/>/g, "&gt;");
	text = text.replace(/'/g, "&#146;");
	return text;
}
function br(text){
	text = html(text).replace(/\n/g, "<br/>");
	return text;	
}
//通用检测函数
function param_verify(v, f) { //长度
	var patrn = /^[0-9]+,[0-9]+$/;
	if (patrn.exec(f)) {
		var tmp = f.split(",");
		if (v.length < tmp[0] && tmp[0] != 0) return false;
		if (v.length > tmp[1] && tmp[1] != 0) return false;
	} //code格式
	if (f == "code") {
		patrn = /^[a-z|0-9][a-z|0-9|_]+[a-z0-9]$/;
		if (!patrn.exec(v)) return false;
	} //正整数不包括0
	if (f == "int") {
		patrn = /^[1-9]\d*$/;
		if (!patrn.exec(v)) {
			alert(v);
			return false;
		}
	} //用户字符串格式
	if (f == "loginname") {
		patrn = /^[a-zA-Z0-9]{4,16}$/;
		if (!patrn.exec(v)) return false;
	}
	return true;
} //设置cookies
function setCookies(n, v, e) {
	var Then = new Date();
	Then.setTime(Then.getTime() + 1000 * e); //小时
	document.cookie = n + "=" + v + ";expires=" + Then.toGMTString();
} //获得cookies
function getCookies(n) {
	var arr = document.cookie.match(new RegExp("(^| )" + n + "=([^;]*)(;|$)"));
	if (arr != null) return unescape(arr[2]);
	return "";
}
//获得屏幕的宽高 返回数组
function fGetViewportSize() { //
	var myWidth = 0,
	myHeight = 0;
	if (typeof(window.innerWidth) == 'number') { //Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) { //IE 6
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) { //IE 4
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	} //alert(myWidth+","+ myHeight);
	return [myWidth, myHeight];
} //获得屏幕左上角的绝对xy值
function getScrollXY() {
	var x, y;
	if (document.body.scrollTop) {
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	} else {
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	return [x, y];
} //userbox相关---------------------------------------
var org_cursor;
function displayUserBox(uid, obj) {
  org_cursor = $(obj).css('cursor');
	$(obj).css('cursor','progress');
  UserBoxToggle = true;
	setTimeout(function () {
		displayBox(uid, obj);
	},
	500);
}
function displayBox(uid, obj) {
	if (!UserBoxToggle) return false;
	var exp = 60; //cookies超时时间
	var boxW = 300; //盒子宽度
	var boxH = 156; //盒子高度
	//计算位置
	var XY = fGetViewportSize(); //屏幕大小
	var screenXY = getScrollXY(); //屏幕左上角相对body的位置
	var x = XY[0] / 2;
	var y = XY[1] / 2; //alert("xy:"+x+","+y);
	var obj = jQuery(obj);
	var offset = obj.offset(); //alert("obj:"+offset.left+","+offset.top);
	if ((offset.left - screenXY[0]) < x) { //x
		_left = offset.left + obj.width() + 8;
	} else {
		_left = offset.left - boxW - 8;
	}
	if ((offset.top - screenXY[1]) < y) { //y
		_top = offset.top + 8;
	} else {
		_top = offset.top - boxH - 8;
	} //alert("box:"+_left+","+_top);
	if (!param_verify(uid, "int")) return false;

  var tmp = getCookies("u" + uid);
	if (tmp != "") { //alert(tmp);
		v = eval('(' + tmp + ')');
		drawUserBox(v, _left, _top);
    obj.css('cursor',org_cursor);
	} else {
    $.ajax({
			type: "POST",
			url: "/ajax.php?action=userinfo",
			data: "uid=" + uid,
			async: false,
			success: function (m) { //alert(m);
				v = eval('(' + m + ')');
				if (v.status) {
					setCookies("u" + v.userid, m, exp);
					drawUserBox(v, _left, _top);
          obj.css('cursor',org_cursor);
				}
			},
			error: function () {
				//alert("transfrom error...");
			}
		});
	}
}
var UserBoxToggle = false;
function drawUserBox(m, l, t) {
	$("#UserBox").hide();
	if (!$("#UserBox")[0]) { //如果没有对象,create it!
		var tmp = "<div id=\"UserBox\" class=\"m_card\" style=\"hidden\">";
		tmp += "	<ul class=\"box_nav\">";
		tmp += "		<li><a id=\"pm\" class=\"letter_icon\" href=\"#\">Send PM</a></li>";
		//tmp += "		<li><a id=\"af\" class=\"add_icon\" href=\"#\">Add to Friends</a></li>";
		tmp += "		<li><div id=\"af\"></div></li>";
		tmp += "		<li><a id=\"vz\" class=\"zone_icon\" href=\"#\">Visit</a></li>";
		tmp += "	</ul>";
		tmp += "	<div class=\"clear\"></div>";
		tmp += "	<div class=\"toleft\"><a id=\"imglink\"href=\"\"><img id=\"avatar\" src=\"\" width=\"100\" height=\"100\"/></a></div>";
		tmp += "	<div class=\"nr\">";
		tmp += "		<ul class=\"star\">";
		tmp += "			<li><span>Name</span><b id=\"login_name\"></b></li>";
		tmp += "			<li><span>Status</span><b id=\"onlinestatus\"></b></li>";
		tmp += "			<li style='height:20px'><span>Level</span><div id=\"grade\" style=\"padding-top:3px;float:left;\"></div></li>";
		tmp += "			<li style='overflow:hidden;line-height:140%;word-wrap:break-word;height:46px;'><a id=\"mood\" href=\"#\">#</a></li>";
		tmp += "		</ul>";
		tmp += "	</div>";
		tmp += "	<div class=\"clear\"></div>";
		tmp += "</div>";
		$("body").append(tmp);
		$("#UserBox").mouseover(function () {
			UserBoxToggle = true;
		});
		$("#UserBox").mouseout(function () {
			hideUserBox();
		});
	} //站内信 好友 空间
	$("#UserBox #pm").attr("href", "/zone/messages/send.php?to=" + m.loginname);
	//$("#UserBox #af").attr("href", "/zone/friends/add.php?to=" + m.loginname);
	$("#UserBox #af").html("<a class=\"add_icon\" href=\"java"+"script:;\" onclick=\"addUser('"+m.loginname+"')\">Add to Friends</a>");
	$("#UserBox #vz").attr("href", "/zone/" + m.loginname + "/index.htm");
	$("#UserBox #imglink").attr("href", "/zone/" + m.loginname + "/index.htm"); //头像
	$("#UserBox #avatar").attr("src", "/avatar.php?id=" + m.userid + "&size=100"); //等级
	$("#UserBox #grade").html(getLevIcon(m.lev, m.gender));
	$("#UserBox #grade").attr("title", "Level: " + m.lev); 
	//登录名
	$("#UserBox #login_name").html(m.loginname); 
	// 状态
	if(m.onlinestatus){
		$("#UserBox #onlinestatus").html("<font color='green'>Online</font>"); 
	}else{
		$("#UserBox #onlinestatus").html("Offline");
	}
	//心情
	if (m.mood != null) {
		if(m.mood.length>60) m.mood=m.mood.substr(0,58)+"..";
		$("#UserBox #mood").html(html(m.mood));
		$("#UserBox #mood").attr("href", "/zone/" + m.loginname + "/mood/index.htm");
	} else {
		$("#UserBox #mood").html("");
		$("#UserBox #mood").attr("href", "java"+"script:;");
	} //颜色
	$("#UserBox").removeClass("m_card");
	$("#UserBox").removeClass("f_card");
	if (m.gender == "M") {
		$("#UserBox").addClass("m_card");
	} else {
		$("#UserBox").addClass("f_card");
	} //位置
	$("#UserBox").css("position", "absolute");
	$("#UserBox").css("z-index", "30000");
	$("#UserBox").css("left", l + "px");
	$("#UserBox").css("top", t + "px");
	$("#UserBox").show();
}
function hideUserBox() {
	UserBoxToggle = false;
	setTimeout(function () {
		if (!UserBoxToggle) {
			$("#UserBox").hide();
		}
	},
	300);
}
function getLevIcon(lev, gender) {
	if (lev == 0) return "<b class='" + gender + "_lev'>&nbsp;</b>"; //获得
	var tmp = 0;
	var D = Math.floor(lev / 27);
	tmp = lev % 27;
	var C = Math.floor(tmp / 9);
	tmp = tmp % 9;
	var B = Math.floor(tmp / 3);
	tmp = tmp % 3;
	var A = tmp; //alert("D:"+D+",C:"+C+",B:"+B+",A:"+A);
	tmp = "";
	if (D > 0) {
		for (var i = 0; i < D; i++) {
			tmp += "<b class='" + gender + "_lev_d'>&nbsp;</b>";
		}
	}
	if (C > 0) {
		for (var i = 0; i < C; i++) {
			tmp += "<b class='" + gender + "_lev_c'>&nbsp;</b>";
		}
	}
	if (B > 0) {
		for (var i = 0; i < B; i++) {
			tmp += "<b class='" + gender + "_lev_b'>&nbsp;</b>";
		}
	}
	if (A > 0) {
		for (var i = 0; i < A; i++) {
			tmp += "<b class='" + gender + "_lev_a'>&nbsp;</b>";
		}
	} //return "<div class='clear'>"+tmp+"</div>";
	return tmp;
} //getLevIcon(0);
//结束userbox---------------------------------
//AD------------------------------------------
/*
$(document).ready(function () {
	displayAD();
});

$(window).scroll( function(){
	displayAD();
}); 
$(window).resize(function () {
	displayAD();
});
function displayAD() {
	var adW = 120;
	var adH = 600;
	var ContentW = 990;
	var xy = fGetViewportSize();
	if (xy[0] < (ContentW + (adW + 25) * 2)) {
		$("#marketBox").hide();
		return false;
	} //计算top位置
	//var screenXY = getScrollXY();
	var t = 10; //var l = xy[0]-((xy[0]-ContentW)/4+(adW/2));
	var l = xy[0] - adW - 25;
	if (!$("#marketBox")[0]) { //如果没有对象,create it!
		var tmp = "<div id=\"marketBox\" style=\"height:" + adH + "px;width:" + adW + "px;display:hidden;position:fixed;z-index:90;\">";
		tmp += "	<iframe src=\"/sg/ad.html\" scrolling=\"no\" frameborder=\"0\" frameborder=\"0\" height=\"600px\" width=\"120px\"></iframe>";
		tmp += "</div>"; //$("body").append(tmp);
		// jacky 改的
	}
	$("#marketBox").css("top", t + "px");
	$("#marketBox").css("left", l + "px");
	$("#marketBox").show();
}
*/ 
/*
//------User menu--------------------------------------------

$(document).ready(function () {
	if (!$("#StatusCenter")[0]) return false;
	var c = $("#StatusCenter");
	c.mouseover(function () {
		showUserMenu();
	});
});
*/
// add friend------
function addUser(u){
	$("#UserBox").hide();//关闭用户名片
	dialog("","loading","","");
		$.ajax({
			type: "POST",
			url:      "/zone/friends/ajax.php?type=addfriend",
			data:     "u="+u,
			async:    false,
			success:  function(m){
				addfriend_v = eval('('+m+')');
			},
			error:    function(){
				alert("transfrom error...");
			}
		});
		if(addfriend_v.loginstatus){
			dialogHiddenLoading();
			if(addfriend_v.status==1){//success
		        dialog("","alert:Invitation has been sent to "+u+" successfully.","400px","auto");
			}else if(addfriend_v.status==2){ //rejected
				dialog("","alert:Your invitation is rejected by "+u+".","400px","auto");
			}else if(addfriend_v.status==3){// ready friend
				dialog("","alert:"+u+" is already your friend.","400px","auto");
	        }else if(addfriend_v.status==4){// padding
				dialog("","alert:You have sent friend request to "+u+" before.","400px","auto");
			}else{
		        alert("Errors.");
		    }
		}else{
	        location.href="/zone/friends/add.php?to="+u;
	    }
}
// end add friend------

// change page
function changePage(v,url){
	v = $.trim(v);
	if(v!=parseInt(v)) return false;
	location.href=url.replace(/%s%/, v);
	
}

//判断无素是否在一个数组中
function in_array(needle, haystack) {
	if(typeof needle == 'string' || typeof needle == 'number') {
		for(var i in haystack) {
			if(haystack[i] == needle) {
					return true;
			}
		}
	}
	return false;
}