// 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 = coef*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;
	var pageHeight;
	
	if(window.innerHeight) {
		pageHeight = window.innerHeight;
	
	}else{
		pageHeight = document.documentElement.clientHeight;
	
	} // end if
	
	if(height > pageHeight-MARGIN){
		coef = (pageHeight-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){
	var pageHeight;
	
	if(window.innerHeight) {
		pageHeight = window.innerHeight;

	}else{
		pageHeight = document.documentElement.clientHeight;

	} // end if
	
	COEFREDUCTION = calculeCoef(width, height);
	PHOTOWIDTH = COEFREDUCTION*width;
	PHOTOHEIGHT = COEFREDUCTION*height;
	
	var image;
	var array_url = url.split(".");

	if(array_url[array_url.length-1].toLowerCase() == "png"){
		image = '<img src="'+url+'" width="0" height="'+PHOTOHEIGHT+'" id="image" style="padding:'+PADDING+'px" />'; //document.createElement("img");
	
	}else{
		image = '<img src="'+url+'" width="0" height="'+PHOTOHEIGHT+'" id="image" style="padding:'+PADDING+'px;background:#FFFFFF" />'; //document.createElement("img");
		
	} // end if
	
	divPhoto = document.createElement("div");
	divPhoto.id = "divPhoto";
	divPhoto.style.position = "fixed";
	divPhoto.style.zIndex = 101;
	divPhoto.style.textAlign = "center";
	divPhoto.style.top = (pageHeight-PHOTOHEIGHT-MARGIN)/2 + "px";
	divPhoto.style.left = (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 = pageHeight + "px";
	divFullScreen.style.width = document.documentElement.scrollWidth + "px";
	divFullScreen.style.position = "fixed";
	divFullScreen.style.zIndex = 100;
	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")){
		var pageHeight;
	
		if(window.innerHeight) {
			pageHeight = window.innerHeight;
		
		}else{
			pageHeight = document.documentElement.clientHeight;
		
		} // end if
	
		document.getElementById("divFullScreen").style.height = pageHeight + "px";
		document.getElementById("divFullScreen").style.width = document.documentElement.scrollWidth + "px";
		
		document.getElementById("divPhoto").style.top = (pageHeight-PHOTOHEIGHT-MARGIN)/2 + "px";
		document.getElementById("divPhoto").style.left = (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()
