/*
* Dynamic Layer ver 2
* written by: Jon M. Toribio, August 18, 1998
* updated by: Jon M. Toribio, November 23, 2000
* filename: dlayer.js
* Requires: Javascript 1.2
*
* update history:
* November 23, 2000 - this script was modified to work with Netscape 6
* August 18, 1998 - created (version 1)
*
* The full version of this script should contain: Core(basic) functionality,
* Clip functionality, Event Handling functionality and Drag functionality.
*/

/* THIS COPY OF THE SCRIPT CONTAINS THE FOLLOWING FUNCTIONALITY: */
/* - Core(basic) functionality  (should not be removed) */
/* - Clip functionality */
/* - Event Handling functionality */
/* - Drag functionality (requires Event Handling functionality) */

document.dlayers = new Array();

function DLayer(id) {
   this.id = id;
   
   if (document.all) {
      this.css = document.all[id].style;
   }
   else if (document.layers) {
      args = DLayer.arguments;
      windowObj = "document"; 
      if (args.length > 1) {
         for( i=1 ; i<args.length ; i++ ) {
            windowObj = "document." + args[i] + "." + windowObj;
         } 
      }
      this.css = eval(windowObj + "." + id);
   }
   else if (document.getElementById) {
      this.css = document.getElementById(id).style;
   }
   this.x = this.getX();
   this.y = this.getY();
   this.z = this.getZ();
   this.w = (document.layers)? this.css.clip.width  : this.css.pixelWidth;
   this.h = (document.layers)? this.css.clip.height : this.css.pixelHeight;
   this.event = (document.all) ? document.all[id] : ((document.layers) ? this.css : document.getElementById(id));
   this.eventObj = new Object();

   document.dlayers[this.id] = this;
}


/***************************************************************************
 * START - Core(basic) functionality  (do not delete section below)
 **************************************************************************/

p = DLayer.prototype;
p.show = DLayer_show;
p.hide = DLayer_hide;
p.isVisible = DLayer_isVisible;
p.inherit = DLayer_inherit;
p.isInherit = DLayer_isInherit;
p.isWithin = DLayer_isWithin;
p.setBGColor = DLayer_setBGColor;
p.setBGImage = DLayer_setBGImage;
p.getX = DLayer_getX;
p.getY = DLayer_getY;
p.getZ = DLayer_getZ;
p.setZ = DLayer_setZ;
p.setZby = DLayer_setZby;
p.moveAbove = DLayer_moveAbove;
p.moveBelow = DLayer_moveBelow;
p.moveTo = DLayer_moveTo;
p.moveBy = DLayer_moveBy;
p.slideTo = DLayer_slideTo;
p.slideBy = DLayer_slideBy;


// Method Definitions
function DLayer_show() {
   this.css.visibility = (document.layers)? "show" : "visible";
}

function DLayer_hide() {
   this.css.visibility = (document.layers)? "hide" : "hidden";
}

function DLayer_isVisible() {
   return (document.layers)? (this.css.visibility == "show") : (this.css.visibility == "visible");
}

function DLayer_inherit() {
   this.css.visibility = "inherit";
}

function DLayer_isInherit() {
   return this.css.visibility == "inherit";
}

function DLayer_isWithin(lower_x, lower_y, upper_x, upper_y) {
   if (lower_x == null) lower_x = Number.NEGATIVE_INFINITY;
   if (lower_y == null) lower_y = Number.NEGATIVE_INFINITY;
   if (upper_x == null) upper_x = Number.POSITIVE_INFINITY;
   if (upper_y == null) upper_y = Number.POSITIVE_INFINITY;
   return ( (this.x > lower_x) && (this.x < upper_x) && (this.y > lower_y) && (this.y < upper_y));
}

function DLayer_setBGColor(color) {
   if (document.layers) this.css.document.bgColor = color;
   else if (document.all || document.getElementById) this.css.backgroundColor = color;
}

function DLayer_setBGImage(img) {
   if (document.layers) this.css.background.src = img;
   else if (document.all || document.getElementById) this.css.backgroundImage = "url('" + img + "')";
}

function DLayer_getX() {
   return  (document.all)? this.css.pixelLeft : parseInt(this.css.left);
}

function DLayer_getY() {
   return  (document.all)? this.css.pixelTop : parseInt(this.css.top);
}

function DLayer_getZ() {
   return this.css.zIndex;
}

function DLayer_setZ(idx) {
   if (idx < 0) idx = 0;
   this.z = idx;
   this.css.zIndex = this.z;
}

