var glob = '0';
var k_name = Array();
var k_title = Array();
var k_desc = Array();
var k_url = Array();
var k_siteurl = Array();
document.write("<style>");
document.write(".keyword {color: red; font-weight: bold; text-decoration: underline; cursor: pointer;}");
document.write(".container {display: none; position: absolute; cursor: pointer; left: 0; top: 0;}");
document.write(".table_hint {border-collapse: collapse; background: lightyellow; font-size: 8pt; font-family: Verdana, Arial, Helvetica, sans-serif; color: black; width: 250px; border-style: solid; border-width: 1px; border-color: black;}");
document.write(".cell_hint {padding: 3px; text-align: left;}");
document.write("</style>");
function getBodyScrollTop() {
   return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || 
      (document.body && document.body.scrollTop);
}

function jsLink(i) {
   window.open(k_url[i], '', '', false);
}

function replace(stPhrase, stPattern, i) {
   var stResult = new String(stPhrase);
   
   //var wre = new RegExp("smart_text_start(.*?)smart_text_end", "img");
   var matches = stResult.match(/<\!--smart_text_start-->[\s\S]*<\!--smart_text_end-->/);
   /*string=document.getElementsByTagName('span');
   matches = [];
   tempStr = '';
   for (i in string) {
      if (string[i].className == 'smart_text') {
         tempStr = '<span class="smart_text">' + string[i].innerHTML + '</span>';
         matches.push(tempStr);
      }
   }*/

   if (1 == glob) { // Highlight all keywords
      var re = new RegExp("(>[^<]*)(" + stPattern + ")([^>]*<)", "img");
   } else { // Highlight first keywords
      var re = new RegExp("(>[^<]*)(" + stPattern + ")([^>]*<)", "im");
   }
   for (j = 0; j < matches.length; j++) {
      var str = new String(matches[j]);
      //alert(str.match(re));
      str = str.replace(re, "$1<span id='key_" + i +
         "' class='keyword' onMouseOver='keyword_over(" + i + ", " + i +
         ")' onMouseOut='keyword_out()' onClick='jsLink(" + i + ")'>$2</span>$3");
      stResult = stResult.replace(matches[j], str);
      /*
      if (0 == glob) {
         var str = new String(matches[j]);
         if (str.match(re)) {
            break;
         }
      }
      */
   }


   /*var stResult = new String(stPhrase);
   if (1 == glob) {
      //var re = new RegExp("(>[^<]*\\b)(" + stPattern + ")(\\b[^>]*<)", "img");
      var re = new RegExp("(whatever>[^<]*\\b)(" + stPattern + ")(\\b[^>]*<\/whatever)", "img");
   } else {
      //var re = new RegExp("(>[^<]*\\b)(" + stPattern + ")(\\b[^>]*<)", "im");
      var re = new RegExp("(whatever>[^<]*\\b)(" + stPattern + ")(\\b[^>]*<\/whatever)", "im");
   }
   stResult = stResult.replace(re, "$1<span id='key_" + i +
      "' class='keyword' onMouseOver='keyword_over(" + i + ", " + i + 
      ")' onMouseOut='keyword_out()' onClick='jsLink(" + i + ")'>$2</span>$3");*/
   return stResult;
}

function showHint(i, j) {
   if ("mozilla" == browser) {
      table_hint = document.getElementById("table_hint");
      id_title = document.getElementById("id_title");
      id_desc = document.getElementById("id_desc");
      id_url = document.getElementById("id_url");
      clientWidth = window.innerWidth;
      clientHeight = window.innerHeight + getBodyScrollTop();
   } else {
      clientWidth  = window.document.body.clientWidth;
      clientHeight = window.document.body.clientHeight;
   }
   if (table_hint.name != i) {
      table_hint.name = i;
      id_title.innerHTML = "<b><span onClick='jsLink(" + i + ")' style='font-size: 8pt; font-family: Verdana, Arial, Helvetica, sans-serif;'>" + 
         k_title[i] + "</span></b>";
      id_desc.innerHTML = "<span onClick='jsLink(" + i + ")' style='font-size: 8pt; " +
         "font-family: Verdana, Arial, Helvetica, sans-serif;'>" + k_desc[i] + "</span>";
      id_url.innerHTML = "<span style='color: gray; text-decoration: underline; font-size: 8pt; " +
         "font-family: Verdana, Arial, Helvetica, sans-serif;' onClick='jsLink(" + i + ")'>" + 
         k_siteurl[i] + "</span>";

      table_hint.style.display = "block";
      if (clientWidth > mousex + 250) {
         table_hint.style.left = mousex + "px";
      } else {
         table_hint.style.left = (mousex - 260) + "px";
      }
      if (clientHeight > mousey + 20 + table_hint.clientHeight) {
         table_hint.style.top = (mousey + 20) + "px";
      } else {
         table_hint.style.top = (mousey - table_hint.clientHeight) + "px";
      }
   }
   window.clearTimeout(timeoutID);
}

