yLS = {}
yLS.delay = 800;
yLS.domain = null;
yLS.timer = null;
yLS.frm = null;
yLS.lastQuery = null;
yLS.counter = 0;
yLS.appid = 'YahooDemo';
yLS.throbState = 1;
yLS.init = function() {
  yLS.frm = document.getElementById('yLSform');
  yLS.frm.p.value = ''; // Clear field; avoids an IE 6 crash bug
  yLS.frm.onsubmit = yLS.onsubmit;
  yLS.frm.p.onkeyup = yLS.onkeyup;
  yLS.domain = yLS.frm.vs.value;
}
yLS.onsubmit = function() {
  yLS.complete(yLS.frm.p.value);
  return false;
}
yLS.onkeyup = function() {
  yLS.delayedComplete(yLS.frm.p.value);
  return true;
}
yLS.delayedComplete = function(query) {
  clearTimeout(yLS.timer);
  yLS.timer = setTimeout(function() {
    yLS.complete(query);
  }, yLS.delay);
}
yLS.complete = function(query) {
  clearTimeout(yLS.timer);
  if (yLS.lastQuery == query) {
    return;
  }
  yLS.lastQuery = query;
  if (query.replace(/\s/g, '') == '') {
    var resultsDiv = document.getElementById('yLSresults');
    if (resultsDiv) {
      resultsDiv.style.display = 'none';
    }
    return;
  }
  var resultsDiv = document.getElementById('yLSresults');
  if (resultsDiv) {
    while(resultsDiv.hasChildNodes()) {
      resultsDiv.removeChild(resultsDiv.lastChild);
    }
    resultsDiv.style.display = 'none';
  }
  var func_name = 'update' + yLS.counter++;
  var script_id = 'yLS' + func_name;
  yLS[func_name] = yLS.makeUpdateFunction(script_id, func_name);
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = yLS.buildQuery(query, func_name);
  window.status = script.src;
  script.id = script_id;
  document.getElementsByTagName('head')[0].appendChild(script);
  yLS.startThrobber();
}
yLS.makeUpdateFunction = function(script_id, func_name) {
  // Factory for functions that clean up their invoking script tag
  return function(o) {
    yLS.update(o);
    var script = document.getElementById(script_id);
    script.parentNode.removeChild(script);
    delete yLS[func_name];
  }
}
yLS.buildQuery = function(query, func_name) {
  var url = 'http://api.search.yahoo.com/WebSearchService/V1/webSearch?';
  url += 'appid=' + yLS.appid + '&site=' + yLS.domain + 
  '&query=' + encodeURI(query) +
  '&results=5&output=json&callback=yLS.' + func_name +
  '&yLSrand=' + encodeURI((new Date()).toString() + Math.random());
  return url;
}
yLS.getResultsDiv = function() {
  var resultsDiv = document.getElementById('yLSresults');
  if (!resultsDiv) {
    resultsDiv = document.createElement('div');
    resultsDiv.id = 'yLSresults';
    yLS.frm.appendChild(resultsDiv);
  }
  return resultsDiv;
}
yLS.update = function(o) {
  yLS.stopThrobber();
  var resultsDiv = yLS.getResultsDiv();
  while(resultsDiv.hasChildNodes()) {
    resultsDiv.removeChild(resultsDiv.lastChild);
  }
  if (o.Error) {
    resultsDiv.innerHTML = '<p class="yLSinfobox">' + o.Error.Title + ' ' + o.Error.Message + '</p>';
    return;
  }
  if (o.ResultSet.totalResultsAvailable == "0") {
    resultsDiv.innerHTML = '<p class="yLSinfobox">No results found</p>';
    return;
  }
  var ol = document.createElement('ol');
  var results = (o.ResultSet && o.ResultSet.Result) || [];
  if (!results.length) {
    results = [results]; // Hack for 1 result only
  }
  for (var i = 0, result; result = results[i]; i++) {
    var a = document.createElement('a');
    a.innerHTML = yLS.makeTitle(result.Title);
    a.href = result.Url;
    var li = document.createElement('li');
    li.appendChild(a);
    var p = document.createElement('p');
    p.appendChild(document.createTextNode(result.Summary));
    li.appendChild(p);
    ol.appendChild(li);
  }
  resultsDiv.appendChild(ol);
  var totalNum = parseInt(o.ResultSet.totalResultsAvailable, 10);
  var linktext;
  if (totalNum <= 5) {
    linktext = 'See these results';
  } else {
    var plural = (totalNum == 1 ? '' : 's');
    linktext = 'See all ' + totalNum + ' result' + plural;
  }
  linktext += ' on Yahoo! Search'
  var p = document.createElement('p');
  p.innerHTML = '<a href="http://search.yahoo.com/search?vs=' + yLS.domain +
  '&p=' + encodeURI(yLS.lastQuery) + '">' + linktext +
    '</a>' + '<a href="http://search.yahoo.com/">' + 
    '<img src="http://us.i1.yimg.com/us.yimg.com/i/us/search/ysan/ysanlogo.gif" ' +
    'width="99" height="33" alt="Yahoo! Search"></a>';
  resultsDiv.appendChild(p);
}
yLS.makeTitle = function(title) {
  if (yLS.frm['yls-remove-prefix'] && yLS.frm['yls-remove-prefix'].value) {
    title = title.replace(new RegExp(
      '^' + yLS.escapeRE(yLS.frm['yls-remove-prefix'].value)), '');
  }
  if (yLS.frm['yls-remove-suffix'] && yLS.frm['yls-remove-suffix'].value) {
    title = title.replace(new RegExp(
      yLS.escapeRE(yLS.frm['yls-remove-suffix'].value) + '$'), '');
  }
  return title;
}
yLS.escapeRE = function(text) {
  var specials = ['/', '\\', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}'];
  var specialsRE = new RegExp('(\\' + specials.join('|\\') + ')', 'g');
  return text.replace(specialsRE, '\\$1');
}
yLS.startThrobber = function() {
  yLS.getThrobber().style.display = 'block';
  yLS.getResultsDiv().style.display = 'block';
  yLS.throbber = setInterval(yLS.throb, 500);
}
yLS.stopThrobber = function() {
  yLS.getThrobber().style.display = 'none';
  clearInterval(yLS.throbber);
}
yLS.getThrobber = function() {
  var throbberP = document.getElementById('yLSthrobber');
  if (!throbberP) {
    throbberP = document.createElement('p');
    throbberP.id = 'yLSthrobber';
    var resultsDiv = yLS.getResultsDiv();
    resultsDiv.appendChild(throbberP);
    throbberP.appendChild(document.createTextNode('Searching'));
    for (var i = 0; i <= 2; i++) {
      var dot = document.createElement('span');
      dot.id = 'yLSdot' + i;
      dot.appendChild(document.createTextNode('.'));
      throbberP.appendChild(dot);
    }
    throbberP.style.display = 'none';
  }
  return throbberP;
}
yLS.throb = function() {
  yLS.throbState++;
  if (yLS.throbState > 2) {
    yLS.throbState = 0;
  }
  for (var i = 0; i <= 2; i++) {
    var dot = document.getElementById('yLSdot' + i);
    if (i < yLS.throbState) {
      dot.style.visibility = 'visible';
    } else {
      dot.style.visibility = 'hidden';
    }
  }
}
yLS.addLoadEvent = function(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

yLS.addLoadEvent(yLS.init);

