$(document).ready(function() {
	$("#newdir").focus(function() {
		hiddenTip("newdir");
    });
    $("#title").focus(function() {
		hiddenTip("title");
    });
    $("#title").blur(function() {
		ckTitle();
    });
    $("#addlogdir").click(function() {
		addLogDir();
    });
	$("#all_check").click(function() {
		if($(this).attr("checked")==true) {
         $("input[@name='checkbox']").each(function(){
          	$(this).attr("checked",true);
         });
       } else {
          $("input[@name='checkbox']").each(function(){
          	$(this).attr("checked",false);
          });
        }
	});
	$("#comment").focus(function() {
		hiddenTip("comment");
    });
    $("#xiala").blur(function() {
		window.setTimeout("closeXiala()", 200);
	});
});
//取得光标的坐标（用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 == "log_xts_xiala_menu"){
		$("div[id='xts_op_batch']").css("display", "none");
		left = 10 ;
	}
	if(divid == "log_xts_setup"){
		$("div[id='xts_xiala_menu']").css("display", "none");
		left = -302 ;
	}
	if(divid == "dir_xaila_menu"){
		$("div[id='xts_setup']").css("display", "none");
		left = -302 ;
	}
	if(divid == "op_batch"){
		$("div[id='xts_dir_xiala_menu']").css("display", "none");
		left = 160;
	}
	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 showtrash(id){

	$("#comment_title_"+id).removeClass();
	$("#comment_title_"+id).addClass("editon");
	$("#trash_"+id).attr("style","display:");
}
function removetrash(id){
	$("#comment_title_"+id).removeClass();
	$("#comment_title_"+id).addClass("editoff");
	$("#trash_"+id).attr("style","display:none");
}
function accjf(id){
	$.post("/note/sub/accjf",{id:id});
}
function initLog(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 showLog(tag,id) {
	showHiddenDiv();
	//在div里写入内容
	$("#"+tag+"Content").html(getLogLoadingImg());
	$.post("/note/"+tag, {id:id}, 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 = "";
}

function closeLog(tag) {
	var Div = document.getElementById(tag+"Div");
	Div.style.display = "none";
	showHiddenDiv();
}
function SubSetUp(){
	return true;
}
function showMoreDir(){
	dirViewIsAll = 1;
	$("li[name^='label']").attr("style","display,''");
	$("#moreDir").css("display","none");
}
function Xiala() {
	if (document.getElementById("xiala").style.display == "none") {
		document.getElementById("xiala").style.display = "";
		$("#xiala").focus();
	} else {
		document.getElementById("xiala").style.display = "none";
	}
}
function closeXiala(){
	document.getElementById("xiala").style.display = "none";
}
function check(type){
	var es = document.getElementsByName("checkbox");
	if (es == null || es == 'undefined') {
			showDragDiv('null');
		}
	var len = es.length;
	var count = 0;
	if (es != null && len > 0) {
		for (i = 0; i < len; i ++) {
			if (es[i].checked) {
				count++;
			}
		}
	}
	if(count==0){
		showDragDiv('null');
	}else{
		if(type=="trash"){
			showDragDiv('trash','');
		}else{
			op(type);
		}
	}
}

function op(type){
	var es = document.getElementsByName("checkbox");
	var count=0;
	var log_type="";
	var len = es.length;
	var id = "";
	for (i = 0; i < len; i ++) {
		if(es[i].checked) {
			id+=es[i].id;
			id+=",";
			log_type=$("input[id='log_type_"+es[i].id+"']").val();
			if(type=="pup"){
				if(log_type==0){
					count++;
				}
			}
			if(type=="pri"){
				if(log_type==1){
					count++;
				}
			}
		}
	}
	id=id.substring(0,id.length-1);
	$.post("/note/sub",{action:"checkbox_"+type,id:id,count:count},function(xml){
			if(xml==21){
			    //alert("您只能将20个记事本添加到主页！");
			    showDragDiv("20","do");
				return;
			}
			if(xml==0){
			    //alert("您选中的条目不能进行相应的操作！");
			    showDragDiv("ck_"+type,"undo");
				return;
			}
			if(type!="trash"&&xml!=21&&xml!=0){
				showDragDiv("ck_"+type,"do");
				return;
			}
			if(type=="trash"){
				closeDrag();
				window.location.reload();
			}
		});
	return;
}
function trash(id){
	$.post("/note/sub",{action:"trash",id:id},function(xml){
		if (xml==0){
			return;
		}else{
			window.location.reload();
	}
	});
}
function trashcomment(id){
	closeDrag();
	$.post("/note/sub",{action:"trashcomment",id:id},function(xml){
		if (xml==1){
			$("div[id='comm_"+id+"']").replaceWith("");
		}else{
			return;
	}
	});
}
function ckTitle(){
	var title = trim($("#title").val());
  	if(isNull(title)){
		showWrongTip("title", "标题不能为空!");
	}else {
		if(title.length>200){
			showWrongTip("title", "标题不能超过200个字符!");
		}else{
			showRightTip("title");
		}
	}
}
function ckDir(){
	var es = $("#dirnameArray").val();
	if (es == "" || es == null) {
		showWrongTip("newdir","分类不能为空！");
		return;
	}
	es = trim(es);
	var temp_es = es.split(" ");
	for (var i=0;i<temp_es.length;i++) {
		checkDirForSelect(temp_es[i]);
	}
}
function sub(){
	ckTitle();
	ckDir();
	var obj2=document.getElementById("eWebEditor1");
	var obj_con=document.getElementById("content");
	obj_con.value = obj2.contentWindow.document.getElementsByTagName("iframe")[0].contentWindow.document.getElementsByTagName("body")[0].innerHTML;
	if ($("#err_newdir").html() == "true" &&$("#err_title").html() == "true") {
		return true;
	}
	return false;
}
function addcomment(tag) {
	ckComment();
	var logid = trim($("input[name='logid']").val());
	var comment =trim(document.getElementById("comment").value);
	comment=caseSens(comment);
	var username = trim($("input[name='username']").val());
	var action;
	if (username == null || username == "") {
		username = "访客";
		action = "/note/sub_guest";
	} else {
		action = "/note/sub";
	}
	if ($("#err_comment").html() == "true" ) {
		$.post(action,{action:"comment",logid:logid,comment:comment},function(xml){
			if (xml==0){
				return;
			}else{
				//更新commentlist
				var v=xml.split(";");
				var id = v[1];
				var date = v[0];
				var html = "";
				html+="<div class='jsb_line' id='comm_"+id+"'>";
				html+="<div id='comment_title' onmouseover='this.className=\"editon\"' onmouseout='this.className=\"editoff\"'>";
		      	html+="<span class='l'><strong>"+username+"</strong></span>";
				html+="<span class='c'>"+date+"</span>";
				if(tag=="true"){
					html+="<span class='r'><a href='#' onclick='showDragDiv(\"trashcomment\",\""+id+"\")'>删除</a></span>";
				}
				html+="<span class='clear'></span>	";
				html+="</div> ";
				html+="<span class='cen'>"+comment.replace(/\r\n/ig, '<br/>')+"</span> ";
		        html+="<span class='clear'></span>";
		        html+="</div>";
				$("#message").after( html);
			}
		});
	}
	$("#comment").val("");
}
function ckComment() {
		var comment = trim(document.getElementById("comment").value);
		if (isNull(comment)) {
			showWrongTip("comment", "留言不能为空！");
		} else {
		    showRightTip("comment");
		}
}
var dirnameArray = "";// 当前帐户的分类
var dirViewIsAll = 0;// 用户是否点击了“更多”，若点击了，就一直显示全部分类，除非刷新页面
function addLogDir(){
	var dirname = trim($("#newdir").val());
	dirname=caseSens(dirname);
	if(isNull(dirname)){
		return;
	}if(dirname == "search" || dirname == "public" || dirname == "private" || dirname == "home"){
		showWrongTip("newdir","分类名"+dirname+"与豌豆关键字冲突，请填写其他分类名！");
		return;
	}else if(!filterStr(dirname)){
		showWrongTip("newdir","分类名中不能有<,>或者--！");	
		return;
	}else if(dirname.length>200){
		showWrongTip("newdir","分类名不能超过200个字符！");
		return;
	}else{
		$.post("/note/sub",{action:"addnewdir",dirname:dirname},function(xml){
			if (xml==0){
				showWrongTip("newdir","该分类名已存在，请选择或修改分类名！");//不更新显示分类名
				return;
			}else{
				dirnameArray += dirname + " ";
				updateDir(dirnameArray, dirViewIsAll);//更新显示分类名，且新添分类名被选中
				return;
			}
		});
	}
}

function getLogLoadingImg() {
	var html = "";
	html += '<div style="width:250px; height: 200px; text-align: center; padding-top: 100px;">';
	html += '<img src="/images/loading.gif">';
	html += '</div>';
	return html;
}
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");
}