function hideHint() {
   if ("mozilla" == browser) {
      table_hint = document.getElementById("table_hint");
   }
   if (table_hint.style.display != "none") {
      timeoutID =
         window.setTimeout("table_hint.style.display = 'none'; table_hint.name = -1;", 200);
   }
}

function table_hint_over() {
   window.clearTimeout(timeoutID);
}

function table_hint_out() {
   hideHint();
}

function keyword_over(i, j) {
   showHint(i, j)
}

function keyword_out() {
   hideHint();
}

function writeHint() {
   document.write('<div id="table_hint" class="container" onMouseOver="table_hint_over()" onMouseOut="table_hint_out()">');
   document.write('<table class="table_hint">');
   document.write('<tr><td id="id_title" class="cell_hint"></td></tr>');
   document.write('<tr><td id="id_desc" class="cell_hint"></td></tr>');
   document.write('<tr><td id="id_url" class="cell_hint"></td></tr></table></div>');
}

function setGlossary() {
   for (var i = 0; i < k_name.length; i++) {
      rat__word_to_inline_reg_exp = new RegExp(k_name[i], "img");
      rat__node_to_inline = rat__find_node_to_inline(rat__word_to_inline_reg_exp, document.getElementsByTagName('body')[0], k_name[i]);
      if ('undefined' != typeof(rat__node_to_inline) && null != rat__node_to_inline) {
         rat__inline_keyword(rat__node_to_inline, k_name[i], i);
      }
   }
}

//----------------------------------------------------------------------------------

function rat__find_node_to_inline(regular_expression_of_word_to_find, node_to_search, kw) {

   if (node_to_search.nodeType == 3) {
      if(node_to_search.data.match(regular_expression_of_word_to_find)) {
         return node_to_search;
      }

   } else {
      var rat__node_to_inline = null;
      for (var next_node = node_to_search.firstChild; next_node != null; next_node = next_node.nextSibling) {
         var include_node_in_gathering = true;

         // this will ignore any tag that has a class, id, or name that contains the word head
         // this is a work around for headings that are not in a h or title tag
         if ( next_node.className != null ) {
            if (next_node.className.indexOf('noInline') != -1 ) {
               include_node_in_gathering = false;
            }

            if (next_node.className.indexOf('head') != -1) {
               include_node_in_gathering = false;
            }
         }

         if ( next_node.id != null ) {
            if ( next_node.id.indexOf('noInline') != -1) {
               include_node_in_gathering = false;
            }
            
            if (next_node.id.indexOf('head') != -1) {
               include_node_in_gathering = false;
            }
         }

         // don't enclude text nodes that have these nodes as parents
         if (next_node.nodeName == 'A')
            include_node_in_gathering = false;

         if (next_node.nodeName == 'H1')
            include_node_in_gathering = false;

         if (next_node.nodeName == 'H2')
            include_node_in_gathering = false;

         if (next_node.nodeName == 'H3')
            include_node_in_gathering = false;

         if (next_node.nodeName == 'H4')
            include_node_in_gathering = false;
         
         if (next_node.nodeName == 'H5')
            include_node_in_gathering = false;
            
         if (next_node.nodeName == 'H6')
            include_node_in_gathering = false;

         if (next_node.nodeName == 'TH')
            include_node_in_gathering = false;

         if (next_node.nodeName == 'SCRIPT')
            include_node_in_gathering = false;

         if (next_node.nodeName == 'NOSCRIPT')
            include_node_in_gathering = false;

         if (next_node.nodeName == 'SELECT')
            include_node_in_gathering = false;

         if (next_node.nodeName == 'IFRAME')
            include_node_in_gathering = false;

         if (next_node.nodeName == 'TITLE')
            include_node_in_gathering = false;

         if (next_node.nodeName == 'TEXTAREA')
            include_node_in_gathering = false;

         if(include_node_in_gathering == true) {
            rat__node_to_inline = rat__find_node_to_inline(regular_expression_of_word_to_find, next_node, kw);

            if (rat__node_to_inline != null) break;

         }
      }
      return rat__node_to_inline;      
   }
}

