var isOpen = false;

function showOverlayBox() {
	//if box is not set to open then don't do anything
	if( isOpen == false ) return;
        // get image size
	$('.overlayBox').css( 'display', 'block' );
        var mywidth=$(".overlayContent > img").width();
        var myheight=$(".overlayContent > img").height();
        if (mywidth>$(window).width()) {
          var myratio=myheight/mywidth;
          mywidth=$(window).width()-40;
          myheight=Math.round(mywidth*myratio);
        }
        if (myheight>$(window).height()) {
          var myratio=mywidth/myheight;
          myheight=$(window).height()-40;
          mywidth=Math.round(myheight*myratio);
        }
        $(".overlayContent > img").width(mywidth);
        $(".overlayContent > img").height(myheight);
        $(".overlayBox").width(mywidth+20);
        $(".overlayBox").height(myheight+20);
	// set the properties of the overlay box, the left and top positions
	$('.overlayBox').css({
		display:'block',
		left:( $(window).width() - $('.overlayBox').width() )/2,
		top:$(document).scrollTop()+( $(window).height() - $('.overlayBox').height() )/2,
		position:'absolute'
	});
	// set the window background for the overlay. i.e the body becomes darker
}
function doOverlayOpen() {
	//set status to open
	isOpen = true;
        var mysrc=$(this).attr('src');
        $('.overlayContent > img').attr('src',mysrc);
	showOverlayBox();
        $('#pagewrapper').fadeTo('slow',0.4);
	// dont follow the link : so return false.
	return false;
}
function doOverlayClose() {
	//set status to closed
	isOpen = false;
        $('#pagewrapper').fadeTo('slow',1);
        // clear everything out
	$(".overlayContent > img").removeAttr('style');
	$(".overlayBox").removeAttr('style');
	$('.overlayBox').css( 'display', 'none' );
}


