
// langswitch object
// Important
var _lang_switch = {

	php_script_obj : '',
	lang_span_id : '',					// hold the id to span containing the language icon for language switching

	// string to identify if page is CHT
	cht_constant : "-cht",
	chs_constant : "-chs",


	// Check the current page if it's ENG or CHT, and display the other language icon for language switching
	init_lang_link : function(phpScriptObject, spanID) {
		// Save up the current php script name
		this.php_script_obj = phpScriptObject;
		this.lang_span_id = spanID

		// Proceed to add language icon to page
		if (document.getElementById(this.lang_span_id)) {

			var searchbox = document.getElementsByName("q");
			var inHtml = ""

			// if current page is CHT
			if (this.is_cht_page(this.php_script_obj['filename'])) {
				if (searchbox.length > 0)
					searchbox[0]['value']  = "關鍵字搜尋";

				// Add English switching icon
				inHtml += "<a class='lang-btn' href='#' onclick='_lang_switch.switch_lang(\"eng\");return false;'><img style='margin:0px;padding:0px;text-align:left;float:none;positon:static;' src='/web/images/en.gif' alt='English' title='English' /></a>";
				// Add CHS switching icon if supported
				if (this.php_script_obj['chs'] && this.php_script_obj['has_chs_page']) {
					inHtml += "<a class='lang-btn' href='#' onclick='_lang_switch.switch_lang(\"chs\");return false;'><img style='margin:0px;padding:0px;text-align:left;float:none;positon:static;' src='/web/images/chs.gif' alt='简体中文' title='简体中文' /></a>";
				}
			}
			else if (this.is_chs_page(this.php_script_obj['filename'])) {

				// Add English switching icon
				inHtml += "<a class='lang-btn' href='#' onclick='_lang_switch.switch_lang(\"eng\");return false;'><img style='margin:0px;padding:0px;text-align:left;float:none;positon:static;' src='/web/images/en.gif' alt='English' title='English' /></a>";
				// Add CHT switching icon if supported
				if (this.php_script_obj['cht'] && this.php_script_obj['has_cht_page']) {
					inHtml += "<a class='lang-btn' href='#' onclick='_lang_switch.switch_lang(\"cht\");return false;'><img style='margin:0px;padding:0px;text-align:left;float:none;positon:static;' src='/web/images/cht.gif' alt='繁體中文' title='繁體中文' /></a>";
				}

			}
			// otherwise display the CHT icon
			else {

				// Add CHT switching icon if supported
				if (this.php_script_obj['cht'] && this.php_script_obj['has_cht_page']) {
					inHtml += "<a class='lang-btn' href='#' onclick='_lang_switch.switch_lang(\"cht\");return false;'><img style='margin:0px;padding:0px;text-align:left;float:none;positon:static;' src='/web/images/cht.gif' alt='繁體中文' title='繁體中文' /></a>";
				}

				// Add CHS switching icon if supported
				if (this.php_script_obj['chs'] && this.php_script_obj['has_chs_page']) {
					inHtml += "<a class='lang-btn' href='#' onclick='_lang_switch.switch_lang(\"chs\");return false;'><img style='margin:0px;padding:0px;text-align:left;float:none;positon:static;' src='/web/images/chs.gif' alt='简体中文' title='简体中文' /></a>";
				}

			}
			document.getElementById(this.lang_span_id).innerHTML = inHtml;
		}

	},
	switch_lang : function(desiredLang) {
		var baseFilename = this.php_script_obj['filename'];
		var fileExtension = this.php_script_obj['extension'];
		var baseFilenameLength = baseFilename.length
		if (baseFilenameLength > 4) {
			if (baseFilename.substring(baseFilenameLength-4, baseFilenameLength) == this.cht_constant ||
					baseFilename.substring(baseFilenameLength-4, baseFilenameLength) == this.chs_constant) {
				baseFilename = baseFilename.substring(0, baseFilenameLength-4);
			}
		}
		switch (desiredLang) {
			case 'eng':
				// strip out the cht_constant from page name and redirect there
				//var new_page = this.php_script_obj['short_name'].replace(this.cht_constant, '');
				var new_page = baseFilename+"."+fileExtension;
				window.location = this.php_script_obj['level']+new_page+'?lang_reset=1';
				break;

			case 'cht':
				// append the cht_consant to paeg name and redirect there
				/*
				var extIndex =  this.php_script_obj['short_name'].lastIndexOf('.')
				if (extIndex == -1) return false;

				var new_page = this.php_script_obj['short_name'].substring(0, extIndex) + this.cht_constant + this.php_script_obj['short_name'].substring(extIndex)
				*/
				var new_page = baseFilename+this.cht_constant+"."+fileExtension;
				window.location = this.php_script_obj['level']+new_page+'?lang_reset=1';
				break;

			case 'chs':
				// append the cht_consant to paeg name and redirect there
				/*
				var extIndex =  this.php_script_obj['short_name'].lastIndexOf('.')
				if (extIndex == -1) return false;

				var new_page = this.php_script_obj['short_name'].substring(0, extIndex) + this.chs_constant + this.php_script_obj['short_name'].substring(extIndex)
				*/
				var new_page = baseFilename+this.chs_constant+"."+fileExtension;
				window.location = this.php_script_obj['level']+new_page+'?lang_reset=1';
				break;
		}
	},

	// Check if given page is a CHT page, look for -cht in string
	is_cht_page : function(page_name) {
		return this.php_script_obj['is_cht_page'];
	},

	// Check if given page is a CHS page, look for -chs in string
	is_chs_page : function(page_name) {
		return this.php_script_obj['is_chs_page'];
	}
}



// Attach to window onload to initialize the necessary language icons
if (document.all){
	window.attachEvent(
		'onload',
		function(){
			if (typeof(PHP_SCRIPT_SOURCE) != "undefined") {
				_lang_switch.init_lang_link(PHP_SCRIPT_SOURCE, 'lang-switch-span');		// PHP_SCRIPT_SOURCE defined in top.php
			}
		}
	);
}
else{
	window.addEventListener(
		'load',
		function(){
			if (typeof(PHP_SCRIPT_SOURCE) != "undefined") {
				_lang_switch.init_lang_link(PHP_SCRIPT_SOURCE, 'lang-switch-span');		// PHP_SCRIPT_SOURCE defined in top.php
			}
		},
		false
	);
}

