/* *****************************************************************
// MODAL LAYER I
******************************************************************** */
//fichier contenant les fonctions permettant l'affichage d'un bloc façon popup, sur fond gris opaque

//fonction créant le div d'écran gris
window.onload=function()
{
	return;
    // Pass true to gray out screen, false to ungray
    // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
    // in any order.  Pass only the properties you need to set.
    var zindex = 2;
    var opacity = 50;
    var opaque = 0.5;
    var bgcolor = '#000000';
    var dark = document.getElementById('darkenScreenObject');
    var tbody = document.getElementsByTagName("body")[0];
    if (!dark) 
    {
        // The dark layer doesn't exist, it's never been created.  So we'll
        // create it here and apply some basic styles.
        // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
        
        var tnode = document.createElement('div');           // Create the layer.
                    // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
        window.document.body.appendChild(tnode);                            // Add it to the web page
        dark=document.getElementById('darkenScreenObject');  // Get the object.
    }
    tbody.style.height = '100%';
    dark.style.height= '100%';

    dark.innerText = ' ';
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
	
    dark.style.display = 'none';
    
    //on crée un bloc dans le div d'écran gris
    //il servira de container pour afficher les infos
    if (tbody != null)
    {
		var div_infos = document.createElement('div');
		div_infos.setAttribute('id', 'modal_infos');
		div_infos.className = 'popupstory';
		window.document.body.appendChild(div_infos);

        //on masque l'écran gris       
        if (document.getElementById('darkenScreenObject') != null)
        {
            document.getElementById('darkenScreenObject').style.display = 'none';
            document.getElementById('darkenScreenObject').style.visibility = 'hidden';
        }
    }
}


function SetPopUpCenter(conteneur)
{
	//Supprimer le div conteneur : son contenu sera copié dans le div modal
	var contenu = conteneur.innerHTML;
	conteneur.parentNode.removeChild(conteneur);
	var dark = document.getElementById('darkenScreenObject');
	var modal = document.getElementById('modal_infos');
	if (dark != null && modal != null)
    {
		modal.innerHTML = contenu;
		modal.style.display = 'block';
		modal.style.visibility = 'visible';

		dark.style.display = 'block';
		dark.style.visibility = 'visible';
    }
}

function closePopUp()
{
	var dark = document.getElementById('darkenScreenObject');
	var modal = document.getElementById('modal_infos');

	if (dark != null && modal != null)
	{
		modal.style.display = "none";
		modal.style.visibility = "hidden";

		dark.display = 'none';
		dark.style.visibility = "hidden";
	}
}


/* **************************************************************
// MODAL LAYER II
//Attention, en mode strict (avec un DOCTYPE xhtml strict),
// utiliser document.documentElement
************************************************************** */

