// DynAPI 2
// DHTML API which provides the core functionality needed in most DHTML applications
// 2000.03.11

// Copyright (C) 2000 Dan Steinman
// Thanks to: Micah Goulart, Chris Bodar, Pascal Bestebroer, Bill Tammen, Scott Andrew, Robert Rainwater, Clark Pacheco, IlMaestro
// Redistributable under the terms of the GNU Library General Public License
// Available at http://www.dansteinman.com/dynapi/

function BrowserCheck() {
	var b = navigator.appName
	if (b=="Netscape") this.b = "ns"
	else if (b=="Microsoft Internet Explorer") this.b = "ie"
	else this.b = b
	this.version = navigator.appVersion
	this.v = parseInt(this.version)
	this.ns = (this.b=="ns" && this.v>=4)
	this.ns4 = (this.b=="ns" && this.v==4)
	this.ns5 = (this.b=="ns" && this.v==5)
	this.ie = (this.b=="ie" && this.v>=4)
	this.ie4 = (this.version.indexOf('MSIE 4')>0)
	this.ie5 = (this.version.indexOf('MSIE 5')>0)
	this.min = (this.ns||this.ie)
	var ua = navigator.userAgent.toLowerCase()
	if (ua.indexOf("win")>-1) this.platform = "win32"
	else if (ua.indexOf("mac")>-1) this.platform = "win32"
	else this.platform = "other"
}
is = new BrowserCheck()

function DynAPILibrary() {this.toString = function() {return "DynAPI"}}

DynAPI = new DynAPILibrary()
DynAPI.getImage = function(src,w,h) {
	var i = new Image(w||0,h||0)
	i.src = src
	i.w = i.width
	i.h = i.height
	return i
}
DynAPI.getDocument = function(elm) {
	return DynAPI.document
	//if (elm == DynAPI.document.doc) 
	//else return null
}
DynAPI.resizeHandler = function() {
	var w = DynAPI.document.getWidth()
	var h = DynAPI.document.getHeight()
	DynAPI.document.findDimensions()
	if (is.ns4 && (w!=DynAPI.document.getWidth() || h!=DynAPI.document.getHeight())) {
		DynAPI.document.recreateAll()
	}
	DynAPI.onResize()
}
DynAPI.onResize = function() {
	
}
DynAPI.loadHandler = function(){
	DynAPI.init(DynAPI.document)
	DynAPI.document.captureMouseEvents()
	DynAPI.onLoad()
}
DynAPI.onLoad = function(){}
DynAPI.init = function(parent){
 	if (is.ns4) var divs = parent.doc.layers
 	
 	if (is.ie) var divs = parent.doc.body.tags("DIV")
 	for (var i=0; i<divs.length; i++) {
 		var id = is.ns4? divs[i].name : divs[i].id
		var dlyr = new DynLayer(id)
		dlyr.assignElement(divs[i])
		
		if (parent.getClass()!=DynDocument) dlyr.isChild = true
		dlyr.parent = parent		
		parent.children[parent.children.length] = dlyr
		
 		var index = id.indexOf("Div")
 		if (index>0) self[id.substr(0,index)] = DynAPI.document.all[id]
		
		dlyr.updateValues()
		
 		if (is.ns4 && dlyr.doc.layers.length>0) DynLayer.check[DynLayer.check.length] = dlyr
 	}
	if (DynLayer.check.i < DynLayer.check.length) {
		DynAPI.init(DynLayer.check[DynLayer.check.i++])
	}
}
DynLayer.check = []
DynLayer.check.i = 0
DynAPI.removeFromArray = function(array, index, id) {
	var temp = []
	var which = typeof index == "object" ? index : array[index]
	for (var i = 0; i < array.length; i++) {
		if (array[i] != which) {
			temp[temp.length] = array[i]
			if (id) temp[array[i].id] = array[i]
		}
	}
	return temp
}

onload = DynAPI.loadHandler
onresize = DynAPI.resizeHandler

