// LabAPI
var currPage;
var currPageContainer;
var previousPage;
var vispage;
var globalPage;
var appName = "index";
var ext = ".htm";
var ie = document.all ? true : false;
function versionChecker() {
  var check = true;
  var ver = parseFloat(navigator.appVersion);
  var app = navigator.appName;
  if ((ie) && (navigator.appVersion.indexOf("MSIE 5"))) { ver = 5; }
  if ((ie) || (app = "Netscape")) {
    if (ver < 5) { check = false; }
  }
  else {
    check = false;
  }  
  if (!check) {
    document.location.href = "index.gen/OldBrowser.htm";
  }
  if (navigator.platform.indexOf("Mac") != -1) { ie = false; }
}
function initPage() {
  versionChecker();
  if (ie) { document.body.scroll = "no"; }
  currPage = document.getElementById("page");
  globalPage = document.getElementById("page");
  vispage = document.getElementById("page");
  if (currPage.childNodes.length > 0) {
    if ((ie) && (currPage.filters.length > 0)) {
      for (var i=0; i < currPage.filters.length; i++) {
        currPage.filters[i].apply();
        currPage.style.visibility = "visible";
        currPage.filters[i].play();
      }
    }
    else {
      currPage.style.visibility = "visible";
    }
  }
  var child = ie ? currPage.children : currPage.childNodes;
  for (var i = 0; i < child.length; i++) {
    child[i].oldStyle = new oldStyle(child[i]);
    child[i].resetStyle = getOldStyle;
    if (isMediaObject(child[i])) {
      setMediaObjectData(child[i]);
      if (child[i].AutoPlay == "true") {
        child[i].play();
      }
    }
  }
  for (var i=0; i < vispage.childNodes.length; i++) {
    if (vispage.childNodes[i].id) {
      setObjectMethods(vispage.childNodes[i]);
      if (ie) {
        if (vispage.childNodes[i].className == "group") {
          var group = vispage.childNodes[i];
          for (var j=0; j < group.childNodes.length; j++) {
            playTransition(group.childNodes[j].id);
          }
        }
        else {
          playTransition(vispage.childNodes[i].id);
        }
      }
      else {
        vispage.childNodes[i].style.visibility = "visible";
      }
    }
  }
  currPage.autoStart.run();
}
function getObject(object) {
  var ref = document.getElementById(object);
  if (ref) {
    if (ref.id == object) {
      return ref;
    }
  }
  else {
    return document;
  }
}
function visObj(object) {
  var ref;
  var ret = false;
  try {
    ref = document.getElementById(object);
    if (ref) {
      ret = true;
    }
  }
  catch (e) {
    ret = false;
  }
  return ret;
}
function isSoundObject(obj) {
  if (obj.className == "media") {
    var cobs = ie ? obj.children : obj.childNodes;
    return (cobs[0].className == "soundObject");
  }
  return false;
}
function isMediaObject(obj) {
  return (obj.className == "media");
}
function setMediaObjectData(obj) {
  if ((obj.className == "media") && (ie)) {
    var mObj;
    for (var i=0; i < obj.childNodes.length; i++) {
      if (obj.childNodes[i].tagName == "OBJECT") {
        mObj = obj.childNodes[i];
      }
    }
    obj.AutoRewind   = mObj.AutoRewind;
    obj.PlayCount    = mObj.PlayCount;
    obj.ShowControls = mObj.ShowControls;
    obj.obj   = mObj;
    obj.play  = function Play() {
      try {
        if (ie ? (obj.obj.PlayState != 6) : (obj.obj.GetPlayState() != 6)) {
         obj.obj.Play();
        }
      } catch (e) {}
    }
    obj.pause = function Pause() {
      try {
        if (ie ? (obj.obj.PlayState == 2) : (obj.obj.GetPlayState() == 2)) {
          obj.obj.Pause();
        }
      } catch (e) {}
    }
    obj.stop  = function Stop() {
      try {
        obj.obj.Stop(); obj.obj.SelectionStart = 0
      } catch (e) {}
    }
  }
  else {
    obj.play = function nofunc() {}
    obj.pause = function nofunc() {}
    obj.stop = function nofunc() {}
  }
}
function renderHtmlOnPage(page,src) {
  var re = /ID=\W(\w{1,})\W/g;
  var res = re.exec(src);
  var flag = false;
  if ((res == null) || (!vispage[res[1]])) {
    if (ie) {
      vispage.insertAdjacentHTML("beforeEnd",src);
    }
    else {
      var range = document.createRange();
      range.setStartAfter(vispage.lastChild);
      var docFrag = range.createContextualFragment(src);
      vispage.appendChild(docFrag);
    }
    if (ie) {
      playTransition(vispage.lastChild.id);
    }
    else {
      vispage.lastChild.style.visibility = "visible";
    }
    flag = true;
  }
  return flag;
}
function playTransition(obj) {
  var transObj;
  try {
    transObj = getObject(obj);
    var vis;
    if ((transObj.style.visibility == "visible") && (transObj.filters.length > 0)) {
      vis = "hidden";
    }
    else {
      vis = "visible";
    }
    for (var i=0; i < transObj.filters.length; i++) {
      transObj.filters[i].apply();
      transObj.style.visibility = vis;
      transObj.filters[i].play();
    }
  }
  catch (e) {
    // ignore
  }
}
function jumpToPage(page) {
  if (page == "Forside") {
    document.location.href = appName + ext;
  }
  else {
    document.location.href = appName + page + ext;
  }
}
function historyBack() {
  history.go(-1);
}
function previousPage() {
  jumpToPage(appPreviousPage);
}
function nextPage() {
  jumpToPage(appNextPage);
}
function oldStyle(obj) {
  if (ie) {
    style = obj.style;
    this.cssText = style.cssText;
  }
}
function getOldStyle() {
  if (ie) {
    this.style.cssText = this.oldStyle.cssText;
  }
}
function autoStartObject() {
  this.lines = new Array();
  this.add = autoStartAdd;
  this.run = autoStartRun;
}
function autoStartRun() {
  for (var i=0; i < this.lines.length; i++) {
    eval(this.lines[i]);
  }
}
function autoStartAdd(line) {
  this.lines[this.lines.length] = line;
}
function setObjectMethods(obj) {
  if (obj.id) {
    obj.incWidth = methodIncWidth;
    obj.incHeight = methodIncHeight;
    obj.incLeft = methodIncLeft;
    obj.incTop = methodIncTop;
    obj.rescale = methodRescale;
    obj.getAttr = methodGetAttr;
    obj.setAttr = methodSetAttr;
  }
}
function methodIncWidth(w) {
  var w2 = parseInt(this.style.width) + w;
  if (w2 >= 0) { this.style.width =  w2 + "px"; }
}
function methodIncHeight(h) {
  var h2 = parseInt(this.style.height) + h;
  if (h2 >= 0) { this.style.height = h2 + "px"; }
}
function methodIncLeft(l) {
  var l2 = parseInt(this.style.left) + l;
  if (l2 >= -32768) { this.style.left = l2 + "px"; }
}
function methodIncTop(t) {
  t2 = parseInt(this.style.top) + t;
  if (t2 >= -32768) { this.style.top = t2 + "px"; }
}
function methodRescale(wh) {
  this.incWidth(wh*2);
  this.incHeight(wh*2);
  this.incLeft(-wh);
  this.incTop(-wh);
}
function methodGetAttr(attrName) {
  var attrVal;
  if (visObj(this.id)) {
    try {
      attrVal = eval("this.style."+attrName);
    } catch (e) {}
  }
  return attrVal;
}
function methodSetAttr(attrName, attrVal) {
  if (visObj(this.id)) {
    try {
      eval("this.style."+attrName+" = '"+attrVal+"';");
    } catch (e) {}
  }
}
function incWidth(Obj, w) {
  var w2 = parseInt(getObject(Obj).style.width) + w;
  if (w2 >= 0) { getObject(Obj).style.width =  w2 + "px"; }
}
function incHeight(Obj, h) {
  var h2 = parseInt(getObject(Obj).style.height) + h;
  if (h2 >= 0) { getObject(Obj).style.height = h2 + "px"; }
}
function incLeft(Obj, l) {
  var l2 = parseInt(getObject(Obj).style.left) + l;
  if (l2 >= -32768) { getObject(Obj).style.left = l2 + "px"; }
}
function incTop(Obj, t) {
  t2 = parseInt(getObject(Obj).style.top) + t;
  if (t2 >= -32768) { getObject(Obj).style.top = t2 + "px"; }
}
function rescale(Obj, wh) {
  incWidth(Obj, wh*2);
  incHeight(Obj, wh*2);
  incLeft(Obj, -wh);
  incTop(Obj, -wh);
}
function getAttr(obj, attrName) {
  var attrVal;
  if (visObj(obj)) {
    try {
      attrVal = eval("getObject(obj).style."+attrName);
    } catch (e) {}
  }
  return attrVal;
}
function setAttr(obj, attrName, attrVal) {
  if (visObj(obj)) {
    try {
      eval("getObject(obj).style."+attrName+" = '"+attrVal+"';");
    } catch (e) {}
  }
}
function getSender() {
  try {
    return (ie ? window.event.srcElement : getObject(event.target));
  } catch (e) {  return null; }
}
// Pathfunktioner :
function createPath(loopPath, grObject) {
  this.currentPosition = 0;
  this.loopPath = loopPath;
  this.grObject = grObject;
  this.objectMoving = false;
  this.play = startMovement;
  this.pause = pauseMovement;
  this.stop = stopMovement;
  this.getPathPos = getPathPos;
  this.getAngle = getAngle;
  this.positionCount = this.xpos.length;
  this.moveObject = moveObject;
  this.currAngle = 0;
  this.deg2radians = Math.PI * 2 / 360;
}
function startMovement() {
  if (visObj(this.id)) {
    if (this.useAngles) {
      try {
        var f = "progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand'); " + getObject(this.grObject).style.filter;
        getObject(this.grObject).style.filter = f;
      }
      catch (e) {}
    }
    if (!this.objectMoving) {
      this.objectMoving = true;
      if (this.onStart != '') { eval(this.onStart); }
      this.timerID = setTimeout('getObject("'+this.id+'").moveObject();',this.delay[this.currentPosition]);
    }
  }
  else {
     this.timerID = setTimeout('getObject("'+this.id+'").play()',25);
  }
}
function stopMovement() {
  this.objectMoving = false;
  this.currentPosition = 0;
  if (this.onStop != '') { eval(this.onStop); }
}
function pauseMovement() {
  this.objectMoving = false;
}
function moveObject() {
  var delay = 0;
  var cur = this.currentPosition;
  if (this.objectMoving) {
    if (cur < this.positionCount) {
      this.getPathPos();
      delay = this.delay[cur];
      this.timerID = setTimeout('getObject("'+this.id+'").moveObject();',delay);
    }
    else if (this.loopPath) {
      this.getPathPos();
      delay = this.delay[this.positionCount - 1];
      this.timerID = setTimeout('getObject("'+this.id+'").moveObject();',delay);
    }
    else {
      this.stop();
    }
  }
}
function getAngle() {
  var pos = this.currentPosition;
  if (this.grObject != "") {
    var add = 0;
    if (pos < (this.xpos.length - 1)) {
      var xdist = (this.xpos[pos] - this.xpos[pos + 1]);
      var ydist = (this.ypos[pos] - this.ypos[pos + 1]);
    }
    else {
      var xdist = (this.xpos[pos] - this.xpos[0]);
      var ydist = (this.ypos[pos] - this.ypos[0]);
    }
    if (((xdist < 0) && (ydist >= 0)) || ((xdist < 0) && (ydist < 0))) {
      this.currAngle = (Math.atan(ydist / xdist) / this.deg2radians) + 180;
    }
    else  {
      this.currAngle = (Math.atan(ydist / xdist) / this.deg2radians);
    }
    var rad = this.currAngle * this.deg2radians;
    costheta = Math.cos(rad);
    sintheta = Math.sin(rad);
    var obj = getObject(this.grObject);
    obj.filters.item(0).M11 = costheta;
    obj.filters.item(0).M12 = -sintheta;
    obj.filters.item(0).M21 = sintheta;
    obj.filters.item(0).M22 = costheta;
  }
}
function getPathPos() {
  var pos = this.currentPosition;
  if (this.grObject != "") {
    if (pos < this.xpos.length) {
      getObject(this.grObject).style.left = this.xpos[pos] + "px";
      getObject(this.grObject).style.top = this.ypos[pos] + "px";
      if ((ie) && (this.useAngles)) { this.getAngle(); }
    }
    else if (this.loopPath) {
      this.currentPosition = -1;
      getObject(this.grObject).style.left = this.xpos[0];
      getObject(this.grObject).style.top = this.ypos[0];
    }
  }
  this.currentPosition++;
}
// Spritefunktioner:
function createSprite(loopAnimation) {
  this.currentFrame = 0;
  this.loopAnimation = loopAnimation;
  this.animationRunning = false;
  this.play = startSprite;
  this.pause = pauseSprite;
  this.stop = stopSprite;
  this.animateSprite = animateSprite;
  this.loadSprite = loadSprite;
  this.frameCount = this.fileName.length;
  this.loadSprite();
}
function startSprite() {
  if (visObj(this.id)) {
    if (!this.animationRunning) {
      this.animationRunning = true;
      this.timerID = setTimeout('getObject("'+this.id+'").animateSprite();',this.delay[this.currentFrame]);
    }
  }
  else {
     this.timerID = setTimeout('getObject("'+this.id+'").play()',25);
  }
}
function stopSprite() {
  this.animationRunning = false;
  this.currentFrame = 0;
}
function pauseSprite() {
  this.animationRunning = false;
}
function animateSprite() {
  var delay = 0;
  var cur = this.currentFrame;
  if (this.animationRunning) {
    if (cur < this.frameCount) {
      this.src = this.image[cur].src;
      delay = this.delay[cur];
      this.currentFrame++;
      this.timerID = setTimeout('getObject("'+this.id+'").animateSprite();',delay);
    }
    else if (this.loopAnimation) {
      this.currentFrame = 0;
      this.timerID = setTimeout('getObject("'+this.id+'").animateSprite();',delay);
    }
  }
}
function loadSprite() {
  this.image = new Array();
  for (var i=0; i < this.frameCount; i++) {
    this.image[i] = new Image();
    this.image[i].src = this.fileName[i];
  }
}
