
//returns the requested GET parameter from the specified URL or window location 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 imgLightboxLink <a>'s click events with startImgOverlay method
$(document).ready(function(){
    bindImgLightboxLinks(0);
});
function bindImgLightboxLinks(id) {
    // make string for optional container id and add a click event
	$((id!=0 ? "#"+id+" " : "")+".imgLightboxLink").click(function(){
		lightboxLink = $(this).attr("href");
		window.startImgOverlay(lightboxLink);
		return false;
	});
}



function startImgOverlay(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");

    //add the lightbox image to the DOM
    if (getURLParam("link", lightboxLink) != 0) {
    	$(".lightboxContainer").html('<a href="'+getURLParam("link", lightboxLink)+'" title="'+getURLParam("link", lightboxLink)+'" target="_blank"><img src="'+lightboxLink+'" alt="" /></a>');
    } else
        $(".lightboxContainer").html('<img src="'+lightboxLink+'" alt="" />');

    //position it correctly after downloading
    border = 12;
    if (getURLParam("border",lightboxLink))
    	border = getURLParam("border",lightboxLink);
	$(".lightboxContainer img").load(function() {
		var imgWidth = $(".lightboxContainer img").width();
		var imgHeight = $(".lightboxContainer img").height();
	    var imgMarginTop = (bodyScrollHeight/2-imgHeight/2-border<25 ? 25 : (bodyScrollHeight/2 - imgHeight/2 - border));
	    var imgMarginLeft = -imgWidth/2 - border;
	    
		$(".lightboxContainer")
			.css({
			    "border":     border+"px solid #eee",
				"top":        bodyScrollTop + "px",
				"left":       "50%",				
				"width":      imgWidth,
				"height":     imgHeight,
				"margin-top": imgMarginTop + "px",
				"margin-left":imgMarginLeft + "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": (imgMarginTop-20) + "px",
                    "margin-left":imgMarginLeft + "px",
                    "opacity":    1
                });
			});

        // you need to initiate the removeOverlay function here, otherwise it will not execute.
		window.removeImgOverlay(lightboxLink);
	});
}

function removeImgOverlay(lightboxLink) {
    // allow users to be able to close the lightbox
	$(".lightboxClose, .lightboxOverlay"+(getURLParam("link", lightboxLink)==0 ? ", .lightboxContainer" : "")).click(function(){ //, .lightboxContainer
		$(".lightboxClose, .lightboxContainer, .lightboxOverlay").animate({"opacity":"0"}, 200, "linear", function(){ // callback function for when it's done
			$(".lightboxClose, .lightboxContainer, .lightboxOverlay").remove();	
	        //$("body").css({"overflow":"auto"});
		});
	});
}

