function gi(id) { return document.getElementById(id); }

function isReady(version) {
	document.title = document.title + " Wisebook " + version;
	interval = setInterval("checkHash()", 200);
	return true;
}

function setTitle(b, t) {
	document.title = t;
	book = b;
}

function setPage(b, t, p) {
	page = p.replace(/ /g, "%20");
	location.hash = "#" + page;
	document.title = t + " #" + page;
	book = b;
	try { if (pageTracker) {
		pageTracker._trackPageview(userid + "/book=" + bookid + "/page=" + page);
	} } catch (e) {}
	return true;
}

function gotoBook(b, p) {
	document.body.focus();
	location.href = href + "?" + b + (p != "" ? "#" + p : "");
}

function gotoURL(url, mode) {
	var win;
	if (url.search(/^(\/|\.|http|mailto|data)/i) < 0) url = "http://" + url;
	if (url.search(/^mailto/i) >= 0) {
		location.href = url;
		return;
	}
	else if (mode.length > 3 && parseInt(mode[2], 10) > 0 && parseInt(mode[3], 10) > 0) {
		win = window.open(url, "wisebook_popup", "width=" + mode[2] + ",height=" + mode[3] + ",resizable=yes,scrollbars=yes");
		//win = window.open(url, mode[0].replace("/", ""), "width=" + mode[1] + ",height=" + mode[2] + ",resizable=yes,scrollbars=yes");
	}
	else {
		win = window.open(url, "wisebook_popup");
		//win = window.open(url, mode.length > 0 ? mode[0] : "wisebook_popup");
	}
	if (!win) alert(wbMsg.gotoURL + url);
	else win.focus();
}

function openCabinet() {
	if (opener) {
		opener.window.focus();
		opener.document.location.href = "/library/cabinet/" + book + "/" + page + "/";
		return "opener";
	}
	else {
		window.open("/library/cabinet/" + book + "/" + page + "/");
		return "newwin";
	}
}

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:" + tomail + "?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="userid" value="' + userid + '">' +
			'<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 {
		var hash = location.hash;
		if (hash.charAt(0) == "#") hash = hash.substr(1);
		if (window.navigator.userAgent.search(/safari/i) >= 0) hash = decodeURI(hash);
		if (page != "" && page != hash) {
			page = hash;
			var w = gi("wisebook");
			if (w != null) {
				w.gotoPage(book, page);
			}
			else {
				document.getElementsByName("wisebook")[0].gotoPage(book, page);
			}
		}
	}
	catch (e) {
		clearInterval(interval);
		alert("ERROR: " + e);
	}
}

function unload()
{
	try {
		if (opener && opener.plugin == true) opener.location.reload();
	}
	catch (e) {}
}

function wisebookFlash(p, o) {
	if (AC_FL_RunContent == 0 || DetectFlashVer == 0) {
		//alert("This page requires AC_RunActiveContent.js.");
	} else {
		var hasProductInstall = DetectFlashVer(6, 0, 65);
		var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
		var flashVars = new Array();
		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 (hasProductInstall && !hasRightVersion) {
			var MMPlayerType = isIE ? "ActiveX" : "PlugIn";
			var MMredirectURL = window.location;
			var MMdoctitle = document.title;

			document.body.removeChild(gi("nonflash"));
			AC_FL_RunContent(
				"src", "http://office.wisebook.jp/v2/oldpiano/playerProductInstall",
				"FlashVars", "MMredirectURL="+MMredirectURL+'&MMplayerType='+MMPlayerType+'&MMdoctitle='+MMdoctitle+"",
				"width", "100%",
				"height", "100%",
				"id", "TVC",
				"quality", "high",
				"bgcolor", "#FFFFFF",
				"name", "pluginInstall",
				"type", "application/x-shockwave-flash",
				"pluginspage", "https://www.adobe.com/go/getflashplayer"
			);
		}
		else
		*/
		if (hasRightVersion) {  // if we've detected an acceptable version
			document.body.removeChild(gi("nonflash"));
			// 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', 'always',
				'allowFullScreen',   'true',
				'movie',             'wisebook',
				'salign',            'lt',
				'flashvars',         flashVars.join('&')
				); //end AC code
		} else if (GetSwfVer() != -1) {  // flash is too old or we can't detect the plugin
			gi("current_flash_version_msg").style.display = "";
			gi("current_flash_version").innerHTML = GetSwfVer();
			gi("require_flash_version").innerHTML = requiredMajorVersion + "." + requiredMinorVersion + "." + requiredRevision;
			//document.write(wbMsg.alternateContent);  // insert non-flash content
		}
	}
}

var interval, book = page = plugins = opened = publisher = "", serial = 1;
var href = location.href.replace(location.hash, "");

var k = href.split(/\?|\#/g);
if (k.length > 1) href = k[0], book = k[1];

page = location.hash;
if (page.charAt(0) == "#") page = page.substr(1);

var publisher = href.split("/");
publisher = publisher[publisher.length - 2];

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>');

