// layerobj.js

var IS_INVIS = 0;
var IS_VIS = 1;
var IS_SLIDE = 2;
var IS_SCROLL = 3;
var IS_APPEAR = 4;
var IS_WIPE = 5;

var layerCount = 0;
var layerArray = new Array();

function layerById(id)
{
  var i;
  for(i=1;i<=layerCount;i++)
    if(layerArray[i].id==id)
      return layerArray[i];
}

function updateLayers()
{
  var i;
  getMarginWidth();
  for(i=1;i<=layerCount;i++)
  {
    l=layerArray[i];
    // special case for scrolling layer
    if (l.mode == IS_SCROLL || l.mode == IS_WIPE) l.moveToX(l.xf);
    else l.moveTo(l.xf,l.yf);
  }
}

function onResizeLayers()
{
  onResize();
  if (is.IE) updateLayers();
}

function startLayerDef(bw)
{
  setBodyWidth(bw);
  getMarginWidth();
}

function endLayerDef()
{
  var i;
  
  for(i=1;i<=layerCount;i++) {
    l=layerArray[i];
    
    // scrolling layer
    if (l.mode == IS_SCROLL) {
      l.scrollOne(-l.hf);
      l.scroller(l.scrollstep,l.timeout);
      l.show();
    }
    // sliding layers
    else if (l.mode == IS_SLIDE) {
      l.moveTo(-marginWidth-70,l.yf);
      l.clipTo(0,l.wf,l.hf,0);
      l.show();
      l.slide(l.xf,l.yf,l.xs,l.ys,l.timeout);
    }
    // wiping layers
    else if (l.mode == IS_WIPE) {
      l.scrollOne(-l.hf);
      l.wiper(l.scrollstep,l.timeout);
      l.show();
    }
    // appearing layers (0 = hidden, 1 = visible)
    else if (l.mode == IS_APPEAR) {
      l.moveTo(l.xf,l.yf);
      l.clipTo(0,l.wf,l.hf,0);
      l.show();
      l.setOpacity(l.opacity);
    } 
    // static layers (0 = hidden, 1 = visible)
    else {
      l.moveTo(l.xf,l.yf);
      l.clipTo(0,l.wf,l.hf,0);
      if (l.mode == IS_INVIS) 
	l.hide();
      else if (l.timeafter > 0)
	setTimeout("layerArray["+l.count+"].onTimer()",l.timeafter);
      else {
	l.show();
	l.launchAfter();
      }
    }
  }
}

// LAYER OBJECT METHODS

function layerObjSlideTo(x,y)
{
  this.x = x;
  if (this.usemargin) this.css.left = marginWidth + this.x;
  else this.css.left = this.x;
  this.y = y;
  this.css.top = this.y;
}

function layerObjSlideToX(x)
{
  this.x = x;
  if (this.usemargin) this.css.left = marginWidth + this.x;
  else this.css.left = this.x;
}

function layerObjMoveTo(x,y)
{
  this.x = x;
  if (this.usemargin) this.css.left = marginWidth + this.x;
  else this.css.left = this.x;
  this.y = y;
  this.css.top = this.y;
}

function layerObjMoveToX(x)
{
  this.x = x;
  if (this.usemargin) this.css.left = marginWidth + this.x;
  else this.css.left = this.x;
}

function layerObjClipTo(t,r,b,l)
{
  if (is.IE || is.NS6)
    this.css.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)";
  else if (is.NS)
  {
    this.css.clip.top = t;
    this.css.clip.right = r;
    this.css.clip.bottom = b;
    this.css.clip.left = l;
  }
}

function layerObjGetClip(which)
{
  if (is.IE || is.NS6)
  {
    var clipv = this.css.clip.split("rect(")[1].split(")")[0].split("px")
    if (which=="t") return Number(clipv[0])
    if (which=="r") return Number(clipv[1])
    if (which=="b") return Number(clipv[2])
    if (which=="l") return Number(clipv[3])
  }
  else if (is.NS)
  {
    if (which=="t") return this.css.clip.top
    if (which=="r") return this.css.clip.right
    if (which=="b") return this.css.clip.bottom
    if (which=="l") return this.css.clip.left
  } 
}

