function AutoSuggestion()
{
	this.initParams();
};

AutoSuggestion.prototype.needsSuggestion = function()
{
	return this.word.length >= 2 && this.word != '' && this.prevWord != this.word;
}

AutoSuggestion.prototype.getParamObject = function()
{
	return {"word" : this.word};
};

AutoSuggestion.prototype.updateParams = function()
{
	this.word = $("#search-freeword").val();
};

AutoSuggestion.prototype.initParams = function()
{
	this.prevWord = '';
	this.word = '';
	this.elm = $("#block-freeword").find("div.module-assist");
};

AutoSuggestion.prototype.updatePrevParams = function()
{
	this.prevWord = this.word;
};

AutoSuggestion.prototype.execute = function()
{
	this.updateParams();
	if(this.needsSuggestion())
	{
		rm.sendRequest(
			contextPath + "/search/word_suggestion.json",
			this.getParamObject(),
			function(self){
				return function(response){
					self.doRender(response["response"])
				}
			}(this)
		)
	}
	else
	{
		if(this.word.length < 2)
		{
			setVisible(false);
        }
	}
	this.updatePrevParams(); // 検索したら前回実行分として条件を保存。
};

AutoSuggestion.prototype.doRender = function(response)
{
	var html = '';
	if(response != null && response.length > 0)
	{
		html += '<ul>';
		for(var i = 0; i < response.length; i++)
		{
			var name = decodeURIComponent(response[i].replace(/\+/g, ' ').replace('%2F', '/')).replace(/^\s+|\s+$/g, '');
			html += '<li><span onclick="autoSuggestion.accept(\'' + name + '\');">' + name + '</span></li>'
		}
		html += '</ul>';
		html += '<p><a href="javascript:void(0)" onclick="setVisible(false)" >閉じる</a></p>';
		this.elm.html(html); // 作ったhtmlを差し込み。
		setVisible(true);
	}
	else
	{
		setVisible(false);
	}
};

AutoSuggestion.prototype.accept = function(val)
{
	$("#search-freeword").val(val);
	this.prevWord = val;
	endSuggestion(this);
};

var timerId = null;
function startSuggestion(suggestion)
{
	if(!suggestion.running)
	{
		suggestion.initParams();
		timerId = setInterval(function(){suggestion.execute();}, 500);
	}
};

function endSuggestion(suggestion)
{
	if(timerId != null)
	{
		var delTimerId = setInterval(function()
		{
			clearInterval(timerId);
			timerId = null;
			setVisible(false);
			clearInterval(delTimerId);
		}
		, 200);
		suggestion.running = false;
	}
};

function setVisible(visible)
{
	if(visible)
	{
		$("#block-freeword").find("div.module-assist").show();
	}
	else
	{
		$("#block-freeword").find("div.module-assist").hide();
	}
}

function applySuggestion()
{
	var autoSuggestion = new AutoSuggestion();
	$(function()
	{
		$("#search-freeword").focus(function()
		{
			startSuggestion(autoSuggestion);
		});
	});
	$(function()
	{
		$("#search-freeword").keyup(function()
		{
			// F5対応。キーアップイベントでスタート。
			if(this.value.length >= 2)
			{
				startSuggestion(autoSuggestion);
			}
		});
	});
	$(function()
	{
		$("#search-freeword").blur(function()
		{
			endSuggestion(autoSuggestion);
		});
	});

	setVisible(false);

	return autoSuggestion;
}


