
function checkIsIE() {
  var userAgent = navigator.userAgent.toLowerCase();
  return (/msie[\/\s](\d+\.\d+)/.test(userAgent));
}

browserIsIE = checkIsIE();

function setElementOpacity(el, op) {
  if (op == 0) {
    if(el.style.visibility != "hidden") {
      el.style.visibility="hidden";
    }
  } else {
    if (el.style.visibility!="visible") {
      el.style.visibility="visible";
    }
  }
  if (!el.currentStyle||!el.currentStyle.hasLayout){
    el.style.zoom=1;
  }
  if (browserIsIE) {
    el.style.filter = (op == 1) ? "":"alpha(opacity="+op*100+")";
  }
  el.style.opacity = op;
}

function addAnimation(anim) {
  var currentAnimations = window.animations;
  if (!currentAnimations) {
    currentAnimations = new Array();
  }
  var animIndex = 0;
  for (animIndex = 0; animIndex < currentAnimations.length; animIndex++) {
    if (!currentAnimations[animIndex] || currentAnimations[animIndex].isFinished) {
      break;
    }
  }
  currentAnimations[animIndex] = anim;
  window.animations = currentAnimations;
  return animIndex;
}

function getMouseoutEventTarget(evt) {
  if (evt.toElement) {
    return evt.toElement;
  } else if (evt.relatedTarget) {
    return evt.relatedTarget;
  } else {
    return null;
  }
}

function elementIsDescendent(child, parent) {
  if (child == null) {
    return false;
  }
  for (var e = child.parentNode; e; e = e.parentNode) {
    if (e == parent) {
      return true;
    }
  }
}

Animate = function(duration, frameDuration, step, completion) {

  this.step = step;
  this.completion = completion;
  this.abort = false;

  this.go = function() {

    var currentTime = new Date().getTime();
    this.duration = duration;
    this.startTime = currentTime;
    this.endTime = this.startTime + this.duration;
    this.isFinished = false;
    this.step(0.0);
    
    this.animIndex = addAnimation(this);
    
    setTimeout("window.animations[" + this.animIndex + "].frame()", frameDuration);
    
  }
  
  this.frame = function() {
  
    var currentTime = new Date().getTime();
    var t = (currentTime - this.startTime) / this.duration;
    if (t >= 1.0) { 
      this.finalFrame();
    } else {
      if (t < 0.0) { t = 0.0 };
      this.step(t);
      if (this.shouldAbort()) {
        this.isFinished = true;
        window.animations[this.animIndex] = null;
      } else {
        setTimeout("window.animations[" + this.animIndex + "].frame()", frameDuration);
      }
    }
  }
  
  this.finalFrame = function() {
      this.step(1.0);
      this.completion();
      this.isFinished = true;
      window.animations[this.animIndex] = null;
    }
  
  this.shouldAbort = function() {
    return this.abort == true;
  }
  
}