// obsolete wipeto, prefer using ontimer()
function layerObjWipeTo(t,r,b,l,step,timeout)
{
  var doit = 0;  
  t0 = this.getClip("t");
  r0 = this.getClip("r");
  b0 = this.getClip("b");
  l0 = this.getClip("l");
  if (t0 + step < t) {t0 += step; doit = 1;} else if (t0 - step > t) {t0 -= step; doit = 1;} else t0 = t;
  if (r0 + step < r) {r0 += step; doit = 1;} else if (r0 - step > r) {r0 -= step; doit = 1;} else r0 = r;
  if (b0 + step < b) {b0 += step; doit = 1;} else if (b0 - step > b) {b0 -= step; doit = 1;} else b0 = b;
  if (l0 + step < l) {l0 += step; doit = 1;} else if (l0 - step > l) {l0 -= step; doit = 1;} else l0 = l;
  if (doit)    
    setTimeout("layerArray["+this.count+"].wipeTo("+t+","+r+","+b+","+l+","+step+","+timeout+")",timeout);
}

function layerObjSlide(xf,yf,xs,ys,t)
{
  this.xf = xf;
  this.yf = yf;
  this.xs = xs;
  this.ys = ys;
  this.timeout = t;
  if (xs != 0 || ys != 0)
    this.onTimer();
}

function layerObjSlideX(xf,xs,t)
{
  this.slide(xf,this.y,xs,0,t);
}

function layerObjSetOpacityTo(opacity)
{
  this.css.filter = "alpha(opacity=" + opacity + ")";
  this.css.MozOpacity = opacity / 100;
  this.opacity = opacity;
  this.appear = 1;
}

function layerObjSetOpacity(opacity)
{
  this.setOpacityTo(opacity);
  this.onTimer();
}

function layerObjOnTimer()
{
  done = 0;

  if (this.mode != IS_VIS && this.mode != IS_INVIS) {
    if (this.run == 0)
      return;
    
    if (this.pause == 1) {
      setTimeout("layerArray["+this.count+"].onTimer()",this.timeout);
      return;
    }
  }
  
  x = this.x;
  y = this.y;

  if (this.sliding == 1) {
    if (x + this.xs < this.xf) x += this.xs; else if (x - this.xs > this.xf) x -= this.xs; else x = this.xf;
    if (y + this.ys < this.yf) y += this.ys; else if (y - this.ys > this.yf) y -= this.ys; else y = this.yf;
    this.slideTo(x,y);
    if (x == this.xf && y == this.yf) {
      this.sliding = 0;
      done = 1;
    }
  }
  else if (this.scrolling == 1 || this.wiping == 1) {
    var maxhidden;
    this.getContentWH();
    if (this.mode == IS_SCROLL)
      maxhidden = this.contentHeight;
    else
      maxhidden = this.contentHeight - this.hf;
    if (maxhidden < 0) {
      this.scrollOne(0);
      this.scrolling = 0;
      this.wiping = 0;
      done = 1;
    }
    else {
      cliptop = this.yf - this.y + this.ys;
      if (this.mode == IS_SCROLL) {
	if (cliptop < -this.hf)
          cliptop = maxhidden;
        if (cliptop > maxhidden)
          cliptop = -this.hf;
      }
      else if (this.mode == IS_WIPE) {
	if (cliptop < 0 && this.ys < 0) {
          cliptop = 0;
	  this.wiping = 0;
	  done = 1;
	}
        if (cliptop > maxhidden && this.ys > 0) {
          cliptop = maxhidden;
	  this.wiping = 0;
	  done = 1;
	}      
      }
      else {
        if (cliptop < 0) {
          cliptop = 0;
	}
        if (cliptop > maxhidden) {
          cliptop = maxhidden;
	}
      }
      this.scrollOne(cliptop);
    }
  }
  else if (this.appear == 1) {
    opacity = this.opacity;
    if (opacity < 100) opacity += this.scrollstep;
    this.setOpacityTo(opacity);
    if (opacity >= 100) {
      this.appear = 0;
      done = 1;
    }
  }
  else {
    this.show();
    done = 1;
  }
  
  if (done)
    this.launchAfter();
  else
    setTimeout("layerArray["+this.count+"].onTimer()",this.timeout);
}

function layerObjLaunchAfter ()
{
  for (i=1;i<=layerCount;i++) {
    if (layerArray[i].startafter == this.id) {
      layerArray[i].run = 1;
      setTimeout("layerArray["+ i +"].onTimer()",layerArray[i].timeafter);
    }
  }
}

function layerObjScroller(move,t)
{
  this.scrolling = 1;
  this.timeout = t;
  this.ys = move;
  if (move != 0)
    this.onTimer();
  else
    this.scrolling = 0;
}

function layerObjWiper(move,t)
{
  this.timeout = t;
  this.ys = move;
  if (move != 0)
    this.onTimer();
  else
    this.wiping = 0;
}

