// Copyright (C) 2000-2003 by iPhrase Technologies, Inc. All rights reserved.
// iPhrase is a registered trademark and One Step is a trademark of iPhrase Technologies, Inc.
// The software accessible via these pages may be protected by one or more U.S. and international patents.

fgcolors = ["black", "black", "black", "black", "black", "white", "white", "white", "white", "white"];
bgcolors = ["#ffff66", "#A0FFFF", "#99ff99", "#ff9999", "#ff66ff", "#880000", "#00aa00", "#886800", "#004699", "#990099"];
hlKey = "&iphl=";
hlName = "_iphl";
colorIdx = 0;

function colorPair(fg, bg)
{
  this.fg = fg;
  this.bg = bg;
}

function hiliteWord(hlReString,hlColors,element)
{
  reWords = new RegExp(hlReString,"i");
  var i;
  for (i=0; i<element.childNodes.length; i++)
  {
    childNode = element.childNodes[i];

    if (typeof childNode.htmlFor != "undefined")
    {
      // Ignore
    }
    else if (childNode.childNodes.length)
    {
      hiliteWord(hlReString,hlColors,childNode);
    }
    else if (childNode.nodeType == 3 /*childNode.TEXT_NODE*/)
    {
      // Don't reprocess already highlighted words
      if (childNode.parentNode.className == hlName) {
        continue;
      }

      // Find all word matches
      var j;
      done = 0;
      while (!done)
      {
        // Get location of first match
        j = childNode.data.search(reWords);
        if (j == -1) {
          done = 1;
          break;
        }

        // Get the first matching word
        words = childNode.data.match(reWords);
        if (words.length == 0) {
          done = 1;
          break;
        }
        word = words[0].toLowerCase()

        // Skip invalid words
        if (!hlColors[word]) {
          done = 1;
          break;
        }

	if (hlColors[word].fg == "") {
          hlColors[word].fg = fgcolors[colorIdx%fgcolors.length];
          hlColors[word].bg = bgcolors[colorIdx%bgcolors.length];
          colorIdx++;
        }

	// Get the post, hilited, and prior texts
	k = j + word.length;
	l = childNode.data.length
	postText = childNode.data.substr(k,l);
	hiliteText = childNode.data.substr(j,word.length);
        priorText = childNode.data.substr(0,j);

	// Just keep the prior text in the child node
        childNode.data = priorText

	// Create node for the hilited text
        hlNode = document.createElement("a");
        hlNode.setAttribute("name",hlName);
        hlNode.setAttribute("id",hlName);
        hltNode = document.createElement("span");
        hltNode.style.color = hlColors[word].fg;
        hltNode.style.background = hlColors[word].bg;
        hltNode.className = hlName;
        hltNode.appendChild(document.createTextNode(hiliteText));
        hlNode.appendChild(hltNode);

	// Create node for the post text
        postNode = document.createTextNode(postText);

        // Insert post text node and the hilite node
        if (i == (element.childNodes.length - 1)) {
          element.appendChild(postNode);
        }
        else {
          element.insertBefore(postNode,element.childNodes[i+1]);
        }
        element.insertBefore(hlNode,postNode);
        i += 2;
        childNode = postNode;
      }
    }
  }
}

function iphraseSearchHilite() {

  // Get hilite terms
  if (!document.createElement) return;
  url = document.URL;
  kidx = url.indexOf(hlKey);
  if (kidx == -1) return;
  hlString = url.substr(kidx+hlKey.length);
  hlList = hlString.split(":");
  hlList.sort(function(a,b) {return (b.length - a.length);});

  // Construct hilite regex
  hlReString = '(';
  var hlColors = new Object();
  for (i=0;i<hlList.length;i++) {
    hlWord = unescape(hlList[i].replace(/\+/g," ")).toLowerCase();
    if (hlWord != "") {
      if (i>0) {
        hlReString = hlReString + "|";
      }
      hlColors[hlWord] = new colorPair("","");
      hlWord = hlWord.replace(/([\\.\\*\\+\\?\\|\/])/g,"\\$1");
      hlReString = hlReString + "\\b" + hlWord + "\\b";
    }
  }
  hlReString = hlReString + ')';

  // Hilite terms
  hiliteWord(hlReString,hlColors,document.documentElement);

  // Goto specified anchor
  hash = document.location.hash;
  if (hash.length == 0) {
    document.location.hash = "#" + hlName;
  }
  else {
    x = hash.indexOf(hlKey);
    if (x != -1) {
      oHash = hash.substr(0, x);
      document.location.hash = oHash;
    }
  }
}

// Run hiliting on window load
window.onload = iphraseSearchHilite;
