/******************************************************************
Javascript to change the URLs of language image buttons

Version	: 2.0b
Author	: irc@isd.gov.hk
Date	: 12 Oct 2006

Usage   :
1.	Include this file to your HTML pages
	e.g. <script type="text/javascript" src="kanhan.js"></script>
2.  Change the variables khPrefix to the URL prefix of either
	'sc.info.gov.hk/gb/' (Big5 to GB) or 'sc.info.gov.hk/TuniS/'
	(UTF-8) according to your web site
2a. If your web pages are UTF-8 encoding, convert this file to UTF-8
	encoding
3.  Change the Language directory variables (chi_path, eng_path) to
	fit your web site structure
4.  Change the values of Chinese alt-text (chi_Alt, sim_Alt) for
	the language buttons
5.  In the HTML pages, add 'id' attribute to the <a> tags of
	the language buttons and assign the value to either
	'engBtn', 'chiBtn' or 'simBtn' accordingly
5a. For accessibility reasons, it is suggested to point the <a>
	tags absolutely to a page that users can select languages
	without JavaScript (e.g. Landing page of web sites adopted CLF)
******************************************************************/


// Kanhan prefix
var khPrefix = "sc.info.gov.hk/gb/";    //Big5 to GB
//var khPrefix = "sc.info.gov.hk/TuniS/";  //UTF-8

// Language directory
var chi_path= "/chi/"
var eng_path= "/eng/"
var sim_path= "/sim/"

var Chi_Alt = "ÁcÅéª©";
var Eng_Alt = "English";
var Sim_Alt = "Â²Ê^ª©";


/* Stop Editing */

function kanhan(engID, chiID, simID) {

	if (!document.getElementById) return;

	//initalize variable
	var e1 = null;
	var e2 = null;
	var href1 = location.href;
	var href2 = location.href;
	var alt = "";

	if (isSim()) {
	    e1 = document.getElementById(chiID);
	    e2 = document.getElementById(engID);
		href1 = location.href.replace(sim_path, chi_path);//location.href.replace(khPrefix, "");
		href2 = location.href.replace(sim_path, eng_path);
	}

	if (isChi()) {
	    e1 = document.getElementById(simID);
	    e2 = document.getElementById(engID);
		href1 = location.href.replace(chi_path, sim_path);//location.href.replace(location.protocol + "//", location.protocol + "//" + khPrefix);
		href2 = location.href.replace(chi_path, eng_path);
		alt = Sim_Alt;
	}

	if (isEng()) {
	    e1 = document.getElementById(chiID);
	    e2 = document.getElementById(simID);
		//href1 = location.href.replace(khPrefix, ""); //remove Kanhan prefix if English pages are browsed under Kanhan
		href1 = location.href.replace(eng_path, chi_path);
		href2 = location.href.replace(eng_path, sim_path);//href1.replace(location.protocol + "//", location.protocol + "//" + khPrefix);
	    alt = Chi_Alt;
	}

	//Update the href and Alt-text
	if (e1) {
		e1.setAttribute("href", href1);
		if (e1.getElementsByTagName("IMG")[0])
			e1.getElementsByTagName("IMG")[0].setAttribute("alt", alt);
	}
	if (e2) e2.setAttribute("href", href2);
}

function isEng() {
	return (location.href.toString().indexOf(eng_path) != -1) ? true : false;
}

function isChi() {
	if (isSim()) return false;
	return (location.href.toString().indexOf(chi_path) != -1) ? true : false;
}

function isSim() {
	return (location.href.toString().indexOf(sim_path) != -1) ? true : false;
}



function init(e) {
	kanhan("engBtn", "chiBtn", "simBtn");
}




if (window.addEventListener)
	window.addEventListener("load", init, false);
else if (window.attachEvent)
	window.attachEvent("onload", init);
else
	window.onload = init();


//for IE/Mac
document.onreadystatechange = function(e) {
	if (document.readyState=="interactive")
	init();
}
