$(function() {
		$('#searchInput')
			.focus(function() {
				if (this.value == "type your question here") 
					this.value = "";
			})
			.blur(function() {
				if (this.value == "") 
					this.value = "type your question here";
			});
	});

  function liFormat (row, i, num) {
    var result = row[0];
    result += "<em class=\"qnt\">";
    if(row[1].type=="article"){
      result += (row[1].rating==-1?"(No rating)":"(Rating: "+(Math.round(row[1].rating*10)/10)+")")+"&nbsp;&nbsp;";
      result += row[1].date+"&nbsp;&nbsp;";
      result += "Views: " + row[1].views;
    }else if(row[1].type=="category"){
      result += "Category";
    };
    result += "</em>";
    return result;
  }
  function selectItem(li) {
    if( li == null ) return;
    if( !!li.extra ) {
      var txt=$("#searchInput").val();
      $("#searchInput").val(txt.replace(/<\/?b>/gi,''));
      $("#searchInput").attr("disabled","disabled");
      window.location=li.extra[0].url;
    };
  }
  $(document).ready(function(){
    var url=$("#searchForm").attr("action").replace('.php','_json.php');
    $("#searchInput").autocomplete(url,{
      width:566,
      minChars:2,
      matchSubset:false,
      cacheLength:10,
      selectFirst:true,
      maxItemsToShow:5,
      extraParams:{"l":5,"include_categories":1},
      formatItem:liFormat,
      onItemSelect:selectItem
     }
    );
  });
