
// 頁面檢查
function ifm_F_check() {
	try	{
		Current_.path=ifm_F.location.href.replace(/(\?|#).+$/, "");
		Current_.title=ifm_F.document.title;
		Current_.hash=ifm_F.location.hash?ifm_F.location.hash.replace(/#/, ""):null;
		if(Current_.title=="404 Not Found") return false;
	} catch(e) { Current_.path=Current_.title=Current_.hash=null; return false;}
	return true;
}

// 顯示錯誤訊息
function ifm_F_showErr(title, content) {
	document.title=Website_.title;
	document.getElementById("div_Trace").innerHTML=title;
	document.getElementById("div_Main").innerHTML=content;
}

// 載入頁面時的處理機制
function ifm_F_onload() {
	var t, f, i, o, th, p, transList, transErr;
	if(!document.getElementById("div_Main")) { setTimeout("ifm_F_onload();", 100); return;}
	if(!TransFunctions_.ready) {
		document.getElementById("div_Main").innerHTML=
			"網站系統載入中，請稍待……<br>（若很久沒有動作，請重新整理）";
		setTimeout("ifm_F_onload();", 300);
		return;
	}

	// 錯誤處理
	if(!ifm_F_check()) {
		ifm_F_showErr("【頁面載入錯誤】",
			"頁面讀取的過程中發生錯誤，這可能是因為您參照了一個不存在的頁面、"+
			"沒有存取該頁面的權限、或是單純的網路阻塞。<br>重新整理頁面可能可以解決問題。<br>"+
			"若有需要，您可以聯絡<a href='mailto:don.tsai@ms5.url.com.tw'>站長</a>以進一步解決問題。");
		return;
	}

	// 標示載入狀態，見註一
	Current_.loading=true;
	
	// 外部匯入頁面內容並執行內容轉換
	t=TransFunctions_.locationTrans(ifm_F); transErr=null;
	if(IS_.getMETA("transList", ifm_F)!=null) {
		transList=IS_.getMETA("transList", ifm_F).split(",");
		for(f in transList) {
			transList[f]=transList[f].replace(/^\s+|\s+$/g, "");
			if(transList[f]) try { t=eval(transList[f])(t);} catch(e) { transErr=transList[f];}
		}
	} else
		for(f in TransFunctions_.list)
			try { t=TransFunctions_.list[f](t);}
			catch(e) { transErr=TransFunctions_.list[f].toString().match(/function (\w+)/)[1];}
	if(transErr) {
		ifm_F_showErr("【頁面轉換錯誤】",
			"頁面內容在套用轉換程式「"+transErr+"」的過程中發生錯誤。<br>"+
			"這可能是因為頁面的內容無法套用該轉換程式，或是該轉換程式並未被載入。");
		return;
	}

	// 搜尋轉換，見註二
	var k=IS_.getSearch("kwd", ifm_F);
	if(k) {
		k=unescape(unescape(k)); i=0;
		t=t.replace(RegExp("<script(.|\\s)+?<\\/script>|<[^>]+>|("+k.replace(/([.\+*?^$(){}=!<>|:]|\[|\])/g, "\\$1")+")", "ig"),
			function($0, $1, $2) {
				if(!$2) return $0; i++;
				return t="<span "+(i>1?"":"id='IS_KWD' ")+"style='color:white; background:navy;'>"+$2+"</span>";
			}
		);
		document.getElementById("div_Search").innerText="在此頁中關鍵字「"+k+"」共出現 "+i+" 次";
	} else document.getElementById("div_Search").innerText="";

	document.getElementById("div_Main").innerHTML=t;

	// 處理標題
	if(ifm_F.document.title=="首頁") document.title=Website_.title;
	else document.title=Website_.title+" - "+Current_.title;

	// 處理足跡
	f=Website_.sitemap.documentElement.getElementsByTagName("item"); o=null;
	for(i=0;i<f.length;i++) if(f[i].getAttribute("href")==IS_.getPureHref(ifm_F)) o=f[i];
	if(o==null)
		document.getElementById("div_Trace").innerText="【系統頁面 >> "+ifm_F.document.title+"】";
	else {
		th="";
		if(o.previousSibling) {
			p=o.previousSibling; while(p!=null&&p.nodeType!=1) p=p.previousSibling;
			if(p!=null) with(p) th+="▲【<a "+(getAttribute("href")?"href='"+
				getAttribute("href")+"'":"")+">"+getAttribute("name")+"</a>】";
		}
		if(o.nextSibling) {
			p=o.nextSibling; while(p!=null&&p.nodeType!=1) p=p.nextSibling;
			if(p!=null) with(p) th+=" ▼【<a "+(getAttribute("href")?"href='"+
				getAttribute("href")+"'":"")+">"+getAttribute("name")+"</a>】";
		}
		t=o.getAttribute("name");
		while(o.parentNode.nodeName=="item") {
			o=o.parentNode; t="<a href='"+o.getAttribute("href")+"'>"+o.getAttribute("name")+"</a> >> "+t;
		}
		document.getElementById("div_Trace").innerHTML=
			"<table width='100%' cellSpacing=0 cellPadding=0><tr><td>【"+t+"】<td align='right'>"+th;
	}

	
	// 處理雜湊
	if(Current_.hash)
		try { document.getElementById(Current_.hash).scrollIntoView();}
		catch(e) { Current_.hash=null;}

	// 顯示搜尋
	if(k) {
		document.getElementById("IS_KWD").scrollIntoView();
		document.getElementById("div_Main").scrollTop-=20;
	}

	// 進行捲軸同步，見註三
	if(!Current_.hash&&!k) IS_.synchroScroll(document.getElementById("div_Main"), ifm_F.document.body);
	else IS_.synchroScroll(ifm_F.document.body, document.getElementById("div_Main"));
	Current_.loading=false;
}

// 更新隱藏框架的捲軸狀態
function div_Main_onScroll() { if(!Current_.loading) IS_.synchroScroll(ifm_F.document.body, document.getElementById("div_Main"));}



/*********************************************

註一：
這個標籤的作用是要阻止載入的時候觸發不正確的逆向捲軸同步，
該情況在 Chrome 等瀏覽器當中、於寫入的新內容少於舊內容的時候，
會因為觸發 div_Main 的 onScroll 事件而發生。
IE 當中則因為處理流程的不同，不會發生這種狀況。

註二：
之所以要進行兩層加密是因為 Lunarpages 主機不知為何不支援 %u.... 的語法，
因此利用兩層加密使得網址呈現出可以被 Lunarpages 主機接受的形式。

註三：
這個機制的目的在於讓訪客在往返瀏覽歷史或重新整理當前頁面時，
頁面捲動的位置能夠如同傳統的框架一般地被保留。
為了達到這個目的，隱藏框架不能真的被隱藏，只能設法使其視覺上不可見；
但這點不能僅是透過更改 z-index 來達成，因為某些在作業系統當中，
iframe 的捲軸是沒有辦法被遮蓋的；所以必須要利用設定負座標的方式，
將它完全移出視覺範圍內。
此外，隱藏框架的 scrollHeight 一定要比 div_Main 要高，
這點可以透過許多方面來予以保證。

*********************************************/