function addLoadEvent(func) { 
var oldonload = window.onload; 
if (typeof window.onload != 'function') { 
  window.onload = func; 
} else { 
  window.onload = function() { 
    if (oldonload) { 
      oldonload(); 
    } 
      func(); 
    } 
  } 
}

addLoadEvent(start_sepia);

function RGBColor(color_string)
{
    this.ok = false;

    // strip any leading #
    if (color_string.charAt(0) == '#') { // remove # if any
        color_string = color_string.substr(1,6);
    }

    color_string = color_string.replace(/ /g,'');
    color_string = color_string.toLowerCase();

    // before getting into regexps, try simple matches
    // and overwrite the input
    var simple_colors = {

    };
    for (var key in simple_colors) {
        if (color_string == key) {
            color_string = simple_colors[key];
        }
    }
    // emd of simple type-in colors

    // array of color definition objects
    var color_defs = [
        {
            re: /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/,
            example: ['rgb(123, 234, 45)', 'rgb(255,234,245)'],
            process: function (bits){
                return [
                    parseInt(bits[1]),
                    parseInt(bits[2]),
                    parseInt(bits[3])
                ];
            }
        },
        {
            re: /^(\w{2})(\w{2})(\w{2})$/,
            example: ['#00ff00', '336699'],
            process: function (bits){
                return [
                    parseInt(bits[1], 16),
                    parseInt(bits[2], 16),
                    parseInt(bits[3], 16)
                ];
            }
        },
        {
            re: /^(\w{1})(\w{1})(\w{1})$/,
            example: ['#fb0', 'f0f'],
            process: function (bits){
                return [
                    parseInt(bits[1] + bits[1], 16),
                    parseInt(bits[2] + bits[2], 16),
                    parseInt(bits[3] + bits[3], 16)
                ];
            }
        }
    ];

    // search through the definitions to find a match
    for (var i = 0; i < color_defs.length; i++) {
        var re = color_defs[i].re;
        var processor = color_defs[i].process;
        var bits = re.exec(color_string);
        if (bits) {
            channels = processor(bits);
            this.r = channels[0];
            this.g = channels[1];
            this.b = channels[2];
            this.ok = true;
        }

    }

    // validate/cleanup values
    this.r = (this.r < 0 || isNaN(this.r)) ? 0 : ((this.r > 255) ? 255 : this.r);
    this.g = (this.g < 0 || isNaN(this.g)) ? 0 : ((this.g > 255) ? 255 : this.g);
    this.b = (this.b < 0 || isNaN(this.b)) ? 0 : ((this.b > 255) ? 255 : this.b);

    // some getters
    this.toRGB = function () {
        return 'rgb(' + this.r + ', ' + this.g + ', ' + this.b + ')';
    }
    this.toHex = function () {
        var r = this.r.toString(16);
        var g = this.g.toString(16);
        var b = this.b.toString(16);
        if (r.length == 1) r = '0' + r;
        if (g.length == 1) g = '0' + g;
        if (b.length == 1) b = '0' + b;
        return '#' + r + g + b;
    }
	
	this.toGrayHex = function() {
		var gray = (Math.round((this.r + this.g + this.b) / 3 )).toString(16);
		var gray1 = (Math.round((this.r + this.g + this.b) / 3.5 )).toString(16);
		return '#' + gray + gray + gray1;
	}

}




function getStyle(el,styleProp)
{
	if (el.currentStyle)
		var y = el.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
	return y;
}

function d2h(d) {return d.toString(16);} // decimal to hex
function h2d(h) {return parseInt(h,16);} // hex to decimal 


function start_sepia() {
	
	var path = document.location.pathname;
	var dir = path.substr(path.indexOf('/'), path.lastIndexOf('/')+1);
	//alert(document.location.href.indexOf('sepia'));
	if (document.location.href.indexOf('?sepia')==-1) return 0;
	
	for ( var i = 0; i < document.images.length; i++ ) {
		if (document.images[i].src.indexOf('sepia.webworks.bg/i.php')==-1) 
			document.images[i].src = 'http://sepia.webworks.bg/i.php?src=' + escape(document.images[i].src);
	}
	
	
	//alert(path);
	
	var elements = document.getElementsByTagName("*");
	for(var i=0; elements[i]; i++) try {
		
		var e = elements[i];
		
		if (e.nodeName == 'EMBED' || e.nodeName == 'OBJECT') continue;
		
		var bimg = getStyle(e,'backgroundImage');
		if ( !bimg ) bimg = getStyle(e,'background-image');
		
		//alert(bimg);
		
		if ( bimg && ( bimg != 'none' )) {
			//pos = e.style.backgroundPosition;
			mask=/^url\([\'"]*([^)\'"]+)[\'"]*\)$/;
			var part = mask.exec(bimg);
				
			url = part[1];
			var bi = '' 
			if ( url.indexOf('http://') == 0 ) {			
				bi = 'url(http://sepia.webworks.bg/i.php?src=' + escape(url) + ')';
			} else if ( url.indexOf('/') == 0 ) {
				bi = 'url(http://sepia.webworks.bg/i.php?src=http://' + document.domain + escape(url) + ')';
			} else {
				bi = 'url(http://sepia.webworks.bg/i.php?src=http://' + document.domain + dir + escape(url) + ')';
			}
			e.style.backgroundImage = bi;
			//e.style.backgroundPosition = pos;
			//alert( bi );
		}  
		
		var bcol = getStyle(e,'backgroundColor') ;
		if(!bcol) bcol = getStyle(e,'background-color');

		if (bcol && ( bcol != 'transparent' ) && ( bcol.indexOf('rgba')==-1) ) {
			newc = new RGBColor(bcol);
			e.style.backgroundColor = newc.toGrayHex();
		}
		
		var bcol = getStyle(e,'color') ;
		if (bcol) {
			newc = new RGBColor(bcol);
			e.style.color = newc.toGrayHex();
		}
		
		var bcol = getStyle(e,'borderColor') ;
		if(!bcol) bcol = getStyle(e,'border-color');

		if (bcol && ( bcol != 'transparent' ) ) {
			newc = new RGBColor(bcol);
			e.style.borderColor = newc.toGrayHex();
		}
	
		
		
		if (e.src && e.src.indexOf('sepia.webworks.bg/i.php')==-1) 
			e.src = 'http://sepia.webworks.bg/i.php?src=' + escape(e.src);
		
	} catch(err) {}
	
	
}