function createModal(render, title, close_title, close_img)
{
	var modalBackground = document.getElementById('modalBackground_' + render);
	if(!modalBackground)
	{
		modalBackground = document.createElement('div');
		modalBackground.className = 'modalBackground';
		modalBackground.setAttribute('id', 'modalBackground_' + render);
		var body = document.getElementsByTagName('body')[0];
		body.appendChild(modalBackground);
		body.style.height = '100%';
		
		var zindex = 10000;
		var opacity = 50;
		var opaque = 0.5;
		var bgcolor = '#000000';
		modalBackground.style.overflow='hidden';
		modalBackground.style.display='none';
		modalBackground.style.height= '100%';
		modalBackground.innerText = ' ';
		modalBackground.style.opacity=opaque;                      
		modalBackground.style.MozOpacity=opaque;                   
		modalBackground.style.filter='alpha(opacity='+opacity+')'; 
		modalBackground.style.zIndex=zindex;        
		modalBackground.style.backgroundColor=bgcolor;  
		modalBackground.style.display = 'none';
	}
	
	var modalContainer = document.getElementById('modalContainer_' + render);
	if(!modalContainer)
	{
		var modalTop = document.createElement('div');
		modalTop.className = 'modalTop';
		modalTop.setAttribute('id', 'modalTop_' + render);
		if(title)
		{
			var spanT = document.createElement('span');
			spanT.appendChild(document.createTextNode(title));
			modalTop.appendChild(spanT);
		}
		if(close_title || close_img)
		{
			var modalButtonCloseMe = document.createElement('div');
			modalButtonCloseMe.className = 'modalButtonCloseMe';
			modalButtonCloseMe.setAttribute('id', 'modalButton_' + render + '_close_me');
			if(close_title)
			{
				var spanC = document.createElement('span');
				spanC.appendChild(document.createTextNode(close_title));
				modalButtonCloseMe.appendChild(spanC);
			}
			if(close_img)
			{
				var imgC = document.createElement('img');
				imgC.src = close_img;
				modalButtonCloseMe.appendChild(imgC);
			}
			modalTop.appendChild(modalButtonCloseMe);
		}
	
		var modalBody = document.createElement('div');
		modalBody.className = 'modalBody';
		modalBody.setAttribute('id', 'modalBody_' + render);
	
		modalContainer = document.createElement('div');
		modalContainer.className = 'modalContainer';
		modalContainer.setAttribute('id', 'modalContainer_' + render);
		var body = document.getElementsByTagName('body')[0];
		body.appendChild(modalContainer);
		
		modalContainer.appendChild(modalTop);
		modalContainer.appendChild(modalBody);
	}
	return modalBackground;
}

function revealModal(render, title, close_title, close_img, close)
{
	var layer = document.getElementById('modalBackground_' + render);
	if(!layer)
	{
		//Hack pour récupérer le onclick sur le premier appel (pb IE)
		var send_login = document.getElementById('id_send_login_' + render);
		var fct;
		if(send_login && send_login.onclick)
		{
			fct = send_login.onclick;
		}
		layer = createModal(render, title, close_title, close_img);
		//var body = document.getElementsByTagName('body')[0];
		//body.appendChild(layer);
		var login_form = document.getElementById('user_login_' + render);
		if(login_form)
		{
			var modalBody = document.getElementById('modalBody_' + render);
			if(modalBody)
			{
				var inner_code = login_form.innerHTML;
				login_form.parentNode.removeChild(login_form);
				var div_login = document.createElement('div');
				div_login.className = 'user_login';
				div_login.setAttribute('id', 'user_login_' + render);
				div_login.innerHTML = inner_code;
				modalBody.appendChild(div_login);
				var send_login = document.getElementById('id_send_login_' + render);
				if(typeof fct == 'function')
				{
					send_login.onclick = fct;
				}
			}
		}
	}
	var modalContainer = document.getElementById('modalContainer_' + render);
	if(modalContainer)
	{
		modalContainer.style.display = "block";
		modalContainer.style.visibility = 'visible';
	}
	if(layer)
	{
		layer.style.display = 'block';
		layer.style.visibility = 'visible';
		var node = layer;
		if(document.getElementById('modalButton_' + render + '_close_me'))
		{
			node = document.getElementById('modalButton_' + render + '_close_me');
		}
		else if(document.getElementById('modalTop_' + render))
		{
			node = document.getElementById('modalTop_' + render);
		}
		if(typeof close == 'function')
		{
			node.onclick = close;
		}
		else
		{
			node.onclick = function(){
				hideModal(render);
			};
		}
	}
	//Désactiver le scroll
	document.documentElement.style.overflow = 'hidden';
}

function hideModal(render)
{
	var modalBackground = document.getElementById('modalBackground_' + render);
	if(modalBackground)
	{
	    modalBackground.style.display = 'none';
	    modalBackground.style.visibility = 'hidden';
	}
	var modalContainer = document.getElementById('modalContainer_' + render);
	if(modalContainer)
	{
	    modalContainer.style.display = 'none';
	    modalContainer.style.visibility = 'hidden';
	}
	//Réactiver le scroll
	document.documentElement.style.overflow = 'auto';
}