function DynLayer() {
	this.elm = new Object()
	this.doc = new Object()
	this.css = new Object()
	var a = arguments
	if (a.length==1 && a[0]!=null && !a[0].length) this.setStyle(a[0])
	else {
		this.id = a[0]||"JSDynLayer"+(DynLayer.nullCount++)
		this.x = a[1]||0
		this.y = a[2]||0
		this.w = a[3]||null
		this.h = a[4]||null
		this.bgColor = a[5]||null
		this.visible = a[6]!='hidden'
	}
	this.children = []
	this.parent = null
	this.isChild = false
	this.created = false
	DynAPI.document.all[DynAPI.document.all.length] = this
	DynAPI.document.all[this.id] = this
}
DynLayer.nullCount = 0
DynLayer.prototype.getComponent = function () {return this}
DynLayer.prototype.setParentComponent = function (par) {this.parentComponent = par}
DynLayer.prototype.getParentComponent = function () {return this.parentComponent}
DynLayer.prototype.getClass = function() {return this.constructor}
DynLayer.prototype.toString = function () {return 'DynAPI.document.all.'+this.id}
DynLayer.prototype.addChild = function() {
	for (var a=0;a<arguments.length;a++) {
		var child = arguments[a].getComponent()
		if (child.created) child.removeFromParent()
		child.isChild = true
		child.parent = this
		this.children[this.children.length] = child
		if (this.created) {
			child.createElement()
		}
	}
	return arguments[arguments.length-1]
}
DynLayer.prototype.removeChild = function() {
	for (var a=0;a<arguments.length;a++) {
		var child = arguments[a].getComponent()
		for (var i=0;i<this.children.length;i++) {
			if (this.children[i]==child) {

				this.children = DynAPI.removeFromArray(this.children, child, false)
				
				child.deleteElement()
				child.parent = null
				child.isChild = false

				break

			}
		}
	}
	return arguments[arguments.length-1]
}
DynLayer.prototype.removeFromParent = function() {
	if (this.parent!=null) this.parent.removeChild(this)
}
DynLayer.prototype.deleteChild = function(child) {
	for (var a=0;a<arguments.length;a++) {
		var child = arguments[a].getComponent()
		for (var i=0;i<this.children.length;i++) {
			if (this.children[i]==child) {

				if (is.ns4) {
				 	if (!this.doc.recycled) this.doc.recycled = []
				 	var r = this.doc.recycled
				 	r[r.length] = child.elm
				}
			
				child.deleteAllChildren()
				this.children = DynAPI.removeFromArray(this.children, child, false)
				DynAPI.document.all = DynAPI.removeFromArray(DynAPI.document.all, child, true)
				if (is.ns4) {
					child.doc.open()
					child.doc.write('')
					child.doc.close()
				}
				child.deleteElement()
				child.parent = null
				child.isChild = false
				break
			}
		}
	}
}
DynLayer.prototype.deleteAllChildren = function() {
	for (var i in this.children) {
		this.children[i].created = false
		this.children[i].parent = null
		this.children[i].isChild = false
		this.children[i].deleteAllChildren()
		DynAPI.document.all = DynAPI.removeFromArray(DynAPI.document.all, this.children[i], true)
	}
	this.children = []
}
DynLayer.prototype.deleteFromParent = function() {this.parent.deleteChild(this)}
DynLayer.prototype.createElement = function(dyndoc) {
	if (this.created || !this.parent || !this.parent.created) return
	
	var id = this.id
	var parent = this.parent
	var w = this.w	
	var lyr
	var doc = parent.doc
	
	if (doc.recycled && doc.recycled.length>0) {
		lyr = doc.recycled[0]
		doc.recycled = DynAPI.removeFromArray(doc.recycled,doc.recycled[0],false)
	}
	else {
		var parentElement = (parent.getClass()==DynLayer)? parent.elm : parent.doc.body
		if (is.ns4) {
			lyr = new Layer(w,parent.elm)
		}
		else if (is.ie4) {
			var code = '<div id="' + id + '" style="position:absolute; left:0px; top:0px; width:'+w+'px;"></div>'
			parentElement.insertAdjacentHTML("beforeEnd", code)
			lyr = parentElement.children[parentElement.children.length-1]
		}
		else if (is.ie5 || is.ns5) {
			lyr = document.createElement("DIV")
			lyr.style.position = "absolute"
			lyr.style.width = w
			lyr.id=id
			parentElement.appendChild(lyr)
		}
	}
	this.assignElement(lyr)

	this.dyndoc = dyndoc
	this.moveTo(this.x,this.y)
	this.setSize(this.w,this.h)
	this.setBgColor(this.bgColor)
	if (this.bgImage!=null) this.setBgImage(this.bgImage)
	if (this.hasEventListeners) this.captureMouseEvents()
	if (this.html!=null) {
		this.setHTML(this.html)
		if (this.w==null) this.w = this.getContentWidth()
		if (this.h==null) this.h = this.getContentHeight()
		this.setSize(this.w,this.h,false)
	}
	if (this.clip) this.setClip(this.clip)
	if (this.z) this.setZIndex(this.z)
	this.setVisible(this.visible)
	this.createChildElements(doc)
	this.onCreate()
}
DynLayer.prototype.assignElement = function(elm) {
	this.elm = elm
	if (is.ns4) {			
		this.css = this.elm
		this.doc = this.elm.document
		this.doc.lyrobj = this
	}
	else if (is.ie || is.ns5) {
		this.css = this.elm.style
		this.doc = this.parent.doc
	}
	this.elm.lyrobj = this
	this.created = true
}
DynLayer.prototype.updateValues = function() {
	if (is.ns) {
		this.x = parseInt(this.css.left)
		this.y = parseInt(this.css.top)
		this.w = is.ns4? this.css.clip.width : parseInt(this.css.width)
		this.h = is.ns4? this.css.clip.height : parseInt(this.css.height)
		if (is.ns4) {
			this.clip = [this.css.clip.top,this.css.clip.right,this.css.clip.bottom,this.css.clip.left]
		}
	}
	else if (ie4) {
		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
	}
}
DynLayer.prototype.createChildElements = function(doc) {
	for (var i in this.children) {
		this.children[i].createElement(doc)
	}
}
DynLayer.prototype.deleteElement = function() {
	this.created = false
	if (is.ns4) this.elm.visibility="hide"
	else {
		this.elm.visibility="hidden"
		this.elm.innerHTML=""
		this.elm.outterHTML=""
	}
	this.elm = new Object()
	this.doc = new Object()
	this.css = new Object()
	this.deleteChildElements()
}
DynLayer.prototype.deleteChildElements = function() {
	for (var i in this.children) {
		this.children[i].deleteElement()
	}
}

