/**
 * @fileoverview  The Vengeful Cow Main Page Scripts
 * @version       $Id$
 */

// ---------------------------------------------------------------------------

/**
 * Constructor for the Vengeful Cow Fader.
 *
 * @class
 *   Animates the sections of the page once the page has loaded.
 *   It serves no useful purpose other than mindless special effects.
 *   You are welcome to re-use this code for your own purposes as long
 *   as you retain the author and copyright information.  I'd appreciate
 *   it if you'd let me know too.  :-)
 * @author
 *   Marshall Elfstrand (marshall@vengefulcow.com)
 * @copyright
 *   Copyright 2005 by Marshall Elfstrand
 *
 * @param {Array} divNames
 *        array of names of DIVs that will fade in/out
 *
 * @constructor
 */
function VCowFader(divNames) {
  var self = this;
  
  // Constants
  var FADE_DELAY  = 50;     // Milliseconds between animation frames
  var FADE_AMOUNT = 10;     // Percentage to adjust each animation frame
  var FADE_MIN    = 50;     // Minimum allowed opacity
  var FADE_MAX    = 100;    // Maximum allowed opacity
  
  // Instance variables
  var _fadeTimer;           // Animation timer
  var _fadeStates = {};     // Stores opacity and direction for each section
  
  /**
   * Initializes the page manager.
   *
   * @param {Array} args
   *        array of names of DIVs that will fade in/out
   *
   * @private
   */
  this.initialize = function(args) {
    for (var i = 0; i < args.length; i++) {
      var name = args[i];
      var div = document.getElementById(name);
      self.setOpacity(div, FADE_MIN);
      _fadeStates[name] = { opacity:FADE_MIN, direction:0 };
      eval('div.onmouseover = function() { self.fadeIn("'  + name + '"); }');
      eval('div.onmouseout  = function() { self.fadeOut("' + name + '"); }');
    }
  }
  
  /**
   * Starts the fade-in animation for a DIV.
   *
   * @param {String} name
   *        the name of the DIV to fade in
   */
  this.fadeIn = function(name) {
    _fadeStates[name].direction = FADE_AMOUNT;
    self.doFade();
  }
  
  /**
   * Starts the fade-out animation for a DIV.
   *
   * @param {String} name
   *        the name of the DIV to fade out
   */
  this.fadeOut = function(name) {
    _fadeStates[name].direction = -FADE_AMOUNT;
    self.doFade();
  }

  /**
   * Goes through the fade states and adjusts the DIVs.
   *
   * @private
   */
  this.doFade = function() {
    if (_fadeTimer != null) clearTimeout(_fadeTimer);
    var shouldContinue = false;
    for (var key in _fadeStates) {
      var obj = _fadeStates[key];
      if (obj.direction != 0) {
        obj.opacity += obj.direction;
        self.setOpacity(document.getElementById(key), obj.opacity);
      }
      if (obj.direction > 0) {
        if (obj.opacity >= FADE_MAX) obj.direction = 0;
        else shouldContinue = true;
      } else if (obj.direction < 0) {
        if (obj.opacity <= FADE_MIN) obj.direction = 0;
        else shouldContinue = true;
      }
    }
    if (shouldContinue) _fadeTimer = setTimeout(self.doFade, FADE_DELAY);
  }
  
  /**
   * Sets the opacity of the given DIV to the given level.  The level
   * must be a value between 0 and 100 inclusive.
   *
   * @param {Object} div
   *        the DIV to adjust
   * @param {int} level
   *        the value to which the opacity will be set
   */
  this.setOpacity = function(div, level) {
    // Mozilla and Safari support the standard opacity setting.
    // IE requires an MS-proprietary filter.
    div.style.opacity = (level / 100);
    div.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" +
      level.toString() + ")";
  }
  
  // Call initializer when constructing.
  this.initialize(divNames);
}

// ---------------------------------------------------------------------------

window.onload = function() {
  new VCowFader(["nonsense", "projects"]);
}