function getPageName () {
	var path = window.location.toString();
	var filename = path.split ('/').pop(); // 取得檔案名稱

	if(filename.indexOf('-detail') != -1){ // detail頁面之左選單與麵包屑等同於其父頁
		filename = filename.replace (/-detail/, '');
		filename = filename.replace (/\?.*/, '');
	}
	if(filename.indexOf('?year=') != -1 || filename.indexOf('?keyword=') != -1){ // 例外情況 
		filename = filename.replace (/\?.*/, '');
	}
	if(filename.indexOf('#') != -1){
	var pageName = filename.split ('#');
	pageName.pop (); //去掉副檔名
	return pageName[0];
	} else {
		return filename;
	}
}
function getLocator (json, pageName) {
	var pd = '';
	if(Boolean(json.sub)){
		arguments.callee (json.sub.item, pageName);
	}else{
		if (Boolean(json.length)){
			for(var mc in json){
				pd += Boolean(json[mc].sub) ? arguments.callee (json[mc].sub.item, pageName) : '';
				json[mc].fileName+"."+json[mc].extension == pageName ? pd = json[mc].locator : null;
			}
		}else{
				json.fileName+"."+json.extension == pageName ? pd = json.locator : null;
		}
	}
	return pd;
}
function createLeftmenu (json, menuDepth, levelNo, idNo) {
	var str = '<ul class=\"level' + levelNo + '\" id=\"leftmenuID' + idNo + 'UL\">';
	if(levelNo==1){
		json = json.item[Number(menuDepth[0]) - 2]; // 導入 json 第一層節點
	}
	if (Boolean(json.sub)) {
		str += '<li>' + createHyperlink(json, levelNo, levelNo + idNo);
		str += arguments.callee(json.sub.item, menuDepth, levelNo + 1, idNo) + '</li>';
	} else {
		if (Boolean(json.length)) {
			for (var mc in json) {
				if(json[mc].locator=='3.4.1'){
					continue;
				}
				str += '<li>' + createHyperlink(json[mc], levelNo, idNo + mc);
				str += Boolean(json[mc].sub) ? arguments.callee(json[mc].sub.item, menuDepth, levelNo + 1, idNo + mc) + '</li>' : '</li>';
			}
		} else {
			str += '<li>' + createHyperlink(json, levelNo, idNo + 0) + '</li>';
		}
	}
	str+='</ul>';
	return str;
}
function createHyperlink (jsonNode, levelNo, idNo) {
	var tagStr = '<a href=\"' + jsonNode.fileName + '.'+ jsonNode.extension + '\" id=\"leftmenuID' + idNo + '\">' + jsonNode.text[0] + '</a>';
	if(levelNo==1){
		tagStr = '<span>' + jsonNode.text[0] + '</span>';
	}
	return tagStr;
}
function setLeftmenuFocus (menuDepth) {
	for(var i = 1, idNo = ''; i < menuDepth.length; i++){
		idNo += menuDepth[i] - 1;
		if($('#leftmenuID' + idNo + 'UL')){
			$('#leftmenuID' + idNo + 'UL' + ' a').addClass('focusgroup');
		}
		$('#leftmenuID' + idNo).removeClass("focusgroup");
		$('#leftmenuID' + idNo).addClass ('focus');
	}
}
function createLocator (json, menuDepth) {
	var depth = Number (menuDepth.shift()) - 2;
	for(var str='';menuDepth.length;depth = Number(menuDepth.shift ()) - 1){
		str += '<a href=' + json.item[depth].fileName + '.'+ json.item[depth].extension + '>' + json.item[depth].text[0] + '</a> &gt; ';
		json = json.item[depth].sub;
	}
	var currentPage = (Boolean(json.item.length) ? json.item[depth].text[0] : json.item.text[0])
	str += '<span>' + currentPage + '</span>';
	document.title += ' | ' + currentPage;
	return str;
}
function createFloatmenu (json) {
	var floatDiv = '', node1, node2;
	for(var i in json.item) {
		floatDiv += '<div class="floatmenu floatmenu0' + (i-0+1) + '" id="topNav' + (i-0+1) + 'floatmenu">';
		node1 = json.item[i];
		if(Boolean(node1.sub)){
			floatDiv += '<ul>';
			if(Boolean(node1.sub.item.length > 1)) {
				for(var j in node1.sub.item){
					node2 = node1.sub.item[j];
					if(node2.locator=='3.4.1'){
						continue;
					}
					floatDiv += '<li><a href=\"' + node2.fileName + '.'+ node2.extension + '\">' + node2.text[0] + '</a></li>';
				}
			}else{
				node2 = node1.sub.item;
				floatDiv += '<li><a href=\"' + node2.fileName + '.'+ node2.extension + '\">' + node2.text[0] + '</a></li>';
			}
			floatDiv += '</ul>';
		}
		floatDiv += '</div>';
	}
	return (floatDiv);
}
function topNavInit (navCount) {
	var floatmenuInitX = [0, 121, 235, 368, 0, 483, 655, 794];
	for(var i=1; i<navCount+1; i++) {
		var nav = document.getElementById ('topNav' + i);
		if(!nav){
			continue;
		}
		var floatmenu = document.getElementById ('topNav' + i + 'floatmenu');
		floatmenu.initX = floatmenuInitX[i-1];
		nav.onmouseover = function () {document.getElementById (this.id + 'floatmenu').onmouseover();}
		nav.onmouseout = function () {document.getElementById (this.id + 'floatmenu').onmouseout();}
		floatmenu.onmouseover = function () {
			var bodyW = document.body.clientWidth;
			if (bodyW%2 == 1 && bodyW > 958) {
				bodyW -= 1;
			}
			if (bodyW > 958) {
				this.style.left = Math.floor(bodyW - 901) / 2 + this.initX - 1 + 'px'; // firefox3, IE6測試OK
			}else{
				this.style.left = this.initX + window.fixBrowserPos + 'px';
			}
			this.style.display = 'block';
		};
		floatmenu.onmouseout = function () {this.style.display = 'none';};
	}
}
function expandDocBackground () {
	var doc = document.getElementById ('docArea');
	var htmlObj = document.getElementsByTagName ('html');
	if(doc.clientHeight < htmlObj[0].clientHeight){
		doc.style.height = htmlObj[0].clientHeight + 'px';
	}
}
jQuery.each(jQuery.browser, function(i) {
	if($.browser.msie){
		window.fixBrowserPos = 27;
	}else{
		window.fixBrowserPos = 28;
	}
});
$.get('leftmenu_xml.php', function(xml){
	var json = $.xml2json(xml);

var pageName = window.getPageName();
	var locator = window.getLocator (json.item, pageName);

	// 產生麵包屑
	var locatorHTML = window.createLocator(json, locator.split('.'));
	$('.locator').append('<a href="index.php">Home</a> &gt; ' + locatorHTML);

	// 產生leftmenu
	var leftmenuHTML = window.createLeftmenu (json, locator.split('.'), 1, '');
	$('.subLeftBar').append(leftmenuHTML);

	// 產生 floatmenu
	var floatmenuHTML = window.createFloatmenu (json);
	$('body').append (floatmenuHTML);

	// 初始化 topNav
	window.topNavInit (8);

	// 初始化 floatmenu
	$('.floatmenu a').mouseover(function(event){
		$(this).addClass ('overStyle');
	});
	$('.floatmenu a').mouseout(function(event){
		$(this).removeClass ('overStyle');
	});

	// 修正 PNG
	// $('.docBG').pngfix();
	// $('.floatmenu a').pngfix();

	// 標記 leftmenu 當前頁面
	window.setLeftmenuFocus (locator.split('.'));

	// 延伸背景
	window.expandDocBackground ();

	// Quick Search Form Initial
	if($('#quickSearchSelect')) {
		$('#quickSearchSelect').change(function () {
			var pagePath = $(this).val();
			if (pagePath != '#') {
				window.location = pagePath;
			}
		});
	}
});