// ŒŸ�õŒ‹‰Ê‚ª200Œ�‚ð‰z‚¦‚Ä‚¢‚é‚©‚Ç‚¤‚©‚Ìƒtƒ‰ƒbƒO
var overMaxCountFlag = false;
/** N-GramƒAƒ‹ƒSƒŠƒYƒ€‚ð—˜—p‚µ‚Ä�A‘S•¶ŒŸ�õ‚ð�s‚¤�B
 * ŒŸ�õŒ‹‰Ê‚ÍSearchResult‚Ì”z—ñ‚Æ‚µ‚Ä•Ô‹p‚³‚ê‚é�B
 * ŒŸ�õŒ‹‰Ê‚ª0Œ�‚Ì�ê�‡�A’·‚³‚ª0‚Ì”z—ñ‚Æ‚µ‚Ä•Ô‹p‚³‚ê�Anull‚ª•Ô‚é‚±‚Æ‚Í–³‚¢�B
 * searchWords ‚ÍŒŸ�õƒL�[ƒ��[ƒhƒŠƒXƒg
 * searchBookName‚Í‘I‘ð‚³‚ê‚Ä‚¢‚é•ª�û‚ÌƒtƒHƒ‹ƒ_–¼�Bƒ}ƒjƒ…ƒAƒ‹ˆê——‚ª‚È‚¢�ê�‡‚Í0‚É‚È‚é
 * ŒŸ�õ‚Íè‡’l‚ª�Ý’è‚Å‚«‚é�B
 */
function search(searchWords, searchBookName)
{
	var result = new Array();
	for (i = 0; i < pages.length; i++)
	{
		if (searchBookName != "" && searchBookName != books[pages[i].bookId].bookFolderName)
		{
			continue;
		}
		//ƒqƒbƒg‚µ‚½‰ñ�”
		var times = 0;
		//ƒ^ƒCƒgƒ‹‚ªƒqƒbƒg‚³‚ê‚½‚©‚Ç‚¤‚©‚Ìflag
		var titleHitflag = false;
		//–{•¶‚ªƒqƒbƒg‚³‚ê‚½‚©‚Ç‚¤‚©‚Ìflag
		var textHitflag = false; 
		//ƒ^ƒCƒgƒ‹‚ªƒqƒbƒg‚³‚ê‚½‰ñ�”
		var titleHitTimes = 0;
		//–{•¶‚ªƒqƒbƒg‚³‚ê‚½‰ñ�”
		var textHitTimes = 0;
		//ŠeŒŸ�õƒL�[ƒ��[ƒh‚Ì“o�ê‰ñ�”
		var wordsHitTime = new Array();
		for (j = 0; j < searchWords.length; j++)
		{
			var reg = new RegExp(searchWords[j], "g");
			var textResult = pages[i].regText.match(reg);
			var titleResult = pages[i].regPageTitle.match(reg);
			//‚P‚ÂƒL�[ƒ��[ƒh‚Ì“o�ê‰ñ�”
			var oneWordHitTime = 0;
			if (textResult != null)
			{
				oneWordHitTime += textResult.length;
				times += textResult.length;
				textHitTimes += textResult.length;
			}
			if (titleResult != null)
			{	
				oneWordHitTime += titleResult.length;
				times += titleResult.length;
				titleHitTimes += titleResult.length;
			}
			wordsHitTime[j] = oneWordHitTime;
		}
		if (isResult(wordsHitTime, 2))
		{
			if (titleHitTimes > 0)
			{
				titleHitflag = true;
			}
			if (textHitTimes > 0)
			{
				textHitflag = true;
			}
			result[result.length] = new SearchResult(pages[i].bookId, i, pages[i].pageTitle, textHitflag, titleHitflag, times);
		}
		//200Œ�ˆÈ�ã‚É‚È‚é‚Æ�AŒŸ�õ‚ðŽ~‚ß‚Ä�A�o‚Ä‚«‚½200Œ�‚ð–ß‚é
		if (result.length == 200)
		{
			overMaxCountFlag = true;
			break;
		}
		else
		{
			overMaxCountFlag = false;
		}
	}
	return result;
}
/**
 * è‡’lˆÈ�ã‚Ì�ê�‡‚ÍŒŸ�õŒ‹‰Ê‚É‚È‚é�B
 * wordsHitTime‚ÍŠeƒL�[ƒ��[ƒh‚Ì“o�ê‰ñ�”‚Ì”z—ñ
 * threshold‚Í�Ý’è‚³‚ê‚Ä‚¢‚½è‡’l
 * –ß‚è’l�Fè‡’lˆÈ�ã‚Ì�ê�‡‚Ítrue
 */