function rat__inline_keyword(node_to_modify, word_to_inline, ii) {
   var broken_apart_text = null;
   var pre_text = null;
   var post_text = '';
   var inline_text = null;
   var inline_tag = null;
   var pre_broken_node_text = '';
   if ('undefined' != typeof(node_to_modify.nodeValue)) {
      pre_broken_node_text = node_to_modify.nodeValue; // this is for ie
   }
   var parent_node = node_to_modify.parentNode;

   rat__word_to_inline_reg_exp = new RegExp((word_to_inline), "img");
   broken_apart_text = node_to_modify.data.split(rat__word_to_inline_reg_exp);
   
   if (broken_apart_text.length == 0) {
      // this happens when the word_to_inline is the only word in the node
      pre_text = '';
      post_text = '';
      pre_text = document.createTextNode(pre_text);
      post_text = document.createTextNode(post_text);
      
   } else if (broken_apart_text.length == 1) {
      // happens when the word_to_inline is either at the beginning or the end of the node
      //if (pre_broken_node_text.indexOf(word_to_inline) > -2 && pre_broken_node_text.indexOf(word_to_inline) < 1) {
      var re = new RegExp("^" + word_to_inline, "i");
      if (pre_broken_node_text.match(re)) {
         // word to inline is at the beginning of the node
         pre_text = '';
         post_text = broken_apart_text[0];
      } else {
         // word to inline is at the end of the node
         pre_text = broken_apart_text[0];
         post_text = '';
      }
      pre_text = document.createTextNode(pre_text);
      post_text = document.createTextNode(post_text);
      
   } else {
      if (broken_apart_text.length > 2) {
         for (var i = 1; i < (broken_apart_text.length -1); i++) {
            post_text += broken_apart_text[i] + word_to_inline;
         }
         post_text += broken_apart_text[(broken_apart_text.length - 1)];
      } else {
         post_text = broken_apart_text[1];
      }
      pre_text = document.createTextNode(broken_apart_text[0]);
      post_text = document.createTextNode(post_text);

   }
   
   // create the inlined keyword tag
   inline_tag = document.createElement('SPAN');

   toMatch = (typeof(node_to_modify.nodeValue) != 'undefined') ? node_to_modify.nodeValue : (typeof(node_to_modify.data) != 'undefined' ? node_to_modify.data : '');
   var re = new RegExp(word_to_inline, "ig");
   var matches = toMatch.match(re);
   if (0 < matches.length) {
      inline_tag.innerHTML = matches[0];
      inline_tag.className = 'keyword';
      inline_tag.id = 'key_' + i;
      inline_tag.onmouseover = function() {
         keyword_over(ii, ii);
      }
      inline_tag.onmouseout = function() {
         keyword_out();
      }
      inline_tag.onclick = function() {
         jsLink(ii);
      }
   }
   
   parent_node.replaceChild(post_text, node_to_modify);
   parent_node.insertBefore(inline_tag, post_text);
   parent_node.insertBefore(pre_text, inline_tag);
   
}

writeHint();

var timeoutID = 0;
var id_key = -1;
var mousex = 0;
var mousey = 0;

if (document.all) {
   browser = "ie";
} else if (navigator.userAgent.indexOf("Opera")>-1) {
   browser = "opera";
} else {
   browser = "mozilla";
}

if ("mozilla" == browser) {
   document.onmousemove = function(e) {
      mousex = e.pageX;
      mousey = e.pageY;
      return true;
   }
} else {
   document.onmousemove = function() {
      mousex=event.clientX + document.body.scrollLeft;
      mousey=event.clientY + getBodyScrollTop();
      return true;
   }
}

xonload = window.onload;
if ('function' == typeof(xonload)) {
   window.onload = function() {
      xonload();
      setGlossary();
   };
} else {
   window.onload = setGlossary;
}
