//
// Copyright(c) 2005 Gammakurve Corporation. All rights reserved.
// 
// All Rights Reserved.
// http://www.gammakurve.com
// Louay Gammo

function Fader()
{
    this.element = null;
    this.steps = 1;
    this.minOpacity = 0.0;
    this.maxOpacity = 1.0;
    this.interval = 50;
    this.direction = "fadeIn";
    this.amt = this.minOpacity;
    this.step = this.maxOpacity;
    this.error = 0;
    this.debug = false;
}

Fader.prototype.init = function(id, steps, minOpacity, maxOpacity, interval)
{
    if (typeof(id) == 'string') {
        this.element = document.getElementById(id);
    } else {
        this.element = id;
    }
    this.steps = steps;
    this.minOpacity = minOpacity;
    this.maxOpacity = maxOpacity;
    this.interval = interval;
    this.amt = this.minOpacity;
    this.recalc();

    return this;
};

Fader.prototype.toggleDebug = function()
{
  this.debug = !this.debug;
}

Fader.prototype.recalc = function()
{
    var op = this.maxOpacity - this.minOpacity;

    if (this.steps <= 0) {
        this.steps = 1;
    }

    this.step = op/this.steps;

    return this;
}

Fader.prototype.setSteps = function(value)
{
    this.steps = value;
    return this.recalc();
}

Fader.prototype.setMinOpacity = function(value)
{
    this.minOpacity = value;
    return this.recalc();
}

Fader.prototype.setMaxOpacity = function(value)
{
    this.maxOpacity = value;
    return this.recalc();
}

Fader.prototype.setInterval = function(value)
{
    this.interval = value;
    return this.recalc();
}

Fader.prototype.setOpacity = function(value)
{
  if (this.element != null) {
     this.element.style.filter="alpha(opacity=" + parseInt(100 * value) + ")";
     this.element.style.opacity=value;
    return this;
  }

  this.error++;
  return this;
}

Fader.prototype.fadeIn = function(minO, maxO)
{
    if (typeof(minO) != 'undefined') {
        this.setMinOpacity(minO);
    }

    if (typeof(maxO) != 'undefined') {
        this.setMaxOpacity(maxO);
    }

    return this.fader('fadeIn');
}

Fader.prototype.fadeOut = function(minO, maxO)
{
    if (typeof(minO) != 'undefined') {
        this.setMinOpacity(minO);
    }

    if (typeof(maxO) != 'undefined') {
        this.setMaxOpacity(maxO);
    }

    return this.fader('fadeOut');
}

Fader.prototype.fader = function(direction)
{
    this.direction = direction;

    switch (this.direction) {
    case 'fadeIn':
        this.amt = this.minOpacity;
        break;
    case 'fadeOut':
        this.amt = this.maxOpacity;
        break;
    default:
        this.error++;
        return this;
    }

    window.faderObject = this;
    var func = "fadeStep()";
    setTimeout(func, this.interval);

    return this;
}

function fadeStep()
{
    if (typeof(this.faderObject) != 'object' || this.faderObject == null) {
        return false;
    }

    switch (this.faderObject.direction) {
    case 'fadeIn':
        this.faderObject.amt = this.faderObject.amt + this.faderObject.step;
	var amt = this.faderObject.amt.toFixed(2);
	if (amt > 1.0) {
	  this.faderObject = null;
	  return true;
	}

	this.faderObject.setOpacity(amt);

        if (this.faderObject.amt > this.faderObject.maxOpacity) {
	  this.faderObject = null;
	  return true;
        }
        break;
    case 'fadeOut':
        this.faderObject.amt = this.faderObject.amt - this.faderObject.step;
	var amt = this.faderObject.amt.toFixed(2);
	if (amt < 0.0) {
	  this.faderObject = null;
	  return true;
	}

	this.faderObject.setOpacity(amt);

        if (this.faderObject.amt < this.faderObject.minOpacity) {
	  this.faderObject = null;
	  return true;
        }
        break;
    default:
        return false;
    }

    var func = "fadeStep()";
    setTimeout(func, this.faderObject.interval);
}
