/////////
// [ alphaAPI.js ] v1.2
// 2002-03-04
// Author: chrisken
// URL: http://www.cs.utexas.edu/users/chrisken/alphaapi.html
/////

function alphaAPI(id, fadeInDelay, fadeOutDelay, startTrans, stopTrans, offsetTime, deltaTrans) {
	this.obj = ("alphaAPIobj" + alphaAPI._count++); // allow setTimeout references to the current object
	eval(this.obj + " = this");
	this.layerObj = document.getElementById( id );
	this.fadeInDelay = fadeInDelay || 40;
	this.fadeOutDelay = fadeOutDelay || this.fadeInDelay;
	this.startTrans = startTrans;
	this.stopTrans = stopTrans;
	this.offsetTime = (offsetTime || 0) * 1000;
	this.deltaTrans = deltaTrans || 10;
	this.timer = null;
	this.command = null;
	this.paused = false;
	this.repeat = true;
	this.started = false;
	
	this.norepeat = function() {
		this.repeat = false;
	}
	
	this.setTransBy = function(deltaTrans) {
		this.setTrans(this.getTrans() + deltaTrans);
	}
	
	this.toggle = function() {
		if( !this.started ) { // we haven't started fading yet
			this.start();
		} else if( this.paused ) {
			this.paused = false;
			this.unpause();
		} else {
			this.paused = true;
			this.pause();
		}
	}
	
	this.timeout = function(command, delay) {
		this.command = command;
		this.timer = setTimeout(command, delay);
	}
	
	this.setTrans = function(opacity) {
		if( !this.layerObj ) return;
		if( this.layerObj.filters ) {
			this.layerObj.filters.alpha.opacity = opacity;
		}
		if (!document.all && this.layerObj.style.setProperty) this.layerObj.style.setProperty("-moz-opacity", opacity / 100, "");
	}	

	this.getTrans = function() {
		if( !this.layerObj ) return 0;
		if (document.all) {
			if( typeof(this.layerObj.filters.alpha.opacity) != "undefined" )
				return this.layerObj.filters.alpha.opacity;
		} else if( this.layerObj.style.getPropertyValue )
				return this.layerObj.style.getPropertyValue("-moz-opacity") * 100;
		return 100;
	}
	
	this.start = function() {
    	if( this.layerObj ) {
    		this.started = true;
        	this.setTrans( this.startTrans );
        	if( this.startTrans > this.endTrans ) this.timeout( this.obj + ".fadeOut()", this.offsetTime );
        	else this.timeout( this.obj + ".fadeIn()", this.offsetTime );
        }
    }
    
    this.unpause = function() {
    	if( !this.started ) { // we never started, so we need to start instead of unpausing
    		this.start();
    	} else eval(this.command); // we were explicitly paused
    }
    
    this.pause = function() {
    	this.stopTimer();
    }
    
    this.stopTimer = function() {
    	clearTimeout(this.timer);
    	this.timer = null;
		this.started = false;
    }
    
    this.stop = function() {
    	this.command = null;
    	this.stopTimer();
    }

    this.fadeOut = function() {
    	this.stopTimer();
	    if (this.getTrans() > this.stopTrans){
	        this.setTransBy(-1 * this.deltaTrans);
	        this.timeout(this.obj + ".fadeOut()", this.fadeOutDelay);
	        }
	    else {
	        if (this.repeat) this.timeout(this.obj + ".fadeIn()", this.fadeInDelay);
	        }
	    }   
    
	this.fadeIn = function() {
		this.stopTimer();
	    if (this.getTrans() < this.startTrans){
	        this.setTransBy(this.deltaTrans);
	        this.timeout(this.obj + ".fadeIn()", this.fadeInDelay);
        }
	    else {
	        if (this.repeat) this.timeout(this.obj + ".fadeOut()", this.fadeOutDelay);
	    }
	}
		
	return this;	
}

alphaAPI._count = 0;

