if(!window.SVFX_dynamicWindow) SVFX_dynamicWindow = new Object();


//*************************************************************************
//		Paramètres
//*************************************************************************
SVFX_dynamicWindow.displayCache = true;
SVFX_dynamicWindow.cacheOk = false;
SVFX_dynamicWindow.alphaCache = 0;
SVFX_dynamicWindow.alphaCacheMax = 70;
SVFX_dynamicWindow.alphaCacheSpeed = 10;
SVFX_dynamicWindow.ajaxDivs = new Array();
SVFX_dynamicWindow.ajaxDivsHeight = new Array();
SVFX_dynamicWindow.divList = new Array();
SVFX_dynamicWindow.divNum = new Array();
SVFX_dynamicWindow.displaying = false;
SVFX_dynamicWindow.currentDivId = '';
SVFX_dynamicWindow.resizeIncrement = 30;
SVFX_dynamicWindow.resizeSpeed = 10;
SVFX_dynamicWindow.resizeTemp = 12;
SVFX_dynamicWindow.isIE = false;
SVFX_dynamicWindow.srcImageLoader = 'Img_clicpostal/Img_site/Lightbox/loader.gif';
SVFX_dynamicWindow.colorCache = '000000';
if(navigator.appName.indexOf("Microsoft Internet Explorer") != -1) SVFX_dynamicWindow.isIE = true;



//*************************************************************************
//		Fonctions secondaires
//*************************************************************************
SVFX_dynamicWindow.empty = function(test){
	if(test == undefined || test == 'undefined' || test == 0 || test == '' || test == false || test == 'false') return true;
	else return false
}

SVFX_dynamicWindow.getScrollPosition = function(){
	return Array((document.documentElement && document.documentElement.scrollLeft) || window.pageXOffset || self.pageXOffset || document.body.scrollLeft,(document.documentElement && document.documentElement.scrollTop) || window.pageYOffset || self.pageYOffset || document.body.scrollTop);
}

SVFX_dynamicWindow.jsInit = function(){
	//
}

SVFX_dynamicWindow.jsEnd = function(){
	//
}


SVFX_dynamicWindow.getPageHeight = function(){
	var offset = document.documentElement.scrollHeight;
	if ( document.documentElement.clientHeight > offset ) offset = document.documentElement.clientHeight;
	if ( document.body.scrollHeight > offset ) offset = document.body.scrollHeight;
	
	/*
	for(var i=0; i<this.ajaxDivsHeight.length; i++){
		offset -= this.ajaxDivsHeight[i];
		offset += parseInt(document.getElementById(this.ajaxDivs[i]).offsetHeight);
	}
	*/
	return offset;
}


//-------------------------------------------------------------------------
//		Ajoute des elements
//-------------------------------------------------------------------------
SVFX_dynamicWindow.add = function(id,width,height,cache,anim){
	var newDiv = new Array();
	newDiv['id'] = id;
	newDiv['width'] = width;
	newDiv['height'] = height;
	newDiv['cache'] = cache;
	newDiv['anim'] = anim;
	SVFX_dynamicWindow.divList[id] = newDiv;
}



SVFX_dynamicWindow.addAjaxContent = function(id){
	SVFX_dynamicWindow.ajaxDivs.push(id);
}

//-------------------------------------------------------------------------
//		Affiche le div
//-------------------------------------------------------------------------
SVFX_dynamicWindow.display = function(id,fctInit,fctEnd){
	var id = id;
	this.jsInit = fctInit;
	this.jsEnd = fctEnd;

	if(this.displaying == false && !this.empty(this.divList[id]['id'])){
		
		//Mémorise l'action
		this.displaying = true;
		this.currentDivId = id;
		
		//Récupère la hauteur du scroll
		var width = document.body.offsetWidth;
		var posHeight = 0;
		var db=!document.body.width?document.documentElement:document.body;
		posHeight = db.scrollTop;
		if(posHeight == 0) posHeight = document.body.scrollTop;
		
		//Initialisation du cache
		var H = this.getPageHeight();
		var cache = document.getElementById("SVFX_dynamicWindow_cache");
		cache.style.height = H+'px';
		this.alphaCache = 0;
		this.cacheOk = false;
		
		//Ajout l'element
		var div = document.createElement('DIV');
		div.setAttribute('id',id);
		div.style.position = 'absolute';
		div.style.top = parseInt(posHeight + 20) + 'px';
		div.style.left = '50%';
		div.style.width = this.divList[id]['width']+'px';
		div.style.marginLeft = '-'+(this.divList[id]['width'] / 2)+'px';
		div.style.height = '50px';
		div.style.overflow = 'auto';
		div.style.backgroundColor = '#ffffff';
		div.style.border = '1px Solid #000000';
		div.style.display = 'block';
		div.style.zIndex = '10001';
		div.innerHTML = '<img src="'+this.srcImageLoader+'" style="position:absolute;top:50%;left:50%;margin-left:-12px;margin-top:-12px;" />';
		document.body.appendChild(div);
		
		//Lance l'animation
		if(this.divList[id]['cache'] == true){
			this.displayCache();
		}
		else{
			this.cacheOk = true;
		}
		
		this.waitCache();
	}
}



//*************************************************************************
//		Fonctions Animations
//*************************************************************************



