/**
 *
 * Version: 	0.1.0 - (Edited since by e4education to alter functionality)
 * Author:		Gianluca Guarini
 * Contact: 	gianluca.guarini@gmail.com
 * Website:		http://www.gianlucaguarini.com/
 * Twitter:		@gianlucaguarini
 *
 * Copyright (c) 2011 Gianluca Guarini
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 **/
(function ($) {

    $.fn.extend({
        greyscale: function () {
            var container = this;
			var supportsCanvas = !!document.createElement('canvas').getContext;
			
			function greyImages(img,canvas,width,height) {  
                var ctx = canvas.getContext('2d'),
                currImg = img,
                imageData, px, length, i = 0,
                grey;
				img.onload = function(){
					ctx.drawImage(img, 0, 0);
					imageData = ctx.getImageData(0, 0, 220, 220);
					px = imageData.data;
					length = px.length;
					for ( ; i < length; i+= 4 ) {
						grey = px[i] * .3 + px[i+1] * .59 + px[i+2] * .11;
						px[i] = px[i+1] = px[i+2] = grey;
					}
					ctx.putImageData(imageData, 0, 0);
				}
            }
			
			this.init = function() {
				if(supportsCanvas &&(!(jQuery.browser.msie && jQuery.browser.version == '9.0'))) {
					$(container).each(function(index,currImageWrapper){
						var pic = $(currImageWrapper).find('img');
						var currWidth  = $(pic).attr('width');
						var currHeight = $(pic).attr('height');
						$('<canvas width="'+currWidth+'" height="'+currHeight+'"></canvas>').prependTo(currImageWrapper);
						var currCanvas = $(currImageWrapper).find('canvas');
						$(currCanvas).css({
							'position':'absolute',
							'display':'none',
							top:0,
							left:0
						});
						greyImages(pic[0],currCanvas[0],currWidth,currHeight);
				    })
				} else {
					$(container).each(function(index,currImageWrapper) {
						var pic = $(currImageWrapper).find('img');
						var picSrc = $(currImageWrapper).find('img').attr('src');
						var currWidth  = $(pic).attr('width');
						var currHeight = $(pic).attr('height');
						$('<img src='+picSrc+' width="'+currWidth+'" height="'+currHeight+'" class="ieFix" /> ').prependTo(currImageWrapper);	
						$('.ieFix').css( {
							'position':'absolute',
							'display':'none',
							top:0,
							left:0,
							'filter': 'progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)'
						});
				    })
				}
			}
        return this.init();
        }
    });
})(jQuery);
