jQuery.preloadImages = function() {
	$('.btn_over').each(function() {
		jQuery("<img>").attr("src", $(this).attr('src').toString().replace('.','_over.'));
	});
}

$(document).ready(function() {
	$('.btn_over').mouseover(function(){
		$(this).attr('src', $(this).attr('src').toString().replace('.','_over.'));
	}).mouseout(function(){
		$(this).attr('src', $(this).attr('src').toString().replace('_over.','.'));
	});
	$('.change_over').each(function() {
		$(this).attr('src', $(this).attr('src').toString().replace('.','_over.'));
	});
	$('#txtSearchText').focus(function() {
		$(this).val("");
	});
	$('#txtSearchText').blur(function() {
		if ($(this).val()=="") {
			$(this).val("Search by Keyword");
		}
	});

	$(".navBtn .sub").each(function() { 
		var currSub = $(this);

		$(this).parent().hover(function(){
			var mainMenu = $(this).parent(".navBtn");
			mainMenu.attr("active", 1)
			if (mainMenu.filter(".posBOTTOM").size() > 0)
			{
			var heightAdjust = 4;
			//if (BrowserDetect.browser=="Explorer" && BrowserDetect.version==6) heightAdjust = 3;
			//currSub.css({display: "block", top: getY(this) + mainMenu.height() - heightAdjust, left: getX(this)});
			currSub.css({display: "block", top: getY(this) + 54, left: getX(this)});
			}
			else //if (mainMenu.filter(".posRIGHT").size() > 0)
			{
			var iWidth = mainMenu.width();
			// Original COde
			//currSub.css({display: "block", top: getY(this), left: getX(this) + iWidth});
			currSub.css({display: "block", top: getY(this) - 4, left: getX(this) + iWidth - 4});
			}
			//currSub.width(mainMenu.width());
			currSub.show();
			}, function() {
			currSub.hide();
			});
	});

	changeFont(getCookie("font_size"));

	$.preloadImages();
})

function getY(arg)
{
	return arg.offsetTop + (arg.offsetParent?getY(arg.offsetParent):0);
}

function getX(arg)
{
	return arg.offsetLeft + (arg.offsetParent?getX(arg.offsetParent):0);
}

function Menu() {
	this.contentId = 0;
	this.text = "";
	this.alt = "";
	this.href = "";
	this.target = "";
	this.childs = new Array();
}

Menu.prototype = {
	addChild : function(m) {
		this.childs[this.childs.length] = m;
	}
}

function generateMenu(rootMenu) {
	for (var i=0; i<rootMenu.childs.length; i++) {
		var m = rootMenu.childs[i];
		document.writeln('<ul>');
		if (m.isCurrentSection) {
			document.writeln('<li><img src="'+m.img.replace(".","_on.")+'" alt="'+m.alt+'" title="'+m.alt+'" ></li>');
		} else {
			document.writeln('<li><a href="'+m.href+'"><img src="'+m.img+'" class="btn_over" border="0" alt="'+m.alt+'" title="'+m.alt+'"></a></li>');
		}
		if (m.childs.length>0) {
			generateMenu(m);
		}
		document.writeln('</ul>');
	}
}

function switchLang(charsetID){
	var url = window.location.toString();
	var isStaticURL = false;
	var langPaths = new Array("en", "tc", "sc");
	var oldLangPath = "";

/* Temp */
/*
if (charsetID==0) {
	alert('我們的網頁現正進行更新。如有任何查詢，請電郵ada@adahk.org.hk或致電(852) 2855 9548或傳真(852)2872 5246與我們聯絡。倘有不便之處，懇請見諒！');
	return;
}

if (charsetID==1) {
	alert('Our website is now under construction. If you have any enquiries, please contact us via email ada@adahk.org.hk or tel (852)2855 9548 or fax 2872 5246. Sorry for any inconvenience caused.');
	return;
}

if (charsetID==3) {
	alert('我們的網頁現正進行更新。如有任何查詢，請電郵ada@adahk.org.hk或致電(852) 2855 9548或傳真(852)2872 5246與我們聯絡。倘有不便之處，懇請見諒！');
	return;
}
*/
/* Temp */

	// Check url type (is static page or dynamic page)
	for (i=0; i < langPaths.length; i++){
		if (url.indexOf("/"+langPaths[i]+"/") != -1){
			oldLangPath = "/"+langPaths[i]+"/";
			isStaticURL = true;
			break;
		}
	}

	if (isStaticURL){
		if (charsetID>0) {
			window.location = url.replace(oldLangPath, "/"+langPaths[charsetID - 1]+"/");
		} else {
			if (url.indexOf("t_index.html")!=-1) {
				window.location = url.replace("t_index.html", "index.html");
			} else {
				window.location = url.replace("index.html", "t_index.html");
			}
		}
	}else{
		if (url.indexOf("lang=") != -1){
			window.location = 
				url.replace(/lang=\d/, "lang="+charsetID);
		}
	}
}

