var lbOpacity = 0.7;
var lbToShow = null;
var funcToRun = null;
var isShowing = null;
var isReallyIE6 = isIE6 || null;
var scrollTop = true;

function initLightBox(func){
    $(".lbContent").hide();
    $(".displayLightbox").click(function(e){
        lbToShow = this.href.split("#")[1];
        isShowing = lbToShow; 
        funcToRun = func || null;
    });
    

    // We could show a lightbox as soon as the user loads the page
    // if they have the hash for the lightBox id in the url e.g. www.website.com/#lightboxID
//     if(!isReallyIE6){
//         var lbLink = location.hash.split("#")[1];
//         if ($("#"+lbLink).hasClass('lbContent')) {
//             lbToShow = lbLink;
//             isShowing = lbToShow; 
//             funcToRun = func || null;
//             $(document).click();
//         }    
//     }
    
}

$(document).bind('click',function(e){
    if (!isShowing) {return;}
    if (lbToShow !== null){
        
        // for ie6 calculate the top position for the lightbox
        var lbContStyle = (isReallyIE6 ? " style='top:"+parseInt($(document).scrollTop() + 100)+"px;' " : undefined );
		
		
		if (scrollTop) {
			window.scrollTo(0,0);
		}
		
        $("#lb").remove();
        var lgHeight = "height:"+$(document).height()+"px;"; 
        var lightBox = "<div id='lb' ><div id='lbbg' class='hide' style='" + lgHeight + "' >&nbsp;</div><div id='lbCont' "+lbContStyle+"><a href='#' id='lbClose'>Close</a><div id='lbInner'></div></div></div>";
        $('body').append(lightBox);        
        var contents = $("#"+lbToShow).html();
        $('#lbInner').html(contents);
        $('#lb').show(); 
        $('#lbInner').hide();
        $('#lbClose').hide();
        $('#lbbg').fadeTo(0,0);
        $('#lbbg').fadeTo('fast',lbOpacity,function(){$('#lbInner').show(); $('#lbClose').show();});
        $('#lbbg').show();
        
        if (funcToRun !== null){
            funcToRun();    
        }

        $('#lbClose').click(function(e){
           $(document).click();
           e.preventDefault();
           
        });
        $('#lbCont').click(function(e){
            e.stopPropagation();
        });
        lbToShow = null;
    }
    else 
    {
        isShowing = null;
        $("#lbbg").fadeOut('fast');
        $('#lbCont').hide('fast',function(){$("#lb").remove();});
    }
});

