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;
	}

}



// JavaScript Document

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function ww_load() {
	
	for ( var i = 0; i < document.images.length; i++ ) {
		MM_preloadImages('http://sepia.webworks.bg/i.php?src=' + escape(document.images[i].src));
	}
}
ww_load();

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() {
	
	//setTimeout('window.open("http://www.nationalgeographic.bg");', 7000 );
	
	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);
	}
	
	var path = document.location.pathname;
	var dir = path.substr(path.indexOf('/'), path.lastIndexOf('/')+1);
	//alert(dir);
	
	var elements = document.getElementsByTagName("*");
	for(var i=0; elements[i]; i++) try {
		
		var e = elements[i];
		
		if (e.nodeName == 'embed' || e.nodeName == 'object') alert(1);
		
		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();
		}
	
		var bimg = getStyle(e,'backgroundImage');
		if ( !bimg || (bimp=='none')) bimg = getStyle(e,'background-image');
		if ( bimg && ( bimg != 'none' )) {
			pos = e.style.backgroundPosition;
			
			mask=/^url\([\'"]*([^)\'"]+)[\'"]*\)$/;
			var part = mask.exec(bimg);
			url = part[1];
			if ( url.indexOf('http://') == 0 ) {			
				e.style.backgroundImage= 'url(http://sepia.webworks.bg/i.php?src=' + escape(url);
			} else if ( url.indexOf('/') == 0 ) {
				e.style.backgroundImage= 'url(http://sepia.webworks.bg/i.php?src=http://' + document.domain + escape(url);
			} else {
				e.style.backgroundImage= 'url(http://sepia.webworks.bg/i.php?src=http://' + document.domain + dir + escape(url);
			}
			e.style.backgroundPosition = pos;
			//alert(bimg+ '|' + url + '|' + url.indexOf('http://') + '|' + e.style.backgroundImage );
		} 
		
		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) {}
	
	
}
