//Interface javascript pour les popups

function ajouterAuPanierPopup(idCorail,desi,prix){
	//Ajouter au panier
	var resultat = ajouterAuPanier(idCorail,desi,prix);
	var message;
	if(resultat == null){
		message = "<span style=\"color: red\">Le corail <span class=\"desi\">"+desi+"</span> n'a pas pu &ecirc;tre ajout&eacute; à votre commande. Veuillez nous contacter pour commander.</span>";
	}else if(resultat == "ok"){
	     message = "Le corail <span class=\"desi\">"+desi+"</span> a &eacute;t&eacute; ajout&eacute; &agrave; votre commande.";
	}else if(resultat == "ko"){
	    message = "Le corail <span class=\"desi\">"+desi+"</span> n'a pas pu &ecirc;tre ajout&eacute; à votre commande.</span>";
	}else if(resultat == "deja"){
	    message = "Le corail <span class=\"desi\">"+desi+"</span> est d&eacute;j&agrave; pr&eacute;sent dans votre panier.";
    }
	//POPUP PANIER
	var widthImg = '450';
	var heightImg = '170';
	var popupPanier = window.document.getElementById('popupPanier');
	if (!popupPanier){
		//Construction de la popup
		popupPanier = window.document.createElement('div');
		popupPanier.id='popupPanier';
		popupPanier.style.display='none';
		window.document.body.appendChild(popupPanier);
	}
	//Mise à jour du contenu de la popup
	popupPanier.innerHTML='<div class="fermer">'+
	'<a href="#" onclick="document.getElementById(\'popupPanier\').style.display=\'none\';document.getElementById(\'popupMasque\').style.display=\'none\';return false;">'+
	'<img src="images/fermer.png"/>'+
	'</a>'+
	'</div>'+
	'<table class="titre"><tr>'+
	'<td><img src="images/epuisette.jpg"/></td>'+
	'<td>Mon panier</td>'+
	'</tr></table>'+
	'<p>'+message+'</p>'+
	'<table class="boutons"><tr>'+
	'<td><a href="#" onclick="document.getElementById(\'popupPanier\').style.display=\'none\';document.getElementById(\'popupMasque\').style.display=\'none\';return false;" class="continuerAchats">Continuer<br/>mes achats</a></td>'+
	'<td><a href="panier.php" class="terminerCommande">Terminer<br/>ma commande<br/></a></td>'+
	'</tr></table>';
	//POPUP MASQUE
	var popupMasque = window.document.getElementById('popupMasque');
	if (!popupMasque){
		//Construction du div
		popupMasque = window.document.createElement('div');
		popupMasque.id='popupMasque';
		popupMasque.style.display='none';
		window.document.body.appendChild(popupMasque);
	}
	
	//STYLES POPUP MASQUE
	popupMasque.style.position='absolute';
	popupMasque.style.zIndex='99999999';
	popupMasque.style.display='block';
	//STYLES POPUP IMAGE
	popupPanier.style.position='absolute';
	popupPanier.style.zIndex='999999999';
	popupPanier.style.display='block';

	//CALCUL POSITIONS
	calculPopup(popupPanier, popupMasque, widthImg, heightImg);

}
function afficherImgEnPopup(url, titre, widthImg, heightImg){
	//POPUP IMAGE
	var popupImage = window.document.getElementById('popupImage');
	if (!popupImage){
		//Construction de la popup
		popupImage = window.document.createElement('div');
		popupImage.id='popupImage';
		popupImage.style.display='none';
		window.document.body.appendChild(popupImage);
	}
	//Mise à jour du contenu de la popup
	popupImage.innerHTML='<div class="fermer"><a href="#" onclick="document.getElementById(\'popupImage\').style.display=\'none\';document.getElementById(\'popupMasque\').style.display=\'none\';return false;"><img src="images/fermer.png"/></a></div><img src="'+url+'"/><span class="legendeImage">'+titre+'</span>';
	//POPUP MASQUE
	var popupMasque = window.document.getElementById('popupMasque');
	if (!popupMasque){
		//Construction du div
		popupMasque = window.document.createElement('div');
		popupMasque.id='popupMasque';
		popupMasque.style.display='none';
		window.document.body.appendChild(popupMasque);
	}
	
	//STYLES POPUP MASQUE
	popupMasque.style.position='absolute';
	popupMasque.style.zIndex='99999999';
	popupMasque.style.display='block';
	//STYLES POPUP IMAGE
	popupImage.style.position='absolute';
	popupImage.style.zIndex='999999999';
	popupImage.style.display='block';

	//CALCUL POSITIONS
	calculPopup(popupImage, popupMasque, widthImg, heightImg);
}

