function gi(id) { return document.getElementById(id); }
function isReady() { return true; }

function setTitle(b, t) {
	document.title = t;
	interval = setInterval("checkHash()", 500);
}

function setPage(b, t, p) {
	document.title = t;
	location.hash = page = p;
	return true;
}

function gotoBook(b, p) {
	document.body.focus();
	location.href = href + "?" + b + (p != "" ? "#" + p : "");
}

function gotoURL(url, mode) {
	var win;
	if (mode.length > 2 && parseInt(mode[1], 10) > 0 && parseInt(mode[2], 10) > 0)
		win = window.open(url, mode[0], "width=" + mode[1] + ",height=" + mode[2] + ",resizable=yes,scrollbars=yes");
	else win = window.open(url);
	if (!win && confirm(wbMsg.gotoURL + url)) location.href = url;
}

function sendmail(tomail, fromname, frommail, body, p, title) {
	if (tomail == "" || fromname == "" || frommail == "" || body == "") {
		alert(wbMsg.sendmail.error);
	}
	else if (location.host == "") {
		body = body.replace("\r", "%0D%0A");
		location.href = "mailto:" + sender + "?subject=" + title + "&body=" + body;
	}
	else {
		gi("wbfi").innerHTML =
			'<input type="hidden" name="frommail" value="' + frommail + '">' +
			'<input type="hidden" name="tomail" value="' + tomail + '">' +
			'<input type="hidden" name="fromname" value="' + fromname + '">' +
			'<input type="hidden" name="url" value="' + location.href.substr(0, location.href.indexOf("#")) + '">' +
			'<input type="hidden" name="book" value="' + book + '">' +
			'<input type="hidden" name="page" value="' + p + '">' +
			'<input type="hidden" name="title" value="' + title.replace('"', '&qout;') + '">' +
			'<input type="hidden" name="body" value="' + body.replace('"', '&qout;') + '">' +
			'<input type="hidden" name="send" value="ok">';
		f = document.forms["wbfo"];
		f.action = "/library/sendmail/";
		f.method = "POST";
		f.submit();
	}
}

function sendmail_done(msg) {
	if (msg == "ok") {
		alert(wbMsg.sendmail.done);
	}
	else {
		alert(wbMsg.sendmail.fail);
	}
}

function exit() {
	if (opener) {
		window.close();
	}
	else {
		window.open('about:blank', '_self').close();
	}
}

function checkHash() {
	try {
		if (page != location.hash.substr(1).replace("/", "")) {
			page = location.hash.substr(1).replace("/", "");
			gi("wisebook").gotoPage(book, page);
		}
	}
	catch (e) {
		clearInterval(interval);
		//alert("ERROR");
	}
}

function wisebookFlash(p, o) {
	if (AC_FL_RunContent == 0 || DetectFlashVer == 0) {
		//alert("This page requires AC_RunActiveContent.js.");
	} else {
		var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
		var flashVars = new Array();
		flashVars.push('lang=ja');
		flashVars.push('book=' + book);
		flashVars.push('page=' + page);
		flashVars.push('data=' + data);

		if (p != "" && p != undefined && p != "undefined") {
			flashVars.push('plugin=' + p + ";");
			if (o != "") flashVars.push('popened=' + o);
		}

		if(hasRightVersion) {  // if we've detected an acceptable version
			// embed the flash movie
			AC_FL_RunContent(
				'codebase',          '//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0',
				'width',             '100%',
				'height',            '100%',
				'src',               'wisebook',
				'quality',           'best',
				'pluginspage',       'http://www.macromedia.com/go/getflashplayer',
				'align',             'middle',
				'play',              'true',
				'loop',              'false',
				'scale',             'noscale',
				'wmode',             'window',
				'devicefont',        'true',
				'id',                'wisebook',
				'bgcolor',           '#FFFFFF',
				'name',              'wisebook',
				'menu',              'true',
				'allowScriptAccess', 'sameDomain',
				'allowFullScreen',   'true',
				'movie',             'wisebook',
				'salign',            'lt',
				'flashvars',         flashVars.join('&')
				); //end AC code
		} else {  // flash is too old or we can't detect the plugin
			document.write(wbMsg.alternateContent);  // insert non-flash content
		}
	}
}

var version = " Wisebook v2.1.58";
document.title += version;

var interval, book = page = plugins = opened = publisher = "", serial = 1;
var href = location.href.replace(location.hash, "");

var k = href.split("?");
if (k.length > 1) href = k[0], book = k[1].replace("/", "");
page = location.hash.substr(1).replace("/", "");

var publisher = href.split("/");
publisher = publisher[publisher.length - 2];

if (book == "" || book == "undefined") {
	var pathBooks = "data/books.xml";
	if (document.all)
	{
		var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
		xmlDoc.async = "false";
		xmlDoc.validateOnParse = "true";
		xmlDoc.load(pathBooks);
		var root = this.xmlDoc.documentElement;
	}
	else
	{
		var ffDoc = new XMLHttpRequest();
		ffDoc.open("GET", pathBooks, false);
		ffDoc.send(null);
		var root = ffDoc.responseXML.documentElement;
	}

	book = root.getElementsByTagName("books")[0].getAttribute("read");
	if (book == null || book == "undefined") book = root.getElementsByTagName("book")[0].getAttribute("id");
}

document.write('<iframe id="wbfr" name="wbfr" width="0" height="0" style="display:none"></iframe>');
document.write('<form name="wbfo" target="wbfr" style="display:none"><div id="wbfi"></div></form>');

