$(document).ready(function(){
	setEventsPageIndex();
});

function setEventsPageIndex(){
	function format(row) {
		return row.keyword;
	}
	
	$("#txtSearch").focus(function(){
		if($("#txtSearch").hasClass("notSearch")){
			$("#txtSearch").attr("value","");
			$("#txtSearch").removeClass("notSearch");
		}
	});
	$("#txtSearch").blur(function(){
		if($("#txtSearch").attr("value") == ''){
			$("#txtSearch").attr("value","Mots clés ou #Annonce");
			$("#txtSearch").addClass("notSearch");
		}
	});
	
	$("#txtSearch").autocomplete('ajax/search/loadListKeywords.php',{
		dataType: "json",
		parse: function(data){
			return $.map(data, function(row){
				return {
					data: row,
					value: row.keyword,
					result: row.keyword
				}
			});
		},
		formatItem: function(item){
			return format(item);
		}
	});
	
	$('#txtSearch').keypress(function(e){
		var code;
		if(!e){
			var e = window.event;
		}
		if(e.keyCode){
			code = e.keyCode;
		}
		else if(e.which){
			code = e.which;
		}
		if(code==13){
			search();
		}
	});
	
	$('#btnSearch').click(function(){
		search();
	});
}

function search(){
	var search = $("#txtSearch").attr('value');
	document.location.href = PAGE_SEARCH+'?q='+search;
}
