//  (c) 2003-2007 SurfWax Inc. All rights reserved. Patents pending.
//  This javascript and the algorithms herein are Copyright 2003-2006 SurfWax Inc.
//    and may not be copied or re-used in any form or in any manner.
//  Do not remove this statement.

var laEnabled = true;
var checking = true;

var userID;
var searchField;
var desiredWidth;
var desiredHeight;
var alignment;
var laDiv;
var on;
var changed;
var lastChange;
var haveResults;

var DOWN_THRESHOLD = 30*1000;
var requestSent = null;
var resultReceived = false;
var msgHeight = 40;

var WAIT = 200;
var TEST = 100;

KEY_UP = 38;
KEY_DOWN = 40;
KEY_ENTER = 13;
var SELECTED_BG = "#dddddd";
var UNSELECTED_BG = "#fff";
var curLength;
var curSelected;
var keyCaught;

var url = "http://lookahead.surfwax.com/servlet/com.surfwax.blogLA.Query?u=";
var fetch = "http://lookahead.surfwax.com/servlet/com.surfwax.blogLA.Fetch?u=";

function make(aEntry, aRSN, aPublished, aTitle) {
  setResults();
  var prevRSN = 0;
  for (var i = 0; i < aRSN.length; i++) {

    var dD = document.createElement('div');
    dD.name = i+1;
      
    var dC = document.createElement('div');
    dC.id = 'c';
    var dE = document.createElement('div');
    dE.id = 'e';
    if (aRSN[i] != prevRSN) {
      dC.innerHTML = aPublished[i];
      dE.innerHTML = aTitle[i];
    }
    else {
      dC.innerHTML = "&nbsp;";
      dE.innerHTML = "&nbsp;";
    }

    var dA = document.createElement('DIV');
    dA.id = 'a';
    if (aRSN[i] != prevRSN && prevRSN != 0) {
      dA.style.borderTop = '#CCCCCC 1px solid';
      dC.style.borderTop = '#CCCCCC 1px solid';
      dE.style.borderTop = '#CCCCCC 1px solid';
    }
        
    var an = document.createElement('A');
    an.href = 'javascript:g(' + aRSN[i] + ')';
    // use this line to put the selected item into the search field:
    // an.href = "javascript:setF('" + aEntry[i].replace(/<\/?[^>]+(>|$)/g, "") + "')";
    an.innerHTML = aEntry[i];

    dA.appendChild(an);
    dD.appendChild(dC);
    dD.appendChild(dE);
    dD.appendChild(dA);
    dD.onmouseover = hiliteIt;
    dD.onmouseout = unhiliteIt;
    laDiv.appendChild(dD);
    prevRSN = aRSN[i];
  }
  curLength = aRSN.length;
  curSelected = 0;
}

function noResults() {
  setNoResults();
  var d = document.createElement('DIV');
  d.id = 'mb';
  d.innerHTML = 'No LookAhead matches; backspace to change or press Enter to search deeper';
  laDiv.appendChild(d);
}
 
function setResults() {
  stopMe();
  if (!haveResults) {
    haveResults = true;
    laDiv.style.height = desiredHeight + "px";
  }
}

function setNoResults() {
  stopMe();
  if (haveResults) {
    haveResults = false;
    laDiv.style.height = msgHeight + "px";
  }
}

function start() {
  if (resultReceived)
    resultReceived = false;
}

function stopMe() {
  if (requestSent != null) {
//    t = (new Date()).getTime() - requestSent.getTime();
//    window.status = "Processing time: " + t + " ms";
    requestSent = null;
    resultReceived = true;
  }
}

function enableLA() {
  if (!laEnabled) {
    laEnabled = true;
    if (!checking) {
      check();
      checking = true;
    }
  }
}

function disableLA() {
  laEnabled = false;
}

function getData(s) {
  scrub();
  start();
  script = document.createElement("SCRIPT");
  script.src = url + userID + "&t=" + escape(s).replace(/\+/g, '%2B');
  laDiv.appendChild(script);
}

function g(r) {
  this.location = fetch + userID + "&r=" + r;
}
  
function r() {
  var curString = searchField.value.toLowerCase();
  if (curString.length >= 2) {
    showDiv("la");
    on = true;
    getData(curString);
  }
}

function unavailable() {
  setNoResults();
  var d = document.createElement('DIV');
  d.id = 'mb';
  d.innerHTML = 'LookAhead is not responding';
  laDiv.appendChild(d);
}

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

