var firstTime = true;
var animEnCours = false;

$(document).ready(function(){   
	tb_init();
});

function tb_init(){
	$("a.photo").click(function(){
		var lien = $(this).attr("href");
		tb_showOverlay(lien, "photo");
		this.blur();
		return false;
	});
	$("a.video").click(function(){
		var lien = $(this).attr("href");
		tb_showOverlay(lien, "video");
		this.blur();
		return false;
	});
}

function tb_showOverlay(lien, type) {
	$("body").append("<div id='tb_overlay'></div><div id='tb' class="+type+"><p id='bt-fermer'><a href='#'><img src='img/litebox/fermer.gif' alt='Fermer' /></a></p><div id='tb-contenu'></div></div>");
	$("#tb-contenu").load(lien);
	
	$("#bt-fermer a").click(function(){
		tb_hideAll();
		this.blur();
		return false;
	});
	
	if($.browser.msie) {
		tb_hideSelects("hidden");
	}
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var positionTop = arrayPageScroll[1] + (arrayPageSize[3] / 15);
	
	var hauteurOverlay = arrayPageSize[1];
	var hauteurBoite = $("#tb").height();
	if(hauteurOverlay < positionTop+hauteurBoite) {
		hauteurOverlay = positionTop+hauteurBoite+30;
	}
	
	$("#tb_overlay").css("height", hauteurOverlay);
	
	var lightboxTop = positionTop + "px";
	$("#tb").css("top", lightboxTop);
	
	$("#tb_overlay").css("opacity", "0");
	$("#tb_overlay").css("display", "block");
	$("#tb").css("display", "block");
}

function tb_hideAll() {
 	$("#tb_overlay").remove();
	$("#tb").remove();
	firstTime = true;
	if($.browser.msie) {
		tb_hideSelects("visible");
	}
	return false;
}

function tb_prepareIE(height, overflow){
	$("body").css("height", height).css("overflow", overflow);
	$("html").css("height", height).css("overflow", overflow);
}

function tb_hideSelects(visibility){
	$("select").each(function(i){
		$(this).css("visibility", visibility);
	});
}

function tb_getScroll(){
	if (self.pageYOffset) {
		yPos = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){
		yPos = document.documentElement.scrollTop; 
	} else if (document.body) {
		yPos = document.body.scrollTop;
	}
}
	
function tb_setScroll(x, y){
	window.scrollTo(x, y); 
}

function tb_getPageHeight(){
	var de = document.documentElement;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	return h;
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

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;
}

