/*_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

	本の枝折 サーチ JavaScript

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/*/
var	_selbx = new SelectBox();
var	_txtbx = new TextBox();
var	_delay = new Timer('_delay.cleartimer()', 2);
var	_oomoji = new Daiji();
var	_fontCookie = new Cookie('daijirinFont', '', 4032);

var	_ebookoff = false;	//eBookOff例外処理

function SelectBox()
{
	this.box = null;
	this.boxindex = ['newbox', 'oldbox', 'etcbox'];
}
SelectBox.prototype.setbox = function(id) {
	if (this.box == id)
		return;
	for (var i in this.boxindex) {
		if (this.boxindex[i] != id)
			dE(this.boxindex[i]).selectedIndex = -1;
	}
	this.box = id;
};
SelectBox.prototype.selected = function(id) {
	if (id)
		this.setbox(id);
	if (!this.box)
		return;
	var	e = dE(this.box);
	var	res = e.options[e.selectedIndex].value;
	return res.length ? res : false;
};


function TextBox()
{
	this.str = { 'titleInput': 'title or words', 'authorInput': 'author or words' };
	this.cf = { 'titleInput': false, 'authorInput': false };	//クリック
	this.home = true;
}
TextBox.prototype.click = function(id) { this.cf[id] = true; };
TextBox.prototype.resetclick = function(id) {
	var	res = this.cf[id];
	this.cf[id] = false;
	return res;
};
TextBox.prototype.del = function(id, f) {
	var	e = dE(id);
	var	eq = e.value == this.str[id];
	e.style.color = !f && e.value.length && !eq ? '#666' : '#bbb';
	if (!e.value.length || f && !eq) {
		e.value = this.str[id];
		return true;
	}
	return false;
};
TextBox.prototype.clean = function(id) {
	var	e = dE(id);
	e.style.color = '#000';
	if (e.value == this.str[id])
		e.value = '';
};
TextBox.prototype.blank = function(id) {
	var	ev = dE(id).value;
	return !ev.length || ev == this.str[id] ? true : false;
};
TextBox.prototype.button = function() {
	var	f = this.blank('titleInput') && this.blank('authorInput');
	if (this.home) {
		if (!f) {
			dE('homeButton').value = 'Delete';
			this.home = false;
		}
	}
	else if (f) {
		dE('homeButton').value = 'HOME';
		this.home = true;
	}
};


function Timer(func, sec)
{
	this.func = func;
	this.sec = sec;
	this.istimer = null;
}
Timer.prototype.cleartimer = function() {
	if (this.istimer) {
		clearTimeout(this.istimer);
		this.istimer = null;
	}
};
Timer.prototype.settimer = function(byo) {
	this.cleartimer();
	if (byo)
		this.sec = byo;
	if (this.sec)
		this.istimer = setInterval(this.func, this.sec * 1000);
};


function Daiji()
{
	this.pg = false;
	this.num = 0;
	this.font = '';
	this.fList = new Array();
}
Daiji.prototype.reset = function() { this.num = 0; };
Daiji.prototype.openpage = function() {
	if (this.pg)
		return;
	unveil('rightbox', false);
	unveil('daijirin', true);
	this.pg = true;
	dE('searchButton').value = '→ → →';
};
Daiji.prototype.closepage = function() {
	if (!this.pg)
		return;
	dE('oomoji').innerHTML = this.font = '';
	unveil('daijirin', false);
	unveil('rightbox', true);
	this.pg = false;
	this.num = 0;
	dE('searchButton').value = 'SEARCH';
};
Daiji.prototype.clear = function() {
	if (this.pg) {
		this.num = 0;
		dE('oomoji').innerHTML = '';
	}
};
Daiji.prototype.oomoji = function(f) {
	var	o = dE('oomoji');
	var	e = dE('fontfamily');
	if (e && e.selectedIndex > -1) {
		if (!e.selectedIndex) {
			_fontCookie.eatCookie();
			o.style.fontFamily = this.font = '';
		}
		else if (e.options[e.selectedIndex].value != this.font) {
			this.font = e.options[e.selectedIndex].value;
			o.style.fontFamily = "'" + this.font + "',serif";
			_fontCookie.bakeCookie(this.font);
		}
	}
	var	t = dE('titleInput').value;
	var	a = dE('authorInput').value;
	if (t == _txtbx.str['titleInput'])
		t = '';
	if (a == _txtbx.str['authorInput'])
		a = '';
	var	s, mj = t + a;
	if (!mj.length) {
		this.num = 0;
		return;
	}
	if (f && this.num)
		this.num--;
	t = mj.charAt(this.num);
	if (!t || !t.length)
		t = mj.charAt(this.num = 0);
	this.num++;
	o.innerHTML = t;
};


