(function($) {

var tb_pathToImage = "/img/loadingAnimation.gif";
var firstTime = true;

$(document).ready(function(){   
	var yPos=0
	var xPos=0
	tb_init();
	imgLoader = new Image();
	imgLoader.src = tb_pathToImage;
});

function tb_init(){
	$("a.thickbox").click(function(){
		var url = $(this).attr("href");
		var classe = $(this).attr("id");
		tb_show(url, classe);
		this.blur();
		return false;
	});
}

function tb_show(url, classe) {
	if(url.indexOf("#") != -1) {
		var params = url.slice(url.indexOf("#"));
		var largeur = params.slice(params.indexOf("w")+1, params.indexOf("h"));
		var hauteur = params.slice(params.indexOf("h")+1);
	} else {
		var largeur = 400;
		var hauteur = 400;
	}
	
	$("body").append("<div id='tb_loading'><img src='"+imgLoader.src+"' /></div><div id='tb_overlay'></div><div id='tb' style='width:"+largeur+"px'><div id='tb_close'><a href='#'>Fermer</a></div><div id='tb_iframe' style='height:"+hauteur+"px'><div id='tb_logo' style='margin:0; padding:10px 0 0 0; background-color:#fff; text-align:right; position:absolute; left:0; bottom:-1px; width:100%;'><img src='/img/logo_monemploi2.gif' alt='' border='0' /></div></div></div>");
	
	$("#tb_close a").click(tb_hide);
	tb_hideSelects("hidden");
	
	// stretch overlay to fill page and fade in
	var arrayPageSize = getPageSize();
	$("#tb_overlay").css("height", arrayPageSize[1]);
	
	// calculate top offset for the lightbox and display 
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 15) + "px";
	$("#tb").css("top", lightboxTop);
	
	// calculate top offset for the loading
	var loadingTop = arrayPageScroll[1] + (arrayPageSize[3] / 15) + 100 + "px";
	$("#tb_loading").css("top", loadingTop);
	
	$("#tb_loading").fadeIn("fast");
	$("#tb_overlay").css("opacity", "0");
	$("#tb_overlay").fadeTo(500, 0.7, function(){
		$("#tb_iframe").append("<iframe frameborder='0' allowtransparency='true' src='"+url+"' onload='tb_showIFrame()'></iframe>");
		// Le onload sur un IFrame ne s'exécute pas dans Safari
		/*if($.browser.safari) {*/
			tb_showIFrame();
		/*}*/
	});
}

function tb_showIFrame(){
	if(firstTime){
		$("#tb_loading").fadeOut("fast", function(){
			$("#tb_loading").remove();
			$("#tb").slideDown(500);
		});
		// Empêche l'animation de s'exécuter plus d'une fois
		firstTime = false;
	}
}

function tb_hide() {
 	$("#tb_close a").unbind("click");
	$("#tb").slideUp(500, function(){
		$(this).css("display", "none");
		$("#tb_overlay").fadeTo(500, 0, function(){
			$(this).remove();
			$("#tb").remove();
			firstTime = true;
			if($.browser.msie) {
				//tb_prepareIE("auto", "auto");
				tb_hideSelects("visible");
				//tb_setScroll(0,yPos);
			}
		});
	});
	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;
}

})(jQuery)