function isResult(wordsHitTime, threshold)
{
	var zeroCount = 0;
	for (var h = 0; h < wordsHitTime.length; h++)
	{
		if (wordsHitTime[h] == 0)
		{
			zeroCount++;
		}
	}
	var hitRate = (wordsHitTime.length - zeroCount)/wordsHitTime.length;
	var rate = threshold / 3;
	if (hitRate >= rate)
	{
		return true;
	}
	else
	{
		return false;
	}
}
/**
 * ŠÖ˜AŒê‚ÌŒŸ�õ‚ð�s‚¤�B�iŒŸ�õƒL�[ƒ��[ƒh‚ª1ƒ��[ƒh‚Ì‚Ý‚Ì�ê�‡�s‚¤�j
 * ŒŸ�õŒ‹‰Ê‚Í”z—ñ‚Æ‚µ‚Ä•Ô‹p‚³‚ê‚é�B
 * ŒŸ�õŒ‹‰Ê‚ª0Œ�‚Ì�ê�‡�A’·‚³‚ª0‚Ì”z—ñ‚Æ‚µ‚Ä•Ô‹p‚³‚ê�Anull‚ª•Ô‚é‚±‚Æ‚Í–³‚¢�B
 * searchWord ‚ÍŒŸ�õƒL�[ƒ��[ƒh
 * searchBookName‚Í‘I‘ð‚³‚ê‚Ä‚¢‚é•ª�û–¼�Bƒ}ƒjƒ…ƒAƒ‹ˆê——‚ª‚È‚¢�ê�‡‚Í0‚É‚È‚é
 */
function searchRelatedWord(searchWord, searchBookName)
{	
	var related = new Array();
	for(i = 0; i < relatedWords.length; i++)
	{
		if (searchBookName == "" && relatedWords[i].book != -1)
		{
			continue;
		}
		if (searchBookName != "")
		{
			if (relatedWords[i].book == -1) 
			{
				continue;
			} 
			else if (searchBookName != books[relatedWords[i].book].bookFolderName) 
			{
				continue;
			}
		}
		if(relatedWords[i].key == searchWord)
		{
			related = relatedWords[i].words;
			break;
		}
	}
	
	return related;
}

/**
 * —Þ‹`Œê‚ÌŒŸ�õ‚ð�s‚¤�B�iŒŸ�õƒL�[ƒ��[ƒh‚ª1ƒ��[ƒh‚Ì‚Ý‚Ì�ê�‡�s‚¤�j
 * ŒŸ�õŒ‹‰Ê‚Í”z—ñ‚Æ‚µ‚Ä•Ô‹p‚³‚ê‚é�B
 * ŒŸ�õŒ‹‰Ê‚ª0Œ�‚Ì�ê�‡�A’·‚³‚ª0‚Ì”z—ñ‚Æ‚µ‚Ä•Ô‹p‚³‚ê�Anull‚ª•Ô‚é‚±‚Æ‚Í–³‚¢�B
 * searchWord ‚ÍŒŸ�õƒL�[ƒ��[ƒh
 * searchBookName‚Í‘I‘ð‚³‚ê‚Ä‚¢‚é•ª�û–¼�Bƒ}ƒjƒ…ƒAƒ‹ˆê——‚ª‚È‚¢�ê�‡‚Í0‚É‚È‚é
 */
function searchSynonym(searchWord, searchBookName)
{
	var synonym = new Array();
	for(i = 0; i < synonyms.length; i++)
	{
		if (searchBookName == "" && synonyms[i].book != -1)
		{
			continue;
		}
		if (searchBookName != "")
		{
			if (synonyms[i].book == -1) 
			{
				continue;
			} 
			else if (searchBookName != books[synonyms[i].book].bookFolderName) 
			{
				continue;
			}
		}
		if(synonyms[i].key == searchWord)
		{
			synonym = synonyms[i].words;
			break;
		}
	}
	
	return synonym;
}
