// Drag Object
// an object that makes an unlimited number DynLayers draggable
// 19991010

// Copyright (C) 1999 Dan Steinman
// Distributed under the terms of the GNU Library General Public License
// Available at http://www.dansteinman.com/dynapi/

function Drag() {
	this.obj = null
	this.array = new Array()
	this.active = false
	this.offsetX = 0
	this.offsetY = 0
	this.zIndex = 0
	this.resort = true
	this.add = DragAdd
	this.remove = DragRemove
	this.setGrab = DragSetGrab
	this.mouseDown = DragMouseDown
	this.mouseMove = DragMouseMove
	this.mouseUp = DragMouseUp
	this.onDragStart = new Function()
	this.onDragMove = new Function()
	this.onDragEnd = new Function()
	this.nbMove = 0
}
function DragAdd() {
	var l = this.array.length
	this.array[l] = arguments[0]
	this.array[l].way = arguments[1]
	this.array[l].col = arguments[2]
	this.array[l].row = arguments[3]
	this.array[l].nbCol = arguments[4]
	this.array[l].nbRow = arguments[5]
	this.array[l].dragGrab = new Array(0,this.array[l].w,this.array[l].h,0)
	this.zIndex += 1
}
function DragSetGrab(dynlayer,top,right,bottom,left) {
	dynlayer.dragGrab = new Array(top,right,bottom,left)
}
function DragRemove() {
	for (var i=0; i<arguments.length; i++) {
		for (var j=0; j<this.array.length; j++) {
			if (this.array[j]==arguments[i]) {
				for (var k=j;k<=this.array.length-2;k++) this.array[k] = this.array[k+1]
				this.array[this.array.length-1] = null
				this.array.length -= 1
				break
			}
		}
	}
}
function DragMouseDown(x,y) {
	for (var i=this.array.length-1;i>=0;i--) {
		var lyr = this.array[i]
		if (checkWithinLayer(x,y,lyr)) {
			this.obj = this.array[i]
			this.offsetX = x-this.obj.x
			this.offsetY = y-this.obj.y
			this.active = true
			break
		}
	}
	if (!this.active) return false
	else {
		if (this.resort) {
			this.obj.css.zIndex = this.zIndex++
			for (var j=i;j<=this.array.length-2;j++) this.array[j] = this.array[j+1]
			this.array[this.array.length-1] = this.obj
		}
		this.onDragStart(x,y)
		return true
	}
}
function DragMouseMove(x,y) {
	if (!this.active) return false
	else {
	x0 = this.obj.x
	y0 = this.obj.y
		if (this.obj.way == 'h')
			this.obj.moveTo(x-this.offsetX,this.obj.y)
		else
			this.obj.moveTo(this.obj.x,y-this.offsetY)
		this.onDragMove(x,y)

		for (var i=0; i<this.array.length && ! this.obj.conflict;i++) {
			var lyr = this.array[i]
			if (this.obj.id != lyr.id) {
				if (checkCrossLayer(this.obj, lyr)) {
					this.obj.conflict = true
				}
			}
		}

		return true
	}
}
function DragMouseUp(x,y) {
	if (!this.active) return false
	else {
		this.active = false
		this.onDragEnd(x,y)

		if (this.obj.conflict) {
			this.obj.moveTo(this.obj.col * 40 + 305, this.obj.row * 40 + 85)
		}
		else {
			col = (this.obj.x - 340) % 40
			col = (this.obj.x - 340 - col) / 40 + 1
			if (col < 1) col = 1
			else if (col + this.obj.nbCol > 7) {
				if (this.obj.id == "XDiv") {
					col = 6
					this.nbMove += Math.abs (this.obj.col - col)
					this.obj.col = col
					this.obj.moveTo(this.obj.col * 40 + 305, this.obj.row * 40 + 85)
					this.array.length = 0
					winner (this.nbMove)
					return;
				}
				else {
					col = 7 - this.obj.nbCol
				}
			}
			this.nbMove += Math.abs (this.obj.col - col)
			this.obj.col = col
			row = (this.obj.y - 120) % 40
			row = (this.obj.y - 120 - row) / 40 + 1
			if (row < 1) row = 1
			else if (row  + this.obj.nbRow > 7) row = 7 - this.obj.nbRow
			this.nbMove += Math.abs (this.obj.row - row)
			this.obj.row = row
		}
		this.obj.moveTo(this.obj.col * 40 + 305, this.obj.row * 40 + 85)
		this.obj.conflict = false

		return true
	}
}

function checkCrossLayer(obj, lyr) {
	cA = (obj.x - 340) % 40
	cA = (obj.x - 340 - cA) / 40 + 1
	cB = cA + obj.nbCol
	rA = (obj.y - 120) % 40
	rA = (obj.y - 120 - rA) / 40 + 1
	rB = rA + obj.nbRow
	c0 = lyr.col
	c1 = lyr.col + lyr.nbCol
	r0 = lyr.row
	r1 = lyr.row + lyr.nbRow
	for (var i = c0; i < c1; i ++)
		for (var j = r0; j < r1; j ++)
			for (var k = cA; k < cB; k ++)
				for (var l = rA; l < rB; l ++)
					if (i == k && j == l) return true
	return false
}

function checkWithin(x,y,left,right,top,bottom) {
	if (x>=left && x<right && y>=top && y<bottom) return true
	else return false
}
function checkWithinLayer(x,y,lyr) {
	if (checkWithin(x,y,lyr.x+lyr.dragGrab[3],lyr.x+lyr.dragGrab[1],lyr.y+lyr.dragGrab[0],lyr.y+lyr.dragGrab[2])) return true
	else return false
}

// automatically define the default "drag" Drag Object
drag = new Drag()
