var LB_hidden = true;
var LB_inHold = false;

$(document).ready(function() {
						   LB_buildLightbox();
});

function LB_appear(fetchedData){
		if(LB_hidden)
		{
			$('#LB_content').text('');
			$('#LB_content').html(fetchedData);
			$("#LB_shadow").fadeIn(500,function(){
													$("#LB_content").css('margin-top','-'+($("#LB_content").outerHeight()/2)+'px');
													$("#LB_content").css('margin-left','-'+($("#LB_content").outerWidth()/2)+'px');												
													$("#LB_content").fadeIn(500, function(){
																								$("#LB_content").css('margin-top','-'+($("#LB_content").outerHeight()/2)+'px');
																								$("#LB_content").css('margin-left','-'+($("#LB_content").outerWidth()/2)+'px');	
																						  });
												 });
		}
		else
		{
			$("#LB_content").fadeOut(250,function(){
													$('#LB_content').text('');
													$('#LB_content').html(fetchedData);												  
													$("#LB_content").css('margin-top','-'+($("#LB_content").outerHeight()/2)+'px');
													$("#LB_content").css('margin-left','-'+($("#LB_content").outerWidth()/2)+'px');	
													$("#LB_content").fadeIn(250, function(){
																								$("#LB_content").css('margin-top','-'+($("#LB_content").outerHeight()/2)+'px');
																								$("#LB_content").css('margin-left','-'+($("#LB_content").outerWidth()/2)+'px');	
																						  });
												 });			
		}
		LB_hidden = false;
}

function LB_dissapear(){
		$("#LB_content").fadeOut(250,function(){
											  	$("#LB_content").html("");
												$("#LB_shadow").fadeOut(500);
											 });
		// For tobiasartz.nl
		centerHold = false;
		LB_hidden = true;
}

function LB_buildLightbox(){
	$("body").append('<div id="LB_shadow"></div>');
	$("#LB_shadow").css('opacity','0.9');
	$("#LB_shadow").css('width','100%');
	$("#LB_shadow").css('height','100%');
	$("#LB_shadow").css('display','none');
	$("#LB_shadow").css('cursor','pointer');
	$("#LB_shadow").click(function()
								   {
									   LB_dissapear();
								   });
	
	$("body").append('<div id="LB_content"></div>');
	$("#LB_content").css('display','none');	
}

function LB_get(url){
	
	if(!LB_inHold)
	{
		$("body").append('<div id="LB_loading"></div>');
		$("#LB_loading").css('position','absolute');
		$("#LB_loading").css('top','50%');
		$("#LB_loading").css('left','50%');
		$("#LB_loading").append('<img src="http://www.ledlamp.nl/css/img/loading.gif">');
		$("#LB_loading").css('margin-top','-'+($("#LB_loading").outerHeight()/2)+'px');
		$("#LB_loading").css('margin-left','-'+($("#LB_loading").outerWidth()/2)+'px');	
		
		$.ajax({
			url: url,
			async: true,
			cache: false,
			dataType: "html",
			success: function(fetchedData){
					$("#LB_loading").remove();
					LB_appear(fetchedData);
					LB_inHold = false;		
				},
			error: function(){
					LB_inHold = false;
					$("#LB_loading").remove();
				}
		});
	}

	LB_inHold = true;
}

function LB(url){
	//just for TobiasArtz.nl
	centerHold = true;	

	if(typeof(url) != 'undefined')
	{	
		LB_get(url);		
	}
}