//
// Global bugDragComponent management -- BDC (Bugimus Drag Component)
//

bugBase.prototype.attachBDC = function() {
	this.BDCnum = 0
	this.BDCobj = new Array()
	this.BDCselected = 0
	this.BDCzbias = 5000
}
bugBase.prototype.BDCreStackZ = function() {
//	alert("reStackZ: selected index = "+this.BDCselected)
	for(i=0; i<this.BDCnum; i++ ) {
		if( this.BDCobj[i].dragObject.getz() > this.BDCobj[this.BDCselected].dragObject.getz() ) {
			this.BDCobj[i].dragObject.setz( this.BDCobj[i].dragObject.getz()-1 )
		}
	}
	this.BDCobj[this.BDCselected].dragObject.setz(bugimus.BDCnum+bugimus.BDCzbias-1)
}
window.bugimus.attachBDC()

//
// Local bugDragDrop functionality
//

// Define main object
function dragComponent(refObj, x, y, w, h) {
	this.objname = "window.bugimus.BDCobj[" + window.bugimus.BDCnum + "]"
	this.objnum = window.bugimus.BDCnum
	window.bugimus.BDCobj[window.bugimus.BDCnum++] = this
	this.enabled = true

	refObj.appendChild(this.dragObject=document.createElement("DIV"))
	this.dragObject.parentObject = this
	with(this.dragObject.style) {
		position="absolute"
		width=w+"px" 
		height=h+"px" 
		zIndex=bugimus.BDCnum+bugimus.BDCzbias-1
		border="0px"
//		backgroundColor="transparent"
//		border="solid 1px #960"
//		backgroundColor="#ff0"
//		clip="rect(0,"+(w+4)+","+(h+4)+",0)"
//		overflow="hidden"
		padding="0px"
	}
	this.dragObject.getx = getoX
	this.dragObject.gety = getoY
	this.dragObject.setx = setoX
	this.dragObject.sety = setoY
	this.dragObject.getw = getoWidth
	this.dragObject.geth = getoHeight
	this.dragObject.setw = setoWidth
	this.dragObject.seth = setoHeight
	this.dragObject.getz = getoZindex
	this.dragObject.setz = setoZindex
	this.dragObject.clip = clipoDiv
	this.dragObject.getp = getoPadding
	this.dragObject.getb = getoBorderWidth
	this.dragObject.setp = setoPadding
	this.dragObject.setb = setoBorderWidth

	this.dragObject.setx(x)
	this.dragObject.sety(y)

	this.dragOffsetX = 0
	this.dragOffsetY = 0
	this.dragging = false
	
	this.dragLimit = false   // constrain drag to specified area
	this.constrainX = false  // disable x movement
	this.constrainY = false  // disable y movement

	this.dragObject.onmousedown = function(e) {
		if(this.parentObject.enabled) {
			var x = 0
			var y = 0
			if(bugimus.IE5) {
			//	alert(getOffsetTops(pRef))
			//	x = window.event.clientX + bugimus.BMIscrollX() - refObj.offsetLeft
			//	y = window.event.clientY + bugimus.BMIscrollY() - refObj.offsetTop
				x = window.event.clientX + bugimus.BMIscrollX() - getOffsetLefts(refObj)
				y = window.event.clientY + bugimus.BMIscrollY() - getOffsetTops(refObj)
			} else {
			//	alert(getOffsetLefts())
			//	x = e.clientX + bugimus.BMIscrollX() - refObj.offsetLeft
			//	y = e.clientY + bugimus.BMIscrollY() - refObj.offsetTop
				x = e.clientX + bugimus.BMIscrollX() - getOffsetLefts(refObj)
				y = e.clientY + bugimus.BMIscrollY() - getOffsetTops(refObj)
			}
			var dx = this.getx()
			var dy = this.gety()
			var dw = this.getw() + 2*this.getb() + 2*this.getp()
			var dh = this.geth() + 2*this.getb() + 2*this.getp()
	//alert(this.parentObject.objname+" = "+x+" "+y+" "+dx+" "+dy+" "+dw+" "+dh+" ")
			if (x >= dx && x <= dx+dw && y >= dy && y <= dy+dh) {
				this.parentObject.dragOffsetX = x-dx
				this.parentObject.dragOffsetY = y-dy
	//alert(this.parentObject.objname+" = x="+x+" y="+y+" dragX="+this.parentObject.dragOffsetX+" dragY"+this.parentObject.dragOffsetY)
				this.parentObject.dragging = true
				bugimus.BDCselected = this.parentObject.objnum
				bugimus.BDCreStackZ()
				return false
			} else {
				return true
			}
		} else {
			bugimus.BDCselected = this.parentObject.objnum
			bugimus.BDCreStackZ()
		}
	}

	this.dragObject.elevate = function() {
		bugimus.BDCselected = this.parentObject.objnum
		bugimus.BDCreStackZ()
	}

	this.dragObject.handlemousemove = function() {
		if (this.parentObject.dragging == true) {
			if(!this.parentObject.constrainX) {
				//var x = bugimus.BMImousex - refObj.offsetLeft - this.parentObject.dragOffsetX
				var x = bugimus.BMImousex - getOffsetLefts(refObj) - this.parentObject.dragOffsetX
				if(bugimus.IE5) x += bugimus.BMIscrollX()
				if(x <= this.parentObject.leftLimit) x = this.parentObject.leftLimit
				if(x >= this.parentObject.rightLimit) x = this.parentObject.rightLimit
				this.parentObject.dragObject.setx(x)
			}
			if(!this.parentObject.constrainY) {
				//var y = bugimus.BMImousey - refObj.offsetTop - this.parentObject.dragOffsetY
				var y = bugimus.BMImousey - getOffsetTops(refObj) - this.parentObject.dragOffsetY
				if(bugimus.IE5) y += bugimus.BMIscrollY()
				if(y <= this.parentObject.topLimit) y = this.parentObject.topLimit
				if(y >= this.parentObject.bottomLimit) y = this.parentObject.bottomLimit
				this.parentObject.dragObject.sety(y)
			}
//if((x-this.parentObject.dragOffsetX) < 100) alert(this.parentObject.objname+" = BMImousex="+bugimus.BMImousex+" dragX="+this.parentObject.dragOffsetX)

			// call external function for this event
			if(this.parentObject.externalCall) setTimeout(this.parentObject.externalCall, 0)

			return false
		} else {
			return true
		}
	}

	this.dragObject.onmouseup = function() {
//alert("hello! "+this.parentObject.dragging)

		this.parentObject.dragging = false
		return true
	}

}

