var hiderId = 'DB_hider';
var bigImgId = 'DB_bigImg';
var imgLoadingId = 'DB_imgLoading';
var texteClose = 'Fermer';
var imgLoadingSrc = 'images/loading.gif'

var hauteurFenetre;
var largeurFenetre;
var firstTime = true;
//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
function showDarkbox(el) {
	var elt = el.firstChild;
	var img = el.href;
	var e = document.getElementById(bigImgId);
	var sizes = getPageSize();
	var innerWidth = sizes[0];
	var innerHeight = sizes[1];
	var hider = document.getElementById(hiderId);
	var imgLoading = document.getElementById(imgLoadingId);
	
	// Mise en place du hider
	hider.style.height = innerHeight + "px";
	hider.style.width = innerWidth + "px";
	hider.style.display = 'block';
	imgLoading.style.top = ((sizes[3] / 2)+document.documentElement.scrollTop)+'px';
	imgLoading.style.left = (sizes[2] / 2)+'px';

	imgPreload = new Image();
	imgPreload.onload = function(){
		// Préparation du bigImg
		if(el.title) { var title = el.title; } else { var title = ''; }
		e.innerHTML = "<img src='"+img+"' id='DB_img' /><p>"+title+"</p><a style='cursor: pointer;'>"+texteClose+"</a>";

		// Affichage du bigImg
		e.style.display = "block";

		// Centrage de l'image
		lleft = (innerWidth / 2) - (e.clientWidth / 2); e.style.left = lleft + 'px';
		ttop = document.documentElement.scrollTop + (hauteurFenetre / 2) - (e.clientHeight / 2); e.style.top = ttop + 'px';

		// On repasse tout ca au premier plan
		e.style.zIndex = '15';
		
		// Cache les selects
		selects = document.getElementsByTagName("select");
	    for (i = 0; i != selects.length; i++) {
	            selects[i].style.visibility = "hidden";
	    }
	}
	imgPreload.src = img;
	return false;
}
function revealAll() {
	document.getElementById(bigImgId).style.display = 'none';
	document.getElementById(bigImgId).style.zIndex = '-15';
	document.getElementById(hiderId).style.display = 'none';
	
	// rend les select a nouveau visibles
	selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}
function initDarkbox(evenement) {
	if(firstTime) {
		sizes = getPageSize();
		largeurFenetre = sizes[2];
		hauteurFenetre = sizes[3];
		firstTime = false;
	}
	document.body.innerHTML = 
		'<div id="DB_hider" onclick="revealAll()"><img src="'+imgLoadingSrc+'" id="DB_imgLoading" /></div>'
		+'<div id="DB_bigImg" onclick="revealAll();">&nbsp;</div>'
		+document.body.innerHTML;
	if (!document.getElementsByTagName){ return; }
	
	var anchors = document.getElementsByTagName("a");
	var myImages = Array();
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		var myImage = myImages[i];
		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "darkbox")) {
			// Attibution du onclik aux liens concernés
			anchor.onclick = function () { return showDarkbox(this); }
			// Préchargement des images pouvant l'etre
			myImage = new Image();
			myImage.src = anchor.getAttribute("href");
		}
	}
}
window.onload = initDarkbox;