// utility for pop up div

aos.PopupInfo = function() { };

aos.PopupInfo.debug = function(msg) {
  return;
  var div = document.getElementById("debug");
  div.innerHTML += msg + "<br/>\n";
};

//Pop up information box II (Mike McGrath (mike_mcgrath@lineone.net,  http://website.lineone.net/~mike_mcgrath))
//Permission granted to Dynamicdrive.com to include script in archive
//For this and 100's more DHTML scripts, visit http://dynamicdrive.com

var old,SKN,iex=(document.all);
var ns4=document.layers
var ns6=document.getElementById&&!document.all
var ie4=document.all

aos.PopupInfo.init = function() {
  if (ns4)
    SKN=document.dek
  else if (ns6)
    SKN=document.getElementById("dek").style
  else if (ie4)
    SKN=document.all.dek.style

  if (ns4)
    document.captureEvents(Event.MOUSEMOVE);
  else {
    SKN.visibility="visible"
    SKN.display="none"
  }
  //document.onmousemove=get_mouse;  // deprecated to follow mouse
}

aos.PopupInfo.timer = null;
aos.PopupInfo.fieldIdx = -1;
aos.PopupInfo.waitingForServer = false;
aos.PopupInfo.retry = false;

aos.PopupInfo.suggest = function(idx, sec) {
  clearTimeout(aos.PopupInfo.hideTimer);
  aos.PopupInfo.debug("Suggesting for idx:" + idx);
  if (aos.PopupInfo.waitingForServer) {
    // already a request sent to the server, need to WAIT response then retry
    aos.PopupInfo.retry = true;
    aos.PopupInfo.fieldIdx = idx;
    return;
  }
  clearTimeout(aos.PopupInfo.timer);
  aos.PopupInfo.fieldIdx = idx;

  if (document.getElementById("comparison" + idx).value == '') sec = 10;
  if (sec == null) sec = 10;  // 10ms delay
  
  aos.PopupInfo.debug("timer queued (" + aos.PopupInfo.timer + ")...");
  aos.PopupInfo.timer = setTimeout("aos.PopupInfo.requestToServer()", sec);
  aos.PopupInfo.debug("...timer sent for idx " + idx + " sec:" + sec);
};

aos.PopupInfo.requestToServer = function() {
  var fieldIdx = aos.PopupInfo.fieldIdx;
  var fieldDiv = document.getElementById("comparison" + fieldIdx);
  if (fieldDiv == null) {
    aos.PopupInfo.debug("Fatal error requestToServer: fieldDiv is null for idx " + fieldIdx);
    aos.PopupInfo.fieldIdx = -1;
    aos.PopupInfo.waitingForServer = false;
    aos.PopupInfo.retry = false;
    return;
  }

  // if current entry is not empty, no need to make suggestion
  if (false && fieldDiv.value != '') {
    aos.PopupInfo.fieldIdx = -1;
    aos.PopupInfo.waitingForServer = false;
    aos.PopupInfo.retry = false;
    aos.PopupInfo.debug("already have entry, skipping...");
    return;
  }

  // generate neighbor's list
  var i = 0;
  var neighborList = Array();
  for (var j = 0; j < CONST.MAX_COMPARISONS; j++) {
    var neighborDiv = document.getElementById("comparison" + j);
    if (neighborDiv == null) break;
    var word = neighborDiv.value;
    if (word != '') {
      neighborList.push("wd" + i + "=" + encodeURI(word));
      i++;
    }
  }
  var param = '';
  if (neighborList.length > 0) {
    param += '&' + neighborList.join('&') + '&currentIdx=' + fieldIdx;
  }

  aos.PopupInfo.debug("suggest.php param: " + param);
  var url = './suggest.php';
  var myAjax = new Ajax.Request(
    url,
    {
      method: 'get',
      parameters: param,
      onSuccess: aos.PopupInfo.showResults,
      onFailure: aos.PopupInfo.failedResults
    });
};

// oh well!
aos.PopupInfo.failedResults = function() { 
  aos.PopupInfo.fieldIdx = -1;
  aos.PopupInfo.waitingForServer = false;
  aos.PopupInfo.retry = false;
  aos.PopupInfo.debug("FAILED request");
};

