// layer.js - Dynamic layer basics

// startup flag
var gMod_Layer = true;

// prerequisites
var gMod_Detect;
if (!gMod_Detect){alert("You must load detect.js before layer.js");}

/////////////////////////////////////////////////////////////////////////////////////////////

function DynLayer(id,nestref,frame) {
	if (!is.ns5 && !DynLayer.set && !frame) DynLayerInit()
	this.frame = frame || self
	if (is.ns) {
		if (is.ns4) {
			if (!frame) {
				if (!nestref) var nestref = DynLayer.nestRefArray[id]
				if (!DynLayerTest(id,nestref)) return;
				this.css = (nestref)? eval("document."+nestref+".document."+id) : document.layers[id]
			}
			else this.css = (nestref)? eval("frame.document."+nestref+".document."+id) : frame.document.layers[id]
			this.elm = this.event = this.css
			this.doc = this.css.document

			this.x = this.css.left;
			this.y = this.css.top;
			this.w = this.css.clip.width;
			this.h = this.css.clip.height;

		}else if (is.ns5) {
			this.elm = document.getElementById(id);
			this.css = this.elm.style;
			this.doc = document;

			this.x = parseInt(this.elm.style.left);
			this.y = parseInt(this.elm.style.top);
			this.w = parseInt(this.elm.style.width);
			this.h = parseInt(this.elm.style.height);
			if (!this.w){this.w = this.elm.offsetWidth;}
			if (!this.h){this.h = this.elm.offsetHeight;}

		}
	}else if ((is.ie5) && (is.mac)){
		this.elm = document.getElementById(id);
		this.event = this.elm;
		this.css = this.elm.style;
		this.doc = document;

		this.x = parseInt(this.elm.style.left);
		this.y = parseInt(this.elm.style.top);
		this.w = parseInt(this.elm.style.width);
		this.h = parseInt(this.elm.style.height);
		if (!this.w){this.w = this.elm.offsetWidth;}
		if (!this.h){this.h = this.elm.offsetHeight;}

	}else if (is.ie) {
		this.elm = this.event = this.frame.document.all[id]
		this.css = this.frame.document.all[id].style
		this.doc = document
		this.x = this.elm.offsetLeft
		this.y = this.elm.offsetTop
		this.w = (is.ie4)? this.css.pixelWidth : this.elm.offsetWidth
		this.h = (is.ie4)? this.css.pixelHeight : this.elm.offsetHeight
	}
	this.id = id
	this.nestref = nestref
	this.obj = id + "DynLayer"
	eval(this.obj + " = this");

	this.dragGrab = new Array();
	this.clickGrab = new Array();
}
DynLayerTest = new Function('return true');

// DynLayerInit Function
function DynLayerInit(nestref) {
	if (!DynLayer.set) DynLayer.set = true
	if (is.ns) {
		if (nestref) ref = eval('document.'+nestref+'.document')
		else {nestref = ''; ref = document;}
		for (var i=0; i<ref.layers.length; i++) {
			var divname = ref.layers[i].name
			DynLayer.nestRefArray[divname] = nestref
			var index = divname.indexOf("Div")
			if (index > 0) {
				eval(divname.substr(0,index)+' = new DynLayer("'+divname+'","'+nestref+'")')
			}
			if (ref.layers[i].document.layers.length > 0) {
				DynLayer.refArray[DynLayer.refArray.length] = (nestref=='')? ref.layers[i].name : nestref+'.document.'+ref.layers[i].name
			}
		}
		if (DynLayer.refArray.i < DynLayer.refArray.length) {
			DynLayerInit(DynLayer.refArray[DynLayer.refArray.i++])
		}
	}
	else if (is.ie) {
		for (var i=0; i<document.all.tags("DIV").length; i++) {
			var divname = document.all.tags("DIV")[i].id
			var index = divname.indexOf("Div")
			if (index > 0) {
				eval(divname.substr(0,index)+' = new DynLayer("'+divname+'")')
			}
		}
	}
	return true
}
DynLayer.nestRefArray = new Array()
DynLayer.refArray = new Array()
DynLayer.refArray.i = 0
DynLayer.set = false

function DynLayerGetAbsX(){

	var theX = this.x;

	if (this.nestref){
		//find the object for the parent
		eval("parentobj = "+this.nestref+"DynLayer");
		theX += parentobj.getAbsX();
	}

	return theX;
}
function DynLayerGetAbsY(){

	var theY = this.y;

	if (this.nestref){
		//find the object for the parent
		eval("parentobj = "+this.nestref+"DynLayer");
		theY += parentobj.getAbsY();
	}

	return theY;
}
DynLayer.prototype.getAbsX = DynLayerGetAbsX;
DynLayer.prototype.getAbsY = DynLayerGetAbsY;

/////////////////////////////////////////////////////////////////////////////////////////////