function layerObjScrollOne(cliptop)
{
  this.clipTo(cliptop,this.wf,cliptop+this.hf,0);
  this.moveTo(this.x,this.yf - cliptop);
}

function layerObjLoadSource(url,frame)
{
  if (is.DOM)
    document.getElementById(frame).src = url;
  else if (is.NS)
    this.css.load(url,this.wf);
  else if (is.IE)
    eval("document."+frame+".document.location = url");
}

function showSource(id,frame)
{
  if (is.DOM)
    document.getElementById(id).innerHTML = window.frames[frame].document.getElementById('theBody').innerHTML;
  else if (is.IE)
    eval("document.all[id].innerHTML = parent."+frame+".document.body.innerHTML;");
}

function layerObjShow() 
{
  this.css.visibility = vis; 
  if ((is2.ie && !is_mac) && this.iframeid != "") {
    this.getIframeParams();
    fs = document.all[this.iframeid].style;
    fs.visibility = vis;
    fs.left = this.iframex;
    fs.top = this.iframey;
    fs.width = this.iframew;
    fs.height = this.iframeh;
  }
}

function layerObjHide() 
{
  this.css.visibility = invis; 
  if ((is2.ie && !is_mac) && this.iframeid != "") {
    document.all[this.iframeid].style.visibility = invis;
  }
}

function layerObjGetIframeParams()
{
  if ((is2.ie && !is_mac) && this.iframeid != "") {
    tablemenuid = 'tab' + this.id;
    this.iframew = document.all[tablemenuid].scrollWidth;
    this.iframeh = document.all[tablemenuid].scrollHeight;
    this.iframex = this.css.left;
    this.iframey = this.css.top;
  }
}

function layerObjIsVisible() { if (this.css.visibility == vis) return true; else return false;}

function layerObjGetContentWH()
{
  if (is.NS6) {
    // TO BE DONE
    this.contentHeight = 500;
    this.contentWidth = 0;
  }
  else {
    this.contentHeight = is.NS ? this.css.document.height : document.all[this.id].scrollHeight;
    this.contentWidth  = is.NS ? this.css.document.width  : document.all[this.id].scrollWidth;
  }
}

function layerObj(id,iframeid,usemargin,mode,wf,hf,xf,yf,xs,ys,t,scrollstep,opacity,startafter,timeafter)
{
  layerCount++;

  this.moveTo = layerObjMoveTo;
  this.moveToX = layerObjMoveToX;
  this.slideTo = layerObjSlideTo;
  this.slideToX = layerObjSlideToX;
  this.slide = layerObjSlide;
  this.slideX = layerObjSlideX;
  this.onTimer = layerObjOnTimer;
  this.clipTo = layerObjClipTo;
  this.getClip = layerObjGetClip;
  this.wipeTo = layerObjWipeTo;
  this.scrollOne = layerObjScrollOne;
  this.scroller = layerObjScroller;
  this.wiper = layerObjWiper;
  this.hide = layerObjHide;
  this.show = layerObjShow;
  this.isVisible = layerObjIsVisible;
  this.loadSource = layerObjLoadSource;
  this.getContentWH = layerObjGetContentWH;
  this.setOpacity = layerObjSetOpacity;
  this.setOpacityTo = layerObjSetOpacityTo;
  this.launchAfter = layerObjLaunchAfter;
  this.getIframeParams = layerObjGetIframeParams;

  if (is.DOM) {
    this.elm = document.getElementById(id);
    this.css = this.elm.style;
  }
  else if (is.NS) this.css = document.layers[id];
  else if (is.IE) this.css = document.all[id].style;

  this.x = xf;
  this.y = yf;

  this.count = layerCount;
  this.id = id;
  this.usemargin = usemargin;
  this.mode = mode;

  this.sliding = (mode == IS_SLIDE);
  this.wiping = (mode == IS_WIPE);
  this.scrolling = (mode == IS_SCROLL);
  this.appear = (mode == IS_APPEAR);
  this.wf = wf;
  this.hf = hf;
  this.xf = xf;
  this.yf = yf;
  this.xs = xs;
  this.ys = ys;
  this.timeout = t;
  this.scrollstep = scrollstep;
  this.pause = 0;
  this.run = 1;
  this.opacity = opacity;
  this.startafter = startafter;
  this.timeafter = timeafter;

  // iframe used because of bug in IE
  this.iframeid = iframeid;
  this.iframew = 0;
  this.iframeh = 0;
  this.iframex = 0;
  this.iframey = 0;

  if (this.timeafter > 0)
    this.run = 0;

  layerArray[layerCount] = this;
}

// End
