// JavaScript Document

//javascript: displayPhoto("fond.jpg", 480, 300);

/* -------------------------- Déclaration des variables ---------------------------- */
var PHOTOHEIGHT = 0;
var PHOTOWIDTH = 0;
var tween = 0;
var PADDING = 10;
var MARGIN = 60;
var COEFREDUCTION = 1;
var SPEED = 100;
var divFullScreen;
var divPhoto;


/* -------------------------- Function calculeCoef ---------------------------- */
// Calcule du coeffcient de reduction total


function calculeCoef(width, height){
	var coef = 1;
	
	if(width > height){
		coef = testWidth(width); // 0.71 => 71%
		coef = coef*testHeight(height*coef);
		
	}else{
		coef = testHeight(height);
		coef = COEFREDUCTION*testWidth(width*coef);
		
	} // end if	
	
	return coef;
	
} // end function calculeCoef


/* -------------------------- Function testWidth ---------------------------- */
// Calcule le coefficient reducteur de la largeur


function testWidth(width){
	var coef;
	
	if(width > document.documentElement.clientWidth-MARGIN){
		coef = (document.documentElement.clientWidth-MARGIN)/width;
	
	}else{
		coef = 1;
		
	} // end if
	
	return coef;
	
} // end function testWidth

/* -------------------------- Function testHeight ---------------------------- */
// Calcule le coefficient reducteur de la hauteur

function testHeight(height){
	var coef;
	
	if(height > document.documentElement.clientHeight-MARGIN){
		coef = (document.documentElement.clientHeight-MARGIN)/height;
	
	}else{
		coef = 1;
		
	} // end if
	
	return coef; // 0.94 => 94%
	
} // end function testHeight


/* -------------------------- Function DisplayPhoto ---------------------------- */
// Récupération des variables URL, Longueurs et hauteurs des photos à agrandir

function displayPhoto(url, width, height){
	COEFREDUCTION = calculeCoef(width, height);
	PHOTOWIDTH = COEFREDUCTION*width;
	PHOTOHEIGHT = COEFREDUCTION*height;

	var image = '<img src="'+url+'" width="0" height="'+PHOTOHEIGHT+'" id="image" style="padding:'+PADDING+'px;background:#FFFFFF" />'; //document.createElement("img");
	
	divPhoto = document.createElement("div");
	divPhoto.id = "divPhoto";
	divPhoto.style.position = "absolute";
	divPhoto.style.zIndex = 4;
	divPhoto.style.textAlign = "center";
	
	divPhoto.style.top = Math.round((getScrollingPosition()[0]+(document.documentElement.clientHeight-PHOTOHEIGHT-PADDING)/2)) + "px";
	divPhoto.style.left = (getScrollingPosition()[1]+(document.documentElement.clientWidth-PHOTOWIDTH-PADDING)/2) + "px";
	divPhoto.style.width = (PHOTOWIDTH+PADDING*2) + "px";
	divPhoto.innerHTML = image;
	
	divFullScreen = document.createElement("div");
	divFullScreen.id = "divFullScreen";
	divFullScreen.style.background = "url(ressources/img/fondDivFullScreen.png)";
	divFullScreen.style.height = document.documentElement.scrollHeight + "px";
	divFullScreen.style.width = document.documentElement.scrollWidth + "px";
	divFullScreen.style.position = "absolute";
	divFullScreen.style.zIndex = 3;
	divFullScreen.style.top = "0px";
	divFullScreen.style.left = "0px";
	
	document.body.appendChild(divFullScreen);
	document.body.appendChild(divPhoto);
	
	tween = setInterval(setSize,5);
	
} // end function displayPhoto

/* -------------------------- Function getScrollingPosition ---------------------------- */
// Récupération de la position du scroll Y

function getScrollingPosition(){
	var scrollY = [0, 0];
	
	if (typeof window.pageYOffset != 'undefined'){
		position = [window.pageYOffset, window.pageXOffset];
	
	}else if(typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0){
		position = [document.documentElement.scrollTop, document.documentElement.scrollLeft];
	
	}else if (typeof document.body.scrollTop != 'undefined'){
		position = [document.body.scrollTop, document.body.scrollLeft];
	
	} // end if
	
	return position;
	
} // end function getScrollingPosition


/* -------------------------- Function resizeDivPhoto ---------------------------- */
// Positionnement des photos au centre de l'écran en fonction de la largeur et hauteur de la photo

function resizeDivPhoto(){
	if(document.getElementById("divFullScreen")){
		document.getElementById("divFullScreen").style.height = document.documentElement.scrollHeight + "px";
		document.getElementById("divFullScreen").style.width = document.documentElement.scrollWidth + "px";
		
		document.getElementById("divPhoto").style.top = (getScrollingPosition()[0]+(document.documentElement.clientHeight-PHOTOHEIGHT-PADDING)/2) + "px";
		document.getElementById("divPhoto").style.left = (getScrollingPosition()[1]+(document.documentElement.clientWidth-PHOTOWIDTH-PADDING)/2) + "px";
	
	} // end if
} // end function resizeDivPhoto


/* -------------------------- Function setSize ---------------------------- */
// Affichage de la photo

function setSize(){
	document.getElementById("image").style.width = (document.getElementById("image").offsetWidth+SPEED)+"px";
	
	if(document.getElementById("image").offsetWidth >= PHOTOWIDTH){
		document.getElementById("image").style.width = PHOTOWIDTH+"px";
		document.body.onclick = closeWindows;
		clearInterval(tween);
		
	} // end if
} // end function setSize()


/* -------------------------- Function closeWindows ---------------------------- */
// Destruction de la div après clic à l'écran

function closeWindows(){
	if(window.document.getElementById("divFullScreen")){
		document.body.removeChild(divFullScreen);
		document.body.removeChild(divPhoto);
		document.body.onclick = "";
	
	} // end if
} // end function closeWindows()
