﻿/******************************************************************************
File:       HoverClass.js
Author:     Rick Blouch
Date:       3/26/2007
Purpose:    
*******************************************************************************/

//Global Properties
var arrFaders = new Array();
var fadeInterval;
var iFadeObjCount = 0;

function FadeClass(oObj, bFadeIn, oHandler){
	//Properties
	this.obj = oObj;
	this.bFadeIn = bFadeIn;
	this.curLevel = (this.bFadeIn) ? 0:100;
	this.handler = oHandler;
	
	//Methods
	
	this.fade = function(){
		var step;

		//Fade Out
		if(!this.bFadeIn && this.curLevel > 0){
			step = this.curLevel * .1;// 3;
			step = Math.max(step, 2);
			this.curLevel -= step;
			this.curLevel = Math.max(this.curLevel, 0);
			
		}else if(this.bFadeIn && this.curLevel < 100){ //Fade In
			step = this.curLevel * .2;
			step = Math.max(step, 2);
			this.curLevel += step;
			this.curLevel = Math.min(this.curLevel, 100);
		}

		try{ //Set the opacity
			this.obj.style.opacity = this.curLevel / 100;
			this.obj.style.filter = "alpha(opacity=" + this.curLevel + ")";
		}catch(err){}

		//Determine the status
		var bDone;
		if(this.bFadeIn){
			bDone = (this.curLevel == 100);
		}else{
			bDone = (this.curLevel == 0);
		}
		
		//Return the status
		return bDone;
	}
}

//Register a new fade
//WARNING: I do not prevent registering the same obj twice before the fade effect is over.  Doing
//         this WILL cause problems.
function FC_FadeReg(obj, bFadeIn, handler){
    this.arrFaders[this.arrFaders.length] = new FadeClass(obj, bFadeIn, handler);
    iFadeObjCount++;
    
	if(fadeInterval == null){
		fadeInterval = window.setInterval(FC_FadeInterval, 25);
	}
}

//A continuous loop while there are still objects in the arrFaders array
function FC_FadeInterval(){
	var bDone;
	
	//Perform the fade for each object in the array
	for(var i = 0; i < arrFaders.length; i++){
		if(arrFaders[i] != null){
		    //Perform the fade
			bDone = arrFaders[i].fade();

			if(bDone){
			    var handler = arrFaders[i].handler;  //Get the handler
			    var obj = arrFaders[i].obj; //get the DOM obj associated with the fader obj

				arrFaders[i] = null;
				iFadeObjCount--;
				
				//If there are no objects in arrFaders, destroy fadeInterval
				if(iFadeObjCount == 0){
				    clearInterval(fadeInterval);
				    fadeInterval = null;
				}
				
				//If there was a handler for the fader object, call it now that the fader object is destroyed
				if(handler != null)
				    handler.call(obj);
			}
		}
	}
	
//	//Clean up the array of faders
//	if(arrFaders.length > 25){
//		var arrNew = new Array();
//		for(var j=0; j<arrFaders.length; j++){
//			if(arrFaders[j] != null){
//				arrNew[arrNew.length] = arrFaders[j];
//			}
//		}
//		
//		arrFaders = arrNew;
//	}
	
}
