
// La hauteur par défaut de la boite d'alerte. On la renseigne lors de l'initalisation du script
var modal_style_height = 0;

// Définir la largeur de la boite dans le CSS fait bugger, donc on la défini ici.
var modal_wanted_width = 505;

// La largeur de la boite actuelle.
var modal_current_width = 0;

// La largeur de la page
function modalGetPageWidth() {
	
	var w = 0;
	
    if( window.innerWidth && window.scrollMaxX ) // Firefox 
    {
		w = window.innerWidth + window.scrollMaxX;
	}
	else if( document.body.scrollWidth > document.body.offsetWidth ) // all but Explorer Mac
	{

	w = document.body.scrollWidth;
	}
	else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
	{ 
		w = document.body.offsetWidth + document.body.offsetLeft; 
	}
    
    return w;
	
}

// La hauteur de la page
function modalGetPageHeight() {
	
	var h = 0;
	
    if( window.innerHeight && window.scrollMaxY ) // Firefox 
    {
		h = window.innerHeight + window.scrollMaxY;
	}
	else if( document.body.scrollHeight > document.body.offsetHeight ) // all but Explorer Mac
	{
	h = document.body.scrollHeight;
	}
	else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
	{ 
		h = document.body.offsetHeight + document.body.offsetTop; 
	}
    
    return h;
	
}

// Afficher la boite selon un contenu HTML (content), une largeur (width) qui peut être nulle pour dire qu'on veut celle par défaut, et de même pour la hauteur (height)
function showmodal(content, width, height) {
    
    var actualposition = document.documentElement.scrollTop;
    var modbox = document.getElementById('modbox');
    var shad = document.getElementById("shad");
    
    modbox.innerHTML = content;
    
    if (width > 0) {
    	
    	modbox.style.width = width;
    	document.getElementById('modbox').style.left = ((modalGetPageWidth()/2) - (width / 2)) +'px';
    	modal_current_width = width;
    	
    } else {
    	modbox.style.width = modal_wanted_width;
    	modal_current_width = modal_wanted_width;
    	document.getElementById('modbox').style.left = ((modalGetPageWidth()/2) - (modal_wanted_width / 2)) +'px';
    	
    }

    if (height > 0) {
    	
    	modbox.style.height = height;
    	
    }
    
    shad.style.display = "block";
    modbox.style.display = "block";
    
    
    window.scrollTo(0, actualposition);
}

// Fermer la boite de dialogue
function closemodal() {
    var modbox = document.getElementById("modbox");
    var shad = document.getElementById("shad");
    
    modbox.style.marginLeft = "";
    modbox.value = '';
    shad.style.display = "none";
    modbox.style.display = "none";
    modbox.style.width = modal_wanted_width;
    modbox.style.height = modal_style_height;
    
    document.body.style.overflow = "";
    document.getElementsByTagName("html")[0].style.overflow = "";
}

// On initialise le script
function initModal() {
	
	// Creation of the opacity div, so the page darkens when a modal is displayed
	var modal_div_background = document.createElement('div');
	modal_div_background.id = 'shad';
	modal_div_background.style.display='none';
	
	document.getElementsByTagName('body')[0].appendChild(modal_div_background);
	
	// Creation of the modal itself
	var modal_div_itself = document.createElement('div');
	modal_div_itself.id = 'modbox';
	modal_div_itself.style.display='none';
	
	document.getElementsByTagName('body')[0].appendChild(modal_div_itself);
	
	modal_style_height = document.getElementById('modbox').style.height;
	
	resizeModalDivs();
	
}

// On redimensionne et repositionne les différents calques
function resizeModalDivs() {
	
	document.getElementById('shad').style.width = modalGetPageWidth()+'px';
	document.getElementById('shad').style.height = modalGetPageHeight()+'px';
	document.getElementById('modbox').style.left = ((modalGetPageWidth()/2) - (modal_current_width / 2)) +'px';
	document.getElementById('modbox').style.top = '300px';
	
	
}

// Au chargement de la page, on initialise le script
window.onload = initModal;

// On redimensionne et repositionne les différents divs quand on touche aux dimensions de la fenêtre
window.onresize = resizeModalDivs;