/***
 * opacityWindow Class v1.0 (03/2007)
 * Author : Bob Gaudaen
***/

// Définition de la classe :
function opacityWindow( id, initObj )
{
	// Init Vars ------------//
	this.containerId = "windowContainer";
	this.hideSelectId = "hideSelect";
	this.overLayerId = "overLayer";
	this.myWindowId = "myWindow";
	this.container;
	this.hideSelect;
	this.overLayer;
	this.myWindow;
	
	// Init Class -----------//
	this.containerId = ( id !== undefined ) ?  id : "windowContainer";
	
	var initObj, _startAlpha, _alpha, _bgColor, _marginTop, _marginLeft, _center;
	with( initObj )
	{
		this.startAlpha = ( _startAlpha !== undefined ) ?  _startAlpha : 0;
		this.alpha = ( _alpha !== undefined ) ?  _alpha : 85;
		this.bgColor = ( _bgColor !== undefined ) ?  _bgColor : '#000000';
		this.marginTop = ( _marginTop !== undefined ) ?  _marginTop : 0;
		this.marginLeft = ( _marginLeft !== undefined ) ?  _marginLeft : 0;
		this.centered = ( _center !== undefined ) ?  _center : true;
	}
	this.myBody = document.getElementsByTagName( "body" )[0];
	document.opacityWindow = this;
	
	// Methods ---------------//
	// Initialisation de la fenetre (creation des elements DOM)
	this.Init = function()
	{
		// Create elements :
		this.container = document.createElement( 'div' );
		this.hideSelect = document.createElement( 'iframe' );
		this.overLayer = document.createElement( 'div' );
		this.myWindow = document.createElement( 'iframe' );
		
		// Set element props :
		this.container.id = this.containerId;
		this.overLayer.id = this.overLayerId;
		this.hideSelect.id = this.hideSelectId;
		this.overLayer.style.backgroundColor = this.bgColor;
		this.myWindow.id = this.myWindowId;
		this.myWindow.style.display = 'none';
		this.myWindow.frameBorder = 0;
		this.myWindow.scrolling = 'no';
		
		// Append :
		this.container.appendChild( this.hideSelect );
		this.container.appendChild( this.overLayer );
		this.container.appendChild( this.myWindow );
	}
	
	// Parametrage de la fenetre :
	this.setWindowParams = function( _width, _height, src )
	{
		this.setWindowSize( _width, _height );
		this.myWindow.src = src;
	}
	
	// Ajuste la taille de la fenetre :
	this.setWindowSize = function( _width, _height )
	{
		with( this.myWindow.style )
		{
			width = _width + "px";
			height = _height + "px";
			marginLeft = ( - (_width/2) + this.marginLeft ) + "px";
			if( this.centered )
			{
				top = "50%";
				marginTop = ( - (_height/2) + this.marginTop ) + "px";
			}
			else
			{
				top = this.marginTop + "px";
			}
		}
	}
	
	// Affiche / masque la fenetre :
	this.showWindow = function()
	{
		if( this.myWindow.style.display == 'none' )
		{
			this.myBody.appendChild( this.container );
			this.myWindow.style.display = '';
			this.fadeIn();
		}
	}
	this.hideWindow = function()
	{
		this.myWindow.style.display = 'none';
		this.fadeOut();
	}
	
	// Fonctions fadeIn / fadeOut :
	this.fadeIn = function()
	{
		this.opacity( this.overLayer.id, this.startAlpha, this.alpha, 700 );
	}
	this.onFadeInEnd = function()
	{
	}
	
	this.fadeOut = function()
	{
		this.opacity( this.overLayer.id, this.alpha, 0, 700 );
	}
	this.onFadeOutEnd = function()
	{
		this.myBody.removeChild( this.container );
	}
	// Fin fonctions fadeIn / fadeOut
	
	this.opacity = function( id, opacStart, opacEnd, millisec )
	{
		var speed = Math.round(millisec / 100);
		var timer = 0;
		
		if(opacStart > opacEnd)
		{
			for( i = opacStart; i >= opacEnd; i-- )
			{
				if( i == opacEnd )
				{
					setTimeout("document.opacityWindow.changeOpacity('" + id + "', " + i + ", 'OUT')",(timer * speed));
				}
				else
				{
					setTimeout("document.opacityWindow.changeOpacity('" + id + "', " + i + ")",(timer * speed));
				}
				timer++;
			}
		}
		else if( opacStart < opacEnd )
		{
			for(i = opacStart; i <= opacEnd; i++)
			{
				if( i == opacEnd )
				{
					setTimeout("document.opacityWindow.changeOpacity('" + id + "', " + i + ", 'IN')",(timer * speed));
				}
				else
				{
					setTimeout("document.opacityWindow.changeOpacity('" + id + "', " + i + ")",(timer * speed));
				}
				timer++;
			}
		}
		else
		{
			this.changeOpacity( this.overLayer.id, opacEnd );
		}
	}
	
	// Change l'opacité dun element html :
	this.changeOpacity = function( id, _opacity, callBack )
	{
		with( document.getElementById( id ).style )
		{
			opacity = (_opacity / 100);
			MozOpacity = (_opacity / 100);
			KhtmlOpacity = (_opacity / 100);
			filter = "alpha(opacity=" + _opacity + ")";
		}
		if( callBack !== undefined )
		{
			if( callBack == 'IN' )
			{
				this.onFadeInEnd();
			}
			else
			{
				this.onFadeOutEnd();
			}
		}
	}
	
	// Init :
	this.Init();
}
