var mocount = 0; //count of <li> that it's on (li's have id corresponding to order i.e. 1,2,3, etc)
var total = 0;   //total number of <li>s returned
var original = ""; //original search entered by user

function createRequestObject() {
  var req;
  if (window.XMLHttpRequest) { // Firefox, Safari, Opera...
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) { // Internet Explorer 5+
    req = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
    // error creating the request object,
    // (maybe an old browser is being used?)
    alert('There was a problem creating the XMLHttpRequest object');
    req = '';
  }
  return req;
}

$(document).ready(function(){
//creates the blur effect, click away from input hide searchsuggestions div
$('#search').blur(function() {
  $("#searchsuggestinner").hide();
});
});

$(document).ready(function(){
  $(".hoverme").live("mouseover mouseout", function(event) {
  if ( event.type == "mouseover" ) {
    $("#" + mocount).removeClass("searchsuggestinnerulhighlight");
    mocount = $(this).attr('id');
    $(this).addClass("searchsuggestinnerulhighlight");
  } else {
    $(this).removeClass("searchsuggestinnerulhighlight");
  }
  });
});

// Make the XMLHttpRequest object
var http = createRequestObject();

//sends request for new <div> tag if keys are not arrows
function sendRequest(searchval,e) {

  var evt = e || window.event;
  evtK = evt.keyCode;
  //determines what to do with special cases
  //(pressing down at bottom or up at top)
  if((evtK == 38) && (mocount == 0) && document.getElementById("searchsuggestinner").innerHTML != "")
  {
    mocount = total - 1;
    evtK = 40;
  }
  else if((evtK == 40) && (mocount == total) && document.getElementById("searchsuggestinner").innerHTML != "")
  {
    evtK = 38;
     $("#search").val(original);
     $("#" + mocount).removeClass("searchsuggestinnerulhighlight");
     mocount = 0;
  }

  //clears out search box and input if escape is hit
  if(evtK == 27)
  {
    $("#searchsuggestinner").hide();
    $("#search").val(original);
  }
  //determines what to do if up arrow is hit
  else if(evtK == 38)
  {
   $("#searchsuggestinner").show();
   if(mocount != 0)
   {
   $("#" + mocount).removeClass("searchsuggestinnerulhighlight");
   mocount--;
   $("#search").val($("#" + mocount).html());
   if((mocount + 1) == 1)
   {
     $("#search").val(original);
   }
   $("#" + mocount).addClass("searchsuggestinnerulhighlight");
   }
   return false;
  }
  //determines what to do if down arrow is hit
  else if(evtK == 40)
  {
   $("#searchsuggestinner").show();
   if(mocount != total)
   {
   $("#" + mocount).removeClass("searchsuggestinnerulhighlight");
   mocount++;
   $("#search").val($("#" + mocount).html());
   $("#" + mocount).addClass("searchsuggestinnerulhighlight");
   }
  }
 else if(evtK != 38 && evtK != 40 && evtK != 13 && evtK != 27 && evtK != 37 && evtK != 39)
  {
  original = searchval;
  var now = new Date();
  http.open('get', 'scs/searchsuggest.cgi?search='+searchval);
  http.onreadystatechange = adamHandleResponse;
  http.send(null);
  }
}

function adamHandleResponse() {
  if(http.readyState == 4 && http.status == 200){
    var response = http.responseText; // Text returned FROM perl script
      var array = response.split("~@`"); //splits returned data into array by ~@`
      
      //fill div with data unless nothing was returned
      if(array[1] != -1){
      document.getElementById("searchsuggest").innerHTML = array[0];
      total = array[1];
      mocount = 0;
    }
    //nothing returned-- empty old div
    else{
      total = 0;
      document.getElementById("searchsuggest").innerHTML = "";
      mocount = 0;
    }
  }  
}  

function searchsuggestSubmit(s) {
  document.getElementById('search').value=s;
  document.getElementById('searchsuggest').innerHTML='';
  document.getElementById('searchform').submit();
}



