/**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 **************************************************
 * 10.28.2001 - fixed minor bug where events
 * sometimes fired off the handle, not the root.
 * 
 *
 *  Dov remplace Drag
 * 
 *
 *
 **************************************************/


var Dov = {

	obj : null,
        mouseX : null,
        mouseY : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper, dontMoveTarget)
	{
                o.dontMoveTarget = dontMoveTarget || false;

		o.onmousedown	= Dov.start;

		o.hmode	= bSwapHorzRef ? false : true ;
		o.vmode	= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDovStart	= new Function();
		o.root.onDovEnd	= new Function();
		o.root.onDov		= new Function();
	},

	start : function(e)
	{
                var o = Dov.obj = this;
		e = event_DOMify(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDovStart(x, y);

		Dov.mouseX = o.lastMouseX = e.clientX;
		Dov.mouseY = o.lastMouseY = e.clientY;

		if (o.hmode) {
			if (o.minX != null) o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null) o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null) o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null) o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		e.preventDefault();
		// Removed this so that ContextMenu could still pick up onmousedown events on the
                // document object and hide itself accordingly. I'm not sure why this was originally
                // here, perhaps it was a mistake.
                // e.stopPropagation();

		Listener.add(document, "onmousemove", Dov.record, Dov);
		Listener.add(document, "onmouseup", Dov.end, Dov);
		
                Dov.timerId = window.setInterval("Dov.dov()", 10);

		return false;
	},

        record : function(e) {
                e = event_DOMify(e);
		this.mouseY = e.clientY;
		this.mouseX = e.clientX;
                return false;
        },

	dov : function()
	{
		var o = Dov.obj;
		var ey	= Dov.mouseY;
		var ex	= Dov.mouseX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		if (Dov.obj.root.onDov(nx, ny, ex, ey) !== false) {
                        if (!o.dontMoveTarget) Dov.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
                        if (!o.dontMoveTarget) Dov.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";

                        Dov.obj.lastMouseX	= ex;
                        Dov.obj.lastMouseY	= ey;
                }
	},

	end : function()
	{
                Listener.remove(document, "onmousemove", Dov.record, Dov);
		Listener.remove(document, "onmouseup", Dov.end, Dov);
                window.clearInterval(Dov.timerId);
		Dov.obj.root.onDovEnd(parseInt(Dov.obj.style.left), parseInt(Dov.obj.style.top), Dov.obj.lastMouseX, Dov.obj.lastMouseY);
		Dov.obj = null;
	}
};

function event_DOMify(e) {
	if (!e && window.event) 
		e = window.event;
	
	if (!e.target && e.srcElement) {
		e.target = e.srcElement;
		e.originalTarget = e.srcElement;
	}
	if (typeof e.layerX == "undefined" && typeof e.offsetX != "undefined") {
		e.layerX = e.offsetX;
		e.layerY = e.offsetY;
	}

	if (!e.preventDefault) e.preventDefault = event_iePreventDefault;
	if (!e.stopPropagation) e.stopPropagation = event_ieStopPropagation;

	return e;
}

function event_iePreventDefault() {
	this.returnValue = false;
}

function event_ieStopPropagation() {
	this.cancelBubble = true;
}