/******************************************************************
Javascript to change the URLs of language image buttons

Version	: 3.0
Author	: irc@isd.gov.hk
Date	: 8 May 2009

Usage   :
1.	Include this file to your HTML pages
	e.g. <script type="text/javascript" src="kanhan.js"></script>
2.  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)
******************************************************************/


// Language directory
var chi_path= "/chi/"
var eng_path= "/eng/"
var sim_path= "/sim/"

var Chi_Alt = "繁體版";
var Sim_Alt = "簡体版";
var Eng_Alt = "English";


/* 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 alt1 = "";
	var alt2 = "";

	if (isSim()) {
	    e1 = document.getElementById(simID);
	    e2 = document.getElementById(engID);
		href1 = href1.replace(sim_path, chi_path);
		href2 = href2.replace(sim_path, eng_path);
		alt1 = Chi_Alt;
		alt2 = Eng_Alt;
	}

	if (isChi()) {
	    e1 = document.getElementById(simID);
	    e2 = document.getElementById(engID);
		href1 = href1.replace(chi_path, sim_path);
		href2 = href2.replace(chi_path, eng_path);
		alt1 = Sim_Alt;
		alt2 = Eng_Alt;
	}

	if (isEng()) {
	    e1 = document.getElementById(chiID);
	    e2 = document.getElementById(simID);
		href1 = href1.replace(eng_path, chi_path);
		href2 = href2.replace(eng_path, sim_path);
		alt1 = Chi_Alt;
		alt2 = Sim_Alt;
	}

	//Update the href and Alt-text
	if (e1) {
		e1.setAttribute("href", href1);
		e1.getElementsByTagName("IMG")[0].setAttribute("alt", alt1);
	}
	if (e2) {
		e2.setAttribute("href", href2);
		e2.getElementsByTagName("IMG")[0].setAttribute("alt", alt2);
	}
}

function isEng() {
	return (location.href.toString().indexOf(eng_path) != -1) ? true : false;
}

function isChi() {
	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();
}

