// uu_ajax.js
// Last Updated: "2010-01-24 Sun 15:21:16"

var gzpattern = "";
var spanCache;
var grepCache;
var isInternetExplorer;
var withpix = 0;
var sex = new Array;
sex["mm"] = "inline";
sex["ff"] = "inline";
sex["mf"] = "inline";
sex["fm"] = "inline";

function gzInitCache() {
	// list.html or list_text.html?
	if(window.location.pathname.match(/youyou\/$|list.html/)) {
		withpix = 1;
	}
	if(navigator.appName.indexOf("Microsoft") >= 0) {
		isInternetExplorer = true;
	} else {
		isInternetExplorer = false;
	}
	spanCache = document.getElementById("ajaxdata").getElementsByTagName("span");
	grepCache = new Array();

	for (var i = 0; i < spanCache.length; i++) {
		var e = spanCache[i];
		if(e.className.match(/^gz ?/)) {
			var line = null;
			if(isInternetExplorer) {
				line = e.innerText;
			} else {
				line = e.innerHTML.replace(/<[^>]+>/g, "");
			}
			// if list.html, get world map info.
			if(withpix) {
				var divs = e.getElementsByTagName("div");
				for (var ii = 0; ii < divs.length; ii++) {
					var adiv = divs[ii];
					if(adiv.className.match(/^w-map$/)) {
						var map = adiv;
						var dot = adiv.firstChild.style.left + adiv.firstChild.style.top;
						break;
					}
				}
			}
			var cache = new Array();
			cache.push(e);
			cache.push(line);
			if(withpix) {
				var pair = new Array();
				pair.push(map);
				pair.push(dot);
				cache.push(pair);
			}
			grepCache.push(cache);
		}
	}
}

function gzGrep(string) {
	if(!grepCache) {
		gzInitCache();
	}
	try {
		var str = string.split(" ");
		for (var i = 0; i < str.length && str[i] != " "; i++) {
			var pattern = new RegExp(str[i], "i");
			var prev_dot = "";
			var prev_num = -1;
			var men = 0;
			var women = 0;
			for (var ii = 0; ii < grepCache.length; ii++) {
				var e = grepCache[ii][0];
				var line = grepCache[ii][1];
				if(line.match(pattern)) {
					if(i == 0) { // alsways match with the first word.
						e.style.display = "inline";
					} else if(e.style.display == "inline") { // match if all the other words matched.
						e.style.display = "inline";
					}
					// check if sex button is off.
					if(withpix && e.style.display == "inline") {
						var cname = e.className.replace("gz ", "");
						if(sex[cname] == "none") {
							e.style.display = "none";
						} else {
							var this_num = Number(e.getElementsByTagName("a")[0].name);
							var same_person = prev_num - this_num;
							if(cname == "mm") {
								men += 2;
							} else if(cname == "ff") {
								women += 2;
							} else {
								men++;
								women++;
							}
							// check if the same parson in the previous picture.
							if(same_person == 1) {
								if(cname == "mm" || cname == "fm") {
									men--;
								} else {
									women--;
								}
							} else if(same_person == -1) {
								if(cname == "mm" || cname == "mf") {
									men--;
								} else {
									women--;
								}
							}
							prev_num = this_num;
						}
					}
					// check if previous world map is same as this.
					if(withpix && e.style.display == "inline") {
						var map = grepCache[ii][2][0];
						var dot = grepCache[ii][2][1];
						if(prev_dot == dot) {
							map.style.display = "none";
						} else {
							map.style.display = "inline";
						}
						prev_dot = dot;
					}
				} else {
					e.style.display = "none";
				}
				if(withpix) {
					document.getElementById("men").innerHTML = men;
					document.getElementById("women").innerHTML = women;
					document.getElementById("total").innerHTML = men + women;
				}
			}
		}
	} catch (e) {
		// Ignore errors of invalid regular expressions.
	}
}

function getPage(pageURL) {
	gzpattern = document.getElementById("pattern").value;
	xmlhttp = createXMLHttp();
	document.getElementById("loading").innerHTML = "<span class='loading-msg'>読み込み中...</span>";
	if(xmlhttp) {
		xmlhttp.onreadystatechange = setPageData;
		xmlhttp.open('GET', pageURL);
		xmlhttp.send(null);
	} else {
		document.getElementById("loading").innerHTML = "<span class='loading-err'>失敗!!</span>";
	}
}

function setPageData() {
	if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		document.getElementById("ajaxdata").innerHTML = xmlhttp.responseText;

		// restore sex button status. if it was "on", do nothing because default value is "on".
		if(sex["mm"] == "none") {
			document.getElementById("btn-mm").className = "off";
		}
		if(sex["ff"] == "none") {
			document.getElementById("btn-ff").className = "off";
		}
		if(sex["mf"] == "none") {
			document.getElementById("btn-mf").className = "off";
		}
		if(sex["fm"] == "none") {
			document.getElementById("btn-fm").className = "off";
		}

		// reload and run gonzui.
		if(gzpattern) {
			document.getElementById("pattern").value = gzpattern;
		}
		gzInitCache();
		gzGrep(document.getElementById("pattern").value);
	}
}

function createXMLHttp() {
	try {
		return new ActiveXObject ("Microsoft.XMLHTTP");
	} catch(e) {
		try {
			return new XMLHttpRequest();
		} catch(e) {
			return null;
		}
	}
	return null;
}

function toggleBtn(cname) {
	var none_inline = ["none", "inline"];
	var off_on = ["off", "on"];
	var e = document.getElementById("btn-" + cname);

	// toggle button appearance.
	e.className = off_on[e.className.indexOf("off") + 1];
	sex[cname] = none_inline[sex[cname].indexOf("none") + 1];
	// update list.
	gzGrep(document.getElementById("pattern").value);
}

function toggleDisp(id) {
	var e = document.getElementById(id);

	if(e.style.display == "inline") {
		e.style.display = "none";
	} else if(e.style.display == "none") {
		e.style.display = "inline";
	} else { // undefined or something else.
		e.style.display = "inline";
	}
}

