// ColorSelectScript
// Version 3.0 [10.02.2001]
// Copyright (c) 1999 - 2001, Pawel Kazakow

// Homepage: http://www.pkworld.de
// Email: info@pkworld.de

var version = 0; version = parseFloat(navigator.appVersion);
if (isNaN(version)) {version = 0;}

if (!document.all || version < 3.0) {location.href="nc_index.htm";}

var ffld, ffld2, ifld, fld1, fld2, txt, sc;
var hexlist = "0123456789ABCDEF";

var gstring = '';
for (i=1;i<85;i++) { gstring = gstring + "|"; }

var Checked = new Image();
 Checked.src = 'images/checked.gif';
var Unchecked = new Image();
 Unchecked.src = 'images/unchecked.gif';

function start()
{
 fld1 = document.all.col1; fld2 = document.all.col2;
 txt = document.all.text; ifld = document.all.helpfield;
 sc = document.all.sc;

 GetSettings();

 RefreshGradient(fld1.value, fld2.value);
 fld1.focus();

 TextInfos();

 InitPage("Webdesign | pkTextGradient");
}

function check(arg)
{
 var img = document.all.sci;
 var scval = 0;

 if (arg == -1) 
 {
  if (img.src.indexOf('un') == -1)
  {sc.value = ""; scval = 0;}
  else
  {sc.value = "0"; scval = 1;}
  SaveSettings(fld1.value+fld2.value+ifld.value, txt.value);  
 }
 else
 {
  if (arg == 1)
  {sc.value = "0";}
  else
  {sc.value = "";}
 }

 if (sc.value == "") 
 {
  img.src = Unchecked.src; 
 }
 else
 {
  img.src = Checked.src;
 }
}

function countspaces(str, norepeat)
{
 var count = 0, lasti = -1, firsti = 0, str2 = " ", str1 = str;
 for (i=0; i<str1.length; i++)
 {
  if (str1.substr(i,1) == str2)
  { 
   if (lasti < i - 1 && norepeat && i > 0)
   {
    count++;
   }
   lasti = i;
  }
  if (lasti == str1.length - 1 && count > 0) count = count-1;
 }
 return count;
}

function CalcLengthWithoutSpaces(str)
{
 var count = 0;
 for (i=0; i<str.length; i++)
 {
  if (str.substr(i,1) != " ")
  { 
   count++;
  }
 }
 return count;
}

function TextInfos(save)
{
 var infos = '';
 var RealLength = CalcLengthWithoutSpaces(txt.value);
 var Spaces = countspaces(txt.value, 1);

 infos = infos + "Textlänge: " + (RealLength + Spaces) + " Byte";
 infos = infos + " (" + Spaces + " Leerzeichen)&nbsp;";

 document.all.txtinfo.innerHTML = infos;
 
 if (save)
 {
  SaveSettings(fld1.value+fld2.value+ifld.value, txt.value);
 }
}

function RealText()
{
 var str = txt.value, lasti = -1, letter, newtxt = '';
 
 for (i=0; i<str.length; i++)
 {
  letter = str.substr(i,1);
  if (str.substr(i,1) == " ")
  { 
   if (lasti < i - 1 && i > 0) {}
   else {letter = "";}

   lasti = i;
  }
  newtxt = newtxt + letter;
 }

 if (lasti == str.length - 1 && count > 0) newtxt = newtxt.substr(0,newtxt.length-1);

 txt.value = newtxt;
 SaveSettings(fld1.value+fld2.value+ifld.value, txt.value);
}

function GetCellColor() {
 var cell = window.event.srcElement;

 if (cell.className == 'cc')
 {
  ffld2 = ffld;
  document.all[ffld].value = cell.bgColor.toUpperCase().substr(1,6);
  RefreshGradient(fld1.value, fld2.value);
 }
}

