// Param
var _btn = "images/btn_close_up.gif";
var _close = "Fermer/Close"
var _w = 825;
var _h = 600;

// ________________________________________________________
// Init lightbox
function lb_init(){
	var arr = document.getElementsByTagName("a");
	var j = 1;
	for(var i = 0; i < arr.length; i++){
		if(arr[i].rel == "lightbox"){
			arr[i].onclick = lb_display;
		}
	}
	
	var div = document.createElement('div');
	div.setAttribute("id", "filter");
	div.onclick = lb_hide;
	document.body.appendChild(div);
}

// ________________________________________________________
// Display lightbox
function lb_display(e){
	var obj = window.event ? window.event.srcElement : e ? e.target : null;
	var html = "";
	while(obj.nodeName != "A"){
		obj = obj.parentNode;
	}
	var ext = obj.href.substring(obj.href.lastIndexOf(".") + 1, obj.href.length);
	if(ext.indexOf("?") > 0){
		ext = ext.substring(0, ext.indexOf("?"));
	}
	if(ext == "html" || ext == "php"){
		$.get(obj.href, function(data){
			lb_create(obj, data);
		});
	}else{
		html = "<p><a href=\"javascript:void(0);\" onclick=\"lb_hide();\"><img src=\"" + obj.href + "\" alt=\"" + obj.title + "\" /></a></p>";
		html += "<p>" + obj.title + "</p>";
		lb_create(obj, html);
	}
	
	window.onresize = lb_set_dim;
	
	$("#filter").css("display", "block");
	$("object").css("visibility", "hidden");
	$("embed").css("visibility", "hidden");
	$("select").css("visibility", "hidden");
	
	return false;
}

// ________________________________________________________
// Create lightbox
function lb_create(obj, html){
	var div = document.createElement('div');
	html = "<p class=\"close\"><a href=\"javascript:void(0);\" onclick=\"lb_hide();\"><img src=\"" + _btn + "\" alt=\"" + _close + "\" /></a></p>" + html;
	div.innerHTML = html;
	div.setAttribute("id", "lightbox");
	document.body.appendChild(div);
	
	lb_set_dim();
}

// ________________________________________________________
// Set lightbox dimensions
function lb_set_dim(){
	var arrSize = __getPageSize();
	var arrPos = __getPageScroll();
	var x = (arrSize[2] - _w) / 2 + arrPos[0];
	var y = (arrSize[3] - _h) / 2 + arrPos[1];
	
	$("#filter").css("height", arrSize[1] + "px");
	$("#lightbox").css({ "display":"block", "width":_w + "px", "height":_h + "px", "top":y + "px", "left":x + "px" });
}

// ________________________________________________________
// Hide lightbox
function lb_hide(){
	window.onresize = null;
	document.body.removeChild(document.getElementById("lightbox"))
	
	$("#filter").css("display", "none");
	$("object").css("visibility", "visible");
	$("embed").css("visibility", "visible");
	$("select").css("visibility", "visible");
}