
function lightbox() {
	
    var links = $('a[rel^=lightbox]');
    var overlay = $(jQuery('<div id="overlay" style="display: none"></div>'));
    var container = $(jQuery('<div id="lightbox" style="display: none"></div>'));
    var close = $(jQuery('<span class="close" ><a href="#close">Close</a></span>'));
    var target = $(jQuery('<div class="target"></div>'));
    var prev = $(jQuery('<a href="#prev" class="prev">Prev</a>'));
    var next = $(jQuery('<a href="#next" class="next">Next</a>'));
    var fullsize = $(jQuery('<a href="#fullsize" class="fullsize">Full Size</a>'));

    $('body').append(overlay).append(container);
    container.append(prev).append(next).append(close).append(fullsize).append(target);
    container.show().css({
        'top': Math.round((($(window).height() > window.innerHeight ? window.innerHeight: $(window).height()) - container.outerHeight()) / 2) + 'px',
        'left': Math.round(($(window).width() - container.outerWidth()) / 2) + 'px',
        'margin-top': 0,
        'margin-left': 0
    }).hide();
    close.click(function(c) {
        c.preventDefault();
        overlay.add(container).fadeOut('normal');
    });

    prev.add(next).click(function(c) {
        c.preventDefault();
        var current = parseInt(links.filter('.selected').attr('lb-position'), 10);
        var to = $(this).is('.prev') ? links.eq(current - 1) : links.eq(current + 1);
        if (!to.size()) {
            to = $(this).is('.prev') ? links.eq(links.size() - 1) : links.eq(0);
        }
        if (to.size()) {
            to.click();
        }
    });
	
    links.each(function(index) {
        var link = $(this);
        link.click(function(c) {
            c.preventDefault();
            open(link.attr('href'));
            links.filter('.selected').removeClass('selected');
            link.addClass('selected');
			fullsize.attr('href', link.attr('href') );
        });
        link.attr({
            'lb-position': index
        });
    });
	
	fullsize.click( function(c) {
        c.preventDefault();
    	window.open( fullsize.attr('href') );
		return false;
    });
	
    var open = function(url) {
        if (container.is(':visible')) {
			target.children().fadeOut('normal',
			function() {
				target.children().remove();
				loadImage(url);
			});
        } else {
            target.children().remove();
			// overlay.add(container);
			// loadImage(url);
			overlay.add(container).fadeIn('normal',
			function() {
				loadImage(url);
			});
        }
    }

    var loadImage = function(url) {
        if (container.is('.loading')) {
            return;
        }
        container.addClass('loading');
        var img = new Image();
        img.onload = function() {
            img.style.display = 'none';

            var maxWidth = ($(window).width() - parseInt(container.css('padding-left'), 10) - parseInt(container.css('padding-right'), 10)) - 0;
            var maxHeight = (($(window).height() > window.innerHeight ? window.innerHeight: $(window).height()) - parseInt(container.css('padding-top'), 10) - parseInt(container.css('padding-bottom'), 0)) - 0;
			
            if (img.width > maxWidth || img.height > maxHeight) {
                // One of these is larger than the window
                if (img.height >= maxHeight) {
					// console.log( "height is bigger" + maxHeight);
	                var ratio = img.width / img.height;
                    img.height = maxHeight;
                    img.width = maxHeight * ratio;
                } else {
					// console.log( "width is bigger" + maxHeight);
	                var ratio = img.height / img.width;
                    img.width = maxWidth;
                    img.height = maxWidth * ratio;
                }

				// console.log( "ratio " + ratio, "img.height " + img.height  );
            }

            container.animate({
                'width': maxWidth,
                'height': maxHeight,
				'top': 0 + 'px', 
				'left': 0 + 'px'
            },
            'normal',
            function() {
                target.append(img);
				$(target).css( "margin-top", (maxHeight-img.height)/2 + 'px' ); 
				$(target).css( "margin-left", (maxWidth-img.width)/2 + 'px' );
                
				$(img).fadeIn('normal',
                function() {
                    container.removeClass('loading');
                });
            })
        }
        img.src = url;
    }

}
// 
// 'top': Math.round(($(window).width() - parseInt(container.css('padding-left'), 10) - parseInt(container.css('padding-right'), 10)) / 2) + 'px',
// 'left': Math.round((($(window).height() > window.innerHeight ? window.innerHeight: $(window).height()) - parseInt(container.css('padding-top'), 10) - parseInt(container.css('padding-bottom'), 0)) / 2) + 'px'