function changeFont(size) {
	if (size==''){
		size='m';
	}
	if (size=='s' || size=='m' || size=='l') {
		var link_tag = document.getElementsByTagName("link");
		for (var i = 0;i < link_tag.length;i++ ) {
			if ((link_tag[i].rel.indexOf( "stylesheet" ) != -1)) {
				if (link_tag[i].href.indexOf("common_small.css") != -1){
					if (size == 's')
						link_tag[i].disabled = false;
					else
						link_tag[i].disabled = true;
				}
				if (link_tag[i].href.indexOf("common.css") != -1){
					if (size == 'm')
						link_tag[i].disabled = false;
					else
						link_tag[i].disabled = true;
				}
				if (link_tag[i].href.indexOf("common_large.css") != -1){
					if (size == 'l')
						link_tag[i].disabled = false;
					else
						link_tag[i].disabled = true;
				}
			}
		}
		if (size=='s' && document.getElementById('font_s')) {
			document.getElementById('font_s').src=document.getElementById('font_s').src.replace("medium.gif","medium_over.gif");
			removeBtnOver('font_s');
		} else if (document.getElementById('font_s')) {
			document.getElementById('font_s').src=document.getElementById('font_s').src.replace("medium_over.gif","medium.gif");
			addBtnOver('font_s');
		}
		if (size=='m' && document.getElementById('font_m')) {
			document.getElementById('font_m').src=document.getElementById('font_m').src.replace("large.gif","large_over.gif");
			removeBtnOver('font_m');
		} else if (document.getElementById('font_m')) {
			document.getElementById('font_m').src=document.getElementById('font_m').src.replace("large_over.gif","large.gif");
			addBtnOver('font_m');
		}
		if (size=='l' && document.getElementById('font_l')) {
			document.getElementById('font_l').src=document.getElementById('font_l').src.replace("extra.gif","extra_over.gif");
			removeBtnOver('font_l');
		} else if (document.getElementById('font_l')) {
			document.getElementById('font_l').src=document.getElementById('font_l').src.replace("extra_over.gif","extra.gif");
			addBtnOver('font_l');
		}

		if (getCookie("font_size")!=size) {
			setCookie("font_size", size, 30);
		}
	}
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) { 
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	deleteCookie(c_name);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString()+";path=/");
}

function deleteCookie(c_name) {
	if (getCookie(c_name))
		document.cookie = c_name + "=;expires=Thu, 01-Jan-1970 00:00:01 GMT;path=/";
}

function addBtnOver(id) {
	$('#'+id).mouseover(function(){
		if ($(this).attr('src').indexOf('_over.gif')<0) {
			$(this).attr('src', $(this).attr('src').toString().replace('.gif','_over.gif'));
		}
	}).mouseout(function(){
		$(this).attr('src', $(this).attr('src').toString().replace('_over.','.'));
	});
}

function removeBtnOver(id) {
	$('#'+id).unbind("mouseover");
	$('#'+id).unbind("mouseout");
}

function printFriendly() {
	if (window.location.href.indexOf("?")==-1) {
		window.open(window.location.href+"?print=1");
	} else {
		window.open(window.location.href+"&print=1");
	}
}

function loadCalendar() {
	var xmlHttp;
	try	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			// Get the data from the server's response
			var content = xmlHttp.responseText;
			var idx;
			idx = content.indexOf("<!-- Start Calendar -->");
			if (idx>=0) {
				content = content.substring(idx);
			}
			idx = content.indexOf("<!-- End Calendar -->");
			if (idx>0) {
				content = content.substring(0,idx-1);
			}
			//alert(content);
			document.getElementById('calendarcontainer').innerHTML = content;
		}
	}

	if (zmsCharsetID=="1"){
		xmlHttp.open("GET","/en/calendar/index.html",true);
	} else if (zmsCharsetID=="2") {
		xmlHttp.open("GET","/tc/calendar/index.html",true);
	} else if (zmsCharsetID=="3") {
		xmlHttp.open("GET","/sc/calendar/index.html",true);
	}
	xmlHttp.send(null);
}