// Dynamic Layer Object
// Copyright 1998 Dan Steinman
// Available at the Dynamic Duo (http://www.dansteinman.com/dynduo/)
// Last Updated Jan 12, 1998.
// In order to use this code you must keep this disclaimer

function DynLayer(id,nestref,iframe) {
        if (!iframe && !DynLayerTest(id,nestref)) return
        if (ns4) {
                this.css = (nestref)? eval("document."+nestref+".document."+id) : document.layers[id]
                this.x = this.css.left
                this.y = this.css.top
        }
        else if (ie4) {
                this.css = (iframe)? parent.frames[iframe].document.all[id].style : document.all[id].style
                this.x = this.css.pixelLeft
                this.y = this.css.pixelTop
        }
        this.id = id
        this.nestref = nestref
        this.w = (ns4)? this.css.clip.width : this.css.pixelWidth
        this.h = (ns4)? this.css.clip.height : this.css.pixelHeight
        this.doc = (ns4)? this.css.document : document
        if (ns4) this.event = this.css
        if (ie4) this.event = (iframe)? parent.frames[iframe].document.all[id] : document.all[id]
        this.obj = id + "DynLayer"
        eval(this.obj + "=this")
        this.moveTo = DynLayerMoveTo
        this.moveBy = DynLayerMoveBy
        this.show = DynLayerShow
        this.hide = DynLayerHide
        this.slideInit = DynLayerSlideInit
        this.clipInit = DynLayerClipInit
        this.write = DynLayerWrite
}
function DynLayerMoveTo(x,y) {
        if (x!=null) {
                this.x = x
                this.css.left = this.x
        }
        if (y!=null) {
                this.y = y
                this.css.top = this.y
        }
}
function DynLayerMoveBy(x,y) {
        this.moveTo(this.x+x,this.y+y)
}
function DynLayerShow() {
        this.css.visibility = (ns4)? "show" : "visible"
}
function DynLayerHide() {
        this.css.visibility = (ns4)? "hide" : "hidden"
}
function DynLayerSlideInit() {
        this.slideTo = DynLayerSlideTo
        this.slideBy = DynLayerSlideBy
        this.slideStart = DynLayerSlideStart
        this.slide = DynLayerSlide
        this.onSlide = new Function("")
        this.onSlideEnd = new Function("")
}
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)
                eval(fn)
                this.onSlideEnd()
        }
}
function DynLayerClipInit(clipTop,clipRight,clipBottom,clipLeft) {
        this.clipTo = DynLayerClipTo
        this.clipBy = DynLayerClipBy
        this.clipValues = DynLayerClipValues
        if (ie4) {
                if (arguments.length==4) this.clipTo(clipTop,clipRight,clipBottom,clipLeft)
                else this.clipTo(0,this.w,this.h,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 (ns4) {
                this.css.clip.top = t
                this.css.clip.right = r
                this.css.clip.bottom = b
                this.css.clip.left = l
        }
        else if (ie4) 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 (ie4) var clipv = this.css.clip.split("rect(")[1].split(")")[0].split("px")
        if (which=="t") return (ns4)? this.css.clip.top : Number(clipv[0])
        if (which=="r") return (ns4)? this.css.clip.right : Number(clipv[1])
        if (which=="b") return (ns4)? this.css.clip.bottom : Number(clipv[2])
        if (which=="l") return (ns4)? this.css.clip.left : Number(clipv[3])
}
function DynLayerWrite(html) {
        if (ns4) {
                this.doc.open()
                this.doc.write(html)
                this.doc.close()
        }
        else if (ie4) {
                this.event.innerHTML = html
        }
}
function DynLayerTest(id,nestref) {
        if (!DynLayerTest.count) DynLayerTest.count = 0
        var ref = new Array()
        if (nestref) ref = nestref.split('.document.')
        ref[ref.length] = id
        var refstr = (ns4)? 'document.'+ref[0] : 'document.all.'+ref[0]
        for (var i=1; i<=ref.length; i++) {
                if (eval(refstr)) {
                        if (ref.length==i) return true
                        else refstr += (ns4)? '.document.'+ref[i] : '.all.'+ref[i]
                }
                else {
                        var str ='DynLayer Error:'
                        for (j in ref) {
                                str += '\n'+ref[j]
                                if (j==i-1) str += '  <-- this layer cannot be found'
                        }
                        if (DynLayerTest.count++<5) alert(str)
                        return false
                }
        }
        return false
}
function DynLayerInit(nestref) {
        if (!DynLayerInit.refArray) {
                DynLayerInit.refArray = new Array()
                DynLayerInit.refArray.i = 0
        }
        if (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
                        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) {
                                DynLayerInit.refArray[DynLayerInit.refArray.length] = (nestref=='')? ref.layers[i].name : nestref+'.document.'+ref.layers[i].name
                        }
                }
                if (DynLayerInit.refArray.i < DynLayerInit.refArray.length) {
                        DynLayerInit(DynLayerInit.refArray[DynLayerInit.refArray.i++])
                }
        }
        else if (ie4) {
                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
}
function css(id,left,top,width,height,color,vis,z) {
        if (id=="START") return '<STYLE TYPE="text/css">\n'
        else if (id=="END") return '</STYLE>'
        var str = (left!=null && top!=null)? '#'+id+' {position:absolute; left:'+left+'; top:'+top+'; ' : '#'+id+' {position:relative; '
        if (width!=null) str += 'width:'+width+'; '
        if (height!=null) str += 'height:'+height+'; clip:rect(0,'+width+','+height+',0); '
        if (color!=null) str += 'background-color:'+color+'; layer-background-color:'+color+'; '
        if (vis!=null) str += 'visibility: '+vis+'; '
        if (z!=null) str += 'z-index: '+z+'; '
        str += '}\n'
        return str
}