function SetOldFocus()
{
 var cell = window.event.srcElement;

 if (cell.className == 'cc' && (ffld2 == 'col1' || ffld2 == 'col2')){ document.all[ffld2].focus(); }
}

function GotFocus(obj)
{
 ffld = obj;
 document.all[ffld].select();
}

function LostFocus(obj)
{
 var fld = document.all[ffld];
 
 fld.value = checkhex(fld.value); 

 ffld = 'helpfield';
}

function KeyPressed()
{
 var fld = window.event.srcElement;
 var keycode = window.event.keyCode;
 var key = unescape("%"+DecToHex(keycode)).toUpperCase();

  if (hexlist.indexOf(key) != -1 || keycode == 8 || keycode == 46)
  {
   RefreshGradient(fld1.value, fld2.value);
  }
  
  if (hexlist.indexOf(key) != -1)
  {
   if (fld.value.length == 6 && fld.name == fld1.name) {fld2.focus();}
   if (fld.value.length == 6 && fld.name == fld2.name) {txt.focus();}
  }
}

function ContentChanged()
{
  var fld = window.event.srcElement;
  RefreshGradient(fld1.value, fld2.value);
  if (fld.value.length == 6 && fld.name == fld1.name) {fld2.focus();}
  if (fld.value.length == 6 && fld.name == fld2.name) {txt.focus();}
}

function checkhex(value)
{
 if (value.length < 6)
 {
  var ext = '';
  for (i=0;i<6-value.length;i++)
  {ext = ext + "0";}
  value = value + ext;
 }
 value = value.toUpperCase();

 var value2 = "";

 for (i=0;i<value.length;i++)
 {
  if (hexlist.indexOf(value.substr(i,1)) != -1)
  {value2 = value2 + value.substr(i,1);}
  else
  {value2 = value2 + "0";}
 }
 
 return value2;
}

function RefreshGradient(c1, c2)
{
 c1 = checkhex(c1);
 c2 = checkhex(c2);
 ci = checkhex(ifld.value);
 
 glayer.innerHTML = gradient(gstring, c1 + " " + c2);
 ilayer.innerHTML = gradient(gstring.substr(0,gstring.length/2), ci + " FFFFFF");
 SaveSettings(c1+c2+ci, txt.value);
}

function SaveSettings(colors, text)
{
 var now=new Date(), dat=new Date(now.getTime()+31536000000);

 while (text.indexOf(';') != -1)
 {text = text.replace(';','¬');}

  var scval = 0;
  if (sc.value == "0")
  {scval = 1;}
  else
  {scval = 0;}
 
 document.cookie="TextGradient="+colors+scval+text+"¤; expires="+dat.toGMTString()+";";
}

function GetSettings()
{
 var cname = "TextGradient=";

 if(document.cookie && document.cookie.indexOf(cname) != -1)
 {
  var settings = document.cookie.substring(document.cookie.indexOf(cname)+cname.length, document.cookie.length);

  while (settings.indexOf('¬') != -1)
  {settings = settings.replace('¬',';');}

  fld1.value = settings.substr(0,6);
  fld2.value = settings.substr(6,6);
  ifld.value = settings.substr(12,6);
  check(settings.substr(18,1));
  txt.value = settings.substring(19, settings.indexOf("¤"));
 }
 else
 {
  fld1.value = "FF0000"; fld2.value = "0000FF"; ifld.value = "000000"; txt.value = "";
 }
}

document.onmousedown = GetCellColor;
document.onmouseup = SetOldFocus;

var browser = "unbekannt";

if (navigator.userAgent.indexOf("Opera") >= 0)browser = "opera";
else if (navigator.userAgent.indexOf("obot") >= 0)browser = "robot";
else if (navigator.appName.indexOf("etscape") >= 0)browser = "netscape";
else if (navigator.appName.indexOf("icrosoft") >= 0)browser = "msie";

var tohex = new Array(256);
var hex = "0123456789ABCDEF";
var count = 0;
for (x=0; x<16; x++) {
 for (y=0; y<16; y++) {
 tohex[count] = hex.charAt(x) + hex.charAt(y);
 count++;
 }
}