DynLayer.prototype.setStyle = function (s) {
	var id=s.id,x=s.left,y=s.top,w=s.width,h=s.height,i=s.backgroundImage,c=s.backgroundColor,v=s.visibility,z=s.zIndex
	if (!this.id && !this.created) this.id = id||"JSDynLayer"+(DynLayer.nullCount++)
	if (x!=null||y!=null) this.moveTo(x,y)
	if (w!=null||h!=null) this.setSize(w,h)
	if (i!=null) this.setBgImage(i)
	if (c!=null) this.setBgColor(c)
	if (z!=null) this.setZIndex(z)
	this.setVisible(v!='hidden')
}
DynLayer.prototype.moveTo = function(x,y) {
	if (x!=null) {
		this.x = x
		if (is.ns) this.css.left = this.x
		else this.css.pixelLeft = this.x
	}
	if (y!=null) {
		this.y = y
		if (is.ns) this.css.top = this.y
		else this.css.pixelTop = this.y
	}
	this.invokeEvent('move')
}
DynLayer.prototype.moveBy = function(x,y) {this.moveTo(this.x+x,this.y+y)}
DynLayer.prototype.setX = function(x) {this.moveTo(x,null)}
DynLayer.prototype.setY = function(y) {this.moveTo(null,y)}
DynLayer.prototype.getX = function() {return this.x}
DynLayer.prototype.getY = function() {return this.y}
DynLayer.prototype.getPageX = function() {
	if (!this.created) return 0
	if (is.ns4) return this.css.pageX
	else return (this.isChild)? this.parent.getPageX()+this.x : this.x
}
DynLayer.prototype.getPageY = function() {
	if (!this.created) return 0
	if (is.ns4) return this.css.pageY
	else return (this.isChild)? this.parent.getPageY()+this.y : this.y
}
DynLayer.prototype.setPageX = function(x) {
	if (is.ns4) this.css.pageX = x
	if (is.ie) {
		if (this.isChild) this.setX(this.parent.getPageX()-x)
		else this.setX(x)
	}
	this.getX()
	this.invokeEvent('move')
}
DynLayer.prototype.setPageY = function(y) {
	if (is.ns4) this.css.pageY = y
	if (is.ie) {
		if (this.isChild) this.setY(this.parent.getPageY()-y)
		else this.setY(y)
	}
	this.getY()
	this.invokeEvent('move')
}
DynLayer.prototype.setVisible = function(b) {
	this.visible = b
	this.css.visibility=b?"inherit":(is.ns4?"hide":"hidden")
}
DynLayer.prototype.setZIndex = function(z) {
	this.z = z
	this.css.zIndex = z
}
DynLayer.prototype.getZIndex = function() {return this.z}
DynLayer.prototype.setBgImage = function(path) {
	this.bgImage = path
	if (!this.created) return
	if (is.ns4) {
		this.elm.background.src = path
		if (!path) this.setBgColor(this.getColor())
	}
	else this.css.backgroundImage = 'url('+path+')'
}
DynLayer.prototype.setBgColor = function(color) {
	if (color==null) color = null
	this.bgColor = color
	if (is.ns4) this.doc.bgColor = color
	else this.css.backgroundColor = color
}
DynLayer.prototype.getBgColor = function(color) {return this.bgColor}
DynLayer.prototype.setHTML = function(html) {
	if (html==null) html = ''
	if (!this.created) {
		this.html = html
		return
	}
	this.elm.innerHTML = html
	if (!this.created) return
	if (is.ns4) {
		this.doc.open()
		this.doc.write(html)
		this.doc.close()
	}
	else if (is.ns5) {
		while (this.elm.hasChildNodes()) this.elm.removeChild(this.elm.firstChild)
		var r = this.elm.ownerDocument.createRange()
		r.selectNodeContents(this)
		r.collapse(true)
		var df = r.createContextualFragment(str)
		this.elm.appendChild(df)
	}
	if (is.ns4) {for (i in this.doc.images) this.doc.images[i].lyrobj = this}
	else {for (i in this.elm.all.tags("img")) this.elm.all.tags("img")[i].lyrobj = this}
}
DynLayer.prototype.getHTML = function() {return this.created?this.elm.innerHTML:this.innerHTML}
DynLayer.prototype.setSize = function(w,h,noevt) {
	if (w==null) w=this.w
	else this.w=w
	if (h==null) h=this.h
	else this.h=h
	if (!this.created) return
	if (is.ie) {
		this.css.width = (w==null)?0:w
		this.css.height = (h==null)?0:h
	}
	this.setClip([0,w,h,0])
	this.invokeEvent('resize')
}
DynLayer.prototype.setWidth = function(w) {this.setSize(w,null)}
DynLayer.prototype.setHeight = function(h) {this.setSize(null,h)}
DynLayer.prototype.getWidth = function() {return this.w}
DynLayer.prototype.getHeight = function() {return this.h}
DynLayer.prototype.getContentWidth = function() {return !this.created?0:(is.ns?this.doc.width:parseInt(this.elm.scrollWidth))}
DynLayer.prototype.getContentHeight = function() {return !this.created?0:(is.ns?this.doc.height:parseInt(this.elm.scrollHeight))}
DynLayer.prototype.setClip = function(clip) {
	if (!this.created) {
		this.clip = clip
		return
	}
	var cc = this.getClip()
	for (var i in clip) if (clip[i]==null) clip[i]=cc[i]
	var c = this.css.clip
	if (is.ns4) {
		c.top = clip[0]
		c.right = clip[1]
		c.bottom = clip[2]
		c.left = clip[3]
	}
	else if (is.ie) this.css.clip = "rect("+clip[0]+"px "+clip[1]+"px "+clip[2]+"px "+clip[3]+"px)"
}
DynLayer.prototype.getClip = function() {
	var c = this.css.clip
	if (c) {
		if (is.ns4) return [c.top,c.right,c.bottom,c.left]
		else {
			c = c.split("rect(")[1].split(")")[0].split("px")
			for (var i in c) c[i]=parseInt(c[i])
			return [c[0],c[1],c[2],c[3]]
		}
	}
	else return [0,0,0,0]
}
DynLayer.prototype.captureMouseEvents = function() {
	if (!this.eventListeners) this.eventListeners = []
	this.hasEventListeners = true
	if (!this.created) return
	var elm = this.elm
	if (is.ns4) elm.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP | Event.CLICK | Event.DBLCLICK)
	elm.onmousedown = elm.onmouseup = elm.onmouseover = elm.onmouseout = elm.onclick = elm.ondblclick = function(e) {
		if (is.ie) {
			var e = event
			e.cancelBubble = true
			if (e.type!="mouseout" && e.type!="mouseover") e.cancelBubble = true
			if (e.type=="mouseout" && this.contains(e.toElement)) return true
			if (e.type=="mouseover" && this.contains(e.fromElement)) return true
		}
		var realsrc = is.ie ? e.srcElement : e.target
		var src = realsrc.lyrobj
		if (!src) {
			src = DynAPI.getDocument(realsrc)
			if (!src) return true
		}
		var evt = DynAPI.document._e
		evt.setEvent(src,e)
		src.invokeEvent(e.type,evt)
		if (e.type=="mouseover" || e.type=="mouseout") return false
		else return evt.bubbleEvent()
	}
}
DynLayer.prototype.addEventListener = function(listener) {
	if (!this.hasEventListeners) this.captureMouseEvents()
	for (var i in this.eventListeners) {
		if (this.eventListeners[i]==listener) return
	}
	this.eventListeners[this.eventListeners.length] = listener
}
DynLayer.prototype.removeEventListener = function(listener) {
	for (var i=0;i<this.eventListeners.length;i++) {
		if (this.eventListeners[i] == listener) {
			for (var j=i;j<this.eventListeners.length-1;j++) this.eventListeners[j] = this.eventListeners[j+1]
			this.eventListeners.length -= 1
			break
		}
	}
}
DynLayer.prototype.invokeEvent = function(type,e) {
	if (!this.hasEventListeners) return
	if (is.ns4 && is.platform=="other") {
		if (type=="mousedown") {
			if (this.dbltimer!=null) type = "dblclick"
			else this.dbltimer = setTimeout(this+'.dbltimer=null',300)
		}
	}
	for (var i in this.eventListeners) {
		if (e) e.target = this.eventListeners[i].target
		else {
			e = new DynEvent(type,this)
			e.target = this.eventListeners[i].target
		}
		this.eventListeners[i].handleEvent(type,e)
	}
	if (is.ns4 && is.platform=="other" && type=="mouseup") this.invokeEvent("click",e)
	if (this.parentComponent) {
		if (e) e.src = this.parentComponent
		else {
			e = new DynEvent(type,this)
			//e.target = this.eventListeners[i].target
		}
		this.parentComponent.invokeEvent(type,e)
	}
}
DynLayer.prototype.onCreate = new Function()



