home *** CD-ROM | disk | FTP | other *** search
/ Macmillan Math: Grade 1 …Pupil's Edition (Florida) / Math Grade 1 - Pupil's Edition (Florida).iso / pc / corsair / generic / javascript / dynlayer.js < prev    next >
Encoding:
JavaScript  |  2003-10-21  |  7.7 KB  |  246 lines

  1. // Dynamic Layer Object
  2. // sophisticated layer/element targeting and animation object which provides the core functionality needed in most DHTML applications
  3. // 19990604
  4.  
  5. // Copyright (C) 1999 Dan Steinman
  6. // Distributed under the terms of the GNU Library General Public License
  7. // Available at http://www.dansteinman.com/dynapi/
  8.  
  9. function DynLayer(id) {
  10.     // Assign properties
  11.     if (is.ns) {
  12.         if (is.ns4) {
  13.             this.css = document.layers[id]
  14.             this.elm = this.event = this.css
  15.             this.doc = this.css.document
  16.             this.x = this.css.left
  17.             this.y = this.css.top
  18.             this.w = this.css.clip.width
  19.             this.h = this.css.clip.height
  20.         } else if (is.ns5) {
  21.             this.elm = document.getElementById(id)
  22.             this.css = this.elm.style
  23.             this.doc = document
  24.             this.x = this.elm.offsetLeft
  25.             this.y = this.elm.offsetTop
  26.             this.w = this.elm.offsetWidth
  27.             this.h = this.elm.offsetHeight
  28.         } // End Netscape Specific IF
  29.     } else if (is.ie) {
  30.         this.elm = document.all[id]
  31.         this.css = document.all[id].style
  32.         this.doc = document
  33.         this.x = this.elm.offsetLeft
  34.         this.y = this.elm.offsetTop
  35.         this.w = (is.ie4)? this.css.pixelWidth : this.elm.offsetWidth
  36.         this.h = (is.ie4)? this.css.pixelHeight : this.elm.offsetHeight
  37.     } // End Browser IF    
  38.     
  39.     this.id = id
  40.     this.obj = id + "DynLayer"
  41.     eval(this.obj + "=this")
  42. } // End dynItem
  43.  
  44. function DynLayerMoveTo(x,y) {
  45.     if (x!=null) {
  46.         this.x = x
  47.         if (is.ns) this.css.left = this.x
  48.         else this.css.pixelLeft = this.x
  49.     }
  50.     if (y!=null) {
  51.         this.y = y
  52.         if (is.ns) this.css.top = this.y
  53.         else this.css.pixelTop = this.y
  54.     }
  55. }
  56. function DynLayerMoveBy(x,y) {
  57.     this.moveTo(this.x+x,this.y+y)
  58. }
  59. function DynLayerShow() {
  60.     this.css.visibility = (is.ns4)? "show" : "visible"
  61. }
  62. function DynLayerHide() {
  63.     this.css.visibility = (is.ns4)? "hide" : "hidden"
  64. }
  65. DynLayer.prototype.moveTo = DynLayerMoveTo
  66. DynLayer.prototype.moveBy = DynLayerMoveBy
  67. DynLayer.prototype.show = DynLayerShow
  68. DynLayer.prototype.hide = DynLayerHide
  69. DynLayerTest = new Function('return true')
  70.  
  71. // DynLayerInit Function
  72. function DynLayerInit(nestref) {
  73.     if (!DynLayer.set) DynLayer.set = true
  74.     if (is.ns) {
  75.         if (nestref) ref = eval('document.'+nestref+'.document')
  76.         else {nestref = ''; ref = document;}
  77.         for (var i=0; i<ref.layers.length; i++) {
  78.             var divname = ref.layers[i].name
  79.             DynLayer.nestRefArray[divname] = nestref
  80.             var index = divname.indexOf("Div")
  81.             if (index > 0) {
  82.                 eval(divname.substr(0,index)+' = new DynLayer("'+divname+'","'+nestref+'")')
  83.             }
  84.             if (ref.layers[i].document.layers.length > 0) {
  85.                 DynLayer.refArray[DynLayer.refArray.length] = (nestref=='')? ref.layers[i].name : nestref+'.document.'+ref.layers[i].name
  86.             }
  87.         }
  88.         if (DynLayer.refArray.i < DynLayer.refArray.length) {
  89.             DynLayerInit(DynLayer.refArray[DynLayer.refArray.i++])
  90.         }
  91.     }
  92.     else if (is.ie) {
  93.         for (var i=0; i<document.all.tags("DIV").length; i++) {
  94.             var divname = document.all.tags("DIV")[i].id
  95.             var index = divname.indexOf("Div")
  96.             if (index > 0) {
  97.                 eval(divname.substr(0,index)+' = new DynLayer("'+divname+'")')
  98.             }
  99.         }
  100.     }
  101.     return true
  102. }
  103. DynLayer.nestRefArray = new Array()
  104. DynLayer.refArray = new Array()
  105. DynLayer.refArray.i = 0
  106. DynLayer.set = false
  107.  
  108. // Slide Methods
  109. function DynLayerSlideTo(endx,endy,inc,speed,fn) {
  110.     if (endx==null) endx = this.x
  111.     if (endy==null) endy = this.y
  112.     var distx = endx-this.x
  113.     var disty = endy-this.y
  114.     this.slideStart(endx,endy,distx,disty,inc,speed,fn)
  115. }
  116. function DynLayerSlideBy(distx,disty,inc,speed,fn) {
  117.     var endx = this.x + distx
  118.     var endy = this.y + disty
  119.     this.slideStart(endx,endy,distx,disty,inc,speed,fn)
  120. }
  121. function DynLayerSlideStart(endx,endy,distx,disty,inc,speed,fn) {
  122.     if (this.slideActive) return
  123.     if (!inc) inc = 10
  124.     if (!speed) speed = 20
  125.     var num = Math.sqrt(Math.pow(distx,2) + Math.pow(disty,2))/inc
  126.     if (num==0) return
  127.     var dx = distx/num
  128.     var dy = disty/num
  129.     if (!fn) fn = null
  130.     this.slideActive = true
  131.     this.slide(dx,dy,endx,endy,num,1,speed,fn)
  132. }
  133. function DynLayerSlide(dx,dy,endx,endy,num,i,speed,fn) {
  134.     if (!this.slideActive) return
  135.     if (i++ < num) {
  136.         this.moveBy(dx,dy)
  137.         this.onSlide()
  138.         if (this.slideActive) setTimeout(this.obj+".slide("+dx+","+dy+","+endx+","+endy+","+num+","+i+","+speed+",\""+fn+"\")",speed)
  139.         else this.onSlideEnd()
  140.     }
  141.     else {
  142.         this.slideActive = false
  143.         this.moveTo(endx,endy)
  144.         this.onSlide()
  145.         this.onSlideEnd()
  146.         eval(fn)
  147.     }
  148. }
  149. function DynLayerSlideInit() {}
  150. DynLayer.prototype.slideInit = DynLayerSlideInit
  151. DynLayer.prototype.slideTo = DynLayerSlideTo
  152. DynLayer.prototype.slideBy = DynLayerSlideBy
  153. DynLayer.prototype.slideStart = DynLayerSlideStart
  154. DynLayer.prototype.slide = DynLayerSlide
  155. DynLayer.prototype.onSlide = new Function()
  156. DynLayer.prototype.onSlideEnd = new Function()
  157.  
  158. // Clip Methods
  159. function DynLayerClipInit(clipTop,clipRight,clipBottom,clipLeft) {
  160.     if (is.ie) {
  161.         if (arguments.length==4) this.clipTo(clipTop,clipRight,clipBottom,clipLeft)
  162.         else if (is.ie4) this.clipTo(0,this.css.pixelWidth,this.css.pixelHeight,0)
  163.     }
  164. }
  165. function DynLayerClipTo(t,r,b,l) {
  166.     if (t==null) t = this.clipValues('t')
  167.     if (r==null) r = this.clipValues('r')
  168.     if (b==null) b = this.clipValues('b')
  169.     if (l==null) l = this.clipValues('l')
  170.     if (is.ns) {
  171.         this.css.clip.top = t
  172.         this.css.clip.right = r
  173.         this.css.clip.bottom = b
  174.         this.css.clip.left = l
  175.     }
  176.     else if (is.ie) this.css.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)"
  177. }
  178. function DynLayerClipBy(t,r,b,l) {
  179.     this.clipTo(this.clipValues('t')+t,this.clipValues('r')+r,this.clipValues('b')+b,this.clipValues('l')+l)
  180. }
  181. function DynLayerClipValues(which) {
  182.     if (is.ie) var clipv = this.css.clip.split("rect(")[1].split(")")[0].split("px")
  183.     if (which=="t") return (is.ns)? this.css.clip.top : Number(clipv[0])
  184.     if (which=="r") return (is.ns)? this.css.clip.right : Number(clipv[1])
  185.     if (which=="b") return (is.ns)? this.css.clip.bottom : Number(clipv[2])
  186.     if (which=="l") return (is.ns)? this.css.clip.left : Number(clipv[3])
  187. }
  188. DynLayer.prototype.clipInit = DynLayerClipInit
  189. DynLayer.prototype.clipTo = DynLayerClipTo
  190. DynLayer.prototype.clipBy = DynLayerClipBy
  191. DynLayer.prototype.clipValues = DynLayerClipValues
  192.  
  193. // Write Method
  194. function DynLayerWrite(html) {
  195.     if (is.ns) {
  196.         this.doc.open()
  197.         this.doc.write(html)
  198.         this.doc.close()
  199.     }
  200.     else if (is.ie) {
  201.         this.event.innerHTML = html
  202.     }
  203. }
  204. DynLayer.prototype.write = DynLayerWrite
  205.  
  206. // BrowserCheck Object
  207. function BrowserCheck() {
  208.     var b = navigator.appName
  209.     if (b=="Netscape") this.b = "ns"
  210.     else if (b=="Microsoft Internet Explorer") this.b = "ie"
  211.     else this.b = b
  212.     this.version = navigator.appVersion
  213.     this.v = parseInt(this.version)
  214.     this.ns = (this.b=="ns" && this.v>=4)
  215.     this.ns4 = (this.b=="ns" && this.v==4)
  216.     this.ns5 = (this.b=="ns" && this.v==5)
  217.     this.ie = (this.b=="ie" && this.v>=4)
  218.     this.ie4 = (this.version.indexOf('MSIE 4')>0)
  219.     this.ie5 = (this.version.indexOf('MSIE 5')>0)
  220.     this.min = (this.ns||this.ie)
  221. }
  222. is = new BrowserCheck()
  223.  
  224. // CSS Function
  225. function css(id,left,top,width,height,color,vis,z,other) {
  226.     if (id=="START") return '<STYLE TYPE="text/css">\n'
  227.     else if (id=="END") return '</STYLE>'
  228.     var str = (left!=null && top!=null)? '#'+id+' {position:absolute; left:'+left+'px; top:'+top+'px;' : '#'+id+' {position:relative;'
  229.     if (arguments.length>=4 && width!=null) str += ' width:'+width+'px;'
  230.     if (arguments.length>=5 && height!=null) {
  231.         str += ' height:'+height+'px;'
  232.         if (arguments.length<9 || other.indexOf('clip')==-1) str += ' clip:rect(0px '+width+'px '+height+'px 0px);'
  233.     }
  234.     if (arguments.length>=6 && color!=null) str += (is.ns)? ' layer-background-color:'+color+';' : ' background-color:'+color+';'
  235.     if (arguments.length>=7 && vis!=null) str += ' visibility:'+vis+';'
  236.     if (arguments.length>=8 && z!=null) str += ' z-index:'+z+';'
  237.     if (arguments.length==9 && other!=null) str += ' '+other
  238.     str += '}\n'
  239.     return str
  240. }
  241. function writeCSS(str,showAlert) {
  242.     str = css('START')+str+css('END')
  243.     document.write(str)
  244.     if (showAlert) alert(str)
  245. }
  246.