/************************
* Benoit Borde LIGHTBOX *
************************/

var isHTMLAppend = false;
var curLightboxDiv = '';
var curURL = '';

function bbOpenOverlay() {
	bbResizeOverlay();
	$('#bbBackgroundOverlay').fadeIn(600, function() {
		$('#bbBackgroundOverlay').css({'filter':'alpha(opacity=60)'});
	});
}

function bbCloseOverlay() {
	$('.formError').remove();
	$('#bbBackgroundOverlay').fadeOut(600);
}

function bbResizeOverlay() {
/*	if ($(window).height() > $('body').height())
		$('#bbBackgroundOverlay').css({'height':$(window).height()+'px'});
	else
		$('#bbBackgroundOverlay').css({'height':$('body').height()+'px'});*/
	$('#bbBackgroundOverlay').css({'height':$(document).height()+'px'});
	$('#bbBackgroundOverlay').css({'width':$(document).width()+'px'});
}

function bbCenterLightbox(div) {
	// Centre horizontal
	var posLeft = ($(window).width() - $('#'+div).width())/2;
	$('#'+div).css({'left':posLeft+'px'});
	
	// Centre vertical si div moin haute que la fenetre
	if ($('#'+div).height() > $(window).height())
		$('#'+div).css({'top':'20px'});
	else {
//		var posTop = ($(window).height() - $('#'+div).height())/2 + $(window).scrollTop();
		var posTop = ($(window).height() - $('#'+div).height())/2;
		$('#'+div).css({'top':posTop+'px'});
	}
}

function bbCenterLightboxMove(div, newHeight) {
	var posLeft = ($(window).width() - $('#'+div).width())/2;
	$('#'+div).animate({'left':posLeft+'px'});

	// Centre vertical si div moin haute que la fenetre
	if ((newHeight+50) > $(window).height())
		$('#'+div).animate({'top':'20px'});
	else {
//		var posTop = ($(window).height() - (newHeight+50))/2 + $(window).scrollTop();
		var posTop = ($(window).height() - (newHeight+50))/2;
		$('#'+div).animate({'top':posTop+'px'});
	}
}

function bbOpenLightbox(div) {
	curLightboxDiv = div;
	bbOpenOverlay();
	bbCenterLightbox(div);
	$('#'+div).css({'display':'block'});
	$('#'+div).animate({'opacity':'1'}, 600);
}

function bbCloseLightbox(div) {
	curLightboxDiv = '';
	bbCloseOverlay();
	$('#'+div).animate({'opacity':'0'}, 600, function() {
		$('#'+div).css({'display':'none'});
	});
}

function bbResizeLightbox() {
	var offset = 50;
	var retHeight = 0;
	var iframeHeight = document.getElementById('bbIframe').contentWindow.document.body.scrollHeight;
	var iframeWidth = document.getElementById('bbIframe').contentWindow.document.body.scrollWidth;
/*	alert(iframeHeight+' / '+$(window).height());*/
	if ((iframeHeight+offset) > $(window).height()) {
/*		$('#eventIframe').height($(window).height() - 150);*/
		retHeight = $(window).height() - 100;
	} else {
/*		$('#eventIframe').height(iframeHeight);*/
		retHeight = iframeHeight;
	}
		
	if (iframeWidth > $(window).width()) {
		$('#bbIframe').width($(window).width() - 100);
		$('#bbLightbox').width(($(window).width() - 100 + 2));
	} else {
		if ((iframeHeight+offset) > $(window).height()) {
			$('#bbIframe').width(iframeWidth + 20);
			$('#bbLightbox').width(iframeWidth + 22);
		} else {
			$('#bbIframe').width(iframeWidth);
			$('#bbLightbox').width((iframeWidth+2));
		}
	}
	
	return retHeight;
}

function bbLightbox(lightboxTitle, iframeURL) {
	bbCreateLightboxHTML();
	curLightboxDiv = 'bbLightbox';
	curURL = iframeURL;
	if (lightboxTitle)
		$('#bbLightboxTitle').html(lightboxTitle);
	else
		$('#bbLightboxTitleBar').css({'display':'none'});
	$('#bbLightboxIframe').html('');
	if (curURL) {
		$('#bbLightboxIframe').html('<iframe id="bbIframe" src="" width="100%" frameborder="0"></iframe>');
		$('#bbIframe').attr('src', iframeURL);
		$('#bbIframe').load(function() {
			setTimeout("bbPositionLightbox()", 1000);
		});
	}
	bbOpenLightbox('bbLightbox');
}

function bbPositionLightbox() {
	var iframeHeight = bbResizeLightbox();
	bbCenterLightboxMove('bbLightbox', iframeHeight);
	bbDisplayIframe(iframeHeight);
}

function bbDisplayIframe(iframeHeight) {
	$('#bbLightboxLoading').fadeOut(500, function() {
		$('#bbIframe').animate({'height':iframeHeight+'px'}, 700);
	});
}

function bbCloseLightbox() {
	curURL = '';
	curLightboxDiv = '';
	bbCloseOverlay();
	$('#bbLightbox').animate({'opacity':'0'}, 600, function() {
		$('#bbLightbox').css({'display':'none'});
		$('#bbIframe').css({'height':'0px'});
		$('#bbLightboxLoading').fadeIn(200);
	});
}

function bbCreateLightboxHTML() {
	if (!isHTMLAppend) {
		isHTMLAppend = true;
		$('body').append('<div id="bbBackgroundOverlay"></div><div id="bbLightbox"  align="right"><div id="bbLightboxClose" onClick="bbCloseLightbox();" onMouseOver="$(this).css({\'background-color\': \'#304D95\',	\'color\':\'#FFF\'})" onMouseOut="$(this).css({\'background-color\': \'#FFF\',	\'color\':\'#304D95\'})">Fermer</div><div id="bbLightboxTitleBar"><div id="bbLightboxTitle"></div><!--div style="float:right"><img src="{URL_IMAGES_COMMON}picto_externe.jpg" style="cursor:pointer;" title="Ouvrir la prévisualisation dans une nouvelle fenêtre" border="0" onClick="openPreviewInNewWindow()" /></div--></div><div id="bbLightboxLoading">Chargement de la prévisualisation</div><div id="bbLightboxContent"></div><div id="bbLightboxIframe"></div></div>');
	}
}

function openPreviewInNewWindow() {
	PopSite(curURL);
}

window.onresize = function(event) {
	if (curLightboxDiv) {
		bbResizeOverlay();
		bbCenterLightbox(curLightboxDiv);
		bbPositionLightbox();
	}
}

/*window.onscroll = function(event) {
	var posTop = ($(window).height() - $('#bbLightbox').height())/2 + $(window).scrollTop();
	$('#bbLightbox').css({'top':posTop+'px'});
}*/
