
function findline(str,pos){
	var endpos1 = str.indexOf("。",pos)+1;
	var endpos2 = str.indexOf(",",pos);
	if (endpos1<pos) endpos1=str.length;
	if (endpos2<pos) endpos2=str.length;
	var endpos = (endpos1<endpos2)? endpos1:endpos2;
	var startpos1 = str.lastIndexOf("。",pos);
	var startpos2 = str.lastIndexOf(",",pos);
	var startpos = (startpos1>startpos2)? startpos1:startpos2;

	return str.substring(startpos+1,endpos);
}

function htmlfind(){
	var space = new RegExp("\ +","g")
	var words = $("#findbox").val().toLowerCase().replace(space," ").replace(/\ $/,"").replace(/^ /,"").split(" ");
	if ((words.length==1)&&(words[0]=="")){
		var arr = [];
		arr.push("「"+$("#findbox").val()+"」 は見つかりませんでした。")
		$("#findarea").html(arr.join("\n")).animate({ scrollTop: 0 }, 'fast');
		return;
	}

	var list = JSON.parse(JSON.stringify(htmllist));
	for (var i in list) list[i].cnt = 0;
	
	var temp=[];
	for (var j in list){
		var str = list[j].text.toLowerCase();
		var c = 0;
		for (var i in words){ if(str.indexOf(words[i])>-1){c++;cnt++;continue;}}
		if (words.length!=c) continue;
		
		var cnt = 0
		for (var i in words){
			var pos = 0;
			var ar = [];
			while((str.indexOf(words[i],pos)!=pos)&&(str.indexOf(words[i],pos)!=-1)){
				pos = str.indexOf(words[i],pos);
				ar.push(pos);
				pos++;
			}
			list[j].cnt += ar.length;
			ar =ar.sort();
			ar = ar.filter(function (x, y, self) { return self.indexOf(x) === y;});
			list[j].pos = ar;
			list[j].tolower = str;
		}
		if((list[j].title.indexOf("-")==1)||(list[j].title.indexOf("-")==2)){ list[j].syo = list[j].title.substr(0,list[j].title.indexOf("-"));}
		else { list[j].syo = 999;}
		temp.push(list[j]);
	}

	temp = hsort(temp,"syo",1);
	var arr = [];
	for (var i in temp){
		var url = (location.pathname.indexOf("manuals/")>-1)? temp[i].file.replace("manuals/",""):temp[i].file;
		arr.push("<a href="+url+">"+temp[i].title+'</a><br>');
		arr.push('&nbsp;<font style="font-size:80%;"color="green">'+temp[i].file+"</font><br>");
		var str = findline(temp[i].text,temp[i].pos[0]);
		if ((str.length<150)&&(temp[i].pos.length>1)) str += "&nbsp;&nbsp;...&nbsp;&nbsp;" + findline(temp[i].text,temp[i].pos[1]);
		if ((str.length<150)&&(temp[i].pos.length>2)) str += "&nbsp;&nbsp;...&nbsp;&nbsp;" + findline(temp[i].text,temp[i].pos[2]);
		
		var ar = [];
		for (var j in words){
			var pos = 0,pos2=0; 
			var str2 = str.toLowerCase()
			while (str2.indexOf(words[j],pos)!=-1){
				pos2 = str2.indexOf(words[j],pos);
				ar.push(str.substring(pos,pos2));
				ar.push("<b>"+str.substr(pos2,words[j].length)+"</b>")
				pos = pos2 + words[j].length;
			}
			ar.push(str.substr(pos,str.length));
			str = ar.join("");
		}
		
		arr.push('<font style="font-size:90%;">'+str+"</font><br><br><br>");
	}
	if (arr.length==0) arr.push("「"+$("#findbox").val()+"」 は見つかりませんでした。")
	$("#findarea").html(arr.join("\n")).animate({ scrollTop: 0 }, 'fast');
}


var hsort = function (hash, key, order) {
    hash.sort(function(a, b){
        var a_s = a[key].toString().toLowerCase();
        var b_s = b[key].toString().toLowerCase();
        if (order*(a[key]-b[key])) {
            return order*(a[key]-b[key]);
        } else if (a_s > b_s) {
            return order*1;
        } else if (a_s < b_s) {
            return order*-1;
        } else {
            return 0;
        }
    });
    return hash;
}


$(function() {
    $( 'a[rel*=leanModal]').leanModal({
        top: 50,                     // モーダルウィンドウの縦位置を指定
        overlay : 0.5,               // 背面の透明度 
    });
});

String.prototype.indexOfCnt = function(word){
	var s = this.toString();
	var cnt=0;
	return s.split(word).length -1;
};
