//  (c) 2003-2005 SurfWax Inc. All rights reserved. Patents pending.
//  This javascript and the algorithms are Copyright 2004-2005 SurfWax Inc.
//    and nay not be copied or re-used in any form or in any manner.

var searchField = null;
var desiredWidth = 400;
var desiredHeight = 400;
var alignment = "left";
var laDiv = null;
var on = false;

var lastRequest;
var lastResponse;
var upCheck = 30*1000;
var downThreshold = 30*1000;
var down = false;

var WAIT = 200;
var TEST = 100;
var changed = false;
var lastChange = 0;

var msgHeight = 40;

var xmlhttp = false;
var url = "http://lookahead.surfwax.com/servlet/com.surfwax.rssLA.Query?t=";

function g(r) {
  parent.location = "http://lookahead.surfwax.com/servlet/com.surfwax.rssLA.Fetch?r=" + r;
}

function make(aEntry, aStoryRSN, aSource, aPublished) {
  laDiv.style.height = desiredHeight + "px";
  scrub();
  for (var i = 0; i < aEntry.length; i++) {
    var dA = document.createElement('DIV');
    dA.id = 'a';
    var an = document.createElement('A');
    an.href = 'javascript:g(' + aStoryRSN[i] + ')';
    an.innerHTML = aEntry[i];
    dA.appendChild(an);
    laDiv.appendChild(dA);
    
    var dB = document.createElement('div');
    dB.id = 'b';
    dB.innerHTML = aSource[i];
    laDiv.appendChild(dB);

    var dC = document.createElement('div');
    dC.id = 'c';
    dC.innerHTML = aPublished[i];
    laDiv.appendChild(dC);
  }
}

function noResults() {
  laDiv.style.height = msgHeight + "px";
  scrub();
  var d = document.createElement('DIV');
  d.id = 'mb';
  d.innerHTML = 'No matching terms';
  laDiv.appendChild(d);
}

function getPosition(element, attribute) {
  p = 0;
  while (element) {
    p += element[attribute];
    element = element.offsetParent;
  }
  return p;
}

function getX() {
  var x = false;
  if (window.XMLHttpRequest) {
    try {
      x = new XMLHttpRequest();
    }
    catch (e) {
      x = false;
    }
  }
  else if (window.ActiveXObject) {
    try {
      x = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e) {
      try {
        x = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e) {
        x = false;
      }
    }
  }
  return x;
}

function getData(s) {
  xmlhttp = getX();
  if (xmlhttp) {
    xmlhttp.onreadystatechange = xChange;
    xmlhttp.open("GET", url + escape(s).replace(/\+/g, '%2B'), true);
    xmlhttp.send("");
  }
}

function xChange() {
  if (xmlhttp.readyState == 4)
    if (xmlhttp.status == 200)
      if (xmlhttp.responseText)
        eval(xmlhttp.responseText);
}

function setup(field, width, height, align) {
  searchField = field;
  desiredWidth = width;
  desiredHeight = height;
  searchField.autocomplete = "off";

  document.onclick = onEventH;
  document.onkeypress = onEventH;
  window.onresize = resizeH;

  laDiv = document.createElement("div");
  laDiv.id = "la";
  laDiv.style.border = "black 1px solid";
  laDiv.style.padding = "0";
  laDiv.style.zIndex = "1";
  laDiv.style.position = "absolute";
  laDiv.style.top = getPosition(searchField, "offsetTop") + searchField.offsetHeight - 1 + "px";
  if (align && align == 'right') {
    alignment = align;
    right = getPosition(searchField, "offsetLeft") + searchField['offsetWidth'];
    laDiv.style.left = right - desiredWidth;
  }
  else 
    laDiv.style.left = getPosition(searchField, "offsetLeft") + "px";
  laDiv.style.width = desiredWidth + "px";
  laDiv.style.height = desiredHeight + "px";
  laDiv.style.backgroundColor = "white";
  laDiv.style.visibility = "hidden";
  laDiv.style.overflow = "auto"; 
  document.body.appendChild(laDiv);

  setTimeout("up()",upCheck);
}

function up() {
  if (lastRequest && lastResponse) {
    howLong = lastResponse - lastRequest;
    if (howLong > downThreshold) {
      down = true;
      alert("LookAhead is not responding.");
    }
  }
  setTimeout("up()",upCheck);
}
 
function onEventH(event) {
  if (!event)
    var event = window.event;
  var element;
  if (event.target)
    element = event.target;
  else if (event.srcElement)
    element = event.srcElement;
  if (event.nodeType == 3)
    element = element.parentNode;
  if (element == searchField) {
    if (on)
      showDiv("la");
  }
  else {
    hideDiv("la");
  }
}

function resizeH() {
  laDiv.style.top = getPosition(searchField, "offsetTop") + searchField.offsetHeight - 1 + "px";
  if (alignment == 'right') {
    right = getPosition(searchField, "offsetLeft") + searchField['offsetWidth'];
    laDiv.style.left = right - desiredWidth;
  }
  else
    laDiv.style.left = getPosition(searchField, "offsetLeft") + "px";
}

function changeVal() {
  lastChange = (new Date()).getTime();
  if (!changed)
    changed = true;
}

function check() {
  if (changed) {
    var now = (new Date()).getTime();
    if (now >= lastChange + WAIT) {
      changed = false;
      curString = searchField.value.toLowerCase();
      if (curString.length < 2) {
        if (on) {
	  scrub();
	  hideDiv("la");
	  on = false;
        }
      }
      else {
        if (!on) {
	  showDiv("la");
	  on = true;
	}
        getData(searchField.value.toLowerCase());
      }
    }
  }
  setTimeout("check()", TEST);
}

setTimeout("check()", TEST);

function scrub() {
  while (laDiv.childNodes.length > 0)
    laDiv.removeChild(laDiv.childNodes[0]);
}

function setDiv(curString) {
  if (down) {
    showDownPage();
    return;
  }
  if (curString.length < 2) {
    hideDiv("la");
    on = false;
    document.getElementById("laFrame").src = "/blank.html";
  }
  else {
    showDiv("la");
    on = true;
    document.getElementById("laFrame").src = "/servlet/com.surfwax.rssLA.Query?t=" + escape(curString);
  }
}

function showDiv(divid)
{
   if (document.layers)
     document.layers[divid].visibility = "show";
   else
     document.getElementById(divid).style.visibility = "visible";
}

function hideDiv(divid)
{
   if (document.layers)
     document.layers[divid].visibility = "hide";
   else
     document.getElementById(divid).style.visibility = "hidden";
}

function showDownPage() {
  document.write("<html><body><h2>LookAhead Alert</h2>LookAhead is temporarily unavailable. Please try us again");
  document.write(" in a few minutes. Sorry for the inconvenience</body?</html?");
}

function swin(url) {
  window.open(url, 'LookAhead', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=320,height=360,left=200,top=100'); 
}

function swin2(url) {
  window.open(url, 'LookAhead', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=320,height=450,left=200,top=100'); 
}
