/*global define*/
define(
	function()
	{
		var signals;
		var image;
		var initialized = false;

		function init( shared )
		{
			signals = shared.signals;
			image = new Image();

			signals['set-new-src'].add( setSrc );

			image.addEventListener( 'load', imageLoaded, false );

			// the image "Abraham Lincoln November 1863" is public domain:
			// https://en.wikipedia.org/wiki/File:Abraham_Lincoln_November_1863.jpg
			setSrc( 'lincoln.jpg' );
		}

		function imageLoaded()
		{
			signals['image-loaded'].dispatch( image );
			initialized = true;
		}

		function setSrc( src )
		{
			image.src = src;

			if (
				initialized &&
				image.naturalWidth !== undefined &&
				image.naturalWidth !== 0
			)
			{
				setTimeout( imageLoaded, 10 );
			}
		}

		return { init: init };
	}
);
@@ -88,16 +88,91 @@
 = false;
+%0A%09%09%0A%09%09// max 2k px per side, so 2000 * 2000%0A%09%09var max_image_area = 4000000;
 %0A%0A%09%09func
@@ -548,24 +548,57 @@
 oaded()%0A%09%09%7B%0A
+%09%09%09constrainImageSize( image );%0A%0A
 %09%09%09signals%5B'
@@ -815,16 +815,16 @@
 %09)%0A%09%09%09%7B%0A
-
 %09%09%09%09setT
@@ -854,24 +854,454 @@
  );%0A%09%09%09%7D%0A%09%09%7D
+%0A%09%09%0A%09%09function constrainImageSize( img )%0A%09%09%7B%0A%09%09%09var ratio = 0;%0A%09%09%09var image_width = img.width;%0A%09%09%09var image_height = img.height;%0A%09%09%09var image_area = image_width * image_height;%0A%0A%09%09%09if ( image_area %3E max_image_area )%0A%09%09%09%7B%0A%09%09%09%09ratio = max_image_area / image_area;%0A%09%09%09%09%0A%09%09%09%09image_width *= ratio;%0A%09%09%09%09image_height *= ratio;%0A%0A%09%09%09%09img.naturalWidth = Math.floor( image_width );%0A%09%09%09%09img.naturalWidth = Math.floor( image_height );%0A%09%09%09%7D%0A%09%09%7D
 %0A%0A%09%09return %7B

add image size constraint