$(document).ready(function(){
	//Creates the lightbox container
	var lb_container = document.createElement("div");
		lb_container.id = "lightbox_container";
    	lb_container.className ="lightbox_container";
    	document.body.appendChild(lb_container);
	
	//Creates the lightbox background
	var lb_bg = document.createElement("div");
		lb_bg.id = "lightbox_bg";
    	lb_bg.className ="lightbox_bg";
    	document.body.appendChild(lb_bg);
    	
    //Creates the ajax loader
    var lb_loader = document.createElement("div");
		lb_loader.id = "lightbox_loader";
    	lb_loader.className ="lightbox_loader";
    	lb_loader.innerHTML = "<img src='img/loader.gif'/>";
    	document.body.appendChild(lb_loader);
    
    //Creates the ligbtox close - tooltip
    var lb_tooltip = document.createElement("div");
		lb_tooltip.id = "lightbox_tooltip";
    	lb_tooltip.className ="lightbox_tooltip";
    	lb_tooltip.innerHTML = "Tillbaks";
    	document.body.appendChild(lb_tooltip);
    
    //Hides the created elements
    $(".lightbox_bg").css({'opacity' : '0', 'display' : 'none'});
    $(".lightbox_container").css({'opacity' : '0', 'display' : 'none'});
  	$("#lightbox_loader").css({'opacity' : '0', 'display' : 'none'});
  	$("#lightbox_tooltip").hide();
	
	//Litsens for a click on the bg - runs the close function
	$(".lightbox_bg").click(function() {
		closeLightBox();
	});
	
	//Makes the tooltip follow the mouse
	$(".lightbox_bg").mousemove(function(e){
        $("#lightbox_tooltip").show();
        $("#lightbox_tooltip").css({
            top: (e.pageY + 15) + "px",
            left: (e.pageX + 10) + "px"
        });
    });
    
    //Hides tooltip when cursor is not on the bg div
    $(".lightbox_bg").mouseout(function(e){
        $("#lightbox_tooltip").hide();
    });
});

function openLightBox(id){
	
	$(".lightbox_bg").css('display', 'block');
	$(".lightbox_bg").fadeTo("fast", 0.7);
	
	$("#lightbox_loader").css('display', 'block');
	$("#lightbox_loader").fadeTo("fast", 1);
	
	$("#lightbox_container").load("load_artist.php?a=lb_showartist&id="+id,{}, function(){
	$("#lightbox_loader").fadeTo("fast", 0);
	
	$(".lightbox_container").css('display', 'block');
	$(".lightbox_container").fadeTo("fast", 1);
	
	var center = document.getElementById('lightbox_container');
	var topMargin = center.offsetHeight / 2;
	var leftMargin = center.offsetWidth / 2;
	center.style.marginLeft = -1*(center.offsetWidth / 2) + 'px';
	center.style.marginTop = -1*(center.offsetHeight / 2) + 'px';
	
	});
};

function closeLightBox(){
	$(".lightbox_bg").css('display', 'none');
	$(".lightbox_bg").css('opacity', 0);
	
	$(".lightbox_container").css('display', 'none');
	$(".lightbox_container").css('opacity', 0);
	
	$("#lightbox_container").load("load_artist.php?a=lb_showartist&id=0",{});
};
