function gi(id) { return document.getElementById(id); }
function getUserAgent() { return window.navigator.userAgent; }
function getHref() { return document.location.href; }

var pageTracker;

function isReady(version) {
	document.title += " Wisebook " + version.replace(/wisebook\s+/i, "");
	interval = setInterval("checkHash()", 200);
	return true;
}

function setTitle(b, t, g) {
	document.title = t;
	try { if (!pageTracker) { pageTracker = _gat._getTracker(g); pageTracker._trackPageview(); } } catch (e) {}
	return true;
}

function setPage(b, t, p) {
	clearInterval(interval);
	page = p.replace(/ /g, "%20");
	document.location.hash = "#" + page;
	document.title = t + " #" + p;
	book = b;
	interval = setInterval("checkHash()", 200);
	try { if (pageTracker) { pageTracker._trackPageview((l=document.location).pathname+l.search+l.hash); } } catch (e) {}
	return true;
}

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

function bpPreview(src) {
	window.open(src + "/html/", "wisebook_blog", "width=180,height=390");
}

function gotoBookmark(n) {
	var s = document.location.href;
	var t = encodeURIComponent(document.title);
	switch (n) {
	case "hatena": s = "http://b.hatena.ne.jp/entry/" + s; break;
	case "buzzurl": s = "http://buzzurl.jp/entry/" + s; break;
	case "delicious": s = "http://delicious.com/save?v=5&noui&jump=close&url=" + encodeURIComponent(s) +"&title=" + t; break;
	case "livedoor": s = "http://clip.livedoor.com/redirect?title=" + t + "&ie=utf8&link=" + s; break;
	case "nifty": s = "http://clip.nifty.com/create?url=" + encodeURIComponent(s); break;
	case "yahoo": s = "http://bookmarks.yahoo.co.jp/bookmarklet/showpopup?t=" + t + "&u=" + encodeURIComponent(s) + "&ei=UTF-8"; break;
	case "fc2": s = "http://bookmark.fc2.com/user/post?url=" + encodeURIComponent(s) + "&title=" + t; break;
	case "google": s = "http://www.google.com/bookmarks/mark?op=edit&bkmk=" + encodeURIComponent(s) + "&title=" + t; break;
	}

	window.open(s, "wisebook_bookmark_" + n);
}

function gotoURL(url, mode) {
	var win;
	if (url.search(/^(\/|\.|http|mailto|data)/i) < 0) url = "http://" + url;
	if (url.search(/^mailto/i) >= 0) {
		document.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 (document.location.host == "") {
		body = body.replace("\r", "%0D%0A");
		document.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="' + document.location.href.substr(0, document.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 = document.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 hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
		var flashVars = new Array();
		flashVars.push('book=' + book);
		flashVars.push('page=' + page);
		flashVars.push('data=' + data);
		flashVars.push('keyword=' + keyword);
		flashVars.push('method=' + method);

		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
			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',           'high',
				'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
		}
	}
}

function protect() {
	if (AC_FL_RunContent == 0 || DetectFlashVer == 0) {
		//alert("This page requires AC_RunActiveContent.js.");
	} else if (DetectFlashVer(10, 0, 0)) {  // if we've detected an acceptable version
		var protector = document.createElement("DIV");
		//protector.style.display = "none";
		protector.innerHTML = AC_FL_RunContent(
				'return',			'1',
				'codebase',          '//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0',
				'width',             '0',
				'height',            '0',
				'src',               'lib/protector',
				'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',             'lib/protector',
				'salign',            'lt'
			); //end AC code
		document.body.appendChild(protector);
	}
}

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

// /v2/[userid]/?[bookid]&k=[keyword]&m=[and|or]#[pagelabel]
var k = href.split(/\?|\&|\#/g);
if (k.length > 1) {
	href = k[0], book = k[1];
	if (k[2] && k[2].search(/k\=/i) >= 0) keyword = k[2].substr(2), method = k.length > 3 ? k[3].substr(2) : "or";
}

page = document.location.hash.replace("#","");
var publisher = href.split("/");
publisher = publisher[publisher.length - 2];

document.write('<iframe src="./lib/blank.html" 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>');

