function GetKeyCode(e) {
  if (e) {
    return e.charCode ? e.charCode : e.keyCode;
  }
  else {
    return window.event.charCode ? window.event.charCode : window.event.keyCode;
  }
}
function processRequest() {
  if (httpRequest.readyState == 4) {
    if(httpRequest.status == 200) {
      var response = httpRequest.responseText;
      if (response == 'EMPTY') {
        document.getElementById("naseptavacDiv").style.visibility = "hidden";
      }
      else {        
        document.getElementById("naseptavacDiv").innerHTML = response;
        document.getElementById("naseptavacDiv").style.visibility = "visible";
      } 
    }
  }
}
jQuery(function() {
  jQuery('body').bind('click',function(){
    document.getElementById("naseptavacDiv").style.visibility = "hidden";
	});
});
jQuery(function() {
  jQuery('#phrase').bind('click',function(){
    document.getElementById("phrase").setAttribute("autocomplete", "off");
	});
});
jQuery(function() {
  jQuery('#phrase').bind('keyup',function(event){
  var unicode = GetKeyCode(event);
  var str = document.getElementById("phrase").value;
  if (unicode != 38 && unicode != 40) {
    if (str != "") {
      if (window.ActiveXObject) {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } else {
        httpRequest = new XMLHttpRequest();
      }

      var url = "http://"+location.hostname+"/ajax/naseptavac.php?str=" + encodeURI(str);
      httpRequest.open("GET", url, true);     
      httpRequest.onreadystatechange= function () {processRequest(); } ;
      httpRequest.send(null);     
    }
    else {     
      document.getElementById("naseptavacDiv").style.visibility = "hidden";
    }
  }
  });
});
jQuery(document).ready(function() {
    window.position=-1;
    window.where='';
    $('#phrase').live('keydown', function(event,where) {
      var href = jQuery("#naseptavacDiv p:not('.color_bold')");
      if(event.keyCode==13 && window.where!='') {
        $(location).attr('href',window.where);
        return false;
      } else if(event.keyCode==40) {
        if(window.position==-1) {
          window.where=jQuery(href[0]).children().attr('href');
          jQuery(href[0]).children().addClass('active');
          window.position=0;
        } else if(window.position<jQuery(href).length-1) {
          jQuery(href[window.position]).children().removeClass('active');
          window.position+=1;
          window.where=jQuery(href[window.position]).children().attr('href');
          jQuery(href[window.position]).children().addClass('active')
        }
      } else if(event.keyCode==38) {
        if(window.position>0) {;
          jQuery(href[window.position]).children().removeClass('active');
          window.position-=1;
          window.where=jQuery(href[window.position]).children().attr('href');
          jQuery(href[window.position]).children().addClass('active')
        }
      }
    });
});
jQuery('.naseptavac').live('hover',function() {
  jQuery(this).children().children().removeClass('active');
});

