// Drag Object
// Copyright 1998 Dan Steinman
// Available at the Dynamic Duo (http://www.dansteinman.com/dynduo/)
// June 5 1998.  Last Updated June 23, 1998.
// In order to use this code you must keep this disclaimer

ns4 = (document.layers)? true:false
ie4 = (document.all)? true:false

function Drag() {
	this.obj = null
	this.array = new Array()
	this.active = false
	this.length = 0
	this.offsetX = 0
	this.offsetY = 0
	this.zIndex = 0
	this.resort = true
	this.add = DragAdd
	this.remove = DragRemove
	this.mouseDown = DragMouseDown
	this.mouseMove = DragMouseMove
	this.mouseUp = DragMouseUp
}
function DragAdd() {
	for (var i=0; i<arguments.length; i++) {
		var index = this.length
		this.array[index] = arguments[i]
		if (ns4) {
			this.array[index].w = this.array[index].css.clip.width
			this.array[index].h = this.array[index].css.clip.height
		}
		else if (ie4) {
			this.array[index].w = this.array[index].css.pixelWidth
			this.array[index].h = this.array[index].css.pixelHeight
		}
		this.zIndex += 1
		this.length += 1
	}
}
function DragRemove() {
	for (var i=0; i<arguments.length; i++) {
		for (var j=0; j<this.length; j++) {
			if (this.array[j]==arguments[i]) {
				for (var k=j;k<=this.length-2;k++) this.array[k] = this.array[k+1]
				this.array[this.length-1] = null
				this.length -= 1
				break
			}
		}
	}
}
function DragMouseDown(x,y) {
	for (var i=this.length-1;i>=0;i--) {
		var lyr = this.array[i]
		if (checkWithin(x,y,lyr.x,lyr.x+lyr.w,lyr.y,lyr.y+lyr.h)) {
			this.obj = this.array[i]
			this.offsetX = x-this.obj.x
			this.offsetY = y-this.obj.y
			this.active = true
			break
		}
	}
	if (this.active && this.resort) {
		//this.obj.css.zIndex = this.zIndex++    //j@b
		
		for (var j=i;j<=this.length-2;j++) this.array[j] = this.array[j+1]
		this.array[this.length-1] = this.obj
	}
}
function DragMouseMove(x,y) {
	this.obj.moveTo(x-this.offsetX,y-this.offsetY)
}
function DragMouseUp() {
	if (drag.active) drag.active = false
}

// automatically define the "drag" object
// drag = new Drag() // jordi

// checkWithin() function is required
function checkWithin(x,y,left,right,top,bottom) {
	if (x>=left && x<right && y>=top && y<bottom) return true
	else return false
}