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)
}

function ajaxDialog(URL, obj, w, h, x, y, callback, block)
{
	if (!gE("ajaxmask"))
		$('<div id="ajaxmask" style="position:absolute;top:0;left:0;opacity:.5;filter: Alpha(opacity=50);z-index:98;display: none;background:#000;"></div>').appendTo(document.body);

	if (!gE("ajaxdyndiv"))
		$('<div id="ajaxdyndiv" style="position:absolute;padding:10px;top:0;background:#e2e2e2;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"
	}
	else
		moveRelTo(dv[0], obj, x || 0, y || 0)

	if (typeof block == "undefined" || block)
		$('#ajaxmask').show().width('100%').height('100%');
	else
		$(document.body).bind("mouseup", function() { dv.hide()})

	dv.show().width(w).height(h).load(URL.replace(/ /, "+"), null, function() { if (callback) callback.call() })
}

function closeDialog()
{
	$("#ajaxmask").hide();
	$("#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"); });

