var theme_c = '運動學'
var theme_e = 'Kinematics'

var pages = new Array(
'trans01',   //1
'trans02',
'trans03',
'trans04',
'dista01',   //5
'dista02',
'speed01', //7
'speed02',
'accel01',  //9
'accel02',
'mo_gr01',  //11
'mo_gr02',
'mo_gr03',
'eq_mo01',  //14
'eq_mo02',
'eq_mo03',
'eq_mo04',
'eq_mo05'
)

var sections = new Array(
'kin',
'for',
'mom',
'ene'
)

var subSections = new Array(
'4 trans01 trans02 trans03 trans04',
'2 dista01 dista02',
'2 speed01 speed02',
'2 accel01 accel02',
'3 mo_gr01 mo_gr02 mo_gr03',
'5 eq_mo01 eq_mo02 eq_mo03 eq_mo04 eq_mo05'
)

var subThemes_c = new Array(
'引言',
'距離與位移',
'速率與速度',
'加速',
'運動的圖示',
'運動方程'
)

var subThemes_e = new Array(
'Introduction',
'Distance and displacement',
'Speed and velocity',
'Acceleration',
'Motion graphs',
'Equations of motion'
)

var currPage
var currSection      //current section
var fullCurrPage = document.location.toString()
var lang = (fullCurrPage.indexOf('_e.html') == -1)? 0 : 1
var fileExt = (lang == 0)? '_c.html' : '_e.html'
var imageExt = (lang == 0)? '_c.jpg' : '_e.jpg'
var currPageNo = findCurrPageNo()
var currSectionNo = findCurrSection()

function findCurrPageNo() {
	var bi = fullCurrPage.lastIndexOf('/')
	var ei = fullCurrPage.indexOf(fileExt)
	currPage = fullCurrPage.substring(bi+1, ei)
	for (var i = 0; i < pages.length; i++) {
		if (currPage == pages[i]) break
	}
	return i
}

function findCurrSection() {
	var ci = fullCurrPage.lastIndexOf('/')
	currSection= fullCurrPage.substring(ci-3, ci)   //example: abo folder
	for (var j = 0; j < sections.length; j++) {
		if (currSection == sections[j]) break
	}
	return j
}

function goFirstPage() {
	document.location = pages[0] + fileExt
}

function goLastPage() {
	document.location = pages[pages.length-1] + fileExt
}

function goPrevPage() {
	var prevPageNo = currPageNo - 1
	if (prevPageNo < 0) prevPageNo = pages.length - 1
	document.location =  pages[prevPageNo] + fileExt
}

function goNextPage() {
	var nextPageNo = currPageNo + 1
	if (nextPageNo > pages.length - 1) nextPageNo = 0
	document.location = pages[nextPageNo] + fileExt
}

function goUpPage() {
	document.location = '../index' + fileExt
}

function writePageLink() {
	var subTheme, linkName
	document.writeln('<SPAN CLASS="DES">')
	for (var i = 0; i < subSections.length; i++) {
		if (i != 0) document.writeln('<B>&nbsp;|&nbsp;</B>')
		var str = pages[currPageNo]
		 if (subSections[i].match(str)) {
		 		subTheme = (lang == 0) ? subThemes_c[i] : subThemes_e[i]
		 		document.writeln('<SPAN CLASS="COLORHEAD">' + subTheme  + '</SPAN>' )
		 		var sno = subSections[i].substr(0, 1)
		 		var no = parseFloat(sno) + 1
		 		 for ( k = 1; k < no; k++) {
		 		   var b = k.toString()
		 		   if (str.match(b)) {
		 		   document.writeln('<SPAN CLASS="COLORHEAD">[' + b  + ']</SPAN>' )
		 		   } else {
		 		      linkName = subSections[i].substr(2, 6) + k + fileExt
		 		      document.writeln('<A HREF="' + linkName + '">' + k + '</A>')
		 		      }}
		 } else {		
		 		for (var j=0; j < pages.length; j++) {
					var str2 = pages[j]
					var str3 = subSections[i].substr(2, 7)
					if (str2.match(str3)) {
						linkName = pages[j] + fileExt
						subTheme = (lang == 0) ? subThemes_c[i] : subThemes_e[i]
						document.writeln('<A HREF="' + linkName + '">' + subTheme + '</A>')
					} 
				}	   		   
			    }
	}
		
	
	document.writeln('</SPAN>')
}

function writeSectionLink() {    //display section images and highlight present section
	var linkName
	var mainImage = '../images/mechanics' + imageExt
	document.writeln('<SPAN><IMG SRC="'+ mainImage +'">')
	for (var i = 0; i < sections.length; i++) {
		if (i != 0) document.writeln('&nbsp;')
		if (currSectionNo == i) {
			var imageLoc = '../images/' + sections[i] + '2' + imageExt
			document.writeln('<IMG SRC="' + imageLoc + '" BORDER="0">')
		} //else {
			//var imageLink = '../images/' + sections[i] + imageExt
			//linkName = '../' + sections[i] + '/' + sections[i] + '1' + fileExt
			//document.writeln('<A HREF="' + linkName + '"><IMG SRC="' + imageLink + '" BORDER="0"></A>')
		//}
	}
	document.writeln('</SPAN>')
}

function writeTitle() {
	document.write(document.title)
}

var mediaWindow

function openMediaWindow(fileName, w, h) {
	closeMediaWindow()
	w += 20
	h += 80
	var dim = 'HEIGHT=' + h + ',WIDTH=' + w
	mediaWindow = window.open(fileName, 'mediaWin', dim)
}

function closeMediaWindow() {
	if (mediaWindow && !mediaWindow.closed) mediaWindow.close()
}
