$(document).ready(function() {
	$("#message").focus(function() {
		hiddenTip("message");
	});
})
//取得光标的坐标（用rd.x/y就可以读取获取的元素的坐标了）
function getDim(htmlObj){
	var rd = {x:0,y:0}
	do{
		rd.x += htmlObj.offsetLeft	//获得对象相对于上级（父）对象的位置坐标
		rd.y += htmlObj.offsetTop
		htmlObj = htmlObj.offsetParent	//上传到上级（父）对象
	} while(htmlObj)
	return rd
}
function xts_view(divid, xtsid){
	var left = 0 ;
	if(divid == "home_setup"){
		$("div[id='xts_home_mng_log']").css("display", "none");
		left = 35 ;
	}
	if(divid == "home_mng_log"){
		$("div[id='xts_home_mng_fav']").css("display", "none");
		left = -300;
	}
	if(divid == "home_mng_fav"){
		$("div[id='xts_home_mng_acc']").css("display", "none");
		left = -300;
	}
	if(divid == "home_mng_acc"){
		$("div[id='xts_home_setup']").css("display", "none");
		left = -300;
	}
	var xts_obj = document.getElementById(divid);
	var xts_page = document.getElementById(xtsid);
	var rd = getDim(xts_obj);
	xts_page.style.left = rd.x + left + "px";
	xts_page.style.top = rd.y - 11 + "px";
	if (xts_page.style.display=="none"){
		xts_page.style.display="";
		currentpos=document.body.scrollTop;
		window.scroll(0,currentpos+rd.y-50);
	}else{
		xts_page.style.display="none"
	}
}

