/* ** FUNCTIONS OF CLASS COLORSTYLE ** */

function ColorStyle_set_color_rgb( newr, newg, newb)
{ // newr, newg, newb are integers 0 to 255.
  var hx = "0123456789ABCDEF";
  this.r = newr;
  this.g = newg;
  this.b = newb;
  this.colorstring = "#"+hx.charAt(this.r>>4)+hx.charAt(this.r&15)+
      hx.charAt(this.g>>4)+hx.charAt(this.g&15)+
      hx.charAt(this.b>>4)+hx.charAt(this.b&15);
}

function ColorStyle_set_color_string( colstring)
{ // colstring is string (input), like "#ffffff" or "rgb(255,255,255)"
  if (colstring.indexOf("#") == 0)
    {
      this.colorstring = colstring;
      this.r = parseInt( this.colorstring.substring(1,3), 16);
      this.g = parseInt( this.colorstring.substring(3,5), 16);
      this.b = parseInt( this.colorstring.substring(5,7), 16);
    }
  else if (colstring.indexOf("rgb(") == 0)
    {
      var strar =
	  colstring.substring(4, colstring.indexOf(")")).split(",");
      this.set_color_rgb( parseInt(strar[0]),parseInt(strar[1]),parseInt(strar[2]));
    }
}

function ColorStyle_set_color_style( classname, numcssrule)
{ // classname is the name (string) of class for foreground text.
  // If just normal text (not special class), classname should be "BODY".
  // numcssrule is the number of rule in Style Sheet that gives color of 
  // this text. For example, 0 if first rule in Style Sheet.
  var css;  // style sheet rulelist
  var classcolor; // string

  if (document.all)    // IE4
    {
      css = document.styleSheets[0].rules;

      if (css[ numcssrule] != null)
	classcolor = css[ numcssrule].style.color;
    }
  else if (document.getElementById)    // NS6
    {
      css = document.styleSheets.item(0).cssRules;
      if (css.item( numcssrule) != null)
	classcolor = css.item( numcssrule).style.color;
    }
  else if (document.classes != null)     // NS4
    {
      if (document.classes[ classname] != null &&
	  document.classes[ classname].all.color != null)
	classcolor = document.classes[ classname].all.color;
      else if (document.tags[ classname] != null)
	classcolor = document.tags[ classname].color;
    }

  if (classcolor == null) classcolor = "#000000";
  this.set_color_string( classcolor);
}

function ColorStyle_set_background_style( bgclassname, numcssrule_bg)
{ // bgclassname is the name (string) of class that sets background color.
  // If just normal background (not special class), bgclassname should be "BODY".
  // numcssrule_bg is the number of rule in Style Sheet that gives 
  // background color. For example, 0 if first rule in Style Sheet.
  var css;  // style sheet rulelist
  var bgclasscolor; // string

  if (document.all)    // IE4
    {
      css = document.styleSheets[0].rules;
      if (css[ numcssrule_bg] != null)
	bgclasscolor = css[ numcssrule_bg].style.backgroundColor;
    }
  else if (document.getElementById)    // NS6
    {
      css = document.styleSheets.item(0).cssRules;
      if (css.item( numcssrule_bg) != null)
	bgclasscolor = css.item( numcssrule_bg).style.backgroundColor;
    }
  else if (document.classes != null)     // NS4
    {
      if (document.classes[ bgclassname] != null &&
	  document.classes[ bgclassname].all.backgroundColor != null)
	bgclasscolor = document.classes[ bgclassname].all.backgroundColor;
      else if (document.tags[ bgclassname] != null)
	bgclasscolor = document.tags[ bgclassname].backgroundColor;
    }

  if (bgclasscolor == null)
    if (document.bgColor != null)
      bgclasscolor = document.bgColor;
    else
      bgclasscolor = "#ffffff";

  this.set_color_string( bgclasscolor);
}

function ColorStyle_copy( src)
{
  this.colorstring = src.colorstring;
  this.r = src.r;
  this.g = src.g;
  this.b = src.b;
}

function ColorStyle()
{ /* CONSTRUCTOR */
  this.colorstring = "#000000";
  this.r = 0;
  this.g = 0;
  this.b = 0;
}

/* Member Functions of Class ColorStyle: */
ColorStyle.prototype.set_color_rgb = ColorStyle_set_color_rgb;
ColorStyle.prototype.set_color_string = ColorStyle_set_color_string;
ColorStyle.prototype.set_color_style = ColorStyle_set_color_style;
ColorStyle.prototype.set_background_style = ColorStyle_set_background_style;
ColorStyle.prototype.copy = ColorStyle_copy;
