function objDrag(){
	this.Layers = new Array()
	this.buffer = new Array()
	this.obj = null
	this.dragActive = false
	this.offsetX = 0
	this.offsetY = 0
	this.add = obj_dragAdd
	this.dragMouseDown = obj_dragMouseDown
	this.dragMouseMove = obj_dragMouseMove
	this.dragMouseUp = obj_dragMouseUp
	this.setFirstLayer = obj_setFirstLayer
}

function obj_dragAdd() {
	for (var i=0; i<arguments.length; i++) {
		this.Layers[this.Layers.length] = arguments[i]
		this.zIndex = i+1
	}
}

function obj_dragMouseDown(x,y){
	if (scrolling) return false
	/* loop through this.Layers to see if the mouse was clicked on a layer */
	for (i=0;i<this.Layers.length;i++){
		var div = this.Layers[i]
		if (checkWithinLayer(x,y,div)) {
			this.obj = this.Layers[i]
			this.offsetX = x-this.obj.x
			this.offsetY = y-this.obj.y
			
			if (!this.obj.dragParent) {currentWindow = this.obj.windowIndex} else if (this.obj.dragParentIndex) {currentWindow = this.obj.dragParentIndex-1}
			
			zIndex++
			if (!this.Layers[i].dragParent && this.Layers[i].windowIndex<100){
				this.obj.css.zIndex = zIndex
			} else if (this.Layers[i].dragParent){
				this.obj.dragParent.css.zIndex = zIndex
			}
			
			this.dragActive = true
			if (!this.obj.dragParent && this.obj.windowIndex==101 && bw.ns4) this.obj.dragChild.hide()
			
			this.setFirstLayer(i)
			break
		}
	}
	if (this.dragActive){
		return true
	}
	else return false
}

function obj_setFirstLayer(m){
	for (n=0;n<this.Layers.length;n++){
		this.buffer[n] = this.Layers[n]
	}
	
	firstLayer = this.Layers[m]
	do {
		this.buffer[m] = this.buffer[m-1]
		m--
	} while (m-1>=0)
	
	this.buffer[0] = firstLayer
	
	for (n=0;n<this.Layers.length;n++){
		this.Layers[n] = this.buffer[n]
	}
}
		

function obj_dragMouseMove(x,y){
	if (this.dragActive) {
		if (this.obj.dragParentIndex) {
			if (y>(this.obj.dragParent.y+75)){
				this.obj.moveTo(this.obj.x,y-this.offsetY)
				windows[VER][this.obj.dragParentIndex-1][3] = this.obj.y+18
				windows[VER][currentWindow][3] = this.obj.y+18
				sizeWindow(this.obj.dragParentIndex-1)
			}
		} else {
			if ((x>0)&&(x<getBrowserWidth()-2)&&(y>windowCeiling+this.offsetY)&&(y<getBrowserHeight()-(16-this.offsetY))){
				this.obj.moveTo(x-this.offsetX,y-this.offsetY)
			} else {
				this.dragMouseUp(x,y)
			}
		}
		return false
	}
	else return true
}

function obj_dragMouseUp(x,y){
	if (this.dragActive&&!this.obj.dragParent&&currentWindow<=windows[VER].length) {
		windows[VER][currentWindow][0] = x-this.offsetX
		windows[VER][currentWindow][1] = y-this.offsetY
		windows[VER][currentWindow].objContent.scrollActive = 0
	}
	writeCookies(currentWindow)
	if (this.dragActive){
		if (this.obj&&this.obj.dragChild) {this.obj.dragChild.globalX=this.obj.x; this.obj.dragChild.globalY=this.obj.y}
		if (!this.obj.dragParent && this.obj.windowIndex==101 && bw.ns4) this.obj.dragChild.show()
	}
	this.dragActive = false
	return true
}

var drag = new objDrag()

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,div) {
	if (checkWithin(x,y,div.x+div.globalX+div.grabRegion[3],div.x+div.globalX+div.grabRegion[1],div.y+div.globalY+div.grabRegion[0],div.y+div.globalY+div.grabRegion[2])) {return true}
	else return false
}

function getBrowserHeight(){
	if (bw.ns4){
		return window.innerHeight
	} else {
		return document.body.clientHeight
	}
}

function getBrowserWidth(){
	if (bw.ns4){
		return window.innerWidth
	} else {
		return document.body.clientWidth
	}
}


