// std dhtml detect
var NS4 = document.layers;
var NS6 = (document.getElementById && !document.all);
var IE4 = (navigator.userAgent.toLowerCase().indexOf("msie 4")!=-1);
var IE5 = (navigator.userAgent.toLowerCase().indexOf("msie 5")!=-1);
var IE6 = (navigator.userAgent.toLowerCase().indexOf("msie 6")!=-1);
var IE7 = (navigator.userAgent.toLowerCase().indexOf("msie 7")!=-1);
var IE5UP = (IE5 || IE6 || IE7);
var IE4UP = (IE4 || IE5UP);

var MAC = (navigator.userAgent.toLowerCase().indexOf("mac")!=-1);
var MAC_IE4 = (IE4 && MAC);

var DYNAMIC = false;
var DOM1 = false;

function returnFalse()  { return false; }
function returnTrue()  { return true; }

if((!NS4 && !IE4UP && !NS6 && !IE7) || MAC_IE4)
{
  /*
   * BAD PRACTICE!
   * if content won't work on a particular browser, don't display an alert! Just degrade gracefully...
   * 
   */
  //alert ('warning: DHTML');
}
else
{
  DYNAMIC = true;
  if(NS6 || IE5UP) DOM1 = true;
}
// end detect

var BlockCounter = 1;

/*  NS4-specific base + positioning methods  */
function NS4_Block_show()        { this.visibility = 'inherit'; }
function NS4_Block_hide()        { this.visibility = 'hidden'; }
function NS4_Block_isVisible()      { return (this.visibility == 'inherit'); }
function NS4_Block_setBgColor(hexStr)  { this.bgColor = hexStr; }
//function NS4_Block_clearBgColor()    { this.bgColor = null; }
//function NS4_Block_setBgImage(url)    { this.background.src = url; }
function NS4_Block_setLeft(left)     { this.left = left; }
function NS4_Block_getLeft()      { return this.left; }
function NS4_Block_setTop(top)      { this.top = top; }
function NS4_Block_getTop()        { return this.top; }
//function NS4_Block_setWidth(width)     { this.width = width; }
function NS4_Block_getWidth()      { return this.width; }
//function NS4_Block_setHeight(height)  { this.height = height; }
function NS4_Block_getHeight()      { return this.height; }
function NS4_Block_setZindex(zIndex)  { this.zIndex = zIndex; }
function NS4_Block_getZindex()      { return this.zIndex; }
function NS4_Block_rewrite(html)
{
  this.document.open();
  this.document.write(html + NS4_blockSpacer(this.getWidth(),this.getHeight()));
  this.document.close();
}

/*  default base + positioning methods  */
function Def_Block_show()        { this.style.visibility = 'inherit'; }
function Def_Block_hide()        { this.style.visibility = 'hidden';  }
function Def_Block_isVisible()      { return (this.style.visibility == 'inherit'); }
function Def_Block_setBgColor(hexStr)  { this.style.backgroundColor = hexStr; }
//function Def_Block_clearBgColor()    { this.style.backgroundColor = (MAC ? "transparent" : ""); }
//function Def_Block_setBgImage(url)    { this.style.backgroundImage = "url("+url+")"; }
function Def_Block_setLeft(left)    { this.style.left = left + "px"; }
function Def_Block_getLeft()      { return parseInt(this.style.left); }
function Def_Block_setTop(top)      { this.style.top = top + "px"; }
function Def_Block_getTop()        { return parseInt(this.style.top); }
//function Def_Block_setWidth(width)    { this.style.width = width+"px"; }
function Def_Block_getWidth()      { return parseInt(this.style.width); }
//function Def_Block_setHeight(height)  { this.style.height = height+"px"; }
function Def_Block_getHeight()      { return parseInt(this.style.height); }
function Def_Block_setZindex(zIndex)  { this.style.zIndex = zIndex; }
function Def_Block_getZindex()      { return this.style.zIndex; }
function Def_Block_rewrite(html)    { this.innerHTML = html; }

function Block_createChild(html, width, height, left, top)
{
  if(!this.childBlocks) this.childBlocks = new Array();
  block =  createBlock(html, width, height, left, top, this);
  this.childBlocks[this.childBlocks.length] = block;
  return block;
}

function enableBlock(block)
{
  if(!block) return false;  
  block.createChild = Block_createChild;
  if(NS4)
  {
    block.show = NS4_Block_show;
    block.hide = NS4_Block_hide;
    block.isVisible = NS4_Block_isVisible;
    block.setBgColor = NS4_Block_setBgColor;
//    block.clearBgColor = NS4_Block_clearBgColor;
//    block.setBgImage = NS4_Block_setBgImage;
    block.rewrite = NS4_Block_rewrite;
  }
  else
  {
    block.show = Def_Block_show;
    block.hide = Def_Block_hide;
    block.isVisible = Def_Block_isVisible;
    block.setBgColor = Def_Block_setBgColor;
//    block.clearBgColor = Def_Block_clearBgColor;
//    block.setBgImage = Def_Block_setBgImage;
    block.rewrite = Def_Block_rewrite;
  }
//  block.enablePositioning = (Block_enablePositioning ? Block_enablePositioning : returnFalse);
//  block.enableClipping = (Block_enableClipping ? Block_enableClipping : returnFalse);
  block.isBlock = true;
  return block;
}

