
//returns the requested GET parameter from the specified URL or window.location.href by default
function getURLParam(param, url) {
    if (!url) url = window.location.href;
    var regex = '[?&]' + param + '=([^&#]*)';
    var results = (new RegExp(regex)).exec(url);
    if(results) return results[1];
    return 0;
}



//binds lightboxLink <a>'s click events with startOverlay method
$(document).ready(function(){
    bindLightboxLinks(0);
});
function bindLightboxLinks(id){
    // make string for optional container id and add a click event
	$((id!=0 ? "#"+id+" " : "")+".lightboxLink").click(function() {
		lightboxLink = $(this).attr("href");
		window.startOverlay(lightboxLink);
		return false;
	});
}



function startOverlay(lightboxLink) {
    //get the body scroll position and height
    var bodyScrollTop = $(window).scrollTop(); //only works if body has overflow
    var bodyScrollHeight = $(window).height();
    var bodyScrollWidth = $(window).width();
    var docHeight = $(document).height();
    
    //make sure there aint no nuttin
	$(".lightboxClose, .lightboxContainer, .lightboxOverlay").remove();    

    //add the elements to the DOM
	$("body")
		.append('<div class="lightboxOverlay"></div><div class="lightboxContainer"></div><div class="lightboxClose">×</div>');
		//.css({"overflow":"hidden"});

    //animate the semitransparent layer
    $(".lightboxOverlay").css({"top":"0px", "height":docHeight+"px"});
	$(".lightboxOverlay").animate({"opacity":"0.6"}, 400, "linear");

    //verify if alternate url
    var url = (getURLParam("alturl", lightboxLink) ? getURLParam("alturl", lightboxLink)+lightboxLink : lightboxLink);
    
    //fill the lightbox container with html
    $.ajax({
       type: "GET",
       url: url,
       async: true,
       success: function(msg) {
            $(".lightboxContainer").html(msg);
       }
    });

    //position it correctly after downloading
    var mediaItemWidth = getURLParam("width", lightboxLink);
    var mediaItemHeight = getURLParam("height", lightboxLink);
    var containerMarginTop = (bodyScrollHeight/2-mediaItemHeight/2-getURLParam("border",lightboxLink)<25 ? 25 : (bodyScrollHeight/2 - mediaItemHeight/2 - getURLParam("border", lightboxLink)));
    var containerMarginLeft = -mediaItemWidth/2 - getURLParam("border", lightboxLink);
    
    $(".lightboxContainer")
        .css({
            "border-width":getURLParam("border", lightboxLink)+"px",
            "top":         bodyScrollTop+"px",
            "left":        "50%",				
            "margin-top":  containerMarginTop + "px",
            "margin-left": containerMarginLeft + "px" //to position it in the middle
        })
        .animate({"opacity":"1"}, 400, "linear", function() {
            //$(".lightboxContainer div").focus(); //focus() dessine une dotted border peu heureuse dans FF...
            $(".lightboxClose")
                .css({
                    "top":        bodyScrollTop+"px",
                    "left":       "50%",				
                    "margin-top": (containerMarginTop-20) + "px",
                    "margin-left":containerMarginLeft + "px",
                    "opacity":    1
                });
        });

    //initiate the removeOverlay
    window.removeOverlay();
}

function removeOverlay() {
    // allow users to be able to close the lightbox
	$(".lightboxClose, .lightboxOverlay").click(function(){
		$(".lightboxClose, .lightboxContainer, .lightboxOverlay").animate({"opacity":"0"}, 200, "linear", function(){
			$(".lightboxClose, .lightboxContainer, .lightboxOverlay").remove();	
	        //$("body").css({"overflow":"auto"});
		});
	});
}

