﻿(function ($) {

	var data = (function(){
            
            var cache = [0],
            expando = 'data' + (+new Date());
            
            return function(elem) {
                var cacheIndex = elem[expando],
                    nextCacheIndex = cache.length;
                if(!cacheIndex) {
                    cacheIndex = elem[expando] = nextCacheIndex;
                    cache[cacheIndex] = {};
                }
                return cache[cacheIndex];
            };
            
        })();

	var colorProps = ['color','backgroundColor','borderBottomColor','borderTopColor','borderLeftColor','borderRightColor','backgroundImage'];

	var getStyle = function(el, prop) {
            
			var style = $(el).css(prop);
            // If format is #FFFFFF: (convert to RGB)
            if (style && /^#[A-F0-9]/i.test(style)) {
                    var hex = style.match(/[A-F0-9]{2}/ig);
                    style = 'rgb(' + parseInt(hex[0], 16) + ','
                                   + parseInt(hex[1], 16) + ','
                                   + parseInt(hex[2], 16) + ')';
            }
            return style;
        };
	var RGBtoGRAYSCALE = function(r,g,b) {
            // Returns single monochrome figure:
            return parseInt( (0.2125 * r) + (0.7154 * g) + (0.0721 * b), 10 );
        };
    var getAllNodes = function(context) {
            var all = Array.prototype.slice.call(context.getElementsByTagName('div'));
            all.unshift(context);
            return all;
        };
	var convert = function(cur) {
		for (var pIndex = 0, pLen = colorProps.length; pIndex < pLen; pIndex++) {
			var prop = colorProps[pIndex],
			style = getStyle(cur, prop);
			if (!style) {continue;}
			
			if ($(cur).css(prop)) {
				
			}
			
			// RGB color:
			if (style.substring(0,4) === 'rgb(') {
				var monoRGB = RGBtoGRAYSCALE.apply(null, style.match(/\d+/g));
				data(cur)[prop] = 'rgb(' + monoRGB + ',' + monoRGB + ',' + monoRGB + ')';
				//$(cur).css(prop,'rgb(' + monoRGB + ',' + monoRGB + ',' + monoRGB + ')');
				continue;
			}
		}
	};
	var swap = function(cur) {
		for (var pIndex = 0, pLen = colorProps.length; pIndex < pLen; pIndex++) {
			var prop = colorProps[pIndex];
			var tmpStyle = $(cur).css(prop);
			$(cur).css(prop,data(cur)[prop]);
			data(cur)[prop] = tmpStyle;
		}
	}
	$.fn.grayscalePrepare = function() {
        return this.each(function(i){
			if ($.browser.msie){
				//var child = $(this).children(":first");
				//alert($(child).attr("class"));
				
				$(this).find('div:not(.autoPadDiv)').each(function(){
					convert(this);
				});
			}else {
				convert(this);
			}
		});
    };
	
	$.fn.grayscaleToggle = function() {
        return this.each(function(i){
			if ($.browser.msie){
				$(this).find('div:not(.autoPadDiv)').each(function(){
					swap(this);
				});//$('#ajaxDebug').append("<br> item " + i);
				
			}else {
				swap(this);
			}
		});
	};
	
	
	
})(jQuery);
