MAX = 15; // 表示数
PATH = "http://choppo.s20.xrea.com/train/";
PAGE_URL = "index.htm";
DUMMY_IMG = "white.png";
SCRIPT_START_ID = "result_url";

function Global()
{
	this.search = null; // 検索関数
	this.fk = new Array; // キーワード
	this.loadFlg = false;
	this.hit = new Array; // ヒットした写真の添え字を格納
	this.photo = new Array;
	this.requestSmall = 0;
}

function Photo(ymd, name, num, station, path, info, hidden)
{
	this.name = name;
	this.num = num;
	this.info = (info)? info:"";
	this.hidden = hidden;
	this.staion = station;
	this.ymd = ymd;
	this.path = path;
	this.isComplete = false;
	this.isCompleteSmall = false;
	
	// メソッド
	this.getURL = function() {
		return this.path +".jpg";
	};
	this.getSmallURL = function() {
		return this.path +"_s.jpg";
	};
	this.getTitle = function() {
		return this.name + " " +this.num;
	};
	this.getNote = function() {
		var tmp = "";
		if (this.info.length > 0) {
			tmp = "*";
		}
		return "@"+ this.staion +"#"+ this.ymd + tmp + this.info;
	};
}

function Keyword(str)
{
	this.func;
	this.not = false;

	// メソッド
	this.search = function(photo){
		if (this.not)
			return !this.func(photo);
		else
			return this.func(photo);
	};
	searchTitle = function(photo) {
		var temp = photo.name + photo.num + photo.hidden;
		temp = temp.toLowerCase();
		return (temp.indexOf(str) != -1);
	};
	searchStaion = function(photo) {
		return (photo.staion == str);
	};
	searchInfo = function(photo) {
		var temp = photo.info.toLowerCase();
		return (temp.indexOf(str) != -1);
	};
	searchYMD = function(photo) {
		return (photo.ymd.indexOf(str) != -1);
	};

	// コンストラクタ
	str = str.toLowerCase();
	str = str.replace(/−/g,"-");
	str = str.replace(/＠/g,"@");
	str = str.replace(/＃/g,"#");
	str = str.replace(/＊/g,"*");
	str = str.replace(/特別急行/g,"特急");
	str = str.replace(/ジョイフルトレイン/g,"jt");
	str = str.replace(/ジョイフル/g,"jt");
	str = str.replace(/本線/g,"");
	str = str.replace(/線/g,"");
	str = str.replace(/駅/g,"");
	str = str.replace(/東京急行/g,"東急");
	str = str.replace(/京浜急行/g,"京急");
	str = str.replace(/形/g,"系");

	this.func = searchTitle;
	if (str.charAt(0) == "-") {
		this.not = true;
		str = str.substring(1, str.length);
	}
	if (str.charAt(0) == "@") {
		this.func = searchStaion;
		str = str.substring(1, str.length);
	} else if (str.charAt(0) == "*") {
		this.func = searchInfo;
		str = str.substring(1, str.length);
	} else if (str.charAt(0) == "#") {
		this.func = searchYMD;
		str = str.substring(1, str.length);
	}
}

function ajax(in_url, in_loadProc, in_arg)
{
	var http;
	if (location.href.indexOf("file://") != -1) {
		http = null;
	} else if (window.XMLHttpRequest) {
		http = new XMLHttpRequest();
	} else if(window.ActiveXObject) { // WinIE
		try {
			http = new ActiveXObject("Msxml2.XMLHTTP"); // MSXML2
		} catch (e) {
			try {
				http =  new ActiveXObject("Microsoft.XMLHTTP"); // MSXML
			} catch (e2) {
				http = null;
			}
		}
	} else {
		http = null;
	}
	if (http) {
		http.open("GET", PATH + in_url, true);
		http.onreadystatechange = function() {
			if (http.readyState == 4) {
				in_loadProc(http, in_arg);
			}
		}
		http.send(null);
	} else {
		var img = new Image;
		img.src = in_url;
		in_loadProc(null, in_arg);
	}
}


function setKeyword(str)
{
	var i;
	str = str.replace(/　/g, " ");
	str = str.split(" ");
	for (i in str) {
		if (str[i].length > 0) {
			g.fk[i] = new Keyword(str[i]);
		}
	}
}

function andSearch(photo)
{
	var i;
	for (i in g.fk) {
		if (g.fk[i].search(photo))
			continue;
		return false;
	}
	return true;
}

function orSearch(photo)
{
	var i;
	for (i in g.fk) {
		if (g.fk[i].search(photo))
			return true;
	}
	return false;
}