function cutPage(page){
	showHome('jflog',page);
}
function search(){
	var sea_username = $("#wd").val();
	sea_username = trim(sea_username);
	var form = $("#sea_form");
	//alert(sea_username);
	if(!isNull(sea_username) && sea_username != "输入会员名"){
		form.action="/home/"+sea_username;
		return true;
	}
	return false;
}
function showtrash(id){
	$("#comm_"+id).removeClass();
	$("#comm_"+id).addClass("cc");
	$("#trash_"+id).attr("style","float:right;display:");
}
function removetrash(id){
	$("#comm_"+id).removeClass();
	$("#comm_"+id).addClass("c");
	$("#trash_"+id).attr("style","float:right;display:none");
}
function updatetop(){
	var top = $("#top").val();
	var type = $(".menuon").attr("name");
	ChgTable(top,type);
}
function jfmenu(){
	$("#jfmenu").addClass("menuon");
	$("#scmenu").removeClass();
	$("#zhmenu").removeClass();
	$("#rzmenu").removeClass();
	$("#rzmenu").addClass("last");

	var top = $("#top").val();
	ChgTable(top,"jf");
}
function scmenu(){
	$("#jfmenu").removeClass();
	$("#scmenu").addClass("menuon");
	$("#zhmenu").removeClass();
	$("#rzmenu").removeClass();
	$("#rzmenu").addClass("last")

	var top = $("#top").val();
	ChgTable(top,"sc");
}
function zhmenu(){
	$("#jfmenu").removeClass();
	$("#scmenu").removeClass();
	$("#zhmenu").addClass("menuon");
	$("#rzmenu").removeClass();
	$("#rzmenu").addClass("last")

	var top = $("#top").val();
	ChgTable(top,"zh");
}
function rzmenu(){
	$("#jfmenu").removeClass();
	$("#scmenu").removeClass();
	$("#zhmenu").removeClass();
	$("#rzmenu").removeClass();
	$("#rzmenu").addClass("menuon");

	var top = $("#top").val();
	ChgTable(top,"rz");
}
function ChgTable(top,type){
	$("table[id$='_table']").attr("style","display:none")
	if(top=="0"){
		$("#all"+type+"_table").attr("style","display:")
	}
	if(top=="1"){
		$("#today"+type+"_table").attr("style","display:")
	}
	if(top=="2"){
		$("#yesterday"+type+"_table").attr("style","display:")
	}
	if(top=="3"){
		$("#week"+type+"_table").attr("style","display:")
	}
	if(top=="4"){
		$("#month"+type+"_table").attr("style","display:")
	}

}
function submitForm() {
	ckmessage();
	if ($("#err_message").html() == "true" ) {
		return true;
	}
	return false;
}
function trashcomment(id){
	closeDrag();
	$.post("/submit.jsp",{action:"trashcomment",id:id},function(xml){
		if (xml==1){
			$("#comm_"+id).replaceWith("");
		}else{
			return;
	}
	});
}
function addmessage(tag){
	ckmessage();
	var message =trim(document.getElementById("message").value);
	message=caseSens(message);
	var session_username = trim($("input[name='session_username']").val());
	var request_username = trim($("input[name='request_username']").val());
	if (session_username == null || session_username == '') {
		session_username = "访客";
		action = "/submit_guest.jsp";
	} else {
		action = "/submit.jsp";
	}
	if ($("#err_message").html() == "true" ) {
		$.post(action,{action:"add",request_username:request_username,message:message},function(xml){
			if (xml==0){
				return;
			}else{
				//更新commentlist
				var v=xml.split(";");
				var id = v[1];
				var date = v[0];
				var html = "";
				html+="<div id='comm_"+id+"'>";
				html+="<span class='l'><strong>"+session_username+"</strong></span>";
				html+="<span class='r'>"+date+"</span>";
				html+="<span class='c' id='comm_"+id+"' onmouseover='showtrash("+id+")' onmouseout='removetrash("+id+")'>";
	    		if(tag=="true"){
					html+="<span id='trash_"+id+"' onmouseover='showtrash("+id+")' style='float:right;display:none'><a href='javascript:void(0)' onclick='showDragDiv(\"trashcomment\","+id+")' >删除</a></span>";
				}
				html+=message.replace(/\r\n/ig, "<br>")+"</span> ";
				html+="</div>";
				$("#newmsg").after(html);
			}
		});
	}
	$("#message").val("");
}
function ckmessage(){
	var message = trim($("#message").val());
	if (isNull(message)) {
		showWrongTip("message", "留言不能为空！");
	} else if(message.length>2000){
		showWrongTip("message", "留言不能超过2000个字符！");
	}else{
	    showRightTip("message");
	}
}
function SubSetUp(){
	var setup = document.getElementById("setup");
	setup.action="/submit.jsp?action=setup";
	setup.submit();
}
function accjf(id){
	$.post("/acc/acc_submit.jsp",{action:"accjf",id:id});
}
function favjf(id){
	$.post("/fav/fav_submit.jsp",{action:"accjf",id:id});
}
function logjf(id){
	$.post("/note/sub/accjf",{id:id});
}
function initHome(tag) {

	var getAbsoluteCoords = function (e) {
		var width = e.offsetWidth;
		var height = e.offsetHeight;
		var left = e.offsetLeft;
		var top = e.offsetTop;
		while (e=e.offsetParent) {
			left += e.offsetLeft;
			top += e.offsetTop;
		};
		var right = left+width;
		var bottom = top+height;
		return {
			'width': width,
			'height': height,
			'left': left,
			'top': top,
			'right': right,
			'bottom': bottom
		};
	};
	var getElementById = function (sId) {
	return document.getElementById(String(sId));
	};

	var wrapId = tag+"Div";
	var wrapTitle = tag+"Title";
	var wrap = getElementById(wrapId);
	var wrapTitle = getElementById(wrapTitle);
	wrapTitle.onmouseover = function () {
		wrapTitle.style.cursor = "move";
		if (window.ActiveXObject)
		wrapTitle.onselectstart = function () { event.returnValue = false; }
		wrapTitle.onmousedown = function (evt) {
			evt = window.event||evt; var a=getAbsoluteCoords(wrap);
			wrap.cx=evt.clientX-(a.left);
			wrap.cy=evt.clientY-(a.top);
			document.onmousemove = function (evt) {
				evt = window.event||evt;
				try {
					wrap.style.left = (evt.clientX-wrap.cx)+"px";
					wrap.style.top = (evt.clientY-wrap.cy)+"px";
				} catch (ex) {};
			};
			document.onmouseup = function () {
				document.onmousemove = null;
				document.onmouseup = null;
				wrap.style.cursor="default";
			};
		};
	}
}
function getHomeLoadingImg() {
	var html = "";
	html += '<div style="width:550px; height: 100px; text-align: center; padding-top: 100px;">';
	html += '<img src="/images/loading.gif">';
	html += '</div>';
	return html;
}
function chgTitle(action) {
	if (action=='account') {
		$("#mngTitleSpan").text("主页帐户管理");
	}
	if (action=='favorite') {
		$("#mngTitleSpan").text("主页收藏管理");
	}
	if (action=='log') {
		$("#mngTitleSpan").text("主页记事本管理");
	}
	if (action=='nav') {
		$("#mngTitleSpan").text("主页网址导航管理");
	}
}
function showHome(tag,id,action,dir,wd,msg) {
   if((tag!="jflog" && tag!="mng")||isNull(id)){
		showHiddenDiv();
	}
	if (action != null && action != "" && !isNumber(action)) {
		chgTitle(action);
	}
	//在div里写入内容
	$("#"+tag+"Content").html(getHomeLoadingImg());
	if (!id) {id = "";}
	if (!action) {action = "";}
	if (!dir) {dir = "";}
	if (!wd) {wd = "";}
	if (!msg) {msg = "";}
	$.post("/home/"+tag, {id:id,action:action,tag:dir,wd:wd,msg:msg}, function(xml) {
		$("#"+tag+"Content").html(xml);
	});

	var Div = document.getElementById(tag+"Div");
	var winWidth = window.screen.width;
	var winHeight = window.screen.height;
	var sTop = document.body.scrollTop||document.documentElement.scrollTop;//网页被卷去的高
	divWidth = parseInt(Div.style.width);
	divHeight = parseInt(Div.style.height);
	Div.style.left = winWidth/2 - divWidth/2 + "px";
	Div.style.top = winHeight/2 + sTop - divHeight/2 - 100 + "px";
	Div.style.display = "";
}

var saveflag = 0; 
function closeHome(tag) {
	var Div = document.getElementById(tag+"Div");
	Div.style.display = "none";
	showHiddenDiv();
	if(saveflag==1){
		window.location.reload();
	}
}

function showWrongTip(type, msg) {
	$("#img_" + type).removeClass();
	$("#img_" + type).addClass("r_wrong");
	$("#err_" + type).css("display", "");
	$("#err_" + type).html(msg);
}

function showRightTip(type) {
	$("#img_" + type).removeClass();
	$("#img_" + type).addClass("r_right");
	$("#err_" + type).css("display", "none");
	$("#err_" + type).html("true");

}

function hiddenTip(type) {
	$("#img_" + type).removeClass();
	$("#img_" + type).addClass("r");
	$("#err_" + type).css("display", "none");
}