
//Fader.js
//Object to create a fading effect on an image
//v1: 25 October 2009
//Author: Tobias Mayer
//===================================================


var fader = function (spec) {

  //pseudo-this: inherits from printable object defined in util.js
  var that = printable(spec);  
  
  spec.imageObject = spec.imageObject || alert("Oops! missing imageObject");
  spec.seconds = spec.seconds || 3;  //default to 3 second delay
  spec.speed = spec.speed || 50;  //default to 50 millisecs between each fade interval
  spec.timerIds = new Array();
  
  fade = function () {
    //alert("spec.seconds " + spec.seconds + " spec.speed " + spec.speed);
    var opacity = 100;
    var msecs = spec.speed;
    var delay = spec.seconds * 1000; //time to view before fade starts
    do {
      opacity = opacity - 1;
      msecs = msecs + spec.speed;
      spec.timerIds[opacity] = setTimeout("_fadeout(" + opacity + ")", (msecs + delay));
    } while (opacity > 0);
  };
  that.fade = fade;

  setOpacity = function (opacityValue) {
    if (BROWSER.isIE()) {
      spec.imageObject.style.zoom = 1;
      spec.imageObject.style.filter = 'alpha(opacity='+ opacityValue +')';
    }
    else {
      spec.imageObject.style.opacity = opacityValue/100;
    }
  };
  that.setOpacity = setOpacity;

  hide = function () {
    _stopFadeout();
    spec.imageObject.style.display = 'none';
  };
  that.hide = hide;

  show = function () {
    _stopFadeout();
    that.setOpacity(100);
    spec.imageObject.style.display = 'block';
  };
  that.show = show;
  
//private function
  _fadeout = function (opacityValue) {
    if (spec.imageObject == null) return;
    setOpacity(opacityValue);
    if (opacityValue == 0) {
      spec.imageObject.style.display = 'none';
    }
  };

//private function
  _stopFadeout = function () {
    for (i=99; i>=0; i--) {
      clearTimeout(spec.timerIds[i]);
    }
  };

  return that;
}; 

//ensure the calling html document loads util.js
document.write("<sc"+"ript type='text/javascript' src='../lib/util.js'>");
document.write("</scr"+"ipt>\n");
