var DIALOG_Z=10

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 x = (window.screen.width - w) / 2;
	var y = (window.screen.height - h) / 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 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;
}

// drag support
function makedraggable(o, dragTargetClass, restore)
{
	var d = document;
	var tbody = truebody()

	o.style.cursor="pointer";
	o.style.position="absolute";
	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.className.indexOf("draghandle")==-1)
			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;

			return false;
		};

		d.onmouseup = function(e)
		{
			e = e || window.event;

			if (o.ondrag)
				$("."+dragTargetClass).each(function(i,target)
					{
						var pt = new Pt(target);
						var x = (ie ? e.clientX : e.pageX);
						var y = (ie ? e.clientY : e.pageY)
						pt.w = pt.x + parseInt(target.offsetWidth);
						pt.h = pt.y + parseInt(target.offsetHeight);

						if (x > pt.x && x < pt.w && y > pt.y && y < pt.h)
							; // tailor-made handler
					});

			if (ie)
				o.releaseCapture();
			else
				window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);

			d.onmousemove = d._mmove;
			d.onmouseup = d._mup;

			if (restore==null || restore)
				o.onmousedown = null
		};

		return false;
	}
}

$.fn.drag = function(targetClass)
{
	this.each(function(i,n) { makedraggable(n, targetClass, false); });
}

$(function() { $(".draggable").drag("dropTarget"); });