DynEvent = function(type,src,target) {
	this.type = type
	this.src = src
	this.target = target
}
DynEvent.prototype.getType = function() {return this.type}
DynEvent.prototype.getSource = function() {return this.src}
DynEvent.prototype.getTarget = function() {return this.target}

EventListener = function(target) {this.target = target}
EventListener.prototype.handleEvent = function(type,e) {
	if (this["on"+type]) this["on"+type](e)
}

MouseEvent = function() {}
MouseEvent.prototype.getType = function() {return this.type}
MouseEvent.prototype.getSource = function() {return this.src}
MouseEvent.prototype.getTarget = function() {return this.target}
MouseEvent.prototype.setEvent = function(src,e) {
	this.bubble = true
	this.src = src
	this.type = e.type
	this.pageX = is.ie ? e.x + document.body.scrollLeft : e.pageX - window.pageXOffset
	this.pageY = is.ie ? e.y + document.body.scrollTop : e.pageY - window.pageYOffset
	this.x = is.ie? e.offsetX : e.layerX
	this.y = is.ie? e.offsetY : e.layerY
}
MouseEvent.prototype.bubbleEvent = function() {
	if (!this.bubble || this.src.getClass()==DynDocument) return this.bubble
	this.x = this.x+this.src.getX()
	this.y = this.y+this.src.getY()
	this.src = this.src.getComponent().parent
	this.src.invokeEvent(this.type,this)
	return this.bubbleEvent()
}
MouseEvent.prototype.getX = function() {return this.x}
MouseEvent.prototype.getY = function() {return this.y}
MouseEvent.prototype.getPageX = function() {return this.pageX}
MouseEvent.prototype.getPageY = function() {return this.pageY}
MouseEvent.prototype.setBubble = function(b) {this.bubble=b}



