/**************************************************
* 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.
**************************************************/

var Drag = {

obj : null,

setStartY: function(y) {
	this.startY = y;
},

init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper) {
	o.onmousedown = Drag.start;

	o.vmode = bSwapVertRef ? false : true ;

	o.root =  o ;
	var locY = this.startY;

	if (!locY) locY='50';
	if (o.vmode && isNaN(parseInt(o.root.style.top ))) o.root.style.top = locY + "px";
	if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

	o.minY = typeof minY != 'undefined' ? minY : null;
	o.maxY = typeof maxY != 'undefined' ? maxY : null;

	o.yMapper = fYMapper ? fYMapper : null;

	o.root.onDragStart = new Function();
	o.root.onDragEnd = new Function();
	o.root.onDrag = new Function();
	this.element = o;
},

start : function(e) {
	var o = Drag.obj = this;
	e = Drag.fixE(e);
	var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
	o.root.onDragStart(0, y);

	o.lastMouseY = e.clientY;


	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;
	}

	document.onmousemove = Drag.drag;
	document.onmouseup = Drag.end;

	return false;
},

drag : function(e)
{
e = Drag.fixE(e);
var o = Drag.obj;

var ey = e.clientY;
var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
var ny;

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);

ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
if (ny<0) ny = 0;
/*if (o.xMapper) nx = o.xMapper(y)
else if (o.yMapper) ny = o.yMapper(x)
*/
Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
Drag.obj.lastMouseY = ey;

Drag.obj.root.onDrag(0, ny);
return false;
},

end : function()
{
$('eyeline').value=Drag.obj.lastMouseY;
document.onmousemove = null;
document.onmouseup = null;
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)
{
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;
}
};