dragComponent.prototype.setDragArea = function(top,right,bottom,left) {
	this.dragLimit = true
	this.topLimit = top
	this.rightLimit = right
	this.bottomLimit = bottom
	this.leftLimit = left
}

dragComponent.prototype.resetDragArea = function() {
	this.dragLimit = false
}

dragComponent.prototype.setCallFunc = function(funcname) { 
	this.externalCall = funcname
}

dragComponent.prototype.enableDrag = function() {
	this.enabled = true
}

dragComponent.prototype.disableDrag = function() {
	this.enabled = false
}

// You have to redefine the global mousemove handler from BMI
bugBase.prototype.handlemousemove = function (e){
	if(bugimus.IE5) { 
		bugimus.BMImousex = event.clientX
		bugimus.BMImousey = event.clientY
	} else if(bugimus.NN4) {
		bugimus.BMImousex = e.pageX+2
		bugimus.BMImousey = e.pageY+2
	} else if(bugimus.NN6) {
		bugimus.BMImousex = e.pageX
		bugimus.BMImousey = e.pageY
	}

	if(bugimus.BDDobj && bugimus.BDDnum) bugimus.BDDobj[bugimus.BDDselected].obj.handlemousemove()
	if(bugimus.BDCobj && bugimus.BDCnum) bugimus.BDCobj[bugimus.BDCselected].dragObject.handlemousemove()

	// prevents selecting text on main page when cursor falls off edge... 
	//a global dragging flag might be a good way to have the cake and eat it too
	return false
}
window.bugimus.attachBMI()

