var timerID = null;
var timerOn = false;
var timecount = 300;
var submenu=new Array();

var requiredVersion = 50;  //set it as =6, in case wanna reactivate the flash intro for IE 
var useRedirect = true;

// system globals
var flash2Installed = false;
var flash3Installed = false;
var flash4Installed = false;
var flash5Installed = false;
var flash6Installed = false;
var flash7Installed = false;
var maxVersion = 7;
var actualVersion = 0;
var hasRightVersion = false;

var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;		// true if we're on ie
var isWin = (navigator.appVersion.indexOf("Windows") != -1) ? true : false; // true if we're on windows

if(isIE && isWin){
	document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n');
	document.write('on error resume next \n');
	document.write('flash2Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.2"))) \n');
	document.write('flash3Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.3"))) \n');
	document.write('flash4Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.4"))) \n');
	document.write('flash5Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.5"))) \n');	
	document.write('flash6Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.6"))) \n');	
	document.write('flash7Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.7"))) \n');	
	document.write('</SCR' + 'IPT\> \n'); // break up end tag so it doesn't end our script
}

if (navigator.plugins){
	if (navigator.plugins["Shockwave Flash 2.0"]
	|| navigator.plugins["Shockwave Flash"]){
		var isVersion2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
		var flashDescription = navigator.plugins["Shockwave Flash" + isVersion2].description;
		var flashVersion = parseInt(flashDescription.charAt(flashDescription.indexOf(".") - 1));
		flash2Installed = flashVersion == 2;		
		flash3Installed = flashVersion == 3;
		flash4Installed = flashVersion == 4;
		flash5Installed = flashVersion == 5;
		flash6Installed = flashVersion == 6;
		flash7Installed = flashVersion == 7;
	}
}
	
for (var i = 2; i <= maxVersion; i++) {	
	if (eval("flash" + i + "Installed") == true) actualVersion = i;
}

if(navigator.userAgent.indexOf("WebTV") != -1) actualVersion = 2;

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
	}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function openWindow(theURL,winName,features) { //v2.0
	winpop=window.open(theURL,winName,features);
	winpop.focus();
}

function switchLang() {
	var selfUrl = self.location.pathname;
	var param = self.location.search;
	var hash = self.location.hash;

	if (selfUrl.indexOf("/en/", 0) >= 0) {  //-1 not found
		from = "/en/";
		to = "/tc/";
	}
	else {
		from = "/tc/";
		to = "/en/";
	}

	if (selfUrl.indexOf(from, 0) >= 0) {  //-1 not found
		pos = selfUrl.indexOf(from, 0);
		targetUrl = selfUrl.substring(0, pos)+to+selfUrl.substring(pos+4,selfUrl.length);
		self.location.href = targetUrl + param + hash;
	} else {
		self.location.href = selfUrl;
	}
	
}

function findLocation() {
	var selfUrl = self.location.pathname;
	var section=-1;
	var subsection=0;
	for (i=0;i<nav.length-1;i++) {
		for (j=0;j<nav[i].length;j++) {
			if (selfUrl.indexOf(nav[i][j].url, 0) >= 0) {  //-1 not found
				section=i;
				subsection=j;
			}
		}
	}

	return(section+','+subsection);
}

