var DIALOG_Z=10



//--------
// 去掉数据的首尾空字符
//--------
function jsTrim(value){
  return value.replace(/(^\s*)|(\s*$)/g,"");
}

//--------
// 校验数据是否为空（当数据为空字符时也为NULL）

//--------
function isNULL( chars ) {
	if (chars == null)
		return true;
	if (jsTrim(chars).length==0)
		return true;
	return false;
}

window.openWindow = function(url,winName,w,h,isCenter,isResizable,isScroll) {
	if (isNULL(url)) return;
	var scrLeft = 0;
	var scrTop = 0;
	var scrolls = "no";

	if(!w){
		w = 800;
	}
	if(!h){
		h = 600;
	}
	if(!winName){
		winName = "popupWindow";
	}

	if (isCenter) {
		scrLeft = (screen.width-w)/2;
		scrTop = (screen.height-h)/2;
	}
	if (isResizable){
		resize="yes";
	}else{
		resize="no";
	}
	if (isScroll){
		scrolls = "yes";
	}else{
		scrolls = "no";
	}
	window.open(url, winName, 'height='+h+',width='+w+',top='+scrTop+',left='+scrLeft+',toolbar=no,menubar=no,scrollbars='+scrolls+',resizable='+resize+',location=no,status=no');
}



window.openDivDialog = function(div, w, h, x, y, wait)
{
	if (typeof wait == "undefined" || wait)
		waitforinput(true)

	w = w || parseInt(div.style.width)
	h = h || parseInt(div.style.height);
	var innerWidth = $.browser.msie ? document.documentElement.offsetWidth : window.innerWidth
	var innerHeight = $.browser.msie ? document.documentElement.offsetHeight : window.innerHeight

	x = x || (innerWidth - w) / 2;
	y = y || (innerHeight - h) / 2;

	div.style.left = truebody().scrollLeft + x + "px"
	div.style.top = truebody().scrollTop + y + "px"
	div.style.width = w
	div.style.height = h
	div.style.display = "block"
	div.style.zIndex = ++DIALOG_Z
	div.lastScrollX = truebody().scrollTop
	div.timer = window.setInterval("floatScrollDiv(gE('"+div.id+"'))", 20);
}

window.closeDivDialog = function(div)
{
	div.style.display = "none"
	div.style.zIndex = 0
	clearInterval(div.timer)
	waitforinput(false)
}

window.openDialog = function(url, w, h, scrollable)
{
	var dh = 0

	if ($.browser.msie6)
	{
		w += 8
		h += 54
	}
	else if ($.browser.safari)
		dh = 56
	else
		dh = 88

	var x = (window.screen.width - w) / 2;
	var y = (window.screen.height - h - dh) / 2;

	var rval = window.showModalDialog(url, window,
			"dialogHeight: " + h + "px; dialogWidth: " + w + "px; dialogTop: " + y + "px; dialogLeft: " + x + "px; edge: Raised; center: Yes; help: No; resizable: Yes; status: No; scroll: " + (scrollable?'Yes':'No'));

	if (rval && rval.callback)
		rval.callback()

	return rval
}

function ajaxDialog(URL, obj, w, h, x, y, callback, block)
{
	if (!gE("ajaxdyndiv"))
		$("<div id=ajaxdyndiv style=position:absolute;top:0;left:0;z-index:99;display:none></div>").appendTo(document.body)

	var dv = $("#ajaxdyndiv")

	if (obj == null)
	{
		var innerWidth = $.browser.msie ? document.documentElement.offsetWidth : window.innerWidth
		var innerHeight = $.browser.msie ? document.documentElement.offsetHeight : window.innerHeight
		x = x || (innerWidth - w) / 2;
		y = y || (innerHeight - h - 20) / 2;
		dv[0].style.left = truebody().scrollLeft + x + "px"
		dv[0].style.top = truebody().scrollTop + y + "px"
		dv.css("zIndex", 1000)
	}
	else
		moveRelTo(dv[0], obj, x || 0, y || 0)

	if (typeof block == "undefined" || block)
		waitforinput(true)
	else
		$(document.body).bind("mouseup", function() { dv.hide()})

	dv.show().width(w).height(h).load(URL.replace(/ /, "+"), null, function() {
			var scripts = this.getElementsByTagName('SCRIPT');
			var scriptsLen = scripts.length;
			for (i = 0; i < scriptsLen; i ++)
				document.getElementsByTagName('HEAD')[0].appendChild(scriptsLen[i]);
			if (callback) callback.call()
		})
}

function closeDialog()
{
	$("#ajaxdyndiv").hide();
	waitforinput(0)
}

function floatScrollDiv(div)
{
	var y = truebody().scrollTop;
	percent = 0.1 * (y - div.lastScrollX);
	percent = percent>0 ? Math.ceil(percent) : Math.floor(percent);
	div.style.pixelTop += percent;
	div.lastScrollX += percent;
}

function Pt(p)
{
	this.x = 0
	this.y = 0

	while (p && p.tagName != "BODY")
	{
		this.x += p.offsetLeft;
		this.y += p.offsetTop;
		p = p.offsetParent;
	}
}

// drag support
$.fn.drag = function(dragTarget)
{
	this.each(function(i, o)
	{
		var d = document;
		var tbody = truebody()

		$(o).css("position", "absolute").find(".draghandle").css("cursor", "pointer")
		d._mmove = d.onmousemove;
		d._mup = d.onmouseup;

		o.onmousedown = function(e)
		{
			var ie = !e
			e = e || window.event
			var tar = ie ? e.srcElement : e.target

			if ((!ie && e.button!=0) || (ie && e.button!=1) || !$(tar).hasClass("draghandle"))
				return 0

			var x = ie ? e.offsetX : e.layerX
			var y = ie ? e.offsetY : e.layerY

			if (ie)
				o.setCapture()
			else
				window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);

			d.onmousemove = function(e)
			{
				e = e || window.event;
				o.style.left = tbody.scrollLeft + (ie ? e.clientX : e.pageX) - x;
				o.style.top  = tbody.scrollTop + (ie ? e.clientY : e.pageY) - y;
			};

			d.onmouseup = function(e)
			{
				e = e || window.event;

				$(dragTarget).each(function(i, target)
					{
						var pt = new Pt(target);
						var x = (ie ? e.clientX : e.pageX);
						var y = (ie ? e.clientY : e.pageY)
						pt.x_ = pt.x + parseInt(target.offsetWidth);
						pt.y_ = pt.y + parseInt(target.offsetHeight);

						if (x > pt.x && x < pt.x_ && y > pt.y && y < pt.y_)
							$(o).trigger("drop", target)
					});

				if (ie)
					o.releaseCapture();
				else
					window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);

				d.onmousemove = d._mmove;
				d.onmouseup = d._mup;
			};

			return false;
		}
	});
}

$(function() { $(".draggable").drag(".dropTarget"); });
