$(document).ready(function(){
	$('.autokeywords-item').hover(function(){
		alert($(this).attr('rowid'));								   
	});
	$(document).click(function(e){
		var targ;	
		if (!e) 
			var e = window.event;	
		if (e.target)
			targ = e.target;	
		else if (e.srcElement)
			targ = e.srcElement;	
		if (targ.nodeType == 3) 
			targ = targ.parentNode;						   
		if(targ.className!='autokeywords-item'&&targ.id!='autokeywords-onoff'){
			if($('.autokeywords-item').length>0){
				$('.autokeywords').hide();
				$('#autokeywords-onoff').attr('src','/images/index-search-menu-off.png');
			}
		}
	}); 
	$('.search_textbox').keyup(function(e){
		var maxrow=$('.autokeywords-item').length;								
		 if(e.which==38){
			if(maxrow<=0) return true;
			var current_rowid = parseInt($('.autokeywords-item[FocusProperty=true]').attr('rowid'));
			if($('.autokeywords-item[FocusProperty=true]').length>0){
				if(current_rowid<=1)
					current_rowid = maxrow;
				else
					current_rowid = current_rowid-1;
			}
			else {
				current_rowid = 1;
			}
			AutoKeywordsItemFocus(current_rowid);
			return true;
		}
		if(e.which==40){
			if(maxrow<=0) return true;
			var current_rowid = parseInt($('.autokeywords-item[FocusProperty=true]').attr('rowid'));
			if($('.autokeywords-item[FocusProperty=true]').length>0){
				if(current_rowid>maxrow)
					current_rowid = 1;
				else
					current_rowid = current_rowid+1;
			}
			else {
				current_rowid = 1;
			}
			AutoKeywordsItemFocus(current_rowid);
			return true;
		}
		
		GetAutoKeywords(this.value);
	});
});

function AutoKeywordsItemFocus(rowid){
	$('.autokeywords-item[rowid!='+rowid+']').each(function(){
			$(this).css('background-color','#fff');
			$(this).attr('FocusProperty','false');
			$(this).css('color','#0c3');
	});
	$('.autokeywords-item[rowid='+rowid+']').css('background-color','#95D5FF');
	$('.autokeywords-item[rowid='+rowid+']').css('color','#FFF');
	$('.autokeywords-item[rowid='+rowid+']').attr('FocusProperty','true');
	$('.search_textbox').val($('.autokeywords-item[rowid='+rowid+']').text());
}
function GetAutoKeywords(keywords){
	var data = "hl={$AcceptLanguage}&keywords="+encodeURI(keywords)+'&r='+Math.random();
	if (!keywords) {autokeywords_clear();return false};
	$.ajax({
		type: "GET", 
		url: "/getautokeywords.php",
		data: data,
		dataType: "json",
		success: GetAutoKeywordsResult
	});	
}
function GetAutoKeywordsResult(result){
	var data = result;
	$('.autokeywords-list').html('');
	if(data['Code']=='succ'){
		var counter =  data['data'] ? data['data'].length : 0;
		if(counter>0){ 
			$('.autokeywords').css('display','block');
			$('#autokeywords-onoff').attr('src','/images/index-search-menu-on.png');
		}
		else 
			$('.autokeywords').css('display','none');

		for(var i=0;i<counter;i++){
			$('.autokeywords-list').append('<li class="autokeywords-item" FocusProperty="false" onmouseover="AutoKeywordsItemFocus('+(i+1)+');" onclick="setquery(\''+data['data'][i].keywords+'\');" rowid="'+(i+1)+'">'+data['data'][i].keywords+'</li>');
		}
	}
}
function setquery(query){
	$('.search_textbox').val(query);
	autokeywords_clear();
	$('.IndexSearchForm').submit();
}
function autokeywords_clear(){
	$('.autokeywords-list').html('');
	$('.autokeywords').css('display','none');
	$('#autokeywords-onoff').attr('src','/images/index-search-menu-off.png');
}
function autokeywords_onoff(){
	if($('.autokeywords').css('display')=='none'){
		$('.autokeywords').show(); 
		$('#autokeywords-onoff').attr('src','/images/index-search-menu-on.png');
	}
	else {
		$('.autokeywords').hide();
		$('#autokeywords-onoff').attr('src','/images/index-search-menu-off.png');
	}
}