(function($) {

	var _imageCount   = 0;
	var _imagesLoaded = 0;

	function _updatePopout()
	{
		var h = $(window).height() -  20;
		var p = h / 2;
		$('#popout>img').attr({
			height: h
		}).css({
			margin: '5px',
			border: '5px solid #444'
		});
	};

	function _imageLoad()
	{
		_imagesLoaded = _imagesLoaded + 1;
		$('#image-load>span.current').html(_imagesLoaded);
		$('#image-load>span.total').html(_imageCount);
		if (_imagesLoaded >= _imageCount) {
			$('#image-load').fadeOut();
		}
	}

	function _onLoad()
	{
		// loading count:
		_imageCount = $('div.item').length;
		$('#image-load>span.current').html(_imagesLoaded);
		$('#image-load>span.total').html(_imageCount);
		$('div.item>p>img').load(_imageLoad);

		// events:
		$('div.item>p>img').click(function() {
			var src = $(this).attr('src');
			var url = window.location+src;
			$('#popout>img').attr('src', url);
			_updatePopout();
			$('div.overlay').show();
		});
		$('div.overlay').click(function() {
			$('div.overlay').hide();
		});
		$(window).resize(_updatePopout);
		$('#image-load').click(function() {
			$(this).fadeOut();
		});
	}

	$(_onLoad);

})(jQuery);