(function($)
{
	var images = [];
	
	function ImageInfo(container, image)
	{
		this.container = container;
		this.image = image;
		this.loaded = false;
		this.ratio = 1;
	}
	
	$(function()
	{
		$(window).resize(windowResized);
		findFullscreenImage();
		setInterval(findFullscreenImage, 100); // bleh
	});
	
	function findFullscreenImage()
	{
		var containers, container, a, img, imageInfo, el, dim;
		
		images = [];
		containers = $('.fsimg');
		
		for (a = 0; a < containers.length; a++)
		{
			container = containers.eq(a);
			img = $('img', container);
			
			if (!img.length)
				continue;
			
			container.css({
				position: 'absolute',
				overflow: 'hidden',
				left: 0,
				top: 0,
				width: '100%',
				height: '100%'
			});
			
			img.css({
				position: 'absolute'
			});
			
			images.push(imageInfo = new ImageInfo(container, img));
			el = img.get(0);
			
			// consider image loaded if there is no support for img.complete
			imageInfo.loaded = el.complete !== false;  // http://www.w3.org/TR/html5/embedded-content-1.html#dom-img-complete
			
			if (imageInfo.loaded)
			{
				dim = getImageDimensions(el);
				
				imageInfo.ratio = dim.width / dim.height;
				if (!imageInfo.ratio)
					imageInfo.ratio = 1;
				updateImageSize(imageInfo);
				el.style.visibility = 'visible';
			}
			else
			{
				imageInfo.ratio = 1; 
				el.style.visibility = 'hidden'; // hide the image until it is done loading
				img.bind('load', imageInfo, fullScreenImage_load);
			}
		}	
	}
	
	function getImageDimensions(imgNode) // call before resizing image!
	{
		var width, height, r;
		
		width = 0;
		height = 0;
		if (imgNode.className)
		{
			className = ' ' + imgNode.className + ' ';
			if ( (r = /\sphysicalWidth(\d+)\s/.exec(className)) )
			{
				width = parseInt(r[1], 10);
			}
			
			if ( (r = /\sphysicalHeight(\d+)\s/.exec(className)) )
			{
				height = parseInt(r[1], 10);
			}
		}
		
		if (!width)
			width = imgNode.naturalWidth;
		
		if (!height)
			height = imgNode.naturalHeight;
		
		if (!width)
			width = $(imgNode).width();
		
		if (!height)
			height = $(imgNode).height();
		
		if (!width)
			width = 0;
		
		if (!height)
			height = 0;
		
		return {width: width, height: height};
	}
	
	function fullScreenImage_load(e)
	{
		fullScreenImageLoaded(e.data);
	}
	
	function fullScreenImageLoaded(imageInfo)
	{
		var dim, el;
		
		if (imageInfo.loaded_check_timer)
		{
			clearInterval(imageInfo.loaded_check_timer);
			imageInfo.loaded_check_timer = 0;
		}
		
		if (imageInfo.loaded)
			return;
		
		el = imageInfo.image.get(0);
		
		imageInfo.loaded = true;
		dim = getImageDimensions(el);
		imageInfo.ratio = dim.width / dim.height;
		if (!imageInfo.ratio)
			imageInfo.ratio = 1;
		
		updateImageSize(imageInfo);
		imageInfo.image.unbind('load', fullScreenImageLoaded);
		el.style.visibility = '';
	}
	
	
	
	function updateImageSize(/*ImageInfo*/ imageInfo)
	{
		var minWidth, minHeight, newWidthBasedOnHeight, newHeightBasedOnWidth;

		if (!imageInfo.loaded)
			return;
		
		minWidth  = imageInfo.container.width();
		minHeight = imageInfo.container.height();
		
		newHeightBasedOnWidth = parseInt(minWidth / imageInfo.ratio, 10);
		newWidthBasedOnHeight = parseInt(minHeight * imageInfo.ratio, 10);
		if (isNaN(newHeightBasedOnWidth))
			newHeightBasedOnWidth = 0;
		
		if (newHeightBasedOnWidth >= minHeight)
		{
			imageInfo.image.css({
				left: 0,
				top: (minHeight - newHeightBasedOnWidth) / 2,
				width: minWidth,
				height: newHeightBasedOnWidth
			});
			
		}
		else //if (newWidthBasedOnHeight >= minWidth)
		{
			imageInfo.image.css({
				left: (minWidth - newWidthBasedOnHeight) / 2,
				top: 0,
				width: newWidthBasedOnHeight,
				height: minHeight
			});
		}
		
	}
	
	function windowResized()
	{
		var a;
		for (a = 0; a < images.length ;a++)
		{
			if (images[a].loaded)
				updateImageSize(images[a]);
		}
	}
	
})(jQuery);

