﻿// Fonctions de Fade in & out
// Utilisez l'objet window.fader pour contrôler les fades sur un objet donné.

// Pour démarrer le fade:       window.fader.start("objectName", "in"/"out", maxOpacity, duration(ms) [, "cssType"]);
// Pour interrompre le faide:   window.fader.stop("objectName");

// Lors d'un start, si un fade était déjà en cours pour cet objet, il sera annulé.



// Fonctions de Fade in & out
    var Fader = function () {
	    if(this == window || this == document) {alert("MessageBox is a constructor function!"); return;}
    	
        this.stepDelay = 40;
        this.fades = new Object(); // Fades en cours
        
       this.validateDHTML = function() {
			try{
				if(setStyleProp) return true;
			} catch(e){}
			if(!dhtmlFound) {
				alert("Fader requires dhtml.js. Please add it prior to initialization.");
				return false;
			}
		 }
        
	    this.start = function(objectName, direction, maxOpacity, duration, cssType) {
			  if(!this.validateDHTML()) return;
	        if(this.fades[objectName]) this.stop(objectName);
	        if(cssType == undefined) cssType = "block";
    	    
	        this.fades[objectName] = {direction: (direction == "out" ? -1 : 1), maxOpacity: maxOpacity, duration: duration, curOpacity: (direction == "out" ? maxOpacity : 0), startTime: new Date(), cssType: cssType};
            
            setStyleProp(objectName, "display", cssType);
	        this.setOpacity(objectName, this.fades[objectName].curOpacity);
    	    
	        this.fades[objectName].interval = setInterval("window.fader.step('" + objectName + "');", this.stepDelay);
	    }
	
        this.stop = function(objectName) {
            if(this.fades[objectName]) {
                if(this.fades[objectName].direction == 1) {
	                // Fin fade-in
                    this.setOpacity(objectName, this.fades[objectName].maxOpacity);
                } else {
	                // Fin fade-out
                    this.setOpacity(objectName, 0);
                    setStyleProp(objectName, "display", "none");
                }
                clearInterval(this.fades[objectName].interval);
                delete this.fades[objectName];
            }
        }
	
	    this.step = function(objectName) {
	        var remaingingTime = this.fades[objectName].duration - ((new Date()) - this.fades[objectName].startTime);
	        var stp = (this.fades[objectName].maxOpacity / remaingingTime) * this.stepDelay;
	        if(this.fades[objectName].direction == -1) stp = 0 - stp;
    	    
	        this.fades[objectName].curOpacity = this.fades[objectName].curOpacity + stp;
	        if((this.fades[objectName].direction == 1 && this.fades[objectName].curOpacity >= this.fades[objectName].maxOpacity) || (this.fades[objectName].direction == -1 && this.fades[objectName].curOpacity <= 0) || remaingingTime <= 0) {
	            // Fin du fade
	            this.stop(objectName);
	        } else {
	            this.setOpacity(objectName, this.fades[objectName].curOpacity);
	        }
	    }
	
        this.setOpacity = function(objectName, opacity) {
            var obj = document.getElementById(objectName);
            if(obj) {
                try {
                    obj.style.opacity = (opacity / 100);  
                    obj.style.MozOpacity = (opacity / 100);  
                    obj.style.KhtmlOpacity = (opacity / 100);  
                    obj.style.filter = "alpha(opacity=" + opacity + ")";
                } catch(e) {
                    alert("Erreur dans setOpacity, fade interrompu: " + e.description);
                    clearInterval(this.fades[objectName].interval);
                    delete this.fades[objectName];
                }
            }
        }
    }
    
    window.fader = new Fader();