/* Site Search
By Isamu Matsunaga 2007.11.18
http://www.saga-n.net/
*/

var searchResponses = new Array();
var searchUrl = location.protocol + '//' + location.host + location.pathname;
if(searchUrl.match(/^(.+\/)[^\/]+$/)) searchUrl = RegExp.$1;
if (window.ActiveXObject && !window.XMLHttpRequest)
{
	window.XMLHttpRequest = function()
	{
	try
		{
			return (new ActiveXObject('Msxml2.XMLHTTP'));
		}
		catch (e) {}
		try
		{
			return (new ActiveXObject('Microsoft.XMLHTTP'));
		}
		catch (e) {}
		return (null);
	}
}

function siteSearch(keyword, element)
{
	if(!keyword || !element) return false;
	searchResponses.length = 0;
	keyword = keyword.replace('　', ' ');
	keyword = keyword.replace(/\s+/, ' ');
	var keywords = keyword.split(' ');
	siteSearch2(location.href, keywords, element);
	var tempArray = new Array();
	for(var i = 0; i < searchResponses.length; i ++)
	{
		if(searchResponses[i]['titleMatch'] == 0 && searchResponses[i]['bodyMatch'] == 0) continue;
		tempArray.push(searchResponses[i]);
	}
	var responseHtml = "<p>サイト内検索結果</p>\n";
	if(tempArray.length > 0)
	{
		responseHtml += "<ol>\n";
		tempArray.sort(arraySort);
		for(var i = 0; i < tempArray.length; i ++)
		{
			if(tempArray[i]['titleMatch'] == 0 && tempArray[i]['bodyMatch'] == 0) break;
			responseHtml += "<li>";
			responseHtml += '<a href="' + tempArray[i]['url'] + '">' + tempArray[i]['title'] + "</a><br>\n";
			responseHtml += tempArray[i]['gist'];
			responseHtml += "</li>\n";
		}
		responseHtml += "</ol>\n";
	}
	else
	{
		responseHtml += "<p>該当するページがありませんでした。</p>\n";
	}
	document.getElementById(element).innerHTML = responseHtml;
	return false;
}

function siteSearch2(url, keywords, element)
{
	for(var i = 0; i < searchResponses.length; i ++)
	{
		if(searchResponses[i]['url'] == url) return;
	}
	xmlhttp = new XMLHttpRequest();
	xmlhttp.open('GET', url, false);
	xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
	xmlhttp.send(null);
	if (xmlhttp.status == 200)
	{
		RegExp.multiline = true;
		searchResponse = new Array();
		searchResponse['url'] = url;
		searchResponse['title'] = '';
		searchResponse['titleMatch'] = 0;
		searchResponse['bodyMatch'] = 0;
		searchResponse['gist'] = '';
		if(xmlhttp.responseText.match(/<title>([^<\/]+)<\/title>/i))
		{
			searchResponse['title'] = RegExp.$1;
			for(var i = 0; i < keywords.length; i++)
			{
				var keyword = new RegExp(keywords[i], 'ig');
				if(keyword.test(searchResponse['title']))
				{
					searchResponse['titleMatch'] += searchResponse['title'].match(keyword).length;
				}
			}
		}
		var body = '';
		var re = new RegExp('(?:(?!>).)*id\s*=[\s|"|\']*' + element, 'i');
		if(xmlhttp.responseText.toLowerCase().search(re))
		{
			body = xmlhttp.responseText.substr(xmlhttp.responseText.toLowerCase().search(re));
		}
		if(body.toLowerCase().indexOf('</body'))
		{
			body = body.substr(0, body.toLowerCase().indexOf('</body'));
		}
		body = body.replace(/<[^>]+>/g, '');
		for(var i = 0; i < keywords.length; i++)
		{
			var keyword = new RegExp(keywords[i], 'ig');
			if(keyword.test(body))
			{
				searchResponse['bodyMatch'] += body.match(keyword).length;
			}
		}
		body = body.replace(/[\s]/g, '');
		body = body.replace(/^(?:&nbsp;)*/, '');
		searchResponse['gist'] = body.substr(0, 200) + '...';
		searchResponses.push(searchResponse);
		var aTags = new Array();
		if(xmlhttp.responseText.match(/<a\s[^>]+>/i))
		{
			aTags = xmlhttp.responseText.match(/<a\s[^>]+>/ig);
			var href = '';
			for(var i = 0; i < aTags.length; i ++)
			{
				if(aTags[i].match(/(?:href\s*=\s*"([^"]+))|(?:href\s*=\s*'([^']+))'/))
				{
					href = RegExp.$1;
					if(href.match(/^\.\.\//)) continue;
					if(href.match(/^\.\//)) href = href.replace(/^\.\//, searchUrl);
					if(!href.match(/http:\/\//)) href = searchUrl + href;
					if(href.indexOf(searchUrl) == 0)
					{
						siteSearch2(href, keywords, element);
					}
				}
			}
		}
	}
}

function arraySort(a, b)
{
	if(b['titleMatch'] == a['titleMatch'])
	{
		return(b['bodyMatch'] - a['bodyMatch']);
	}
	else
	{
		return(b['titleMatch'] - a['titleMatch']);
	}
}
