var isdown = 0, isoff = 0;
var istop = 0 ,isleft = 0, isright = 0, isbottom = 0;
var x0 = 0, y0 = 0, x = 0, y = 0, dx = 0, dy = 0;
var newpopup = null;
var popid = "nullbox";
var poptitle = "noname";
var popstr_search = "<table height=\"100%\" width=\"100%\"><tr><td align=\"center\"><img src=\"images/ajax.gif\"/>正在查询中......</td></tr></table>";
var popstr_update = "<table height=\"100%\" width=\"100%\"><tr><td align=\"center\"><img src=\"images/ajax.gif\"/>正在更新中......</td></tr></table>";

function setpos(){
	newpopup.style.top = (document.documentElement.clientHeight - $("#div_popup_"+popid).height()) / 2 - 40 + "px";
	newpopup.style.left = (window.screen.width - $("#div_popup_"+popid).width()) / 2 + "px";
	newpopup.style.position = "absolute";
	newpopup.style.zIndex = "9999";
}

function setpopupcontent(str){
	if (popid != "nullbox") {
		$("#"+popid).html(str);
		setpos();
		initial();
	}
}

function popclose(){
	$("#"+popid).html("");
	$("#div_popup_"+popid).remove();
	$("#div_pop_mask").remove();
	newpopup = null;
	popid = "nullbox";
	poptitle = "noname";
	setSelect("inline");
}

function mask(){
	var newmask = document.createElement("div");
	newmask.id = "div_pop_mask";
	newmask.style.top = "0";
	newmask.style.left = "0";
	newmask.style.width = document.body.scrollWidth + "px";
	newmask.style.height = document.documentElement.scrollHeight + "px";
	document.body.appendChild(newmask);
}

function popup(id, title, width){
	//mask();
	if (popid != "nullbox") return;
	if (id != null) popid = id;
	if (title != null) poptitle = title;
	newpopup = document.createElement("div");
	newpopup.id = "div_popup_" + popid;
	if (width !== null && width > 0) {
		newpopup.style.width = width + "px";
	} else {
		newpopup.style.width = "500px";
	}

	if (popid == "nullbox") {
		newpopup.style.height = "200px";
		newpopup.innerHTML =
		'<table class="tbl_popup" width="100%">'+
			'<tr class="tr_poptitle" id="'+popid+'_poptitle">'+
				'<td class="td_poptitle_icon"><div class="div_poptitle_icon"></div></td>'+
				'<td class="td_poptitle_text" id="td_poptitle_name">'+poptitle+'</td>'+
				'<td class="td_poptitle_close"><div class="div_poptitle_close" id="'+popid+'_popclose" onclick="popclose()"></div></td>'+
			'</tr>'+
			'<tr bgcolor="#fbfeff"><td colspan="3" class="td_popcontent" id="'+popid+'">'+
				'<table id="" width="100%">'+
					'<tr><td height="30px" width="20%"></td>'+
					'<td height="30px" width="80%"><input class="input_popcontent_edit"/></td></tr>'+
					'<tr><td height="60px" width="20%"></td>'+
					'<td height="60px" width="80%"><textarea class="text_popcontent_edit"></textarea></td></tr>'+
				'</table>'+
			'</td></tr>'+
			'<tr><td colspan="3" class="td_popfoot">'+
				'<button class="btn_pop_confirm" onclick="popclose()"></button>&nbsp;&nbsp;'+
				'<button class="btn_pop_exit" onclick="popclose()"></button>'+
			'</td></tr>'+
		'</table>';
	} else {
		newpopup.innerHTML =
		'<div class="popup">'+
			//'<div class="head"><div class="head-right"></div></div>'+
			'<div class="main">'+
				'<div class="head" id="'+popid+'_poptitle">'+
					'<div class="headtitle">'+poptitle+'</div>'+
					'<a href="javascript:popclose()" class="closebtn" id="'+popid+'_popclose"><img src="images/close2.gif" alt="关闭窗口"/></a>'+
				'</div>'+
				'<div class="content" id="'+popid+'"></div>'+
			'</div>'+
			//'<div class="foot"><div class="foot-right"></div></div>'+
		'</div>';
	}

	//create & initial popup
	document.body.appendChild(newpopup);
	setpos();
	initial();
	setSelect("none");

	//ESC close popup
	$(document).keydown(function(e){
		if (!e) e = window.event;
		if (e.keyCode == 27) popclose();
	});

	//move popup
	$("#"+popid+"_popclose").mousedown(function(){
		isoff = 1;
	});
	$("#"+popid+"_poptitle").mousedown(function(e){
		if (!e) e = window.event;
		x0 = e.clientX;
		y0 = e.clientY;
		dx = x0 - parseInt(newpopup.style.left);
		dy = y0 - parseInt(newpopup.style.top);
		isdown = (isoff == 1) ? 0 : 1;
	});
	$(document).mousemove(function(e){
		if (!e) e = window.event;
		x = e.clientX;
		y = e.clientY;
		istop = (y < 0) ? 1 : 0;
		isleft = (x < 0) ? 1 : 0;
		isright = (x > document.body.clientWidth) ? 1 : 0;
		isbottom = (y > document.body.clientHeight) ? 1 : 0;
		if (isdown == 1 && istop == 0 && isleft == 0 && isright == 0 && isbottom == 0) {
			newpopup.style.top = y - dy + "px";
			newpopup.style.left = x - dx + "px";
		}
	});
	$(document).mouseup(function(e){
		if (!e) e = window.event;
		x = e.clientX;
		y = e.clientY;
		if (isdown == 1 && istop == 0 && isleft == 0 && isright == 0 && isbottom == 0) {
			newpopup.style.top = y - dy + "px";
			newpopup.style.left = x - dx + "px";
		}
		isdown = 0;isoff = 0;istop = 0;isleft = 0;isright = 0;isbottom = 0;
	});
}