// Constants
var totalJottingNum = 46;

// Numeric support functions
function getRandNum(n){
	return Math.floor(Math.random() *  n);
}

function getRandNumEx(n, m){
	if (n < m)
		return Math.floor(Math.random() * n);
	var result = Math.floor(Math.random() * n);
	while (n == m)
		result = Math.floor(Math.random() * n);
	return result;
}

// Environmental support functions
var ua = navigator.userAgent.toLowerCase();

function getOS(){
	if ((ua.indexOf("windows") != -1)||(ua.indexOf("win32") != -1))
	// Microsoft Windows
		return 1;
	if ((ua.indexOf("macintosh") != -1)||(ua.indexOf("mac os x") != -1))
	// Mac OS
		return 2;
	if (ua.indexOf("linux") != -1)
	// Linux-like
		return 3;
	if (ua.indexOf("adobeair") != -1)
	// Adobe Air
		return 4;
}

function getBrowser(){
	if (ua.indexOf("msie") != -1)
	// Windows Internet Explorer
		return 1;
	if (ua.indexOf("firefox") != -1)
	// Firefox
		return 2;
	if (ua.indexOf("safari") != -1)
	// Safari
		return 3;
	if (ua.indexOf("chrome") != -1)
	// Google Chrome
		return 3;
	if (ua.indexOf("opera") != -1)
	// Opera
		return 5;
	if (ua.indexOf("camino") != -1)
		return 6;
	if (ua.indexOf("gecko/") != -1)
		return 7;
   return 0;
}

browserType = getBrowser();
osType = getOS();

// Key handlers
eventHandlerHead = (browserType == 1) ?
	"document.onkeydown=function keyDown()" :
	// Internet Explorer support
	"document.onkeydown=function keyDown(e)";
	// non-Internet Explorer support

if (browserType == 1)
	// Internet Explorer support
	nonFuncKeys = "event.keyCode";
else if (browserType == 2)
	// Firefox support
	nonFuncKeys = "e.which";
else if ((browserType == 3) || (browserType == 4))
	// Safari and Chrome support
	nonFuncKeys = "e.keyCode";

funcKeys = (browserType == 1) ?
	"event.keyCode" :
	// Internet Explorer support
	"e.keyCode";
	// non-Internet Explorer support

function eventHandlerTemplate(keyType, value, code){
	return "if (" + keyType + " == " + value + ") " + code + " else ";
}

// Functional functions
function jottingOffset(n){
	return (n >= 46) ? 4 : 3;
}

function goRandJotting(){
	var jottingNum = getRandNum(totalJottingNum) + 1;
	window.location="./jottings/" + jottingNum + ".htm";
}

function goAnotherJotting(n){
	var jottingNum = getRandNumEx(totalJottingNum, n - 1) + 1;
	window.location="./" + jottingNum + ".htm";
}

function goLatestJotting(){
	window.location="./" + totalJottingNum + ".htm";
}

function generateJottingHeader(n){
	var prevJottingNum = n - 1;
	var succJottingNum = n + 1;
	
	document.write("<hr><font color=red>跳转</font>　");
	
	firstAndPrevCode = 
		(n == 1) ? 
		("首篇&nbsp;|&nbsp;前一篇&nbsp;|") :
		("<a id=gofirst href=./1.htm onclick=javascript:urchinTracker('Jottings/" + n + "/GoFirst')>首篇</a>&nbsp;|&nbsp;<a id=goprev href=./" + prevJottingNum + ".htm onclick=javascript:urchinTracker('Jottings/" + n + "/GoPrev')>前一篇</a>&nbsp;|");
	
	succAndLatestCode = 
		(n == totalJottingNum) ?
		("&nbsp;后一篇&nbsp;|&nbsp;最新一篇&nbsp;|") :
		("&nbsp;<a id=gosucc href=./" + succJottingNum + ".htm onclick=javascript:urchinTracker('Jottings/" + n + "/GoSucc')>后一篇</a>&nbsp;|&nbsp;<a id=golatest href=javascript:goLatestJotting() onclick=javascript:urchinTracker('Jottings/" + n + "/GoLatest')>最新一篇</a>&nbsp;|")
	
	document.write(firstAndPrevCode + succAndLatestCode + "&nbsp;<a id=gorandom href=javascript:goAnotherJotting(" + n + ") onclick=javascript:urchinTracker('Jottings/" + n + "/GoRandom')>另一篇</a><br><font color=red>操作</font>　<a href=http://jottings.gaobo.org/?p="+ (n + jottingOffset(n)) +"#respond target=_blank title=欢迎您就本文发表评论 onclick=javascript:urchinTracker('Jottings/" + n + "/PostComment')>评论</a>&nbsp;|&nbsp;<a href=print/" + n + ".htm target=blank onclick=javascript:urchinTracker('Jottings/" + n + "/Print')>打印</a>　&nbsp;|&nbsp;<a href=http://feed.gaobo.org target=_blank onclick=javascript:urchinTracker('Jottings/" + n + "/Subscription/ByRSS')>订阅</a>　&nbsp;|&nbsp;<a href=http://www.feedsky.com/msub_wr.html?burl=gaoboorg target=_blank onclick=javascript:urchinTracker('Jottings/" + n + "/Subscription/ByEmail')>邮件分发</a><hr>");
}