function DLayer_setZby(dz) {
   this.setZ(this.z + dz);
}

function DLayer_moveAbove(dlayerObj) {
   this.setZ(dlayerObj.getZ() + 1);
}

function DLayer_moveBelow(dlayerObj) {
   this.setZ(dlayerObj.getZ() - 1);
}

function DLayer_moveTo(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 DLayer_moveBy(x,y) {
   this.moveTo( this.x+x , this.y+y );
}

function DLayer_slide(id,endx,endy,dx,dy,reps,dlay,fn) {
   lyr = document.dlayers[id];
   if (reps-- > 1) {
      lyr.moveBy(dx,dy);
      setTimeout("DLayer_slide('"+id+"',"+endx+","+endy+","+dx+","+dy+","+reps+","+dlay+",'"+fn+"')",dlay);
   }
   else {
      lyr.moveTo(endx,endy);
      lyr.slideactive = false;
      eval(fn);
   }
}

function DLayer_slideTo(endx,endy,forp,dlay,fn) {
   if (this.slideactive) return;
   if (endx == null) endx = this.x;
   if (endy == null) endy = this.y;
   var distx = endx - this.x;
   var disty = endy - this.y;
   byinc = false;
   if (forp.indexOf) {
      if (forp.indexOf("fm") != -1) {
         byinc = false;
         forp = forp.slice(0,forp.indexOf("fm"));
      }
      else if (forp.indexOf("px") != -1) {
         byinc = true;
         forp = forp.slice(0,forp.indexOf("px"));
      }
   }

   if (byinc) {
      var reps = Math.sqrt(Math.pow(distx,2) + Math.pow(disty,2))/forp; 
   }
   else {
      var reps = forp;
   }
   var dx = distx/reps;
   var dy = disty/reps;
   if (!fn) fn = null;
   this.slideactive = true;
   DLayer_slide(this.id,endx,endy,dx,dy,reps,dlay,fn);
}

function DLayer_slideBy(distx,disty,inc,dlay,fn) {
   this.slideTo(this.x + distx, this.y + disty, inc, dlay, fn);
}
/***************************************************************************
 * END - Core(basic) functionality  (do not delete section above)
 **************************************************************************/

/***************************************************************************
 * START - Clip functionality
 **************************************************************************/

/* add functions to prototype */
p.clip = DLayer_clip;
p.clipv = DLayer_clipv;
p.clipTo = DLayer_clipTo;
p.clipBy = DLayer_clipBy;
p.wipeTo = DLayer_wipeTo;
p.wipeBy = DLayer_wipeBy;
p.clyp   = DLayer_clyp;
p.clypTo = DLayer_clypTo;
p.clypBy = DLayer_clypBy;
p.wypeTo = DLayer_wypeTo;
p.wypeBy = DLayer_wypeBy;
 
function DLayer_clip(top,right,bot,left) {
   arg = DLayer_clip.arguments;
   this.clipt = (arg[0])? top   : 0;
   this.clipr = (arg[1])? right : this.w;
   this.clipb = (arg[2])? bot   : this.h;
   this.clipl = (arg[3])? left  : 0;
   if (document.layers) {
      this.css.clip.top    = this.clipt;
      this.css.clip.right  = this.clipr;
      this.css.clip.bottom = this.clipb;
      this.css.clip.left   = this.clipl; 
   }
   if (document.all || document.getElementById) {
      this.css.clip = "rect("+this.clipt+"px "+this.clipr+"px "+this.clipb+"px "+this.clipl+"px)"; 
   }
}

function DLayer_clyp(topx,topy,botx,boty) {
   this.clip(topy,botx,boty,topx);
}

function DLayer_clipv(which) {
   if (document.all || document.getElementById) var clipval = this.css.clip.split("rect(")[1].split(")")[0].split("px")
   if (which == 't') return (document.layers)? this.css.clip.top : Number(clipval[0]);
   if (which == 'r') return (document.layers)? this.css.clip.right : Number(clipval[1]);
   if (which == 'b') return (document.layers)? this.css.clip.bottom : Number(clipval[2]);
   if (which == 'l') return (document.layers)? this.css.clip.left : Number(clipval[3]);
}

function DLayer_clipTo(top,right,bot,left) {
   t = (top != null)?   top   : this.clipt;
   r = (right != null)? right : this.clipr;
   b = (bot != null)?   bot   : this.clipb;
   l = (left != null)?  left  : this.clipl;
   this.clip(t,r,b,l);
} 

function DLayer_clipBy(top,right,bot,left) {
   this.clipTo(this.clipt+top , this.clipr+right , this.clipb+bot , this.clipl+left);
}

function DLayer_wipe(id,top,right,bot,left,dt,dr,db,dl,reps,dlay,fn) {
   lyr = document.dlayers[id]; 
   if (reps-- > 1) {
      lyr.clipBy(dt,dr,db,dl);
      setTimeout("DLayer_wipe('"+id+"',"+top+","+right+","+bot+","+left+","+dt+","+dr+","+db+","+dl+","+reps+","+dlay+",'"+fn+"')",dlay);  
   }
   else {
      lyr.clipTo(top,right,bot,left);
      lyr.wipeActive = false;
      eval(fn);
   }
}

function DLayer_wipeTo(top,right,bot,left,reps,dlay,fn) {
   if (this.wipeActive) return;
   if (top == null)   top = this.clipv('t');
   if (right == null) right = this.clipv('r');
   if (bot == null)   bot = this.clipv('b');
   if (left == null)  left = this.clipv('l');
   dt = (top - this.clipv('t'))/reps;
   dr = (right - this.clipv('r'))/reps;
   db = (bot - this.clipv('b'))/reps; 
   dl = (left - this.clipv('l'))/reps;
   this.wipeActive = true;
   DLayer_wipe(this.id,top,right,bot,left,dt,dr,db,dl,reps,dlay,fn);
}

function DLayer_wipeBy(distt,distr,distb,distl,reps,dlay,fn) {
   this.wipeTo(this.clipv('t')+distt, this.clipv('r')+distr, this.clipv('b')+distb, this.clipv('l')+distl,reps,dlay,fn);
}

function DLayer_clyp(topx,topy,botx,boty) {
   this.clip(topy,botx,boty,topx);
}

function DLayer_clypTo(topx,topy,botx,boty) {
   this.clipTo(topy,botx,boty,topx);
}

function DLayer_clypBy(topx,topy,botx,boty) {
   this.clipBy(topy,botx,boty,topx);
}

function DLayer_wypeTo(topx,topy,botx,boty,reps,dlay,fn) {
   this.wipeTo(topy,botx,boty,topx,reps,dlay,fn);
}

function DLayer_wypeBy(topx,topy,botx,boty,reps,dlay,fn) {
   this.wipeBy(topy,botx,boty,topx,reps,dlay,fn);
}
/***************************************************************************
 * END - Clip functionality
 **************************************************************************/

/***************************************************************************
 * START - Event Handling functionality
 **************************************************************************/

/* add functions to prototype */
p.addEventHandler = DLayer_addEventHandler;
p.removeEventHandler = DLayer_removeEventHandler;

function DLayer_initEventMasks() {
   if (DLayer._eventmasks) return;
   else {
DLayer._eventmasks = {
   onabort:Event.ABORT,
   onblur:Event.BLUR,
   onchange:Event.CHANGE,
   onclick:Event.CLICK,
   ondblclick:Event.DBLCLICK,
   ondragdrop:Event.DRAGDROP,
   onerror:Event.ERROR,
   onfocus:Event.FOCUS,
   onkeydown:Event.KEYDOWN,
   onkeypress:Event.KEYPRESS,
   onkeyup:Event.KEYUP,
   onload:Event.LOAD,
   onmousedown:Event.MOUSEDOWN,
   onmousemove:Event.MOUSEMOVE,
   onmouseout:Event.MOUSEOUT,
   onmouseover:Event.MOUSEOVER,
   onmouseup:Event.MOUSEUP,
   onmove:Event.MOVE,
   onreset:Event.RESET,
   onresize:Event.RESIZE,
   onselect:Event.SELECT,
   onsubmit:Event.SUBMIT,
   onunload:Event.UNLOAD
};
   }
}

function DLayer_addEventHandler(eventname, handler) {
   if (document.layers) {
      DLayer_initEventMasks();
      this.event.captureEvents(DLayer._eventmasks[eventname]);
   }
   var thislayer = this;  
   this.event[eventname] = function(e) {
      if (document.all) {
         var e = window.event;
         e.cancelBubble = true;
      }
      var eventObj = new Object();
      eventObj.dlayer = thislayer;
      eventObj.type   = e.type;
      eventObj.x      = (document.layers)? e.pageX : e.clientX;
      eventObj.y      = (document.layers)? e.pageY : e.clientY;
      eventObj.button = (document.layers)? e.which : e.button;
      eventObj.key    = (document.layers)? e.which : e.keyCode;
      eventObj.shift  = (document.layers)? ((e.modifiers & Event.SHIFT_MASK) != 0) : e.shiftKey;
      eventObj.ctrl   = (document.layers)? ((e.modifiers & Event.CONTROL_MASK) != 0) : e.ctrlKey;
      eventObj.alt    = (document.layers)? ((e.modifiers & Event.ALT_MASK) != 0) : e.altKey;
      return handler(eventObj);
      
   }   
}

function DLayer_removeEventHandler(eventname) {
   if (document.layers) {
      DLayer_initEventMasks();
      this.event.releaseEvents(DLayer._eventmasks[eventname]);
   }
   this.event[eventname] = null;   
}
/***************************************************************************
 * END - Event Handling functionality
 **************************************************************************/

/***************************************************************************
 * START - Drag functionality  (requires Event Handling functionality)
 **************************************************************************/

/* global variables */
document.draglayers = new Array();
var draglayers_hi_z = 0;
var draglayers_lo_z = Number.MAX_VALUE;
var draglayers_zbuffer = 3;
var draglayers_front_z;

/* add functions to the prototype */
p.drag = DLayer_drag;
p.undrag = DLayer_undrag;

function DLayer_drag(noshift) {
   if (!(this.dragable)) {
      this.addEventHandler("onmousedown",DLayer_dragMousedown);
      this.addEventHandler("onmousemove",DLayer_dragMousemove);
      this.addEventHandler("onmouseup",DLayer_dragMouseup);
      document.draglayers[this.id] = new Object();
      document.draglayers[this.id].ref = this;
      document.draglayers[this.id].orig_z = this.z;
      this.dragable = true;
   }
   if (noshift == "noshift") this.noshift = true;
   else this.noshift = false;

   DLayer_setDragZLimits();
}

function DLayer_undrag() {
   if (this.dragable) {
      this.removeEventHandler("onmousedown");
      this.removeEventHandler("onmousemove");
      this.removeEventHandler("onmouseup");
      this.setZ(document.draglayers[this.id].orig_z);
      delete document.draglayers[this.id];
      delete this.noshift;
      this.dragable = false;
      DLayer_setDragZLimits();
   }
}

function DLayer_dragMousedown(eventObj) {
   var dlayer = eventObj.dlayer;
   dlayer.offsetX = eventObj.x - dlayer.getX();
   dlayer.offsetY = eventObj.y - dlayer.getY();
   dlayer.slideactive = false;
   dlayer.dragActive = true;
   if (!(dlayer.noshift)) {
      DLayer_shiftDragLayers(dlayer.getZ());
      dlayer.setZ(draglayers_front_z);
   }
   drag_onmousedown(eventObj);
}

function DLayer_dragMousemove(eventObj) {
   var dlayer = eventObj.dlayer;
   if (dlayer.dragActive) {
      dlayer.moveTo(eventObj.x - dlayer.offsetX,eventObj.y - dlayer.offsetY);
      drag_onmousemove(eventObj);
   }
}

function DLayer_dragMouseup(eventObj) {
   var dlayer = eventObj.dlayer;
   dlayer.dragActive = false;
   drag_onmouseup(eventObj);
}

function DLayer_setDragZLimits() {
   var lst = document.draglayers;
   draglayers_hi_z = 0;
   draglayers_lo_z = Number.MAX_VALUE;
   for (i in lst) {
      if (!(lst[i].ref.noshift)) {
         if (lst[i].orig_z > draglayers_hi_z) {
            draglayers_hi_z = lst[i].orig_z;
            draglayers_front_z = parseInt(draglayers_hi_z) + parseInt(draglayers_zbuffer);
         }
         if (lst[i].orig_z < draglayers_lo_z) draglayers_lo_z = lst[i].orig_z;
      }
   }
}

function DLayer_shiftDragLayers(tgt_z) {
   var lst = document.draglayers;
   for (i in lst) {
      if ( !(lst[i].ref.noshift) && (lst[i].ref.getZ() > draglayers_lo_z) && (lst[i].ref.getZ() >= tgt_z)) lst[i].ref.setZby(-1);
   } 
}

/* pre-define the supplemental drag functions */
function drag_onmousedown(eventObj) { }
function drag_onmousemove(eventObj) { }
function drag_onmouseup(eventObj) { }

/***************************************************************************
 * END - Drag functionality
 **************************************************************************/

