// Browser and OS detection
var ns, ie, mac, objRef;


var pageLoaded=false;

function initLayerLib() {
  if (document.all) ie=true; else ie=false;
  if (document.layers) ns=true; 
  else ns=false;
  if (navigator.userAgent.indexOf("Mac")>0) mac=true; else mac=false;
  //objRef=new Array();
  pageLoaded=true;
}




// ##################### INTERACTIVE LAYER CLASS ###########################

// --- Helper function for creating multiple layers ---
// baseNames: array of layernames. layerNum (optional): when creating multiple
// layers with same basename (lay1, lay2, lay3,...)
function makeInteractiveLayers(baseNames, layerNum) {
  var layerArray = new Array();
  for (var n=0; n<baseNames.length; n++) {
    if (layerNum) for (var i=0; i<layerNum; i++)
      layerArray[n*layerNum+i]=new InteractiveLayer(baseNames[n]+(i+1));
    else layerArray[n]=new InteractiveLayer(baseNames[n]);
  }
  return layerArray;
}

// --- Create layer object ---
function InteractiveLayer(layerName, xpos, ypos, visibility) {
  if (ie) {
    this.obj=document.all[layerName];
    this.xpos=this.obj.style.pixelLeft;
    this.ypos=this.obj.style.pixelTop;
    this.width=this.obj.style.pixelWidth;
    this.height=this.obj.offsetHeight;
    //this.height=this.obj.style.pixelHeight;
    this.zIndex=this.obj.style.zIndex;
    this.setVisibility=IL_IE_setVisibility;
    this.setZindex=IL_IE_setZindex;
    this.moveTo=IL_IE_moveTo;
    this.setMouseHandlers=IL_setMouseHandlers;
    this.setClipping=IL_IE_setClipping;
  }
  else if (ns) {
    this.obj=IL_NS_FindLayer(document, layerName);
    this.xpos=this.obj.left;
    this.ypos=this.obj.top;
    this.width=this.obj.clip.width;
    this.height=this.obj.clip.height;
    this.zIndex=0; //this.obj.style.zIndex;
    this.setVisibility=IL_NS_setVisibility;
    this.setZindex=IL_NS_setZindex;
    this.moveTo=IL_NS_moveTo;
    this.setMouseHandlers=IL_setMouseHandlers;
    this.setClipping=IL_NS_setClipping;
  }
  this.moveBy=IL_moveBy;
  this.moveRelObject=IL_moveRelObject;
  this.visible=false;
  this.objId=layerName;
  objRef[this.objId]=this;
  if (!isNaN(xpos) && !isNaN(ypos)) this.moveTo(xpos, ypos, visibility);
}

// --- Set mousehandler functions for layer. ---
function IL_setMouseHandlers(mouseOver, mouseOut) {
  this.obj.onmouseover=mouseOver;
  this.obj.onmouseout=mouseOut;
  if (ns) this.obj.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
}

// --- Find layer in nested document (NS) ---
function IL_NS_FindLayer(parentLayer, id) {
  if (parentLayer.layers[id]) return parentLayer.layers[id];
  else for (n=0; n<parentLayer.layers.length; n++) {
    var checkLayer = IL_NS_FindLayer(parentLayer.layers[n], id);
    if (checkLayer) return checkLayer;
  }
  return null;
}

// --- Set visibility state ---
function IL_IE_setVisibility(state) {
  this.visible=state;
  if (state) this.obj.style.visibility="visible";
  else this.obj.style.visibility="hidden";
}
function IL_NS_setVisibility(state) {
  this.visible=state;
  if (state) this.obj.visibility = "show";
  else this.obj.visibility = "hide";
}

// --- Sets layer stackning order ---
function IL_IE_setZindex(zindex) {
  this.zindex = zindex;
  this.obj.style.zIndex = zindex;
}
function IL_NS_setZindex(zindex) {
  this.zindex = zindex;
  this.obj.zIndex = zindex;
}

// --- Move layer to position ---
function IL_IE_moveTo(xpos, ypos, show) {
  this.xpos = xpos;
  this.ypos = ypos;
  this.obj.style.left = this.xpos;
  this.obj.style.top = this.ypos;
  if (show) this.setVisibility(true);
}
function IL_NS_moveTo(xpos, ypos, show) {
  this.xpos = xpos;
  this.ypos = ypos;
  this.obj.left = this.xpos;
  this.obj.top = this.ypos;
  if (show) this.setVisibility(true);
}

// --- Move layer in relation to another object. ---
function IL_moveRelObject(sourceObject, dx, dy, show) {
  var po=getObjectPos(sourceObject);
  this.moveTo(po.x+dx, po.y+dy, show);
}

// --- Move layer by length ---
function IL_moveBy(dx, dy) {
  this.xpos += dx;
  this.ypos += dy;
  this.moveTo(this.xpos, this.ypos);
}

// --- Set layer clipping ---
function IL_IE_setClipping(t, r, b, l) {
  this.obj.style.clip = "rect("+t+"px "+r+"px "+b+"px "+l+"px)";
}
function IL_NS_setClipping(t, r, b, l) {
  this.obj.clip.top=t;
  this.obj.clip.right=r;
  this.obj.clip.bottom=b;
  this.obj.clip.left=l;
}


// ################# HELPER CLASSES AND FUNCTIONS #########################

function Coordinate(x, y) {
  this.x=x;
  this.y=y;
}

// Get position of an object
function getObjectPos(obj) {
  if (ns) return new Coordinate(obj.x, obj.y);
  else {
    var x=obj.offsetLeft;
    var y=obj.offsetTop;
    while (obj.offsetParent) {
      obj=obj.offsetParent;
      x+=obj.offsetLeft;
      y+=obj.offsetTop;
    }
    return new Coordinate(x, y);
  }
}

// Get a named anchor's object
function getAnchorObj(link) {
  if (ie) return document.all[link];
  else return document.anchors[link];
}