//-------------------------------------------------------------------------
//		Ferme le div
//-------------------------------------------------------------------------
SVFX_dynamicWindow.sleep = function(){
	this.hideCache();
	var div = document.getElementById(this.currentDivId);
	div.style.display = 'none';
	document.body.removeChild(div);
	this.jsEnd();
}



//-------------------------------------------------------------------------
//		Anime le redimensionnement du bloc
//-------------------------------------------------------------------------
SVFX_dynamicWindow.resizeBlocAnimation = function(H){
	
	var div = document.getElementById(this.currentDivId);
	//Récupère la hauteur du bloc
	if(H == undefined) H = div.offsetHeight;
	var maxH = this.divList[this.currentDivId]['height'];
	var newH = parseInt(H) + parseInt(this.resizeIncrement);
	
	var again = true;
	if(newH > maxH){
		newH = maxH;
		again = false;
	}
	
	//Affecte les nouvelles dimensions
	div.style.height = newH + 'px';
	
	//Relance l'animation si le redimensionnement n'est pas fini
	if(again) setTimeout(function(){ SVFX_dynamicWindow.resizeBlocAnimation(newH); },SVFX_dynamicWindow.resizeSpeed);
	else{
		this.jsInit();
	}
}



//-------------------------------------------------------------------------
//		Attend la fin de l'animation du cache
//-------------------------------------------------------------------------
SVFX_dynamicWindow.waitCache = function(){
	//Attend que l'animation du cache soit fini avant de lancer celle du redimentionnement du bloc
	if(this.cacheOk == true){
		if(this.divList[this.currentDivId]['anim'] == true){
			this.resizeBlocAnimation();
		}
		else{
			document.getElementById(this.currentDivId).style.height = this.divList[this.currentDivId]['height']+'px';
			this.jsInit();
		}
	}
	else setTimeout(function(){ SVFX_dynamicWindow.waitCache(); },100);
}



//-------------------------------------------------------------------------
//		Affecte une opacité à un bloc
//-------------------------------------------------------------------------
SVFX_dynamicWindow.setOpacity = function(elt,op){
	if(this.isIE) elt.style.filter = "alpha(opacity="+op+")";
	else{
		var opacite = (op / 100);
		elt.style.setProperty( "-moz-opacity",opacite , "");
		elt.style.setProperty( "-khtml-opacity", opacite, "");
		elt.style.setProperty( "opacity",opacite , "");
	}
}


//-------------------------------------------------------------------------
//		Anime l'affichage du cache
//-------------------------------------------------------------------------
SVFX_dynamicWindow.displayCache = function(){
	var relance = false;
	if(this.alphaCache < this.alphaCacheMax){
		this.alphaCache += this.alphaCacheSpeed;
		relance = true;
	}
	else this.alphaCache = this.alphaCacheMax;
	
	var elt = document.getElementById('SVFX_dynamicWindow_cache');
	this.setOpacity(elt,this.alphaCache);
	elt.style.display = 'block';
	
	if(relance) setTimeout("SVFX_dynamicWindow.displayCache()",10);
	else{
		elt.onclick = function(){ SVFX_dynamicWindow.sleep(); }
		this.cacheOk = true;
	}
}


//-------------------------------------------------------------------------
//		Anime le masquage du cache
//-------------------------------------------------------------------------
SVFX_dynamicWindow.hideCache = function(){
	this.cacheOk = false;
	var relance = false;
	if(this.alphaCache > 0){
		this.alphaCache -= this.alphaCacheSpeed;
		relance = true;
	}
	else this.alphaCache = 0;
	
	var elt = document.getElementById('SVFX_dynamicWindow_cache');
	this.setOpacity(elt,this.alphaCache);
	
	if(relance) setTimeout("SVFX_dynamicWindow.hideCache()",33);
	else{
		elt.style.height = '0px';
		elt.onclick = null;
		this.displaying = false;
	}
}


//-------------------------------------------------------------------------
//		Initialise l'objet
//-------------------------------------------------------------------------
SVFX_dynamicWindow.init = function(){
	
	//Recherche les div ajax
	for(var i=0; i<SVFX_dynamicWindow.ajaxDivs.length; i++){
		var elt = document.getElementById(SVFX_dynamicWindow.ajaxDivs[i]);
		SVFX_dynamicWindow.ajaxDivsHeight[i] = elt.offsetHeight;
	}
	
	//Créé l'élément cache
	if(SVFX_dynamicWindow.empty(document.getElementById('SVFX_dynamicWindow_cache'))){
		var width = document.body.offsetWidth;
		var cache = document.createElement("DIV");
		cache.setAttribute("id","SVFX_dynamicWindow_cache");
		cache.style.backgroundColor = '#'+SVFX_dynamicWindow.colorCache;
		cache.style.position = 'absolute';
		cache.style.top = '0px';
		cache.style.left = '0px';
		cache.style.right = '0px';
		//cache.style.bottom = '0px';
		//cache.style.width = width+'px';
		//cache.style.height = '0px';
		cache.style.zIndex = '10000';
		cache.style.display = 'none';
		document.body.appendChild(cache);
	
		var elt = document.getElementById('SVFX_dynamicWindow_cache');
		SVFX_dynamicWindow.setOpacity(elt,0);
	}
}


//---------------------------------------------------------------
//   Appel de la function au chargement de la page
//---------------------------------------------------------------
if(Event.domReady) Event.domReady.add(SVFX_dynamicWindow.init);
else{
	var fn = window.onload;
	window.onload = function(){
		SVFX_dynamicWindow.init();
		fn();
	}
}
