// filename: dynlayer.js
// Dynamic Layer Object: sophisticated layer/element targeting and animation object
// which provides the core functionality needed in most DHTML applications
// version 1.12 - 2001-09-04
// authors: written by Dan Steinman (http://www.dansteinman.com/dynduo/),
// improved by Martin Kliehm (mak) martin@bembelterror.de and goosh goosh@morbid.de

// window.onerror = null // standard error catching added by mak

// DynLayerInit() spots <DIV> elements along the document tree and passes their IDs to DynLayer() to create DynLayer objects
function DynLayerInit(nestref) {
	if (!DynLayer.set) DynLayer.set = true
	if (is.ns4) {
		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) {
				var dynName = divname.substr(0,index) // added 02/2000 by goosh
				eval(dynName+' = new DynLayer("' + divname + '")')
				// added by dg 02/2000: WinIE4 doesn't display layers in correct width unless resize
				// eval(dynName+".resizeTo("+dynName+".elm.scrollWidth,"+dynName+".elm.scrollHeight)")
				}
			}
		}
	else if (is.dom) {
		if (is.opera) { // added 06 Aug 2001 by mak
			for (var i = 0; i < document.body.getElementsByTagName("div").length; i++) {
				var divname = document.body.getElementsByTagName("div")[i].id
				var index = divname.indexOf("Div")
				if (index > 0) {
					eval(divname.substr(0,index)+' = new DynLayer("'+divname+'")')
					}
				}
			}
		else {
			for (var i = 0; i < document.getElementsByTagName("div").length; i++) {
				var divname = document.getElementsByTagName("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

// create DynLayer objects
function DynLayer(id,nestref,frame) {
	if (!DynLayer.set && !frame) DynLayerInit()
	
	dynLayerNamesArray[dynLayerNamesArray.length] = id // added 02/2000 by goosh

	this.frame = frame || self
	// deleted is.ns5 code 12 May 2000 by mak
	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
		this.bgColor = this.css.bgColor // added 12/1999 by mak
		}
	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.bgColor = this.css.backgroundColor // added 12/1999 by mak
		}
	// added W3C DOM code 12 May 2000 by mak
	else if (is.dom) {
		this.elm = document.getElementById(id)
		this.css = this.elm.style
		this.doc = document
		this.x = parseInt(getComputedStyle(this.elm, null).getPropertyValue('left'))
		this.y = parseInt(getComputedStyle(this.elm, null).getPropertyValue('top'))
		this.w = parseInt(getComputedStyle(this.elm, null).getPropertyValue('width')) // changed 08 Dec 2000 by mak; this.css.width returns empty string
		this.h = parseInt(getComputedStyle(this.elm, null).getPropertyValue('height')) // changed 08 Dec 2000 by mak
		this.bgColor = this.css.bgColor
		}

	this.z = this.css.zIndex // added 12/1999 by goosh
	this.id = id
	this.name = id.substr(0,id.length - 3) // added 12/1999 by goosh
	this.nestref = nestref
	this.obj = id + "DynLayer"
	eval(this.obj + "=this")
	}
	
dynLayerNamesArray = new Array() // added 02/2000 by goosh

// move DynLayer
function DynLayerMoveTo(x,y) {
	if (x != null) {
		this.x = x
		if (is.ns4) this.css.left = (leftOffset) ? this.x + leftOffset : this.x; //  "(leftOffset) ? this.x + leftOffset : this.x;" added by mak 12/1999 // added "4" to "is.ns" 08 Dec 2000 by mak
		else if (is.ie) this.css.pixelLeft = (leftOffset) ? this.x + leftOffset : this.x; //  "(leftOffset) ? this.x + leftOffset : this.x;" added by mak 12/1999
		else if (is.dom) this.css.left = (leftOffset) ? (this.x + leftOffset) + "px" : this.x + "px"; // added 12 May 2000
		}
	if (y != null) {
		this.y = y
		if (is.ns4) this.css.top = this.y // added "4" to "is.ns" 08 Dec 2000 by mak
		else if (is.ie) this.css.pixelTop = this.y
		else if (is.dom) this.css.top = this.y + "px" // added 12 May 2000 by mak
		}
	}

function DynLayerMoveBy(x,y) { this.moveTo(this.x + x,this.y + y) }
function DynLayerShow() { this.css.visibility = (is.ns4) ? "show" : "visible" } // added is.ns4 12 May 2000 by mak
function DynLayerHide() { this.css.visibility = (is.ns4) ? "hide" : "hidden" } // added is.ns4 12 May 2000 by mak

// set background color // changed by mak 30 Nov 2000
function DynLayerSetbg(color) {
	if (is.ie || is.dom) this.css.backgroundColor = color // added is.dom 08 Dec 2000
	else if (is.ns4) this.css.bgColor = color
	}

if (!DynLayer.prototype) DynLayer.prototype = true // added 4 Sep 2001 by mak
DynLayer.prototype.moveTo = DynLayerMoveTo
DynLayer.prototype.moveBy = DynLayerMoveBy
DynLayer.prototype.show = DynLayerShow
DynLayer.prototype.hide = DynLayerHide
DynLayer.prototype.setbg = DynLayerSetbg
DynLayer.prototype.setBgColor = DynLayerSetbg // added 12/1999 by goosh
DynLayerTest = new Function('return true')
leftOffset = 0

// Slide Methods
function DynLayerSlideTo(endx,endy,inc,speed,fn) {
	if (endx==null) endx = this.x
	if (endy==null) endy = this.y
	var distx = endx-this.x
	var disty = endy-this.y
	this.slideStart(endx,endy,distx,disty,inc,speed,fn)
	}

function DynLayerSlideBy(distx,disty,inc,speed,fn) {
	var endx = this.x + distx
	var endy = this.y + disty
	this.slideStart(endx,endy,distx,disty,inc,speed,fn)
	}

function DynLayerSlideStart(endx,endy,distx,disty,inc,speed,fn) {
	if (this.slideActive) return
	if (!inc) inc = 10
	if (!speed) speed = 20
	var num = Math.sqrt(Math.pow(distx,2) + Math.pow(disty,2))/inc
	if (num==0) return
	var dx = distx/num
	var dy = disty/num
	if (!fn) fn = null
	this.slideActive = true
	this.slide(dx,dy,endx,endy,num,1,speed,fn)
	}

function DynLayerSlide(dx,dy,endx,endy,num,i,speed,fn) {
	if (!this.slideActive) return
	if (i++ < num) {
		this.moveBy(dx,dy)
		this.onSlide()
		if (this.slideActive) setTimeout(this.obj+".slide("+dx+","+dy+","+endx+","+endy+","+num+","+i+","+speed+",\""+fn+"\")",speed)
		else this.onSlideEnd()
		}
	else {
		this.slideActive = false
		this.moveTo(endx,endy)
		this.onSlide()
		this.onSlideEnd()
		eval(fn)
		}
	}

function DynLayerSlideInit() {}
DynLayer.prototype.slideInit = DynLayerSlideInit
DynLayer.prototype.slideTo = DynLayerSlideTo
DynLayer.prototype.slideBy = DynLayerSlideBy
DynLayer.prototype.slideStart = DynLayerSlideStart
DynLayer.prototype.slide = DynLayerSlide
DynLayer.prototype.onSlide = new Function()
DynLayer.prototype.onSlideEnd = new Function()

// Clip Methods
// changed 31 Jul 2000 by mak; added DOM compliance
function DynLayerClipInit(clipTop,clipRight,clipBottom,clipLeft) {
	if (is.ie) {
		if (arguments.length==4) this.clipTo(clipTop,clipRight,clipBottom,clipLeft)
		else if (is.ie4) this.clipTo(0,this.css.pixelWidth,this.css.pixelHeight,0)
		}
	}

function DynLayerClipTo(t,r,b,l) {
	if (t==null) t = this.clipValues('t')
	if (r==null) r = this.clipValues('r')
	if (b==null) b = this.clipValues('b')
	if (l==null) l = this.clipValues('l')
	if (is.ns4) {
		this.css.clip.top = t
		this.css.clip.right = r
		this.css.clip.bottom = b
		this.css.clip.left = l
		}
	else if (is.ie || is.dom) this.css.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)"
	}

function DynLayerClipBy(t,r,b,l) {
	this.clipTo(this.clipValues('t')+t,this.clipValues('r')+r,this.clipValues('b')+b,this.clipValues('l')+l)
	}

function DynLayerClipValues(which) {
	if (is.ie || is.dom) var clipv = this.css.clip.split("rect(")[1].split(")")[0].split("px")
	if (which=="t") return (is.ns4) ? this.css.clip.top : Number(clipv[0])
	if (which=="r") return (is.ns4) ? this.css.clip.right : Number(clipv[1])
	if (which=="b") return (is.ns4) ? this.css.clip.bottom : Number(clipv[2])
	if (which=="l") return (is.ns4) ? this.css.clip.left : Number(clipv[3])
	}

DynLayer.prototype.clipInit = DynLayerClipInit
DynLayer.prototype.clipTo = DynLayerClipTo
DynLayer.prototype.clipBy = DynLayerClipBy
DynLayer.prototype.clipValues = DynLayerClipValues

// write methods
function DynLayerWrite(html) {
	if (is.ns4) {
		this.doc.open()
		this.doc.write(html)
		this.doc.close()
		}
	else if (is.ie) {
		this.elm.innerHTML = html
		}
	// added DOM code by mak 15 May 2000
	else if (is.dom) {
		this.elm.innerHTML = html // innerHTML is experimental in NS6 - further research for more elegant ways is neccessary
		}
	}
DynLayer.prototype.write = DynLayerWrite

// css() was double. Deleted one copy 03 Jan 2001 by mak

// CSS function // copied from Dan Steinman's dynlayer.js 30 Nov 2000 by mak
// returns CSS syntax for generated layers
function css(id,left,top,width,height,color,vis,z,other) {
	// write start and end tags
	if (id=="start") return '<style type="text/css">\n'
	else if (id=="end") return '</style>'
	// start style definition
		// position
		var str = (left!=null && top!=null)? '#' + id + ' {position: absolute; left: ' + left + 'px; top: ' + top + 'px;' : '#' + id + ' {position: relative;'
		 // width
		if (arguments.length >= 4 && width != null) str += ' width: ' + width + 'px;'
		if (arguments.length >= 5 && height != null) {
			// height
			str += ' height: ' + height + 'px;'
			// clipping
			if (arguments.length < 9 || other.indexOf('clip') == -1) str += ' clip: rect(0px, '+width+'px, '+height+'px, 0px);'
			}
		// background-color
		if (arguments.length >= 6 && color != null) {
			str += ' background-color: ' + color + '; layer-background-color: ' + color + ';'
			}
		// visibility
		if (arguments.length >= 7 && vis != null) str += ' visibility: ' + vis + ';'
		// z-index
		if (arguments.length >= 8 && z != null) str += ' z-index:' + z + ';'
		// other style definitions
		if (arguments.length == 9 && other != null) str += ' ' + other
	// end style definition
	str += '}\n'
	return str
	}

function writeCSS(str,showAlert) {
	str = css('start') + "\t<!--\n" + str + "\t-->\n\t" + css('end') // added comments, tabs, and new lines 30 Nov 2000 by mak
	document.write(str)
	if (showAlert) alert(str)
	}
	
// added functions/methods start here
// WindowAttributes() creates universal accessible window attribute objects
function WindowAttributes() {
	if (is.ie) {
		this.width = document.body.clientWidth
		this.height	= document.body.clientHeight
		}			
	else if (is.ns || is.dom) { // changed 10 Aug 2000 by mak: added "else"
		this.width = window.innerWidth
		this.height = window.innerHeight
		}
	}

// updates width and height memorized values of elements
// changed 28 Jul 2000 by mak; added DOM compliance
function newUpdate() {
	if (is.ns4) {
		this.w = this.css.clip.width
		this.h = this.css.clip.height
		}
	else if (is.ie) {
		this.w = (is.ie4)? this.css.pixelWidth : this.elm.offsetWidth
		this.h = (is.ie4)? this.css.pixelHeight : this.elm.offsetHeight
		}
	else if (is.dom) {
		this.w = parseInt(this.css.width)
		this.h = parseInt(this.css.height)
		}
	}

// sets the zIndex of an element
function newSetZIndex(z) {
	this.z = z
	this.css.zIndex = z
	}

// resizes an element by some pixels
function newResizeLayerBy(dw,dh) {
	this.w = this.w + dw
	this.h = this.h + dh
	this.resizeTo(this.w,this.h)
	}

// resizes an element to a total size
// changed 28 Jul 2000 by mak; added DOM compliance
function newResizeLayerTo(w,h) {
	if (w!=null) this.w = w
	if (h!=null) this.h = h
	if (is.ie4) {
		this.css.pixelWidth = this.w
		this.css.pixelHeight = this.h
		}
	else if (is.ns4) this.css.resizeTo(this.w,this.h)
	else if (is.dom) {
		this.css.width = this.w + "px"
		this.css.height = this.h + "px"
		}
	}

DynLayer.prototype.update = newUpdate
DynLayer.prototype.resizeBy = newResizeLayerBy
DynLayer.prototype.resizeTo = newResizeLayerTo
DynLayer.prototype.setZIndex = newSetZIndex