function writeHeader() {
	var location=new Array();
	var html='';
	var space=760;
	var count=0;

	location=findLocation().split(",");

	html+='<a name="top"></a>\n';
	html+='<div style="width:760px; background-color:#C2E273;">\n';

	html+='<img src="images/header_';
	html+=(location[0]<0)? 7 : location[0];  // page not in navigation array?
	html+='.jpg" width="760" height="69" border="0"><br>\n';

	for (i=0;i<nav.length-1;i++) {
		temp=nav[i][0];
		html+='<a href="'+temp.url+'" onMouseOut="MM_swapImgRestore();';
		html+=(nav[i].length>1)? ' startTime();' : '';  // got sub navigation?
		html+='" onMouseOver="';
		html+=(nav[i].length>1)? 'showSubMenu(\'submenu'+i+'\'); stopTime(); ' : '';  // got sub navigation?
		html+='MM_swapImage(\''+temp.image+'\',\'\',\'images/'+temp.image+'_over.gif\',1);return document.MM_returnValue"><img name="'+temp.image+'" src="images/'+temp.image;
		html+=(location[0]==i)? '_on' : '';  // in current section?
		html+='.gif" width="'+temp.width+'" height="32" border="0" alt="'+temp.name+'"></a>';
		space-=temp.width;

		MM_preloadImages('images/'+temp.image+'_on.gif');
	}

	temp=nav[nav.length-1][0];  // Language Icon
	space-=temp.width;          // Calculate spacer width
  html+='<img src="images/spacer.gif" width="'+space+'" height="1" border="0" alt="">';

	// Display Language Icon
	html+='<a href="'+temp.url+'" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\''+temp.image+'\',\'\',\'images/'+temp.image+'_over.gif\',1);return document.MM_returnValue"><img name="'+temp.image+'" src="images/'+temp.image+'.gif" width="'+temp.width+'" height="32" border="0" alt="'+temp.name+'"></a><br>\n';

//  Display round corner at right end (no language switching)
//	html+='<img src="images/nav_end.gif" width="55" height="32" border="0" alt=""><br>\n';
	html+='</div>\n';

//	MM_preloadImages('images/'+temp.image+'_on.gif');

	html+='<img src="images/spacer.gif" width="1" height="5" border="0" alt=""><br>\n';

	html+=(location[0]<0)? '<img src="images/spacer.gif" width="1" height="20" border="0" alt=""><br>\n' : '';  // page not in navigation array?

	for (i=1;i<nav.length-1;i++) {
		if (nav[i].length>1) {  // if got sub navigation
			submenu[count]=i;
			count++;
			html+='<div id="submenu'+i+'" style="padding:0px; visibility:hidden; overflow:hidden; position:absolute; top:101px; left:';
//			left=-1;
			left=((getWindowWidth()-754)/2)-4;
			left=(left<0)? -1:left;
			for (j=0;j<i;j++) {
				left+=nav[j][0].width;
			}
			html+=left+'px;" onMouseOver="stopTime();" onMouseOut="startTime();">';
			html+='<table cellspacing="0" cellpadding="0" border="0" bgcolor="#99CC33"><tr><td><table cellspacing="1" cellpadding="0" border="0">';
			for (j=1;j<nav[i].length;j++) {
				html+='<tr><td bgcolor="#EBF3B7" style="padding:5px 10px 5px 10px; cursor:hand;cursor:pointer;" onClick="location.href=\'..'+nav[i][j].url+'\'";><a href="..'+nav[i][j].url+'" class="subnavlink">'+nav[i][j].name+'</a></td></tr>';
			}
			html+='</table></td></tr></table>';
			html+='</div>';
		}
		
		/*  Disable the submenu - edited by Michael Yeung on 18 Nov 08 
		if (i==location[0]) {
			if (nav[i].length>1) {
				temp=nav[i][1];
				html+='<div align="left" style="padding-left:15px; width:754px;">';
				html+='<a href="..'+temp.url+'" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\''+temp.image+'\',\'\',\'images/'+temp.image+'_over.gif\',1);return document.MM_returnValue"><img name="'+temp.image+'" src="images/'+temp.image;
				html+=(location[1]==1)? '_on' : '';  // in current section?
				html+='.gif" width="'+temp.width+'" height="20" border="0" alt="'+temp.name+'"></a>';
				for (j=2;j<nav[i].length;j++) {
					temp=nav[i][j];
					html+='<img src="images/nav_sub_div.gif" width="20" height="20" border="0"><a href="..'+temp.url+'" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage(\''+temp.image+'\',\'\',\'images/'+temp.image+'_over.gif\',1);return document.MM_returnValue"><img name="'+temp.image+'" src="images/'+temp.image;
					html+=(location[1]==j)? '_on' : '';  // in current section?
					html+='.gif" width="'+temp.width+'" height="20" border="0" alt="'+temp.name+'"></a>';
				}
				html+='</div>';
			}
			else {
				html+='<img src="images/spacer.gif" width="1" height="20" border="0" alt=""><br>\n';
			}
		}  */
	}

	document.write(html);
}

function writeSitemap() {
	html='';

	for (i=0;i<nav.length-1;i++) {
		html+='<a href="..'+nav[i][0].url+'">'+nav[i][0].name+'</a><br>\n';
		if (nav[i].length>1) {
			html+='<ul class="bulletlist2">\n';
			for (j=1;j<nav[i].length;j++) {
				html+='<li><a href="..'+nav[i][j].url+'">'+nav[i][j].name+'</a></li>\n';
			}
			html+='</ul>\n';
		}
		html+='<br>\n';
	}

	document.write(html);
}

function getObj(name) {
	if (document.getElementById) {
		return document.getElementById(name);
	}
	else if (document.all) {
		return document.all[name];
	}
	else if (document.layers) {
		return document.layers[name];
	}
}

function hideAll() {
	for (i=0;i<submenu.length;i++) {
		id="submenu"+submenu[i];
		hideSubMenu(id);
	}
}

function startTime() {
	if (timerOn == false) {
		timerID = setTimeout( "hideAll()" , timecount);
		timerOn = true;
	}
}

function stopTime() {
	if (timerOn) {
		clearTimeout(timerID);
		timerID = null;
		timerOn = false;
	}
}

function showSubMenu(id) {
	hideAll();
	obj=getObj(id);
	obj.style.visibility="visible";
}

function hideSubMenu(id) {
	obj=getObj(id);
	obj.style.visibility="hidden";
}

function getWindowWidth() {
	var myWidth = 0, myHeight = 0;
	if (typeof(window.innerWidth) == 'number') {  //Non-IE
		myWidth = window.innerWidth;
	} else if (document.body && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {  //IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
//	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {  //IE 4 compatible
	} else {
		myWidth = document.body.clientWidth;
	}

	return(myWidth);
}

function reload(){
	window.location.reload()
}

window.onresize = reload;
