/*
  Look in layerapi.html for comments!!!!
  CROSS-BROWSER MODULAR DHTML API (c) 2001-2004 Angus Turnbull, http://www.twinhelix.com
  This notice may not be altered or removed. See my site for licensing and more scripts!

*/


var isDOM=document.getElementById?1:0,
 isIE=document.all?1:0,
 isNS4=navigator.appName=='Netscape'&&!isDOM?1:0,
 isIE4=isIE&&!isDOM?1:0,
 isOp=self.opera?1:0,
 isDyn=isDOM||isIE||isNS4,
 isWin=navigator.platform.indexOf('Win')!=-1?1:0;

function getRef(i, p)
{
 p=!p?document:p.navigator?p.document:p;
 return isIE ? p.all[i] :
  isDOM ? (p.getElementById?p:p.ownerDocument).getElementById(i) :
  isNS4 ? p.layers[i] : null;
};

function getSty(i, p)
{
 var r=getRef(i, p);
 return r?isNS4?r:r.style:null;
};

if (!self.LayerObj) var LayerObj = new Function('i', 'p',
 'this.ref=getRef(i, p); this.sty=getSty(i, p); return this');

function getLyr(i, p) { return new LayerObj(i, p) };

function LyrFn(n, f)
{
 LayerObj.prototype[n] = new Function('var a=arguments,p=a[0],px=isNS4||isOp?0:"px"; ' +
  'with (this) { '+f+' }');
};

LyrFn('x','if (!isNaN(p)) sty.left=p+px; else return parseInt(sty.left)');
LyrFn('y','if (!isNaN(p)) sty.top=p+px; else return parseInt(sty.top)');


LyrFn('w','if (p) (isNS4?sty.clip:sty).width=p+px; ' +
 'else return (isNS4?ref.document.width:ref.offsetWidth)');
LyrFn('h','if (p) (isNS4?sty.clip:sty).height=p+px; ' +
 'else return (isNS4?ref.document.height:ref.offsetHeight)');

LyrFn('vis','sty.visibility=p');

LyrFn('bgColor','if (isNS4) sty.bgColor=p?p:null; ' +
 'else sty.background=p?p:"transparent"');
LyrFn('bgImage','if (isNS4) sty.background.src=p?p:null; ' +
 'else sty.background=p?"url("+p+")":"transparent"');

LyrFn('clip','if (isNS4) with(sty.clip){left=a[0];top=a[1];right=a[2];bottom=a[3]} ' +
 'else sty.clip="rect("+a[1]+"px "+a[2]+"px "+a[3]+"px "+a[0]+"px)" ');

LyrFn('write','if (isNS4) with (ref.document){write(p);close()} else ref.innerHTML=p');

LyrFn('alpha','var f=ref.filters,d=(p==null),o=d?"inherit":p/100; if (f) {' +
 'if (!d&&sty.filter.indexOf("alpha")==-1) sty.filter+=" alpha(opacity="+p+")"; ' +
 'else if (f.length&&f.alpha) with(f.alpha){if(d)enabled=false;else{opacity=p;enabled=true}} }' +
 'else if (isDOM)sty.opacity=sty.MozOpacity=o');

function setLyr(v, dw, p)
{
 if (!setLyr.seq) setLyr.seq=0;
 if (!dw) dw=0;
 var o = !p ? isNS4?self:document.body : !isNS4&&p.navigator?p.document.body:p,
  IA='insertAdjacentHTML', AC='appendChild', id='_sl_'+setLyr.seq++;

 if (o[IA]) o[IA]('beforeEnd', '<div id="'+id+'" style="position:absolute"></div>');
 else if (o[AC])
 {
  var n=document.createElement('div');
  o[AC](n); n.id=id; n.style.position='absolute';
 }
 else if (isNS4)
 {
  var n=new Layer(dw, o);
  id=n.id;
 }

 var l=getLyr(id, p);
 with (l) if (ref) { vis(v); x(0); y(0); sty.width=dw+(isNS4?0:'px') }
 return l;
};

if (!self.page) var page = { win: self, minW: 0, minH: 0, MS: isIE&&!isOp,
 db: document.compatMode&&document.compatMode.indexOf('CSS')>-1?'documentElement':'body' };

page.winW=function()
 { with (this) return Math.max(minW, MS?win.document[db].clientWidth:win.innerWidth) };
page.winH=function()
 { with (this) return Math.max(minH, MS?win.document[db].clientHeight:win.innerHeight) };

page.scrollX=function()
 { with (this) return MS?win.document[db].scrollLeft:win.pageXOffset };
page.scrollY=function()
 { with (this) return MS?win.document[db].scrollTop:win.pageYOffset };

page.docW=function()
 { with (this) return Math.max(minW, isNS4?win.document.width:win.document.offsetWidth) };
page.docH=function()
 { with (this) return Math.max(minH, isNS4?win.document.height:win.document.offsetHeight) };

page.elmPos=function(e,p)
{
 var x=0,y=0,w=p?p:this.win;
 e=e?(e.substr?(isNS4?w.document.anchors[e]:getRef(e,w)):e):p;
 if(isNS4){if(e&&(e!=p)){x=e.x;y=e.y};if(p){x+=p.pageX;y+=p.pageY}}
 else if (e && e.focus && this.MS && /Mac/.test(navigator.platform))
 {
  e.onfocus = new Function('with(event){self.tmpX=clientX-offsetX;' +
   'self.tmpY=clientY-offsetY}');
  e.focus();x=tmpX;y=tmpY;e.blur()
 }
 else while(e){x+=e.offsetLeft;y+=e.offsetTop;e=e.offsetParent}
 return{x:x,y:y};
};

function addEvent(o, i, f, c)
{
 var l = 'addEventListener', n = 'on'+i, b = o[n], a = o.evts = o.evts || [],
  m = / u=(\d+),r/.exec(b), u = m ? parseInt(m[1]) : a.length;
 if (o[l]) return o[l](i, f, false);
 a[u] = a[u] || [];
 if (!m)
 {
  if (b) a[u][0] = b;
  o[n] = new Function('e', 'e=e||self.event;var u=' + u + ',r=true,o=this,n="' + n +
   '",t=n+"TMP",a=o.evts[u];for(var f in a){o[t]=a[f];r=o[t](e)!=false&&r}return r&&isNS4&&' +
   c + '?o.routeEvent(e):r');
  if (isNS4 && c) o.captureEvents(Event[i.toUpperCase()]);
 }
 a[u][a[u].length] = f;
}

function updateForm(e)
{
 if (document.forms.pageObjectDemo) with (document.forms.pageObjectDemo)
 {
  winWidth.value = page.winW();
  winHeight.value = page.winH();
  ancPos.value = page.elmPos('ancTest').y;
 }
}

// My NS4 bug check code, checks for horizontal resize and refreshes.
var nsWinW = window.innerWidth, nsWinH = window.innerHeight;
addEvent(window, 'resize',
 new Function('if (isNS4 && (nsWinW!=innerWidth || nsWinH!=innerHeight)) history.go(0)'), false);

