var watchlist = [];
// var startTime		= 100;
// var fromCountDown	= startTime;
// var toCountDown		= startTime;
var candidates_MAX = 10;
var andMore = '';
var btns = {
	"start-ic-top"		:"search-button-top",
	"end-ic-top"		:"search-button-top",
	"start-ic-bottom"	:"search-button-bottom",
	"end-ic-bottom"		:"search-button-bottom"
    };
var pairs = {
	"start-ic-top"		:"start-ic-top",
	"end-ic-top"		:"start-ic-top",
    "start-ic-bottom"	:"start-ic-bottom",
	"end-ic-bottom"		:"start-ic-bottom"
	};
var tuples = {
	"start-ic-top"		:"end-ic-top",
	"end-ic-top"		:"start-ic-top",
    "start-ic-bottom"	:"end-ic-bottom",
	"end-ic-bottom"		:"start-ic-bottom"
	};
// var btnID = '';

function polling() {
	var box;
	var boxVal;
	var btnID;
	for (var boxID in watchlist) {
		box = document.getElementById(boxID);
		if (box) {
			boxVal = box.value;
			btnID = btns[boxID];
			if (boxVal != watchlist[boxID] && boxVal != "") {
				watchlist[boxID] = boxVal;
				$(btnID).value = '検 索';
				$(btnID).disabled = false;
				suggest(boxID,'suggest-box',boxVal);
			} else if (boxVal == "" && watchlist[boxID] != "") {
				$(btnID).value = '検 索';
				$(btnID).disabled = false;
				kill('suggest-box');
			}
		}
	}
}

function checkRoute(btnID, req) {
	if (req.responseText == '1') {
		var btn = $(btnID);
		btn.value = "経路あり";
		btn.disabled = false;
	} else {
		var btn = $(btnID);
		btn.value = "経路なし";
		btn.disabled = true;
	}
}

function suggest(baseBoxID, suggestBoxID, str) {
	var baseBox		= document.getElementById(baseBoxID);
	var suggestBox	= document.getElementById(suggestBoxID);;
	var tmp = cumulativeOffset(baseBox);
//	var h = baseBox.offsetTop + baseBox.offsetHeight;
	var h = tmp[1] + baseBox.offsetHeight;
	var l = tmp[0];
	var btnID = btns[baseBoxID];

//	ajaxCheckRoute(baseBoxID);
//	suggestBox.style.left		= baseBox.offsetLeft + 'px';
	suggestBox.style.left		= l + 'px';
	suggestBox.style.top		= h + 'px';
//	suggestBox.style.height		= (2 * baseBox.offsetHeight) + 'px';
	suggestBox.style.height		= 'auto';
	suggestBox.style.width		= '400px';
	suggestBox.style.padding	= '5px';
	suggestBox.style.zIndex    = '999';
	comps = completion(str);
	//		alert(comps);
	sep = '';
	closeBtn = '<span onClick="kill(\'' + suggestBoxID + '\');" onMouseOver="color(this);" onMouseOut="uncolor(this);" style="cursor:pointer;"><u>[×]</u></span>';
//	alert('<table class="suggestTbl"><tr><td align="left"><font color="#4169e1">&lt;候補一覧&gt;</font></td><td align="right">'+closeBtn+'</td></tr></table>');
	suggestBox.innerHTML = '<table class="suggestTbl"><tr><td align="left"><font color="#4169e1">&lt;候補一覧&gt;</font></td><td class="right">'+closeBtn+'</td></tr></table>';
	for ( i = 0 ; i < comps.length; ++i ) {
		//			alert(mkOnClick('start-ic','suggest-from',comps[i]));
		suggestBox.innerHTML += sep + mkOnClick(baseBoxID,suggestBoxID,comps[i]);
		sep = ',<wbr>';
	}
	suggestBox.innerHTML += andMore;
}

function mkOnClick(boxID,suggestID,str) {
	return ('<span onClick="reflection(\'' + boxID + '\',\'' + suggestID + '\',\'' + str + '\');" onMouseOver="color(this);" onMouseOut="uncolor(this);" style="cursor: pointer; white-space: nowrap;"><u>' + str + '</u></span>');
}

function completion(str) {
	var i,j = 0;
	var comps = Array();
	andMore = '';
	if (str.match(/^\s*$/)) {
	} else {
		for ( i = 0; i < icNamesLen; i++ ) {
//			if (icNames[i].match(str)) {
			if (icNames[i].search(str) > -1) {
				j++;
				if (j > candidates_MAX) {
					andMore = ' ... ';
					break;
				}
				comps.push(icNames[i]);
			}
		}
	}
	return comps;
}

function reflection(boxID,suggestID,str) {
	var box     = document.getElementById(boxID);

	watchlist[boxID] = str;

//	ajaxCheckRoute(boxID);

	box.value = str;
	kill(suggestID);
}

function ajaxCheckRoute(baseBoxID) {
	var fID = icIDs[watchlist[pairs[baseBoxID]]];// = icIDs[watchlist[baseBoxID]];
	var tID = icIDs[watchlist[tuples[pairs[baseBoxID]]]];// = icIDs[watchlist[pairs[baseBoxID]]];
	var btnID = btns[baseBoxID];
//	btnID = btns[baseBoxID];
//	alert(watchlist[pairs[baseBoxID]]);
//	alert('f=' + fID + '&t=' + tID);
	if ((typeof fID != 'undefined') && (typeof tID != 'undefined')) {
//		alert('check: f=' + fID + '&t=' + tID);
		$(btnID).value = '経路確認中...';
		var ajax = new Ajax.Request(
			'api/hasRoute.php'
			,{
				method: 'get',
				parameters: 'f=' + fID + '&t=' + tID,
				onSuccess: function(req) { checkRoute(btnID,req); }
			});
	}
}

function color(obj){
	obj.style.color  = '#ff0000';
}

function uncolor(obj){
	obj.style.color = '#000000';
}

function kill(suggestBoxID){
	var box = document.getElementById(suggestBoxID);

	box.style.left		= '0px';
	box.style.top		= '0px';
	box.style.width		= '0px';
	box.style.height	= '0px';
	box.style.padding	= '0px';
	box.style.zIndex    = '-999';
	box.innerHTML = '';
	return false;
}

function cumulativeOffset(element) {
    var valueT = 0, valueL = 0;
    do {
      valueT += element.offsetTop  || 0;
      valueL += element.offsetLeft || 0;
      element = element.offsetParent;
    } while (element);
    return [valueL, valueT];
}

function init() {
	var s = document.createElement('script');
//	s.charset='Shift_JIS';
	s.src='js/icNames.js';
	document.body.appendChild(s);
}
