function PhotoPreview() {
}

PhotoPreview.init = function() {
	PhotoPreview.popup = document.getElementById("popup");
	var popupAnchor = PhotoPreview.popup.getElementsByTagName("a");
	popupAnchor[0].onclick = PhotoPreview.closeHigh;
	
	var preview = new Array();
	if (document.getElementById("main_content_onglets")) {
		preview = document.getElementById("main_content_onglets").getElementsByTagName("img");
	} else if (document.getElementById("main_content")){
		preview = document.getElementById("main_content").getElementsByTagName("img");
	}
	for (var i = 0; i < preview.length; i ++) {
		preview[i].onclick = PhotoPreview.openHigh;
	}
};

PhotoPreview.openHigh = function(e) {
	PhotoPreview.current = this.parentNode.parentNode;
	PhotoPreview.popup.className = "none";
	PhotoPreview.popup.style.top = getY(this) + "px";
	PhotoPreview.popup.style.left = "150px";
	PhotoPreview.legend = PhotoPreview.current.getElementsByTagName("p").item(0).innerHTML;
	
	PhotoPreview.legendHigh = document.createElement("p");
	PhotoPreview.legendHigh.className = "sans_indent";
	PhotoPreview.legendHigh.innerHTML = PhotoPreview.legend + ", cliquez pour fermer.";
	PhotoPreview.loading = document.createElement("p");
	PhotoPreview.loading.className = "italic";
	PhotoPreview.loading.innerHTML = "Chargement...";
	
	PhotoPreview.popup.appendChild(PhotoPreview.loading);
	PhotoPreview.popup.appendChild(PhotoPreview.legendHigh);

	var imageHighSrc = this.parentNode.getAttribute("href");
	var xhr = getXHR();
	xhr.open("GET", imageHighSrc, true);
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200) {
			PhotoPreview.popup.removeChild(PhotoPreview.loading);
			PhotoPreview.imageHigh = document.createElement("img");
			PhotoPreview.imageHigh.setAttribute("border", "0");
			PhotoPreview.imageHigh.setAttribute("src", imageHighSrc);
			PhotoPreview.imageHigh.setAttribute("alt", PhotoPreview.legend);
			PhotoPreview.popup.insertBefore(PhotoPreview.imageHigh, PhotoPreview.legendHigh);
		}
	};
	xhr.send(null);
	return(false);
};

PhotoPreview.closeHigh = function() {
	PhotoPreview.popup.className = "hide";
	PhotoPreview.popup.removeChild(PhotoPreview.imageHigh);
	PhotoPreview.popup.removeChild(PhotoPreview.legendHigh);
	return(false);
};

function getY(obj) {
	var y = 0;
	while (obj != null) {
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return(y);
}

function getXHR() {
	xhr = null;
	if(window.XMLHttpRequest) { // Firefox 
		xhr = new XMLHttpRequest(); 
	}
	else if(window.ActiveXObject) { // Internet Explorer 
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	} else { // XMLHttpRequest non supporté par le navigateur 
		alert("Votre navigateur doit être mis à jour."); 
		xhr = false;
	}
	return(xhr);
}