function calculPopup(popupImage, popupMasque, widthImg, heightImg){
	if(popupImage!=null && popupImage.style.display!="none"){
		//POSITION POPUP MASQUE
		var documentSize = getDocumentSize();
		popupMasque.style.height = documentSize[1] + 'px';
		popupMasque.style.width = documentSize[0] + 'px';
		popupMasque.style.top = '0px';
		popupMasque.style.left = '0px';
		
		//POSITION POPUP IMAGE
		var scrollPos = getScrollXY();	
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
		if(heightImg!="" && heightImg!="0" && widthImg!="" && widthImg!="0"){
			var posHeight = (fullHeight - (parseInt(heightImg))) / 2;
			if(posHeight>0){
				popupImage.style.top = scrollPos[1] + 5 + posHeight + 'px';
			}else{
				popupImage.style.top = scrollPos[1] + 5 + 'px';
			}
			var posWidth = (fullWidth - (parseInt(widthImg))) / 2;
			if(posWidth>0){
				popupImage.style.left = scrollPos[0] + 5 + posWidth + 'px';
			}else{
				popupImage.style.left = scrollPos[0] + 5 + 'px';
			}
		}else{
			popupImage.style.top = (scrollPos[1] + 5) + 'px';
			popupImage.style.left = (scrollPos[0] + 5) + 'px';
		}
	}
}


function getDocumentSize(){
	//Récupération de la fenêtre
	var windowPrincipal = window.top;
	if(windowPrincipal==null){
		windowPrincipal = window;
	}
	var documentPrincipal = windowPrincipal.document;
	var width;
	if(documentPrincipal.documentElement && documentPrincipal.documentElement.scrollWidth){
		width = documentPrincipal.documentElement.scrollWidth;
	}else{ 
		if(documentPrincipal.body.scrollWidth > documentPrincipal.body.offsetWidth){
			width = documentPrincipal.body.scrollWidth;
		}else{
			width = documentPrincipal.body.offsetWidth;
		}
    }
    var height;
    if(documentPrincipal.documentElement && documentPrincipal.documentElement.scrollHeight){
    	height =  documentPrincipal.documentElement.scrollHeight;
    }else{
    	if(documentPrincipal.body.scrollHeight > documentPrincipal.body.offsetHeight){
    		height = documentPrincipal.body.scrollHeight;
    	}else{
    		height = documentPrincipal.body.offsetHeight;
    	}
    }
    
    return new Array(width,height);
}

function getScrollXY() {
	//Récupération de la fenêtre
	var windowPrincipal = window.top;
	if(windowPrincipal==null){
		windowPrincipal = window;
	}
	var documentPrincipal = windowPrincipal.document;
	var scrOfX = 0, scrOfY = 0;
	if( typeof( windowPrincipal.pageYOffset ) == 'number' ) {
	  //Netscape compliant
	  scrOfY = windowPrincipal.pageYOffset;
	  scrOfX = windowPrincipal.pageXOffset;
	} else if( documentPrincipal.body && ( documentPrincipal.body.scrollLeft || documentPrincipal.body.scrollTop ) ) {
	  //DOM compliant
	  scrOfY = documentPrincipal.body.scrollTop;
	  scrOfX = documentPrincipal.body.scrollLeft;
	} else if( documentPrincipal.documentElement && ( documentPrincipal.documentElement.scrollLeft || documentPrincipal.documentElement.scrollTop ) ) {
	  //IE6 standards compliant mode
	  scrOfY = documentPrincipal.documentElement.scrollTop;
	  scrOfX = documentPrincipal.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}

function getViewportHeight() {
	//Récupération de la fenêtre
	var windowPrincipal = window.top;
	if(windowPrincipal==null){
		windowPrincipal = window;
	}
	var documentPrincipal = windowPrincipal.document;
	if (windowPrincipal.innerHeight!=windowPrincipal.undefined) return windowPrincipal.innerHeight;
	if (documentPrincipal.compatMode=='CSS1Compat') return documentPrincipal.documentElement.clientHeight;
	if (documentPrincipal.body) return documentPrincipal.body.clientHeight; 
	return windowPrincipal.undefined; 
}
function getViewportWidth() {
	//Récupération de la fenêtre
	var windowPrincipal = window.top;
	if(windowPrincipal==null){
		windowPrincipal = window;
	}
	var documentPrincipal = windowPrincipal.document;
	if (windowPrincipal.innerWidth!=windowPrincipal.undefined) return windowPrincipal.innerWidth; 
	if (documentPrincipal.compatMode=='CSS1Compat') return documentPrincipal.documentElement.clientWidth; 
	if (documentPrincipal.body) return documentPrincipal.body.clientWidth; 
	return windowPrincipal.undefined; 
}


function getElementSize(elem){
	var width = 0;
	var height = 0;
	if(elem!=null){
		if(elem.scrollWidth > elem.offsetWidth){
			width = elem.scrollWidth;
		}else{
			width = elem.offsetWidth;
		}
		if (width == 0)
		{
			width = parseInt(elem.style.width);
		}
	   	if(elem.scrollHeight > elem.offsetHeight){
	   		height = elem.scrollHeight;
	   	}else{
	   		height = elem.offsetHeight;
	   	}
	   	if (height == 0)
	   	{
			height = parseInt(elem.style.height);
	   	}
	}
    return new Array(width,height);
}