function doh() {
  searchField.autocomplete = "off";
  searchField.setAttribute("autocomplete","off"); // for firefox
  searchField.onkeydown = onKeyEventH;
  searchField.blur();

  document.onclick = onEventH;
  window.onresize = resizeH;
  
  laDiv = document.createElement("div");
  laDiv.id = "la";
  laDiv.className = "g";
  laDiv.style.border = "black 1px solid";
  laDiv.style.padding = "0";
  laDiv.style.zIndex = "1001";
  laDiv.style.position = "absolute";
  laDiv.style.top = getPosition(searchField, "offsetTop") + searchField.offsetHeight - 1 + "px";
  if (alignment == 'right') {
    x = getPosition(searchField, "offsetLeft") + searchField['offsetWidth'] - desiredWidth;
    laDiv.style.left = x + "px";
  }
  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);
}

function setup(id, field, width, height, align) {
  userID = id;
  searchField = field;
  desiredWidth = width;
  desiredHeight = height;
  on = false;
  changed = false;
  lastChange = 0;
  if (align && align == 'right')
    alignment = 'right';
  else
    alignment = 'left';
  haveResults = false;
  curSelected = 0;
  keyCaught = false;
  doh();
}
       
function setF(s) {
  i = s.indexOf(",&nbsp;");
  if (i == -1) 
    searchField.value = s;
  else
    searchField.value = s.substring(i + 8) + ' ' + s.substring(0, i);
  searchField.select();
  searchField.focus();
}

function onKeyEventH(event) {
  if (laEnabled) {
    if (!event)
      event = window.event;
      if (event.type == "keyup")
        return true;
    var key = event.keyCode;
    if (key == KEY_ENTER) {
      if (curSelected > 0) {
        var curDiv = laDiv.childNodes[curSelected].childNodes[1];
	var href = curDiv.childNodes[0].href;
//	alert(href);
	eval(href);
	return false;
      }
    }
    else if (key != KEY_UP && key != KEY_DOWN) {
      keyCaught = false;
    }
    else {
      keyCaught = true;
      setSelected(key);
    }
  }
}

function setSelected(key) {
  if (curSelected > 1)
    unhilite(laDiv.childNodes[curSelected]);
//    laDiv.childNodes[curSelected].style.backgroundColor = UNSELECTED_BG;
  if (key == KEY_UP) {
    if (curSelected > 1) {
      curSelected--;
    hilite(laDiv.childNodes[curSelected]);
//      laDiv.childNodes[curSelected].style.backgroundColor = SELECTED_BG;
    }
  }
  if (key == KEY_DOWN) {
    if (curSelected < curLength) {
      curSelected++;
     hilite(laDiv.childNodes[curSelected]);
//     laDiv.childNodes[curSelected].style.backgroundColor = SELECTED_BG;
    }
  }
//  window.status = "setSelected: " + curSelected + "/" + curLength;
}

function hiliteIt() {
//  window.status = "setSelected: " + this.name;
  if (curSelected > 1)
    unhilite(laDiv.childNodes[curSelected]);
//    laDiv.childNodes[curSelected].style.backgroundColor = UNSELECTED_BG;
  curSelected = this.name;
  hilite(this);
}

function hilite(item) {
  item.childNodes[0].style.backgroundColor = SELECTED_BG; 
  item.childNodes[1].style.backgroundColor = SELECTED_BG;
  item.childNodes[2].style.backgroundColor = SELECTED_BG; 
}

function unhiliteIt() {
  unhilite(this);
}

function unhilite(item) {
  item.childNodes[0].style.backgroundColor = UNSELECTED_BG; 
  item.childNodes[1].style.backgroundColor = UNSELECTED_BG;
  item.childNodes[2].style.backgroundColor = UNSELECTED_BG; 
//  item.style.backgroundColor = UNSELECTED_BG;
}

function onEventH(event) {
  if (laEnabled) {
    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() {
  if (laEnabled && !keyCaught) {
    lastChange = (new Date()).getTime();
    if (!changed)
      changed = true;
  }
}

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

setTimeout("check()", TEST);

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

function showDiv(divid)
{
  laDiv.style.overflow = "auto"; // firefox bug fix
  if (document.layers)
    document.layers[divid].visibility = "show";
  else
    document.getElementById(divid).style.visibility = "visible";
}

function hideDiv(divid)
{
  laDiv.style.overflow = "hidden"; // firefox bug fix
  if (document.layers)
    document.layers[divid].visibility = "hide";
  else
    document.getElementById(divid).style.visibility = "hidden";
}

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

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