/*Lightbox init scripts*/
/* Init */

function lightbox_init() {
	buildFrame();
	activateLightboxLinks('body');
}

//sets an event handler on the click of specific rel="lightbox" links to open the next element in a lightbox
function activateLightboxLinks(element) {
    console.log('pipo4');
    $.each($('input[rel]'), function () {
        console.log('pipo3');
        if ($(this).attr('rel') == "lightbox") {
            console.log('pipo2');
            $(this).click(function () {
                console.log('pipo1');
                activateFrame($(this).next());
                return false;
            });
        };
    });
    //$(element).find('a[rel="lightbox"]').click(function(){ activateFrame($(this).next()); return false; })
}

//builds the lightbox and renders it in the body
function buildFrame(){
	$('body').append('<div class="lightBox"> <a class="close">Close</a> <div class="content">Demo c0de</div> </div> <a href="" class="overlay">&nbsp;</a>')
	$('.lightBox, .overlay').hide();
}

//shows the lightbox with the content provided as an element
function activateFrame(content){
	var content = content || "";
	
	$('.lightBox .content').empty().append( $(content).html() )
	
    $('.overlay').show().css({opacity:.8,'width':$(document).width(),'height':$(document).height()})
	$('.overlay').click(function(){
		$(this).fadeOut(600);
		$('.lightBox').fadeOut(300);
		return false;
    })

$('.lightBox').css({ 'margin-left': -$('.lightBox').width() / 2, 'margin-top': -$('.lightBox').height() / 2 });
    
    $('.lightBox').fadeIn(500);

	$('.lightBox .close').click(function(){
		$(this).parent().fadeOut(600);
		$('.overlay').fadeOut(300);
		return false;
	});
}

//if the window resizes, the overlay of the lightbox scales accordingly.
$(window).resize(function(){
	if($('.overlay').css('display') != "none"){
		$('.overlay').css({'width':$(document).width(),'height':$(document).height(),opacity:.5})
	}
});