function blockHTML(html,id,width,height,left,top,visibility,bgColor)
{
  visibility = (visibility ? "inherit" : "hidden");

  if(NS4)
  {
    str = '<layer id="'+id+'" visibility="'+visibility+'" width="'+width+'" height="'+height+'" clip="'+width+','+height+'" top="'+top+'" left="'+left+'"';
    if(bgColor) str+= ' bgColor="'+bgColor+'"';
    str += '>';
    str += html;
    str += '</layer>';
  }
  else
  {
    str = '<div id="'+id+'" style="top: '+top+'px; left: '+left+'px; width:'+width+'px; height:'+height+'px; position:absolute; visibility:'+visibility+'; z-index: '+BlockCounter+'; overflow:hidden; clip:rect(0,'+width+','+height+',0);';
    if(bgColor) str+= 'background-color:'+bgColor;
    str += '">';
    str += html;
    str += '</div>';
  }
  return str;
}

function createBlock(html,width,height,left,top,parent)
{
  var block;

  if(!(width>=0 && width<10000) || !(height>=0 && height<10000)) return false;
  id = "block" + BlockCounter++;
  left = (left ? left : 0);
  top = (top ? top : 0);
  visibility = 'hidden';
  
  if(NS4)
  {
    if(!parent)block = new Layer(width);
    else block = new Layer(width, parent);

    block.id = id;
    
    // init style
    block.visibility = visibility;
    block.top = top;
    block.height = height;
    block.left = left;
    block.width = width;
    block.clip.right = width;
    block.clip.bottom = height;

    block.document.open();
    block.document.write(html + NS4_blockSpacer(width,height));
    block.document.close();
  }
  if(DOM1)
  {
    // getObject
    if(!parent) parent = document.getElementsByTagName("body")[0];

    block = document.createElement("div");
    block.setAttribute("id",id);
    block.style.position = "absolute";

    // init style
    block.style.width = width + "px";
    block.style.height = height + "px";
    block.style.top = top + "px";
    block.style.left = left + "px";
    block.style.visibility = "hidden";
    block.style.overflow = "hidden";
    block.style.zIndex = BlockCounter;

    block.innerHTML = html;
    parent.appendChild(block);
    block.style.clip = "rect(0,"+width+","+height+",0)";
  }
  if(IE4)  /* warning: capturing the reference is problematic here on ie4pc, and ie4Mac barfs entirely  */
  {
    if(!parent) parent = document.body;
    parent.insertAdjacentHTML("BeforeEnd", blockHTML(html,id,width,height,left,top,visibility));    
    block = document.all[this.id];    
  }
  enableBlock(block);
  return block;
}

function writeBlock(html,width,height,left,top,visibility)
{
  var block;

  if(!(width>=0 && width<10000) || !(height>=0 && height<10000)) return false;
  id = "block" + BlockCounter++;
  left = (left ? left : 0);
  top = (top ? top : 0);
//  visibility = (visibility ? 'inherit' : 'hidden');
  
  document.write(blockHTML(html,id,width,height,left,top,visibility));  
  if(NS4)
  {
    block = document.layers[document.layers.length-1];
    block.width=width;
    block.height=height;
  }
  if(IE4)  block = document.all[id];
  if(DOM1) block = document.getElementById(id);

  enableBlock(block);
  return block;
}

function NS4_blockSpacer(width,height)
{
  return '<div style="position:absolute; visibility:hidden; top:0px; left:0px;"><img width="'+width+'" height="'+height+'" border="0"></div>';
}

function enablePositioning(block)
{
  if(!block.isBlock) return false;
  if(NS4)
  {
    block.setLeft = NS4_Block_setLeft;
    block.getLeft = NS4_Block_getLeft;
    block.setTop = NS4_Block_setTop;
    block.getTop = NS4_Block_getTop;
//    block.setWidth = NS4_Block_setWidth;
    block.getWidth = NS4_Block_getWidth;
//    block.setHeight = NS4_Block_setHeight;
    block.getHeight = NS4_Block_getHeight;
    block.setZindex = NS4_Block_setZindex;
    block.getZindex = NS4_Block_getZindex;  
  }
  else
  {
    block.setLeft = Def_Block_setLeft;
    block.getLeft = Def_Block_getLeft;
    block.setTop = Def_Block_setTop;
    block.getTop = Def_Block_getTop;
//    block.setWidth = Def_Block_setWidth;
    block.getWidth = Def_Block_getWidth;
//    block.setHeight = Def_Block_setHeight;
    block.getHeight = Def_Block_getHeight;
    block.setZindex = Def_Block_setZindex;
    block.getZindex = Def_Block_getZindex;  
  }
  block.positionable = true;
  return true;
}

function findBlock(objID,scope)
{
  return findElement("block",objID,scope);
}

function findImage(imgName,scope)
{
  return findElement("image",imgName,scope);
}

function findLink(linkId,scope)
{
  return findElement("link",linkId,scope);
}

function findElement(type,id,scope)
{
  if(!scope) scope=window;
  if(type=="block")
  {
    if(document.layers) coll = scope.document.layers;
    else if(document.all) coll = scope.document.all.tags("div");
    else if(document.getElementById)
    {
      coll = scope.getElementsByTagName("div");
    }
  }
  else coll = scope.document[type+'s'];
  if(!coll) return false;

  var retObj = coll[id];
  if(!retObj && document.layers)
  {
    for(var nsl=0; nsl<scope.document.layers.length; nsl++)
    {
      retObj = findElement(type,id,scope.document.layers[nsl]);
      if(retObj) return retObj;
    }
    return false;
  }
  return retObj;
}
