//  (c) 2003-2006 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 laCount = 0;
var curLA = 0;
var laEnabled = true;
var checking = true;

var clientKey = new Array();
var searchField = new Array();
var desiredWidth = new Array();
var desiredHeight = new Array();
var alignment = new Array();
var laDiv = new Array();
var on = new Array();
var changed = new Array();
var lastChange = new Array();
var haveResults = new Array();

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

var WAIT = 200;
var TEST = 100;

var url = "http://www.onlinepress.eu/query.aspx?c=";
var multiUrl = "http://www.onlinepress.eu/query.aspx?c=";
var fetch = "http://www.onlinepress.eu/";

function setResults() {
  stopMe();
  if (!haveResults[curLA]) {
    haveResults[curLA] = true;
    laDiv[curLA].style.height = desiredHeight[curLA] + "px";    
  }
}

function setNoResults() {
  stopMe();
  if (haveResults[curLA]) {
    haveResults[curLA] = false;
    laDiv[curLA].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(curLA);
  start();
  script = document.createElement("SCRIPT");
  script.src = url + clientKey[curLA].value + "&n=" + curLA + "&t=" + escape(s).replace(/\+/g, '%2B');
  //alert(script.src);
  laDiv[curLA].appendChild(script);
}

function g(r, s) {
  this.location = fetch + s +"-" + r+"-1.html";
}
  
function m(r) {
  showDiv("la" + curLA);
  on[curLA] = true;
  scrub(curLA);
  start();
  script = document.createElement("SCRIPT");
  script.src = multiUrl + clientKey[curLA].value + "&n=" + curLA + "&r=" + escape(r);
  laDiv[curLA].appendChild(script);
}

function r() {
  var curString = searchField[curLA].value.toLowerCase();
  if (curString.length >= 3) {
    showDiv("la" + curLA);
    on[curLA] = true;
    getData(curString);
  }
}

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

function getp(element, attribute) {
  p = 0;
  while (element) {
    alert(element.name + ": " + element[attribute]);
    p += element[attribute];
    element = element.offsetParent;
  }
  alert("result: " + p);
}

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

function doh(num) {
  searchField[num].autocomplete = "off";
  searchField[num].setAttribute("autocomplete","off"); // for firefox
  searchField[num].blur();

  if (laCount == 1) {
    document.onclick = onEventH;
    document.onkeypress = onEventH;
    window.onresize = resizeH;
  }
  
  laDiv[num] = document.createElement("div");
  laDiv[num].id = "la" + num;
  laDiv[num].className = "g";
  laDiv[num].style.border = "black 1px solid";
  laDiv[num].style.padding = "0";
  laDiv[num].style.zIndex = "3000";
  laDiv[num].style.position = "absolute";
  laDiv[num].style.top = getPosition(searchField[num], "offsetTop") + searchField[num].offsetHeight + 0 + "px";
  if (alignment[num] == 'right') {
    sl = getPosition(searchField[num], "offsetLeft") + searchField[num]['offsetWidth'] - desiredWidth[num];
    laDiv[num].style.left = sl + "px";
  }
  else 
    laDiv[num].style.left = getPosition(searchField[num], "offsetLeft") + "px";
  laDiv[num].style.width = "330px";
  //laDiv[num].style.width = desiredWidth[num] + "px";
  laDiv[num].style.height = desiredHeight[num] + "px";   
  laDiv[num].style.backgroundColor = "white";
  laDiv[num].style.visibility = "hidden";
  laDiv[num].style.overflow = "auto";  
  document.body.appendChild(laDiv[num]);
}

function setup(key, field, width, height, align, num) {
  if (!num)
    num = 0;
  laCount++;
  clientKey[num] = key;
  searchField[num] = field;
  desiredWidth[num] = width;
  desiredHeight[num] = height;
  on[num] = false;
  changed[num] = false;
  lastChange[num] = 0;
  if (align && align == 'right')
    alignment[num] = 'right';
  else
    alignment[num] = 'left';
  haveResults[num] = false;
  doh(num);
}
       
function setF(s) {
  i = s.indexOf(",&nbsp;");
  if (i == -1) 
    searchField[curLA].value = s;
  else
    searchField[curLA].value = s.substring(i + 8) + ' ' + s.substring(0, i);
  searchField[curLA].select();
  searchField[curLA].focus();
}

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;
    for (var i = 0;i < laCount;i++) {
      if (element == searchField[i]) {
        if (on[i])
          showDiv("la" + i);
      }
      else {
        hideDiv("la" + i);
      }
    }
  }
}

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

function changeVal(n) {
  if (laEnabled) {
    curLA = (typeof n == 'number' ? n : 0);
    lastChange[curLA] = (new Date()).getTime();
    if (!changed[curLA])
      changed[curLA] = true;
  }
}

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

setTimeout("check()", TEST);

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

function showDiv(divid)
{
  laDiv[curLA].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[curLA].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=440,left=200,top=110'); 
}

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