function ColorCode(hexcode) {
  if (hexcode.length == 7) {
    this.r = parseInt(hexcode.substring(1,3),16);
    this.g = parseInt(hexcode.substring(3,5),16);
    this.b = parseInt(hexcode.substring(5,7),16);
  }
  else if (hexcode.length == 6) {
    this.r = parseInt(hexcode.substring(0,2),16);
    this.g = parseInt(hexcode.substring(2,4),16);
    this.b = parseInt(hexcode.substring(4,6),16);
  }
  else {
    this.r = this.g = this.b = 0;
    alert("Error: ColorCode constructor failed");
  }
  if (isNaN(this.r)||isNaN(this.g)||isNaN(this.b))
    alert("Error: ColorCode constructor failed");
}

function ColorList(hexcodes) {
  var i = 0;
  var c = 0;
  this.codes = new Array(Math.round(hexcodes.length/7));
  while (i < hexcodes.length) {
    if (isNaN(parseInt(hexcodes.substring(i,i+6),16))) ++i;
    else {
      this.codes[c] = new ColorCode(hexcodes.substring(i,i+6));
      i += 7;
      ++c;
    }
  }
  this.len = c;
}

function interpolate (x1, y1, x3, y3, x2) {
  if (x3 == x1) return y1
  else return (x2-x1)*(y3-y1)/(x3-x1) + y1
}

function lowcolorindex (x, y, z) {
  if (y == 1) return 0
  else return Math.floor( (x*(z-1))/(y-1) )
}

function hicolorindex (x, y, z, low) { 
  if ( low*(y-1) == x*(z-1) ) return low
  else if (y == 1) return 0
  else return Math.floor( (x*(z-1))/(y-1) + 1 )
}

function gradient (thetext,thecolors) {
  if (((browser == "netscape")||(browser == "msie")||(browser == "opera"))&&(version>=3.0))
  {
    var colors = new ColorList(thecolors);
    var numcolors = colors.len;
    var numchars = thetext.length;
    var rr = 0, gg = 0, bb = 0;
    var lci = 0; //lower color index
    var hci = 0; //high color index
    var retvar = '';

    for (i=0; i<numchars; ++i)
    {
      lci = lowcolorindex(i, numchars, numcolors); hci = hicolorindex(i, numchars, numcolors, lci);
      rr = Math.round(interpolate( lci/(numcolors-1), colors.codes[lci].r, hci/(numcolors-1), colors.codes[hci].r, i/(numchars-1)));
      gg = Math.round(interpolate( lci/(numcolors-1), colors.codes[lci].g, hci/(numcolors-1), colors.codes[hci].g, i/(numchars-1)));
      bb = Math.round(interpolate( lci/(numcolors-1), colors.codes[lci].b, hci/(numcolors-1), colors.codes[hci].b, i/(numchars-1)));
      if (browser == "opera"){rr = 255 - rr;gg = 255 - gg;bb = 255 - bb;}
      retvar = retvar + thetext.charAt(i).fontcolor(tohex[rr]+tohex[gg]+tohex[bb]);
    }
  }
  else
  {
   return thetext;
  }
//  return "&nbsp;" + retvar + "&nbsp;"; 
  return retvar;
}

function DecToHex(dec)
{		
 max = Math.pow(16,8);

 if(dec > max) { return; }
 if(dec < 0) { return; }

 var z = hexlist.split('');
 var x = '';

 var i = 1, v = dec, r = 0;
 while(v > 15)
  { v = Math.floor(v / 16); i++; }
 v = dec;
 for(j=i;j>=1;j--)
  {
   x = x + z[Math.floor(v / Math.pow(16,j-1))];
   v = v - (Math.floor(v / Math.pow(16,j-1)) * Math.pow(16,j-1));
  } 
 return x; 
}
