/*
 * jQuery.imagescale.js - Smoother Client-Side Image Scaling
 * Provides a canvas-element alternative for scaled images.
 * 
 * Requires:  excanvas.js for IE compatibility
 * 
 * Randall Morey.  February 2008.
 */

(function ($) {
$.fn.extend({
	imageScale: function (settings) {
		settings = $.extend({
			canvasClass: 'scaled'
		}, settings);
			
		var processImage = (!$.browser.safari && !$.browser.opera) ?
			function () {
				if ($(this).is('img')) {
					$(this).load(function () {
						var width = $(this).width(),
							height = $(this).height(),
							imgOrigin = $(this),
							imgClone = $('<img />').load(function () {
								var canvas = $('<canvas />')
									.attr('width', width)
									.attr('height', height)
									.addClass(settings.canvasClass)
									.insertAfter(imgOrigin).get(0);
								
								if ($.browser.msie)
									canvas = G_vmlCanvasManager.initElement(canvas);
									
								$(imgOrigin).hide();
								
								canvas.getContext('2d').drawImage(imgClone[0], 0, 0, width, height);
							}).attr('src', $(this).attr('src'));
					});
				}
				return this;
			} :
			function () { return this; };
		
		return $(this).each(processImage);
	}
});
})(jQuery);