// The basis of this script came from www.TwinHelix.com
// Please check this site for the real script and licensing terms
// Under no circumstances should you use this script on your own website
// without getting the "real" version from TwinHelix

// An array of colours that will be applied to H1 tags.
//var h1Cols = ['#FFFFFF', '#DDDDDD', '#BBBBBB', '#999999', '#777777', '#555555'];
var h1Cols = ['#FFFFFF', '#CCCCFF', '#9999FF', '#6666FF', '#3333FF', '#0000FF'];

// Main script.
var h1Arr = [];
var wFactor = 0;
function h1Setup()
{
 h1Arr = document.getElementsByTagName('h1');
 for (var h = 0; h < h1Arr.length; h++)
 {
  var h1 = h1Arr[h], text = h1.firstChild.nodeValue;
  h1.removeChild(h1.firstChild);
  h1.animNodes = [];
  wFactor = text.length;
  if (wFactor < 1) wFactor = 99;
  if (wFactor > 21) {
	  wFactor = 0
  } else {
	  //wFactor = (14 / (wFactor ^ 3.9));
	  wFactor = 0.6;
  };

  for (var i = 0; i < text.length; i++)
  {
   var span = document.createElement('span');
   span.appendChild(document.createTextNode(text.substring(i, i+1)));
   h1.appendChild(span);
   h1.animNodes[h1.animNodes.length] = span;
  }
  h1.animCount = 0;
  h1.animTimer = setInterval('h1Anim(' + h + ')', 30);
 }
};

function h1Anim(h)
{
 var h1 = h1Arr[h], c = h1.animCount++, noAnim = 1, h1Length = h1.animNodes.length;
 for (var i = 0; i < h1.animNodes.length; i++)
 {
  var s = h1.animNodes[i], frac = Math.max(0, Math.min(1, (c-i)/10)),
   marg = document.all && !window.opera ? 'marginRight' : 'marginLeft';
  if (s.animDone) continue;
  noAnim = 0;
  s.style.color = h1Cols[Math.floor(frac * h1Cols.length)];
  if (frac == 1)
  {
   s.style[marg] = '0';
   s.animDone = 1;
  }
  else s.style[marg] = (wFactor)*(1-frac) + 'em';
 }
 if (noAnim) clearInterval(h1.animTimer);
 h1.style.visibility = 'inherit';
};

if (document.documentElement)
{
 // Hide H1 elements for animation and trigger show on load.
 //document.write('<style type="text/css"> h1 { visibility: hidden } </style>');
 document.write('<style type="text/css"> h1 </style>');
 var h1aOL = window.onload;
 window.onload = function()
 {
  if (h1aOL) h1aOL();
  h1Setup();
 }
}