function DynDocument(frame) {
	this.doc = frame.document
	this.elm = frame
	this.all = []
	this.children = []
	this.id = "DynDocument"+DynDocument.count++
	this.obj = this.id+"Object"
	eval(this.obj+"=this")
	this._e = new MouseEvent()
}
DynDocument.count = 0
DynDocument.prototype.isChild = false
DynDocument.prototype.created = true
DynDocument.prototype.toString = function() {return this.obj}
DynDocument.prototype.getClass = DynLayer.prototype.getClass
DynDocument.prototype.getComponent = DynLayer.prototype.getComponent
DynDocument.prototype.addChild = DynLayer.prototype.addChild
DynDocument.prototype.removeChild = DynLayer.prototype.removeChild
DynDocument.prototype.deleteChild = DynLayer.prototype.deleteChild

DynDocument.prototype.recreateAll = function() {
	for (var i in this.children) {
		this.children[i].deleteElement()
	}
	for (var i in this.children) {
		this.children[i].createElement()
	}
}
DynDocument.prototype.getX = DynDocument.prototype.getY = DynDocument.prototype.getPageX = DynDocument.prototype.getPageY = function() {return 0}
DynDocument.prototype.getWidth = function() {if (!this.w) this.findDimensions(); return this.w}
DynDocument.prototype.getHeight = function() {if (!this.h) this.findDimensions(); return this.h}
DynDocument.prototype.findDimensions = function() {
	this.w = (is.ns4)? window.innerWidth : document.body.offsetWidth-20
	this.h = (is.ns4)? window.innerHeight : document.body.offsetHeight-4	
}
DynDocument.prototype.load = function(path) {
	this.doc.href = path
}
DynDocument.prototype.captureMouseEvents = function() {
	if (this.mouseEventsCaptured) return
	this.mouseEventsCaptured = true
	if (!this.eventListeners) this.eventListeners = []
	this.hasEventListeners = true
	if (is.ns4) this.doc.captureEvents(Event.MOUSEMOVE | Event.MOUSEDOWN | Event.MOUSEUP | Event.CLICK | Event.DBLCLICK)
	this.doc.onmousemove = this.doc.onmousedown = this.doc.onmouseup = this.doc.onclick = this.doc.ondblclick = function(e) {
		if (is.ie) {
			var e = event
			e.cancelBubble = true
			if (e.type!="mouseout" && e.type!="mouseover") e.cancelBubble = true
			if (e.type=="mouseout" && this.contains(e.toElement)) return true
			if (e.type=="mouseover" && this.contains(e.fromElement)) return true
		}
		var realsrc = is.ie ? e.srcElement : e.target
		var src = realsrc.lyrobj
		if (!src) {
			src = DynAPI.getDocument(realsrc)
			if (!src) return true
		}
		var evt = DynAPI.document._e
		evt.setEvent(src,e)
		src.invokeEvent(e.type,evt)
		if (is.ie && (e.type=="mouseover" || e.type=="mouseout")) return false
		return evt.bubbleEvent()
	}
}
DynDocument.prototype.addEventListener = DynLayer.prototype.addEventListener
DynDocument.prototype.removeEventListener = DynLayer.prototype.removeEventListener
DynDocument.prototype.invokeEvent = function(type,e) {
	if (!this.hasEventListeners) return
	for (var i in this.eventListeners) {
		if (e) e.target = this.eventListeners[i].target
		else {
			e = new DynEvent(type,this)
			e.target = this.eventListeners[i].target
		}
		this.eventListeners[i].handleEvent(type,e)
	}
}
DynAPI.document = new DynDocument(self)