function generateJottingsKeyEventsHandler(n){
	var prevJottingNum = n - 1;
	var succJottingNum = n + 1;
	
	if (osType == 2){
	// Mac OS (has special design in keyboards, different from PC keyboards)
		goHomeKey = 46;
		postCommentKey = 45;
		goRandomKey = 32;
		goPrevKey = 37;
		goSuccKey = 39;
		goFirstKey = 38;
		goLatestKey = 40;
		
		printKey = 80;
		subscribeKey = 83;
	}
	else {
	// Microsoft Windows and Linux-like OSes, assume PC keyboards
		goHomeKey = 46;
		postCommentKey = 45;
		goRandomKey = 32;
		goPrevKey = 33;
		goSuccKey = 34;
		goFirstKey = 36;
		goLatestKey = 35;
		
		printKey = 80;
		subscribeKey = 83;
	}
	
	goHomeCode = "{ urchinTracker('Jottings/" + n + "/GoHome'); window.location = '../'; }";
	postCommentCode = "{ urchinTracker('Jottings/" + n + "/PostComment'); window.location = 'http://jottings.gaobo.org/?p="+ (n + jottingOffset(n)) +"#respond'; }";
	goRandomCode = "{ urchinTracker('Jottings/" + n + "/GoRandom'); goAnotherJotting(" + n + "); }";
	goFirstCode = "{ urchinTracker('Jottings/" + n + "/GoFirst'); window.location = './1.htm'; }";
	goLatestCode = "{ urchinTracker('Jottings/" + n + "/GoLatest'); goLatestJotting(); }";
	goPrevCode = "{ urchinTracker('Jottings/" + n + "/GoPrev'); window.location = './" + prevJottingNum + ".htm'; }";
	goSuccCode = "{ urchinTracker('Jottings/" + n + "/GoSucc'); window.location = './" + succJottingNum + ".htm'; }";
	
	printCode = "{ urchinTracker('Jottings/" + n + "/Print'); window.location = 'print/" + n + ".htm'; }";
	subscribeCode = "{ urchinTracker('Jottings/" + n + "/Subscription/ByRSS'); window.location = 'http://feed.gaobo.org'; }";
	
	document.write("<script type=text/javascript> " + eventHandlerHead + "{" 
		+ eventHandlerTemplate(funcKeys, goHomeKey, goHomeCode) 
		+ eventHandlerTemplate(funcKeys, postCommentKey, postCommentCode)
		+ eventHandlerTemplate(nonFuncKeys, goRandomKey, goRandomCode)
		+ eventHandlerTemplate(nonFuncKeys, printKey, printCode)
		+ eventHandlerTemplate(nonFuncKeys, subscribeKey, subscribeCode)
	);

	if (n != 1)
		document.write(eventHandlerTemplate(funcKeys, goFirstKey, goFirstCode) + eventHandlerTemplate(funcKeys, goPrevKey, goPrevCode));
	if (n !=totalJottingNum)
		document.write(eventHandlerTemplate(funcKeys, goLatestKey, goLatestCode) + eventHandlerTemplate(funcKeys, goSuccKey, goSuccCode));
	document.write("; } </script>");
}



