jQuery.extend({
    //dims the screen
    dimScreen: function(speed, opacity, callback) {
        opacity = opacity || 0.5;
        return $('<div></div>').attr('id', '__dimScreen').css({
            background: '#000'
            ,height: document.body.clientHeight + 'px'
            ,left: '0px'
            ,top: '0px'
            ,position: 'absolute'
            ,width: document.body.clientWidth + 'px'
            ,zIndex: '100'
			,opacity: '0.0'
        }).appendTo(document.body).fadeTo(speed, opacity, callback);
    },
   
    //stops current dimming of the screen
    dimScreenStop: function() {
        $('#__dimScreen').remove();
    }
});


Thumbnailer = {
  active : 0,
  apply : function(img) {
	if(img[0].src.indexOf('/t_') != -1 && img[0].has_t != 1) {
	  img.css({
	  	cursor : 'move',
		border : 'solid 2px #777777',
		padding : '2px'
	  }).click(Thumbnailer.toggle);
	  img[0].has_t = 1;
	}
  },
  toggle : function(e) {
	if(Thumbnailer.active) {
	  Thumbnailer.sc.fadeOut('fast');
	  Thumbnailer.img.fadeOut('fast', function() {
		document.body.removeChild(Thumbnailer.img[0]);
		$.dimScreenStop();
	  });
	  Thumbnailer.active = 0;
	} else {
	  document.body.scrollTop = 0;
	  var sc = $.dimScreen('fast', 0.85), dw = $(document).width(), dh = $(window).height();
	  
	  var img = $('<img src="' + e.target.src.replace('/t_', '/') + '">')
	  	.load(function(e) {
	  	var mh = $(window).height() - 32,
			py = mh - img[0].height,
			dx = img[0].width / img[0].height;
			
		if(py < 0) {
			img[0].height = mh;
			img[0].width = mh * dx;
		}
		
	  	var mw = $(window).width() - 52,
			px = mw - img[0].width;
			
		if(px < 0) {
			img[0].width = mw;
			img[0].height = mh / dx;
		}
		
		img.css('left', (dw / 2 - img[0].width / 2)+'px')
		   .css('top', ($(document).scrollTop() + dh / 2 - img[0].height / 2)+'px');
		
	  });
	  img.css({
	  	position : 'absolute',
	  	border : 'solid 2px white',
	  	padding : '2px',
	  	zIndex : '1001',
	  	left : (img[0].width > 100 ? dw / 2 - img[0].width / 2 : 10) + 'px',
	  	top : ($(document).scrollTop() + (img[0].height > 100 ? dh / 2 - img[0].height / 2 : 10))+'px'
	  });
	  
	  $(document.body).prepend(img);
	  sc.click(Thumbnailer.toggle);
	  img.click(Thumbnailer.toggle);
	  Thumbnailer.sc = sc;
	  Thumbnailer.img = img;
	  Thumbnailer.active = 1;
	  
	  return false;
	}
  },
  setup: function(path) {
    $(window).load(function() { Thumbnailer.setupCB(path); });
  },
  setupCB: function(path) {
	  $(path).each(function(i,img) {
	    Thumbnailer.apply($(img));
	  });
  }
};

Thumbnailer.setup('.menu img');