$(document).ready(function() {
//	var dir_display=$("#dir_display").val();
//	if(dir_display=="0"){
//		showList();
//	}
//	if(dir_display=="1"){
//		showLevel();
//	}
	initDirEdit();
	$("#xiala2").blur(function() {
		window.setTimeout("closeXiala2()", 150);
	});
});
var saveflag = 0;
function initDirEdit() {
	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 = "dirDiv";
	var wrapTitle = "dirTitle"; 
	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 moveEntryToDir(type) {
	var entryid = "";
	var i = 0;
	$("input[@name='enrty_checkbox[]']").each(function(){
       	if($(this).attr("checked")==true) {
       		entryid += $(this).val();
       		entryid +=",";
       		i++;
       	}
    });
    if (i == 0) {
    	return;
    } else {
    	entryid = entryid.substring(0,entryid.length-1);
    	$("#enrty_list").html(getLoadingImgForEdit());
    	var newdirid = $("select[@id='moveToDir']").val();
    	//var newdirname = $("#newdirEntry_" + newdirid).text();
    	var olddirid = $("select[@id='dirselect']").val();
    	//var olddirname = $("#dirEntry_" + olddirid).text();
    	//for (j=0;j<entryid.length;j++) {
    		$.post("/dir/entry_submit.jsp",{action:"move", type:type, id:entryid, newdirid:newdirid, olddirid:olddirid},function(xml){
    			if(xml == 0){
    				return;
    			}else{
    				saveflag = 1 ;
    				changeDirList($("#dirselect").val());
    				return;
    			}
			});
		//}
    }
}

//初始化条目编辑
function showEntryEditFromDir(type) {
	document.getElementById("xiala2").style.display = "none";
	showHiddenDiv();
	$.post("/dir/entry_submit.jsp", {type: type, action: "init"}, function(xml) {
		//alert(xml);
		showEntryEdit(xml);
		var dirDiv = document.getElementById("dirDiv");
		var winWidth = window.screen.width;
		var winHeight = window.screen.height;
		var sTop = document.body.scrollTop||document.documentElement.scrollTop;//网页被卷去的高
		divWidth = parseInt(dirDiv.style.width);
		divHeight = parseInt(dirDiv.style.height);
		dirDiv.style.left = winWidth/2 - divWidth/2 + "px";
		dirDiv.style.top = winHeight/2 + sTop - divHeight/2 - 100 + "px";
		dirDiv.style.display = "";
	});
}

//分类管理中打开条目编辑
function showEntryEdit(dirid,type) {
	var button="";
	button+="<input type='button' name='button3' id='button3' value='关闭' class='btm' onclick='closeDirEdit()'/> ";
	$("#dir_add").html("");
	$("#dirTitleSpan").html("条目管理");
	$("#div_button").html(button);
	$("#biaoge").html(getLoadingImgForEdit());
	$.post("/dir/entry.jsp", {right_type: type, dirid: dirid}, function(xml) {
		$("#biaoge").html(xml);
	});
}

function changeDirList (id) {
	$("#enrty_list").html(getLoadingImgForEdit());
	$.post("/dir/entry_list.jsp?v=" + Math.random(), {dirid: id}, function(xml) {
		$("#enrty_list").html(xml);
	});
}

function selectAllCheckbox() {
	if($("#allcheckbox").attr("checked")==true) {
       $("input[@name='enrty_checkbox[]']").each(function(){
        	$(this).attr("checked",true);
       });
     } else {
        $("input[@name='enrty_checkbox[]']").each(function(){
        	$(this).attr("checked",false);
        });
    }
}
//删除条目
function deleteEntry(type) {
	var entryid = "";
	var i = 0;
	$("input[@name='enrty_checkbox[]']").each(function(){
          	if($(this).attr("checked")==true) {
          		entryid += $(this).val();
          		entryid +=",";
          		i++;
          	}
    });
    if (i == 0) {
    	return;
    } else {
    	entryid = entryid.substring(0,entryid.length-1);
    	$("#enrty_list").html(getLoadingImgForEdit());
   		$.post("/" + type + "/" + type + "_submit.jsp",{action:"checkbox_trash",id:entryid},function(xml){
	   		if(xml == 0){
	   			return;
	   		}else{
				saveflag = 1 ;
				changeDirList($("#dirselect").val());
	   			return;
			}
		});
	}
}

//打开分类编辑
function showDirEdit(type) {
	html="";
	html="<div style='float:left;padding-top:2px;'><input type='text' name='newdir' id='newdir'  onfocus='closeAddDirErr();'class='put1' /></div>";
	html+="<div style='float:left;padding-top:2px;_padding-top:3px;'><img name='add' id='dirEdit_add' value='新增分类' src='/images/btmbg2.gif' onclick='addDir()'/></div>";
	html+="<div style='clear:both;'><i id='err_newdir' style='display: none;'></i></div>";
	var button="";
	button+="<i id='dirPrompt'></i>";
	button+="<input type='button' name='button3' id='button3' value='保存' class='btm' onclick='save_order()'/>&nbsp;&nbsp;";
	button+="<input type='button' name='button3' id='button3' value='关闭' class='btm' onclick='closeDirEdit()'/> ";
	document.getElementById("xiala2").style.display = "none";
	showHiddenDiv();
	$("#dirTitleSpan").html("分类管理");
	$("#dir_add").html(html);
	$("#div_button").html(button);
	$("#biaoge").html(getLoadingImgForEdit());
	$.post("/dir/dir_edit.jsp", {right_type: type}, function(xml) {
		$("#biaoge").html(xml);
	});
	var dirDiv = document.getElementById("dirDiv");
	var winWidth = window.screen.width;
	var winHeight = window.screen.height;
	var sTop = document.body.scrollTop||document.documentElement.scrollTop;//网页被卷去的高
	divWidth = parseInt(dirDiv.style.width);
	divHeight = parseInt(dirDiv.style.height);
	dirDiv.style.left = winWidth/2 - divWidth/2 + "px";
	dirDiv.style.top = winHeight/2 + sTop - divHeight/2 - 100 + "px";
	dirDiv.style.display = "";
}
//关闭分类编辑
function closeDirEdit() {
	var dirDiv = document.getElementById("dirDiv");
	dirDiv.style.display = "none";
	showHiddenDiv();
	if(saveflag==1){
		showLevelOrList();
	}
}
//添加分类
function addDir(){
	var dirname = trim($("#newdir").val());
	if(isNull(dirname)){
		showWrongTip("newdir","分类名不能为空。");
		return;
	} 
	if(dirname == "home" || dirname == "comm" || dirname == "auto" || dirname == "public" || dirname == "private" || dirname == "search"){
	  	showWrongTip("newdir", "分类名"+dirname+"与豌豆关键字冲突，请重新输入分类名。");
	  	return;
	}else{
		$.post("/dir/submit.jsp",{action:"addnewdir",dirname:dirname},function(xml){
			if (xml=="0" || xml == 0){
				showWrongTip("newdir", "该分类名已存在，请重新输入分类名。");
	  			return;
			}else{
				$("#dirEditContent").html(getLoadingImgForEdit());
				$.post("/dir/dir_edit_list.jsp", {right_type: "fav"}, function(xml) {
					$("#dirEditContent").html(xml);
				});
				showLevelOrList();
			}
		});
	}
}
//分类更名
function changeNameDir(id, type) {
	var oldname = $("#dirName_" + id).html();
	var html = "";
	var html2 = "";
	html += '<input type="text" name="dirName_' + id + '" id="dirNameText_' + id + '" class="put1" value="' + $("#dirName_" + id).html() + '"/>';
   
   
    html2 = ' <input type="button" name="button" id="button" value="更名" onclick="confirmChange(' + id + ', \'' + type + '\')" class="btm3" onmouseover="this.className=\'btm3bg\'" onmouseout="this.className=\'btm3\'"/>';
    html2 += ' <input type="button" name="button" id="button" value="取消" onclick="cancelChangeName(' + id + ', \'' + type + '\', \'' + oldname + '\')" class="btm3" onmouseover="this.className=\'btm3bg\'" onmouseout="this.className=\'btm3\'"/>';
	$("#dirName_" + id).html(html);
	$("#button_" + id).html(html2);
	//$("#button_" + id).css("display", "none");
}
//确定更名
function confirmChange(dirid, type){
	if (dirid == null || dirid == 0 || type == null || type == "") {
		return;	
	}
	var newdirname = $("#dirNameText_" + dirid).val();
	if(newdirname == "home" || newdirname == "comm" || newdirname == "auto" || newdirname == "public" || newdirname == "private" || newdirname == "search"){
	  	showWrongWithDirEdit (dirid, "分类名"+newdirname+"与豌豆关键字冲突，请重新输入分类名。");
	  	return;
	}
	$.post("/dir/submit.jsp", {type: type, dirid: dirid, dirname: $("#dirNameText_" + dirid).val()}, function(xml) {
		 if (xml == "2"|| xml == 2) {//参数不全
		  	return;
		  } else if (xml == "0"|| xml == 0) {//还有条目，不许删除
		  	showWrongWithDirEdit (dirid, "分类名已存在，请重新输入分类名。");
		  	return;
		  } else if (xml == "1" || xml == 1) {//成功
		  	$("#dirEditContent").html(getLoadingImgForEdit());
			$.post("/dir/dir_edit_list.jsp", {right_type: type}, function(xml) {
				$("#dirEditContent").html(xml);
				showLevelOrList();
			});
		  	return;
		  }
	});
}
// 取消更名
function cancelChangeName(id, type,oldname) {
	$("#dirName_" + id).html(oldname);
	$("#dirErr_" + id).css("display", "none");
	//$("#button_" + id).css("display", "");
	var html = "<a href='javascript:void(0);'><img src='/images/ico/ico_edit.gif' alt='更名' width='15' height='13' align='absmiddle' onclick='changeNameDir(" + id + ",\""+ type +"\")'/></a>&nbsp; ";
	html += "<a href='javascript:void(-1);'><img src='/images/ico/ico_del.gif' alt='删除' width='12' height='13' align='absmiddle' onclick='deleteNameDir(" + id + ",\""+ type +"\")'/></a>&nbsp; ";
	$("#button_" + id).html(html);
}
//删除分类
function deleteNameDir(id, type) {
	var oldname = $("#dirName_" + id).html();
	var html = "";
	var html2 = "";
	html += '您确定要删除［<i>' + $("#dirName_" + id).html() + '</i>］吗？';
    html2 = '<input type="button" name="button" id="button" value="确定" onclick="confirmDelete(' + id + ', \'' + type + '\')" class="btm3" onmouseover="this.className=\'btm3bg\'" onmouseout="this.className=\'btm3\'"/>&nbsp;';
   	html2 += '<input type="button" name="button" id="button" value="取消" onclick="cancelDeleteName(' + id + ', \'' + type + '\', \'' + oldname + '\')" class="btm3" onmouseover="this.className=\'btm3bg\'" onmouseout="this.className=\'btm3\'"/>';
	$("#dirName_" + id).html(html);
	$("#button_" + id).html(html2);
}
//确定删除
function confirmDelete(dirid, type){
	if (dirid == null || dirid == 0 || type == null || type == "") {
		return;	
	}
	$.post("/dir/submit.jsp", {type: type, dirid: dirid}, function(xml) {
		 if (xml == "2") {//参数不全
		  	return;
		  } else if (xml == "-1" || xml == -1) {//还有条目，不许删除
		  	showWrongWithDirEdit (dirid, "当前分类里还有条目，不能删除。 <a href='javascript:void(0);' onclick='showEntryEdit(" + dirid + ")'>条目管理</a>");
		  	return;
		  } else if (xml == "1"|| xml == 1) {//成功
		  	$("#dirEditContent").html(getLoadingImgForEdit());
			$.post("/dir/dir_edit_list.jsp", {right_type: type}, function(xml) {
				$("#dirEditContent").html(xml);
				showLevelOrList();
			});
		  return;
		  }
	});
}
function showLevelOrList() {
try {
	if(dir_type=="dir_level.jsp"){
			showLevel();
		}else{
			showList();
	}
} catch (e) {
	updateDir();
}
}
//显示错误信息
function showWrongWithDirEdit (id, text) {
	$("#dirErrHtml_" + id).html(text);
	$("#dirErr_" + id).css("display", "");
}
//取消删除
function cancelDeleteName(id, type,oldname) {
	$("#dirName_" + id).html(oldname);
	$("#dirErr_" + id).css("display", "none");
	
	var html = "<a href='javascript:void(0);'><img src='/images/ico/ico_edit.gif' alt='更名' width='15' height='13' align='absmiddle' onclick='changeNameDir(" + id + ",\""+ type +"\")'/></a>&nbsp; ";
	html += "<a href='javascript:void(-1);'><img src='/images/ico/ico_del.gif' alt='删除' width='12' height='13' align='absmiddle' onclick='deleteNameDir(" + id + ",\""+ type +"\")'/></a>&nbsp; ";
	$("#button_" + id).html(html);
}
//关闭添加分类的错误信息
function closeAddDirErr() {
	document.getElementById("err_newdir").style.display = "none";
}
//显示分类的下拉菜单
function showXiala() {
	if (document.getElementById("xiala2").style.display == "none") {
		document.getElementById("xiala2").style.display = "";
		$("#xiala2").focus();
	} else {
		document.getElementById("xiala2").style.display = "none";
	}
}
function closeXiala2() {
	document.getElementById("xiala2").style.display = "none";
}
//按云图显示
function showLevel(username, d) {
	document.getElementById("xiala2").style.display = "none";
	$("#showType").html(getLoadingImg());
	
	$.post("/dir/dir_level.jsp", {username:username,d:d}, function(xml) {
		$("#showType").html(xml);
	});
}
//按列表显示
function showList(username, d) {
	document.getElementById("xiala2").style.display = "none";
	$("#showType").html(getLoadingImg());
	
	$.post("/dir/dir_list.jsp", {username:username,d:d}, function(xml) {
		$("#showType").html(xml);
	});
}
//得到分类的Loading图
function getLoadingImg() {
	var html = "";
	html += '<div style="width:179px; height: 80px; text-align: center; padding-top: 50px;">';
	html += '<img src="/images/loading.gif">';
	html += '</div>';
	return html;
}
//得到编辑窗口的Loading图
function getLoadingImgForEdit() {
	var html = "";
	html += '<div style="width:100%; height: 100%; text-align: center; padding-top: 170px;">';
	html += '<img src="/images/loading.gif">';
	html += '</div>';
	return html;
}
//打开警告
function showErrorDiv(type,id) {
	//在div里写入内容
	innerDivHtml(type,id);
	var dragDiv = document.getElementById("dragDiv");
	var winWidth = window.screen.width;
	var winHeight = window.screen.height;
	var sTop = document.body.scrollTop||document.documentElement.scrollTop;//网页被卷去的高
	divWidth = parseInt(dragDiv.style.width);
	divHeight = parseInt(dragDiv.style.height);
	dragDiv.style.left = winWidth/2 - divWidth/2 + "px";
	dragDiv.style.top = winHeight/2 + sTop - divHeight/2 - 100 + "px";
	dragDiv.style.display = "";
	return;
}
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");
}