function renovate()
{
	g.hit.length = 0;
	var i;
	var temp = document.getElementById("keybox").value;
	var company_str = document.getElementById("company").value;

	if (temp != "") {
		setKeyword(temp);
		if (document.getElementById("find_how").value == "or")
			g.search = orSearch;
		else
			g.search = andSearch;
	} else { // 検索条件なし
		g.search = function(target) { return true; };
	}

	var temp_array = new Array(0);
	for (i in g.photo) {
		if (company_str != "all" && g.photo[i].path.indexOf(company_str) != 0)
			continue;
		if (g.search(g.photo[i])) {
			temp_array[temp_array.length] = i;
		}
	}
	if (document.getElementById("order").value == "new") { // 新しい順に
		g.hit = temp_array;
	} else { // 古い順に
		for (i in temp_array)
			g.hit[temp_array.length - 1 - i] = temp_array[i];
	}
	if (g.loadFlg) {
		document.getElementById("photo_num").selectedIndex = 0;
	}
	photosSet();
	showSmallPhoto();
}

function v(id, h)
{
	document.getElementById(id).style.visibility = (h==0)? "hidden":"visible";
}

// 写真データ生成
function d(ymd, name, num, station, path, info)
{
	var temp = name.split(":");
	var name = temp[0];
	var hidden = (temp.length>1)? temp[1]:"";
	g.photo[g.photo.length] = new Photo(ymd, name, num, station, path, info, hidden);
}

// 情報表示
function resultInfo()
{
	var temp = "", info1 = result = " ";
	if (!document.getElementById("find_result").firstChild) {
		return; // 非対応
	}
	temp = g.photo.length;
	if (g.hit.length > 0) {
		if (g.photo.length != g.hit.length) {
			temp += "枚のうちの" + g.hit.length;
		}
		result = temp +"枚の写真を表示中！";
		info1 = "写真をクリックすると拡大表示になります。";
	} else {
		temp = "写真が見つかりませんでした。";
		result = temp;
	}
	document.getElementById("find_result").firstChild.nodeValue = result;
	document.getElementById("info_box1").firstChild.nodeValue = info1;
	document.getElementById("info_box2").firstChild.nodeValue = " ";
}

// 写真縮小表示
function showSmallPhoto()
{
	var op = document.getElementById("photo_num");
	var page = op.selectedIndex;
	var start = page * MAX;

	setResultURL();
	v("prev", 0);
	v("next", 0);
	v("photo_num", 0);	
	document.getElementById("zoom_photo").src = DUMMY_IMG;
	document.getElementById("zoom_photo").style.display = "none";
	for(i=0; i<MAX; i++){
		v("photo"+i, 0);
		document.getElementById("photo"+i).title = " ";
	}
	if (page >= op.options.length || g.hit.length < 1) {
		resultInfo();
		return;
	}
	setStatusBox(false);
	for(request=0; request<MAX && g.hit.length>start+request; request++);
	g.requestSmall = request;
	for(i=0; i<request; i++){
		var index = g.hit[start+i];
		var data = new requestSmallPhotoData(index, i);
		if(g.photo[index].isComplateSmall){
			completeSmallPhoto(null, data);
		} else {
			ajax(g.photo[index].getSmallURL(), completeSmallPhoto, data);
		}
	}
}

function requestSmallPhotoData(index, i)
{
	this.index = index;
	this.i = i;
}

function completeSmallPhoto(http, arg)
{
	g.photo[arg.index].isCompleteSmall = true;
	g.requestSmall--;
	v("photo"+arg.i, 1);
	document.getElementById("photo"+arg.i).src = g.photo[arg.index].getSmallURL();
	document.getElementById("photo"+arg.i).title = g.photo[arg.index].getTitle();
	if (g.requestSmall > 0) {
		return;
	}
	setStatusBox(true);
	resultInfo();
	v("photo_num", 1);
	var op = document.getElementById("photo_num");
	var page = op.selectedIndex;
	v("prev", (page == 0 || page >= op.options.length)? 0:1);
	v("next", (page >= op.options.length - 1)? 0:1);
}

function completeZoomPhoto(http, arg)
{
	g.photo[arg].isComplete = true;
	setZoomPhoto(arg);
}

function requestZoomPhoto(index)
{
	setStatusBox(false);
	ajax(g.photo[index].getURL(), completeZoomPhoto, index);
}

// photo[index]の写真拡大表示初期設定
function setZoomPhoto(index)
{
	if(document.getElementById("info_box1").firstChild){
		document.getElementById("info_box1").firstChild.nodeValue = g.photo[index].getTitle();
		document.getElementById("info_box2").firstChild.nodeValue = g.photo[index].getNote();
	}
	setStatusBox(true);
	document.getElementById("zoom_photo").src = g.photo[index].getURL();
	document.getElementById("zoom_photo").style.display = "inline";
}

