# dom-drag.js
# Sourced from : www.youngpup.net
# Modified to allow style to be specified in css, not overwrite existing events
# further extra info gained much later
# http://code.google.com/p/personal-libraries/source/browse/trunk/JS/dom-drag.js?spec=svn31&r=31

var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
#        dbg('fns Drag.init');
		o.onmousedown	= Drag.start;

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

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

		var cs=o.root.currentStyle?o.root.currentStyle : window.getComputedStyle(o.root, null);
		var s=o.root.style;
	
		if (o.hmode  && isNaN(parseInt(s.left  ))) s.left   = !isNaN(parseInt(cs.left))   ? cs.left   : "0px";
		if (o.vmode  && isNaN(parseInt(s.top   ))) s.top    = !isNaN(parseInt(cs.top))    ? cs.top    : "0px";
		if (!o.hmode && isNaN(parseInt(s.right ))) s.right  = !isNaN(parseInt(cs.right))  ? cs.right  : "0px";
		if (!o.vmode && isNaN(parseInt(s.bottom))) s.bottom = !isNaN(parseInt(cs.bottom)) ? cs.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.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
        dbg('fns Drag.start');
		var o = Drag.obj = this;
		e = Drag.fixE(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.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		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;
		}
		
		o.old_onmousemove	= document.onmousemove;
		o.old_onmouseup		= document.onmouseup;

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;
		
		if(o.stop) stopEvent(e);
		
		return false;
	},

	drag : function(e)
	{
#        dbg('fns Drag.drag');
		e = Drag.fixE(e);
		var o = Drag.obj;
		
		if (!o) {
#			if (!o.ignoring_error)
#				dbg("ERROR in dom-drag.js (ignoring) Work out WHY and FIX");
#			o.ignoring_error = true;	
			return;
		}

		var ey	= e.clientY;
		var ex	= e.clientX;
		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, restrict;

		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));

		dbg("move nx="+nx+" x="+x+" ex="+ex+" xref="+o.lastMouseX);
		
		if (o.xgrid) {
			var mx = Math.floor(o.xgrid/2);
			var delta = nx%o.xgrid - mx
			ex = ex - delta;
			nx = nx - delta;
			var my = Math.floor(o.ygrid/2);
			delta = ny%o.ygrid - my
			ey = ey - delta;
			ny = ny - delta;
		}
		if (o.validate_cb) {
			dbg('---')
			var xy = o.validate_cb(o,nx,ny);
			var delta = Math.floor(nx-xy[0]);
			dbg('B4 x='+nx+' y='+ny+' delta='+delta);
			ex = ex - delta;
			nx = nx - delta;
			delta = Math.floor(ny-xy[1]);
			ey = ey - delta;
			ny = ny - delta;
			dbg('AF x='+nx+' y='+ny);
		}

		if (o.xMapper){restrict=o.xMapper(y); if(restrict) nx=restrict;}
		if (o.yMapper){restrict=o.yMapper(x); if(restrict) ny=restrict;}

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
#        dbg('fns Drag.end');
		var o = Drag.obj;
		if (!o) return;
		document.onmousemove = o.old_onmousemove;
		document.onmouseup   = o.old_onmouseup;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
#        dbg('fns Drag.fixE');
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	},
	
	grid : function(o,x,y)
	{
		o.xgrid = x;
		o.ygrid = y;
	},
	
	validate : function(o,cb,stop)
	{
		o.validate_cb = cb;
		if(stop) o.stop = stop;
	},
	cb : function(o,cb1,cb2,cb3)
	{
		if(cb1) o.onDragStart=cb1;	
		if(cb2) o.onDrag=cb2;
		if(cb3) o.onDragEnd=cb3;
	}
	
};
# Originally devt location: ..\divs_1\dom-drag.js
setTimeout(function(){dbg("module Drag source file loaded (delayed)");},1000); 
