/**************************************************
 * 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.
 **************************************************/
function dragListener(h) {
				var theHandle = h;
				var theRoot   = h.parentNode;
				initX = theRoot.offsetLeft;
				initY = theRoot.offsetTop;
				Drag.init(theHandle, theRoot, initX, initY);
}
var Drag = {

	obj : null,

	init : function(o, oRoot, initX, initY, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

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

		if (isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = initX + "px";
		if (isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = initY + "px";
	
		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)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.root.style.top);
		var x = parseInt(o.root.style.left);
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

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

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

		Drag.obj.root.style.cursor = "move;";

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		Drag.obj.root.style.opacity = ".75";
		Drag.obj.root.style.filter = "alpha(opacity=75);";

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.root.offsetTop);
		var x = parseInt(o.root.offsetLeft);
		var nx, ny, nxmax, nymax;

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

		if (ex > 0) {
			nx = x + (ex - o.lastMouseX);
			nxmax = nx + o.root.clientWidth;
		}
		if (ey > 0) {
			ny = y + (ey - o.lastMouseY);
			nymax = ny + o.root.clientHeight;
		}

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

		//if ((nx >= 0) && (nxmax < o.root.parentNode.clientWidth) && (e.layerX > 0) && (e.layerY < o.root.clientWidth)) {
		if ((nx >= 0) && (e.layerX > 0)) {
			Drag.obj.root.style.left = nx + "px";
		}

		//if ((ny >= 0) && (nymax < o.root.parentNode.clientHeight) && (e.layerY > 0) && (e.layerY < o.root.clientHeight)) {
		if ((ny >= 0) && (e.layerY > 0)) {
			Drag.obj.root.style.top = ny + "px";
		}
		
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

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

	end : function()
	{
		Drag.obj.root.style.opacity = "1";
		Drag.obj.root.style.filter = "alpha(opacity=100);";
		Drag.obj.root.style.cursor = "auto;";
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style.left), 
									parseInt(Drag.obj.root.style.top));
		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;
	}
};

////////////////////////////////// STACKING FUNCTIONS

var prevObj = null;
var prevLayer = 0;
var layerMax = 0;

function setLayer(r, top) {
	var objStyle = r.style;
	objStyle.zIndex = top;
	layerMax = top;
}
function findLayer(r) {
	var objStyle =  r.style;
	if (objStyle.zIndex != null) {
		return objStyle.zIndex;
	} else {
		objStyle.zIndex = layerMax;
		return objStyle.zIndex;
	}
	return null;
}
function setColor(r, color) {
	var objStyle = r.style;
	if (window.XMLHttpRequest) {
		r.childNodes[1].style.backgroundColor = color;
	} else if (window.ActiveXObject) {
		r.childNodes[0].style.backgroundColor = color;
	}
}
function swapZ(r) {
	if (prevObj != null) {
		setColor(prevObj, "#909090");
	}
	setLayer(r, (layerMax + 3));
	setColor(r, "#404080");
	prevLayer = findLayer(r);
	prevObj = r;
}

////////////////////////////////// SHOW-HIDE FUNCTIONS

var show = true;
var saved;
function toggle(t) {
	var obj = t.parentNode;
	if (show) {
		show = false;
		t.innerHTML = "+";
		saved = obj.style.height;
		obj.style.height = null;
		obj.className = "rootmin";
	} else {
		show = true;
		t.innerHTML = "_";
		obj.style.height = saved;
		obj.className = "root";
	}
}

////////////////////////////////// RESIZING FUNCTIONS

function sizeListener(h) {
		var theHandle = h;
		var theRoot   = h.parentNode;
		if (navigator.appName=="Microsoft Internet Explorer") {
			var theContent = theRoot.childNodes[2];
		} else {
			var theContent = theRoot.childNodes[5];
		}
		var theSpace  = theRoot.parentNode;
		Size.init(theHandle, theContent, theRoot);
}
var Size = {

	obj : null,

	init : function(o, oContent, oRoot, initX, initY, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Size.start;
		
		o.root = oRoot;
		o.root.content = oContent;
	
		// SET STYLE VALUES FOR LENGTH/WIDTH	
		o.root.content.style.width = o.root.content.offsetWidth + "px";
		o.root.content.style.height = o.root.content.offsetHeight + "px";
		o.root.style.width = (o.root.clientWidth) + "px";
		o.root.style.height = (o.root.clientHeight) + "px";

		o.root.onSizeStart	= new Function();
		o.root.onSizeEnd	= new Function();
		o.root.onSize		= new Function();
	},

	start : function(e)
	{
		var o = Size.obj = this;
		e = Size.fixE(e);
		var rw = o.root.style.width;
		var rh = o.root.style.height;
		var cw = o.root.content.style.width;
		var ch = o.root.content.style.height;
		o.root.onSizeStart(rw, rh, cw, ch);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		document.onmousemove	= Size.drag;
		document.onmouseup		= Size.end;

		return false;
	},

	drag : function(e)
	{
		e = Size.fixE(e);
		var o = Size.obj;
		// MOUSE POSITION
		var ex	= e.clientX;
		var ey	= e.clientY;
		// SIZE
		var rw = parseInt(o.root.style.width);
		var rh = parseInt(o.root.style.height);
		var cw = parseInt(o.root.content.style.width);
		var ch = parseInt(o.root.content.style.height);
		// INITIALIZE NEW SIZE
		var nrw, nrh;
		var ncw, nch;

		// NEW WIDTH = OLD WIDTH + (MOUSEPOSITION - OLDMOUSEPOSITION)
		nrw = rw + (ex - o.lastMouseX);
		nrh = rh + (ey - o.lastMouseY);
		ncw = cw + (ex - o.lastMouseX);
		nch = ch + (ey - o.lastMouseY);
		
		//if ((nx >= -1) && (nxmax < o.root.space.clientWidth) && (e.layerX > 0) && (e.layerY < o.root.clientWidth)) {
		Size.obj.root.style.width = nrw + "px";
		Size.obj.root.content.style.width = ncw + "px";
		//if ((ny >= -1) && (nymax < o.root.space.clientHeight) && (e.layerY > 0) && (e.layerY < o.root.clientHeight)) {
		Size.obj.root.style.height = nrh + "px";
		Size.obj.root.content.style.height = nch + "px";
		
		Size.obj.lastMouseX	= ex;
		Size.obj.lastMouseY	= ey;

		Size.obj.root.onSize(nrw, nrh, ncw, nch);
		return false;
	},
	
	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Size.obj.root.onSizeEnd(	parseInt(Size.obj.root.style.width), 
									parseInt(Size.obj.root.style.height),
									parseInt(Size.obj.root.content.style.height),
									parseInt(Size.obj.root.content.style.height));
		Size.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;
	}
};

