function AutoSuggestion()
{
	this.initParams();
};

AutoSuggestion.prototype.needsSuggestion = function()
{
	return this.word.length >= 2 && this.word != '' && !(this.prevWord == this.word && this.prevCustomerType == this.customerType && this.prevAreaCode == this.areaCode && this.prevSuggestionType == this.suggestionType);
}

AutoSuggestion.prototype.getParamObject = function()
{
	return {"SearchWord" : encodeURI(this.word), "CustomerTypeCode" : this.customerType, "AreaCode" : this.areaCode, "IsStudySearch" : this.isStudySearch, "IsEventSearch" : this.isEventSearch, "IsExamSearch" : this.isExamSearch, "SuggestionType" : this.suggestionType};
};

AutoSuggestion.prototype.updateParams = function()
{
	this.word = getTargetInput().val();
	this.customerType = $("#customer_type").val();
	this.areaCode = $("#area_type").val();
	this.suggestionType = $("#school-name").attr("checked") ? 1 : 2;
	this.isStudySearch = $("#study_form").size() > 0 ? true : false;
	this.isEventSearch = $("#block-event-left").size() > 0 ? true : false;
	this.isExamSearch = $("#exam_form").size() > 0 ? true : false;
//	alert($("#customer_type_1").attr("checked"));
	if($("#school-name").length != 0)
    {
            this.suggestionType = $("#school-name").attr("checked") ? 1 : 2;
    }
    else
    {
            this.suggestionType = 1;
    }
};

AutoSuggestion.prototype.initParams = function()
{
	this.prevWord = '';
	this.word = '';
	this.prevCustomerType = '';
	this.customerType = '';
	this.prevAreaCode = '';
	this.areaCode = '';
	this.isStudySearch = false;
	this.isEventSearch = false;
	this.isExamSearch = false;
	this.suggestionType = 1;
	this.prevSuggestionType = 0;
};

AutoSuggestion.prototype.updatePrevParams = function()
{
	this.prevWord = this.word;
	this.prevCustomerType = this.customerType;
	this.prevAreaCode = this.areaCode;
	this.prevSuggestionType = this.suggestionType;
};

AutoSuggestion.prototype.execute = function()
{
	this.updateParams();
	if(this.needsSuggestion())
	{
		Kumu.Ajax.executeTeedaAjax(this.render,
		{
			"component" : "searchWordSuggestion",
			"action" : "search",
			"paramJson" : Kumu.JSONSerializer.serialize(this.getParamObject())
		});
	}
	else
	{
		if(this.word.length < 2)
		{
			setVisible(false);
        }
	}
	this.updatePrevParams(); // 検索したら前回実行分として条件を保存。
};

AutoSuggestion.prototype.render = function(response)
{
//	alert('' + response.length);
//	var rplcd = response.replace('+', ' ');
//	var ary = decodeURI(rplcd).split(",");
//	var ary = decodeURI(response).split(",");
	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, '');
//			var name = decodeURI(response[i].replace('+', ' ').replace('%2F', '/'));
			html += '<li><span onclick="autoSuggestion.accept(\'' + name + '\');">' + name + '</span></li>'
		}
		html += '</ul>';
		html += '<p><a href="javascript:void(0)" onclick="setVisible(false)" >閉じる</a></p>';
		$("#suggestion-box").html(html); // 作ったhtmlを差し込み。
		setVisible(true);
	}
	else
	{
		setVisible(false);
	}
};

AutoSuggestion.prototype.accept = function(val)
{
	getTargetInput().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)
	{
		$("#suggestion-box").show();
	}
	else
	{
		$("#suggestion-box").hide();
	}
}

function select(ele)
{
	$(ele).toggleClass("selected");
}

function applySuggestion()
{
	var autoSuggestion = new AutoSuggestion();
	$(function()
	{
		getTargetInput().focus(function()
		{
			startSuggestion(autoSuggestion);
		});
	});
	$(function()
	{
		getTargetInput().keyup(function()
		{
			// F5対応。キーアップイベントでスタート。
			if(this.value.length >= 2)
			{
				startSuggestion(autoSuggestion);
			}
		});
	});
	$(function()
	{
		getTargetInput().blur(function()
		{
			endSuggestion(autoSuggestion);
		});
	});

	setVisible(false);

	return autoSuggestion;
}

function getTargetInput()
{
	var ret = $("#searchfield");
	return ret.size() > 0 ? ret : $("#search-freeword");
}

/* 以下をhtml側に記述。
var autoSuggestion = applySuggestion();
*/