function generateJottingFooter(n){
	document.write("<hr><div align=right>您正在阅读<a href=http://go.gaobo.org/fwlink/?LinkID=19 target=_blank onclick=javascript:urchinTracker('Jottings/" + n + "/Fullview') title=以博客形式查看所有文章>GAOBO.ORG随笔和评论</a>的<a href=http://jottings.gaobo.org/?p=" + (n + jottingOffset(n)) + " target=_blank title=以博客形式查看本文章>第" + n + "篇</a></span>&nbsp;|&nbsp;<a href=http://creativecommons.org/licenses/by-nc-nd/2.5/cn target=license onclick=javascript:urchinTracker('Jottings/" + n + "/Licensing')>许可</a>&nbsp;|&nbsp;<a href=../ onclick=javascript:urchinTracker('Jottings/" + n + "/GoHome')>首页</a></div>");
}

function generateHomeKeyEventsHandler(){
	if (osType == 2){
	// Mac OS (has special design in keyboards, different from PC keyboards)
		goJottingsKey = 32;
		goBibleKey = 49;
		goSpaceKey = 50;
		goPhotosKey = 51;
		goReadingsKey = 52;
		goBlogKey = 53;
		goAdminLoginKey = 54;
		goCheckInboxKey = 55;
		goCheckCalendarKey = 56;
		goCheckDocsKey = 57;
		goHostingServicesKey = 48;
	}
	else {
	// Microsoft Windows and Linux-like OSes, assume PC keyboards
		goJottingsKey = 32;
		goBibleKey = 49;
		goSpaceKey = 50;
		goPhotosKey = 51;
		goReadingsKey = 52;
		goBlogKey = 53;
		goAdminLoginKey = 54;
		goCheckInboxKey = 55;
		goCheckCalendarKey = 56;
		goCheckDocsKey = 57;
		goHostingServicesKey = 48;
	}
	
	goJottingsCode = "{ urchinTracker('Home/Jottings'); goRandJotting(); }";
	goBibleCode = "{ urchinTracker('Home/Bible'); window.location = 'http://go.gaobo.org/fwlink/?LinkID=1'; }";
	goSpaceCode = "{ urchinTracker('Home/Space'); window.location = 'http://go.gaobo.org/fwlink/?LinkID=2'; }";
	goPhotosCode = "{ urchinTracker('Home/Photos'); window.location = 'http://go.gaobo.org/fwlink/?LinkID=3'; }";
	goReadingsCode = "{ urchinTracker('Home/Douban'); window.location = 'http://go.gaobo.org/fwlink/?LinkID=4'; }";
	
	goBlogCode = "{ urchinTracker('Home/Blog'); window.location = 'http://go.gaobo.org/fwlink/?LinkID=5'; }";
	goAdminLoginCode = "{ urchinTracker('Home/AdminLogin'); window.location = 'http://go.gaobo.org/fwlink/?LinkID=6'; }";
	goCheckInboxCode = "{ urchinTracker('Home/CheckInbox'); window.location = 'http://go.gaobo.org/fwlink/?LinkID=7'; }";
	goCheckCalendarCode = "{ urchinTracker('Home/CheckCalendar'); window.location = 'http://go.gaobo.org/fwlink/?LinkID=8'; }";
	goCheckDocsCode = "{ urchinTracker('Home/CheckDocs'); window.location = 'http://go.gaobo.org/fwlink/?LinkID=9'; }";
	goHostingServicesCode = "{ urchinTracker('Home/HostingServices'); window.location = 'http://go.gaobo.org/fwlink/?LinkID=10'; }";

	
	document.write("<script type=text/javascript> " + eventHandlerHead + "{" 
		+ eventHandlerTemplate(nonFuncKeys, goJottingsKey, goJottingsCode)
		+ eventHandlerTemplate(nonFuncKeys, goBibleKey, goBibleCode)
		+ eventHandlerTemplate(nonFuncKeys, goSpaceKey, goSpaceCode)
		+ eventHandlerTemplate(nonFuncKeys, goPhotosKey, goPhotosCode)
		+ eventHandlerTemplate(nonFuncKeys, goReadingsKey, goReadingsCode)
		+ eventHandlerTemplate(nonFuncKeys, goBlogKey, goBlogCode)
		+ eventHandlerTemplate(nonFuncKeys, goAdminLoginKey, goAdminLoginCode)
		+ eventHandlerTemplate(nonFuncKeys, goCheckInboxKey, goCheckInboxCode)
		+ eventHandlerTemplate(nonFuncKeys, goCheckCalendarKey, goCheckCalendarCode)
		+ eventHandlerTemplate(nonFuncKeys, goCheckDocsKey, goCheckDocsCode)
		+ eventHandlerTemplate(nonFuncKeys, goHostingServicesKey, goHostingServicesCode)
		+ "; } </script>"
	);
}