function lightbox() {
  var links = $('a[target^=popup]');
  var overlay = $(jQuery('<div id="overlay" style="display: none"></div>'));
  var container = $(jQuery('<div id="lightbox" style="display: none"></div>'));
  var target = $(jQuery('<div class="target"></div>'));
  var head = $(jQuery("<h2></h2>"));
  var closeButton = $(jQuery('<div class="close"><a href="#close">&times; Close</a></div>'));
  var prev = $(jQuery('<a href="#prev" class="prev">&laquo; Previous</a>'));
  var next = $(jQuery('<a href="#next" class="next">Next &raquo;</a>'));
	
			
  $('body').append(overlay).append(container);
  
  overlay.css('width', $(window).width());
  overlay.css('height', $(window).height());
  
  container.append(closeButton);
  container.append(target);
  container.append(head);
  container.append(prev);
  container.append(next);
  
  container.show().css({
  			'top': Math.round(($(window).height() - container.outerHeight()) / 2) + 'px',
  			'left': Math.round(($(window).width() - container.outerWidth()) / 2) + 'px',
  			'marginTop': 0,
  			'marginLeft': 0
  			}).hide();
	
  closeButton.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'), link.attr('title'));
      links.filter('.selected').removeClass('selected');
      link.addClass('selected');
    });
    link.attr({'lb-position': index});
  });

  var open = function(url, t) {
	prev.hide();
	next.hide();
	
  	if(container.is(':visible')) {
      target.children().fadeOut('normal', function() {
        target.children().remove();
        loadimage(url, t);
      });
    } else {
      
      target.children().remove();
      overlay.add(container).fadeIn('normal',function(){
        loadimage(url, t);
      });
    }
  }
  
  var loadimage = function(url, t) {
    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)) - 100;
      var maxHeight = ($(window).height() - parseInt(container.css('padding-top'),10) - parseInt(container.css('padding-bottom'), 10)) - 100;
      
      if(img.width > maxWidth || img.height > maxHeight) { // One of these is larger than the window
        var ratio = img.width / img.height;
        if(img.height >= maxHeight) {
          img.height = maxHeight;
          img.width = maxHeight * ratio;
        } else {
          img.width = maxWidth;
          img.height = maxWidth * ratio;
        }
      }
      
      head.text(t);
      _top = Math.round(($(window).height() - img.height - parseInt(container.css('padding-top'),10) - parseInt(container.css('padding-bottom'),10)) / 2) + $(document).scrollTop();
      container.animate({'width': img.width,'height': img.height+50, 'top': _top + 'px', 'left': Math.round(($(window).width() - img.width - parseInt(container.css('padding-left'),10) - parseInt(container.css('padding-right'),10)) / 2) + 'px'},'normal', function(){
        target.append(img);
        $(img).fadeIn('normal', function() {
        	//prev.show();
      		//next.show();
      		container.removeClass('loading');
        });
      })
    }
    img.src = url;
  }
}

$(document).ready(function() {
	lightbox();
});