function doubleClick(id)
{
	var	e = dE(id);
	if (_txtbx.resetclick(id)) {
		e.value = '';
		_txtbx.button();
		_oomoji.reset();
	}
	else if (e.value.length)
		_txtbx.click(id);
}


function focusInput(id)
{
	_txtbx.clean(id);
}


function blurInput(id)
{
	_txtbx.del(id);
	_txtbx.resetclick(id);
	_txtbx.button();
}


function getFontFamily()
{
	var	len, e = dE('dialogHelper');
	if (!e || !e.fonts || (len = e.fonts.count) < 2)
		return 0;
	var	i, n = 0;
	for (i = 1; i < len; i++)
		_oomoji.fList[n++] = e.fonts(i);
	return n;
}


function fontsListHTML()
{
	s = '<div class="komidashi">※大字臨のフォントを変更</div><select id="fontfamily" class="selector" onchange="_oomoji.oomoji(1)"><option value="">初期設定</option>';
	var	fnt = _fontCookie.cutCookie();
	if (fnt)
		_fontCookie.bakeCookie(fnt);
	for (var i in _oomoji.fList) {
		s += '<option value="' + _oomoji.fList[i] + '"';
		if (fnt && fnt == _oomoji.fList[i])
			s += ' selected="selected"';
		s += '>' + _oomoji.fList[i] + '</option>';
	}
	s += '</select>';
	return s;
}


function initIndex()
{
	var	e = dE('fontslist');
	if (getFontFamily())
		e.innerHTML = fontsListHTML();
	else
		e.innerHTML = '<div class="center"><img style="margin-top:10px;width:60px;height:30px" src="/img/shiori/mini/sprout.png" alt=""/></div>';
}


function windowOpen(q)
{
	if (!dE('blank').checked) {
		if (++_rand > 9999)
			_rand = 0;
	}
	window.open(q, 'win' + _rand);
}


function locateURL(id)
{
	var	s;
	if (!(s = _selbx.selected(id)))
		return;
	dE('favicon').src = '/img/shiori/mini/favicon/' + (_favicon[s].length ? _favicon[s] : 'glass.png');
	if (s == 'daijirin') {
		_oomoji.openpage();
		_oomoji.oomoji();
		return;
	}
	else if (_oomoji.pg) {
		_oomoji.closepage();
		return;
	}
	if (_delay.istimer)
		return;
	var	t = dE('titleInput').value;
	if (t == _txtbx.str['titleInput'])
		t = '';
	var	a = dE('authorInput').value;
	if (a == _txtbx.str['authorInput'])
		a = '';
	if (!t.length && !a.length)
		return;
	var	sflg = false;
	var	q = _path + '?v=!&x=' + encodeURIComponent(s);
	if (!_ebookoff && s == 'ebookoff') {
		q += '&ex=1';
		_ebookoff = true;
		sflg = 'eBOOKOFF';
	}
	if (t.length)
		q += '&t=' + encodeURIComponent(t);
	if (a.length)
		q += '&a=' + encodeURIComponent(a);
	_delay.settimer();
	windowOpen(q);
//	if (sflg)
//		alert("【本の枝折】" + sflg + "で検索するには、もう一度[SEARCH]ボタンを押してください。");
}


function searchWords()
{
	locateURL(_selbx.box);
}


function keyInput(id, ev)
{
	_txtbx.resetclick(id);
	if (ev.keyCode == 0x0d)
		searchWords();
	else
		_txtbx.button();
}


function clickHomeButton()
{
	var	f = false;
	if (_txtbx.del('titleInput', true))
		f = true;
	if (_txtbx.del('authorInput', true))
		f = true;
	if (f) {
		_txtbx.button();
		_oomoji.clear();
		return;
	}
	var	s = _selbx.selected();
	if (!s || s == 'daijirin')
		return;
	if (!_ebookoff && s == 'ebookoff')
		_ebookoff = true;
	_delay.settimer();
	windowOpen(_path + '?v=!&x=' + encodeURIComponent(s));
}