aos.PopupInfo.showResults = function(originalRequest){
  aos.PopupInfo.waitingForServer = false;
  var fieldIdx = aos.PopupInfo.fieldIdx;
  aos.PopupInfo.debug("Result from server BACK for idx:" + fieldIdx);
  var fieldDiv = document.getElementById("comparison" + fieldIdx);
  if (fieldDiv == null) {
    aos.PopupInfo.debug("Fatal error in showResults: fieldDiv is null with idx " + fieldIdx);
    aos.PopupInfo.fieldIdx = -1;
    aos.PopupInfo.waitingForServer = false;
    return;
  }
  if (aos.PopupInfo.retry) {
    aos.PopupInfo.debug("discarded result, retrying...");
    aos.PopupInfo.fieldIdx = -1;
    aos.PopupInfo.waitingForServer = false;
    aos.PopupInfo.retry = false;
    aos.PopupInfo.suggest(fieldIdx, 100);
    return;
  }
  aos.PopupInfo.fieldIdx = -1;

  aos.PopupInfo.debug("FINISHING..." + originalRequest.responseText);
  var compared = '';
  var synonym = '';
  try {
    var myVar = eval('(' + originalRequest.responseText + ')');
    compared = myVar.compared;
    synonym = myVar.synonym;
  } catch (e) { 
    aos.PopupInfo.debug("ERROR: " + originalRequest.responseText);
  }

  var msg = '';
  if (true || fieldDiv.value == '') {
    // if empty field and we have comparison terms, then suggest it
    var wordsWithLink = Array();
    for (var i = 0; i < compared.length; i++) {
      var link = '<span class="selection" onclick="aos.PopupInfo.autoFill(\'' +
        compared[i] + '\',' + fieldIdx + '); ' +
        ' aos.Ads.fillSearchSelections();' +
        '">' +
        compared[i] + '</span>';
      wordsWithLink.push(link);
    }
    if (compared != '') {
      msg = 'Samples: <b>' + wordsWithLink.join(', ') + '</b>';
    }
  } else if (synonym != '') {
    msg = ('Synonyms to try, separated by commas:' +
           '<b>' + synonym.join(', ') + '</b>');
  }

  var pos = aos.PopupInfo.findPos(fieldDiv);
  SKN.left = pos[0] + fieldDiv.offsetWidth;
  SKN.top = pos[1];  // + (fieldDiv.offsetHeight);

  document.getElementById("dek").innerHTML = msg;
  SKN.display = (msg != '') ? '' : 'none';

  aos.PopupInfo.fieldIdx = -1;
  aos.PopupInfo.waitingForServer = false;
};

aos.PopupInfo.findPos = function(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft
    curtop = obj.offsetTop
    while (obj = obj.offsetParent) {
      curleft += obj.offsetLeft
      curtop += obj.offsetTop
    }
  }
  return [curleft,curtop];
}

aos.PopupInfo.hide = function() {
  if(ns4){
    SKN.visibility="hidden";
  } else if (ns6||ie4) {
    SKN.display="none"
  }
};

aos.PopupInfo.hideTimer = null;
aos.PopupInfo.hide = function() {
  clearTimeout(aos.PopupInfo.hideTimer);
  aos.PopupInfo.hideTimer = setTimeout("aos.PopupInfo.hideForReal()", 500);
};
aos.PopupInfo.hideForReal = function() {
  var div = document.getElementById("dek");
  if ((div == false) || (div == null)) return;
  div.style.display = 'none';
};

aos.PopupInfo.autoFill = function(word, idx) {
  var div = document.getElementById("comparison" + idx);
  if (div == null) return;
  div.value = word;

  // check if next blank field should get auto treatment
  for (var i = idx + 1; i < CONST.MAX_COMPARISONS; i++) {
    var div = document.getElementById("comparison" + i);
    if (div != null && div.value == '') {
      div.focus();
      aos.PopupInfo.suggest(i);
      return;
    }
  }
};

aos.PopupInfo.fillAll = function(word) {
  var words = word.split(/\s*,\s*/);
  for (var i = 0; i < words.length && i < CONST.MAX_COMPARISONS; i++) {
    var div = document.getElementById("comparison" + i);
    word = words[i];
    div.value = word != '...' ? words[i] : '';
  }
  for (var i = words.length; i < CONST.MAX_COMPARISONS; i++) {
    var div = document.getElementById("comparison" + i);
    div.value = '';
  }
}