function setStatusBox(bool)
{
	if(document.getElementById("status_box").firstChild){
		document.getElementById("status_box").firstChild.nodeValue = bool? " ":"読み込み中" ;
	}
}

// セレクトボックスのセット
function photosSet()
{
	var i;
	var obj = document.getElementById("photo_num");
	var op_max = Math.ceil(g.hit.length / MAX);
	if (op_max == 0) {
		op_max = 1;
	}
	obj.options.length = op_max;
	for (i=0; i<op_max; i++) {
		obj.options[i].text = Number(i) + 1;
	}
}

function setResultURL()
{
	var result_url = "";
	var temp = document.getElementById("keybox").value;
	if(temp.length > 0){
		result_url = "key="+ escape(temp).replace(/%/g,"%25");
	}
	if(document.getElementById("find_how").value == "or"){
		if(result_url.length > 1)
			result_url += "&";
		result_url += "find_how=or";
	}
	if(document.getElementById("photo_num").selectedIndex > 0){
		if(result_url.length > 1)
			result_url += "&";
		result_url += "photo_num="+ (document.getElementById("photo_num").selectedIndex+1);
	}
	if(document.getElementById("company").value != "all"){
		if(result_url.length > 1)
			result_url += "&";
		result_url += "company="+ document.getElementById("company").value;
	}
	if(document.getElementById("order").value == "old"){
		if(result_url.length > 1)
			result_url += "&";
		result_url += "order=old";
	}
	document.getElementById("result_url").href = PAGE_URL;
	if(result_url.length > 0)
		document.getElementById("result_url").href += "?"+ result_url;
}

function clickPhoto()
{
	var start, index;
	if (g.requestSmall > 0) {
		return;
	}
	start = document.getElementById("photo_num").selectedIndex * MAX + (this.id.substring(5) - 0);
	index = g.hit[start];
	if (g.photo[index].isComplete) {
		setZoomPhoto(index);
	} else {
		requestZoomPhoto(index);
	}
}

function prevFunc()
{
	var obj = document.getElementById("photo_num");
	if (obj.selectedIndex > 0) {
		obj.selectedIndex--;
		showSmallPhoto();
	}
}

function nextFunc()
{
	document.getElementById("photo_num").selectedIndex++;
	showSmallPhoto();
}

function keyupFunc(e)
{
	if ((window.event && window.event.keyCode == 13) || (e && e.keyCode == 13)){
		renovate();
	}
}

// 初期設定
function firstSetting() // data.js から呼び出し
{
	var first_photo_num_index = 0;
	var i;
	if(!document.getElementById(SCRIPT_START_ID)) {
		setTimeout("firstSetting()", 250);
		return;
	}
	if(location.href.indexOf("?") != -1){
		var get_start = location.href.indexOf("?") + 1; // ?の次の位置を取得
		var get_str = location.href.substr(get_start); // ?以降の文字列取得
		get_str = get_str.split("&");
		for(i in get_str){
			var str = get_str[i].split("=");
			var sharp_cut_str=str[1].split("#");
			if(str[0] == "key"){
				str[1] = unescape(str[1].replace(/%25/g,"%"));
				document.getElementById("keybox").value = str[1];
			}
			else if(str[0] == "find_how")
				document.getElementById("find_how").value = sharp_cut_str[0];
			else if(str[0] == "photo_num")
				first_photo_num_index = sharp_cut_str[0]-1;
			else if(str[0] == "company")
				document.getElementById("company").value = sharp_cut_str[0];
			else if(str[0] == "order")
				document.getElementById("order").value = sharp_cut_str[0];
		}
	}
	renovate();
	if (first_photo_num_index > 0 && first_photo_num_index < document.getElementById("photo_num").options.length) {
		document.getElementById("photo_num").selectedIndex = first_photo_num_index;
		showSmallPhoto();
	}
	with (document) {
		getElementById("prev").onclick = prevFunc;
		getElementById("photo_num").onchange = showSmallPhoto;
		getElementById("next").onclick = nextFunc;
		getElementById("find_btn").onclick = renovate;
		getElementById("keybox").onkeyup = keyupFunc;
		getElementById("find_how").onkeyup = keyupFunc;
		getElementById("company").onkeyup = keyupFunc;
		getElementById("order").onkeyup = keyupFunc;
		getElementById("for_find_option").onclick = function(){
			getElementById("for_find_option").style.display = "none";
			getElementById("find_option").style.display = "block";
			return false;
		};
	}
	for (i=0; i<MAX; i++) {
		var obj = document.getElementById("photo"+i);
		obj.onclick = clickPhoto;
	}

	g.loadFlg = true;
}

g = new Global();
