var numAuthors=1;
var activeAuthor=1;

var numKeywords=1;
var activeKeyword=1;

var what = "title";
var authorId = -1;

//*******************************************/

$(document).ready(function() {
	
	what = $("#what_title").attr("checked")=="checked" ? "title":"author";
	what = $("#what_keyword").attr("checked")=="checked" ? "keyword":what;	
	
	$("#q").autocomplete({
			source: function( request, response ) {
				$.ajax({
					url: "doSuggestSearchArticle.jsp",
					dataType: "json",
					data: {
						maxRows: 20,
						term: request.term,
						what: what,
						kwLang: "isl",
						authorId: authorId
					},
					success: function( data ) {
						response( $.map( data, function( item ) {
							return {
								label: item.label.replace(/&#34;/g,'"'),
								desc: item.desc.replace(/&#34;/g,'"'),
								id: item.id
							}
						}));
						
					},
					error: function(jqXHR, textStatus, errorThrown){
						//alert(textStatus+" "+errorThrown);
					}
					
				});
			},
			delay:300,
			minLength:3,
			focus: function( event, ui ) {
					$( "#q" ).val( ui.item.label );
					return false;
			},
			select: function( event, ui ) {
				$( "#authorId" ).val( what=="author" ? ui.item.id : -1 );
				$( "#q" ).val( ui.item.label );
				$( "#searchform" ).submit();
				return false;
			}
	})
	.data( "autocomplete" )._renderItem = function( ul, item ) {
		return $( "<li></li>" )
			.data( "item.autocomplete", item )
			.append( "<a>" + item.label + " " + item.desc + "</a>" )
			.appendTo( ul );
	};;

	if(document.searchform.q.value.length==0){
		document.searchform.q.select();
	}

});


//*******************************************/

function setWhat(s){
	what = $("#what_title").attr("checked")=="checked" ? "title":"author";
	what = $("#what_keyword").attr("checked")=="checked" ? "keyword":what;	
	track_search();
	document.location.href="article_search_init.jsp?q="+$("#q").val()+"&what="+s;
}



