var isIE = (navigator.appName.indexOf("Microsoft") > -1);
var d = document;

if (!levelArray)	var levelArray = new Array();
getLevel((location != top.location));

function init()
{
	checkisIFrame();
	setA();
	getFontSize();
	//checkIFrameScroll();
}

function checkIFrameScroll()
{
	var mainIFrame = parent.d.getElementById('mainIFrame');
	mainIFrame.style.overflow = (levelArray[1] == 'funfacts' || levelArray[1] == 'faces')?	'hidden':'visible';
}

function getLevel(iframe)
{
	var url = (iframe)?	location.pathname:location.hash;
	var array = url.split('/');
	
	for (var i=0; i<array.length; i++)
	{
		/*if (array[i].indexOf('_') >= 0)
		{
			var array1 = array[i].split('_');
			for (var j=0; j<array1.length; j++)
			{
				levelArray.push(array1[j]);
			}
		}
		else
		{
			levelArray.push(array[i]);
		}*/
		levelArray.push(array[i]);
	}
	
	levelArray.shift();
	levelArray.shift();
	
	var obj = levelArray[levelArray.length-1];
	if (obj != 'index.html')
	{
		if (levelArray.length > 1)
		{
			//levelArray[levelArray.length-1] = obj.replace('.html', '');
			var array = obj.split('.html');
			levelArray[levelArray.length-1] = array[0];
		}
	}
	else
	{
		levelArray.pop();
	}
	//alert(levelArray);
}

function highlightLeftNav()
{
	if (!d.getElementById('inside_nav'))	return;
	var leftNav = d.getElementById('inside_nav');
	
	//Highlight Left Nav
	var id = levelArray[2];
	if (d.getElementById(id))
	{
		var nav = d.getElementById(id)
		nav.className += ' active';
	}
	
	//setLeftNav(leftNav);
	
	function setLeftNav(menu)
	{
		var a = menu.getElementsByTagName('a');
		
		for (var i=0; i<a.length; i++)
		{
			a[i].onclick = function()
			{
				changeTopHash(this);
			}
		}
	}
}

function setA()
{
	var a = d.getElementsByTagName('a');
	
	for (var i=0; i<a.length; i++)
	{
		if (a[i].href == '#')	continue;
		if (a[i].target == '_blank')	continue;
		if (a[i].href.indexOf('javascript:') >= 0)	continue;
		if (a[i].parentNode.parentNode.id == 'breadcrumb')	continue;
		
		a[i].onclick = function()
		{
			changeTopHash(this);
		}
	}
}

function NewWindow(mypage, myname, w, h, scroll,resizable) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable='+resizable+','
	win = window.open(mypage, myname, winprops)
	win.self.focus()
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function createImg(src, alt, w, h, link, t, className)
{
	var img = d.createElement('img');
	if (src)	img.setAttribute('src', src);
	if (alt)	img.setAttribute('alt', alt);
	if (w)	img.setAttribute('width', w);
	if (h)	img.setAttribute('height', h);
	if (className)	img.className = className;
	
	if (link)
	{
		img.setAttribute('border', 0);
		
		var a = d.createElement('a');
		a.setAttribute('href', link);
		if (t && typeof(t) != 'undefined')	a.setAttribute('target', t);
		a.appendChild(img);
		return a;
	}
	else
	{	
		return img;
	}
}

function createA(link, t, txt, id, className)
{		
	var a = d.createElement('a');
	if (link)	a.setAttribute('href', link);
	if (t && typeof(t) != 'undefined')	a.setAttribute('target', t);
	
	//if (txt)	a.appendChild(d.createTextNode(txt));
	if (txt)	a.innerHTML = txt;
	if (id)	a.setAttribute('id', id);
	if (className)	a.className = className;
	
	return a;
}

function createDiv(id, className, txt)
{		
	var div = d.createElement('div');
	if (id)	div.setAttribute('id', id);
	if (className)	div.className = className;
	//if (txt)	div.appendChild(d.createTextNode(txt));
	if (txt)	div.innerHTML = txt;
	return div;
}

function createTag(tag, id, className)
{		
	var div = d.createElement(tag);
	if (id)	div.setAttribute('id', id);
	if (className)	div.className = className;
	return div;
}

function createInput(name, type, value, className)
{		
	var input = d.createElement('input');
	if (name)	input.setAttribute('name', name);
	if (type)	input.setAttribute('type', type);
	if (value)	input.setAttribute('value', value);
	if (className)	input.className = className;
	return input;
}

function setChildNodes(obj, tagName)
{
	var array = new Array();
	
	for (var i=0; i<obj.childNodes.length; i++)
	{
		if (tagName)
		{
			//alert(obj.childNodes[i].tagName)
			if (obj.childNodes[i].tagName != tagName)	continue;
		}
		if (obj.childNodes[i].toString().toLowerCase().indexOf('text') >= 0)	continue;
		array.push(obj.childNodes[i]);
	}
	
	return array;
}

function getElementsByClassName(p, c, selected)
{
	var array = new Array();
	var tags = p.getElementsByTagName('*');
	
	for (var i=0; i<tags.length; i++)
	{
		if (!tags[i].className)	continue;
		if (selected)
		{
			if (tags[i].className.indexOf(c) >= 0)	array.push(tags[i]);
		}
		else
		{
			if (tags[i].className == c)	array.push(tags[i]);
		}
	}
	
	return array;
}

function GetParam(name)
{
	var start=location.search.indexOf("?"+name+"=");
	if (start<0) start=location.search.indexOf("&"+name+"=");
 	if (start<0) return '';
 	start += name.length+2;
 	var end=location.search.indexOf("&",start)-1;
 	if (end<0) end=location.search.length;
 	var result=location.search.substring(start,end);
 	var result='';
 	for(var i=start;i<=end;i++)
 	{
 		var c=location.search.charAt(i);
 		result=result+(c=='+'?' ':c);
 	}
 	//alert(unescape(result));
 	return unescape(result);
}

function startFading(el)
{
  if (el.fadinTimeout)	clearTimeout(el.fadinTimeout);
  el.style.visibility = 'visible';
  el.style.zIndex = 2;
  setOpacity(el, 0);
  fadeImage(el, 0);
}

function fadeImage(el, currentOpacity)
{
  currentOpacity += 10;

  if (currentOpacity > 100)
  {
    setOpacity(el, 100);
    el.style.zIndex = 1;
	if (el.fadinTimeout)	clearTimeout(el.fadinTimeout);
  }
  else
  {
    setOpacity(el, currentOpacity);
    el.fadinTimeout = setTimeout(function() { fadeImage(el, currentOpacity); }, 30);
  }
}

function setOpacity(el, opacity)
{
	opacity /= 100;
	el.style.opacity = opacity;
	el.style.MozOpacity = opacity;
	el.style.filter = "alpha(opacity=" + (opacity*100) + ")";
}

function setScroll(container, sBar)
{
	var nav = d.getElementById(container);
	var scroll = d.getElementById(sBar);
	var total;
	var scroller;
	var scrollbar;
	var self = this;
	
	this.init = function ()
	{
		//Set Container's Width
		var h = nav.parentNode.offsetHeight;
		
		while (nav.offsetHeight > nav.parentNode.offsetHeight)
		{
			var num = nav.offsetWidth;
			num ++;
			nav.style.width = num + 'px';
		}
		//alert(nav.offsetWidth + '  :  ' + nav.parentNode.offsetWidth)
		//alert(nav.offsetHeight + '  :  ' + nav.parentNode.offsetHeight)
		
		scroller  = new Scrolling.Scroller(nav, scroll.offsetWidth, scroll.offsetHeight);
		scrollbar = new Scrolling.Scrollbar(scroll, scroller, new Scrolling.ScrollTween());
	};
	
	this.init();
}

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 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_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 addEvent (o, t, f)
{
	removeEvent (o, t, f);
	if (o.attachEvent) o.attachEvent('on'+ t, f);
	else o.addEventListener(t, f, false);
};

function removeEvent (o, t, f)
{
	if (o.detachEvent) o.detachEvent('on'+ t, f);
	else o.removeEventListener(t, f, false);
};

function checkisIFrame()
{
	if (location != top.location)	return;
	
	var array = new Array('en', 'tc', 'sc');
	var lang;
	var url = location.href;
	if (location.hash)	url = location.href.replace(location.hash, '');
	var host = location.hostname;
	var path = location.pathname;
	var search = location.search;
	
	for (var i=0; i<array.length; i++)
	{
		if (url.indexOf('/' + array[i] + '/') < 0)	continue;
		lang = array[i];
		break;
	}
	
	location.replace('http://' + host + '/' + lang + '/#' + path + search);
}

function changeTopHash(btn)
{
	//if (location == top.location)	return;
	//if (btn.href.indexOf('javascript:') >= 0)	return;
	
	var url = (btn.url)?btn.url:btn.href;
	var array = url.split('/');
	array.shift();
	array.shift();
	array.shift();
	var path = array.join('/');
	var topUrl = top.location.href.replace(top.location.hash, '');
	
	top.location.href = topUrl + '#/' + path;
}

function genLeasePulldown() {
    var scrollArray = new Array();
    for (var i = 0; i < listArray.length; i++) {
        if (d.getElementById(listArray[i]) != null)
            scrollArray.push(d.getElementById(listArray[i]));
    }
    for (var i = 0; i < scrollArray.length; i++) {
        var scrollContainer = getElementsByClassName(scrollArray[i], 'drowdownItmeLink')[0];
        var scroll = getElementsByClassName(scrollArray[i], 'Scrollbar-Container')[0];

        var scroller = new Scrolling.Scroller(scrollContainer, scroll.offsetWidth, scroll.offsetHeight);
        var scrollbar = new Scrolling.Scrollbar(scroll, scroller, new Scrolling.ScrollTween());
    }
}

function initPullDown(motherID, childID, valueID, type) {
    if (!d.getElementById(motherID) && !d.getElementById(childID)) return;

    var pullDownDiv = getElementsByClassName(d.getElementById(motherID), "pulldownMid");
    var itemList = getElementsByClassName(d.getElementById(childID), "pulldownListMid");

    for (var i = 0; i < itemList.length; i++) {
        var a = itemList[i].getElementsByTagName('a');

        for (var j = 0; j < a.length; j++) {
            a[j].section = i;

            if (type == 0) {
                a[j].onclick = function() {
                    pullDownDiv[0].innerHTML = this.innerHTML;
                    valuePass(valueID, this.innerHTML);
                }
            }

            if (type == 1) {
                a[j].onclick = function() {
                    pullDownDiv[0].innerHTML = this.innerHTML;
                }
            }

            if (type == 2) {
                a[j].onclick = function() {
                    pullDownDiv[0].innerHTML = this.innerHTML;
                    valuePass(valueID, this.attributes["dropdownValue"].value);
                }
            }
            if (type == 3) {
                a[j].onclick = function() {
                    pullDownDiv[0].innerHTML = this.innerHTML;
                    loadRmb(1, parseInt(this.attributes["dropdownValue"].value));
                }
            }
        }
    }
}

function showPulldown(btn, id, showID) {
    var menu = d.getElementById(id);

    showLayer(1);
    btn.onclick = function() { menu.hit = true; showLayer(1); };
    menu.onmouseover = function() { menu.hit = true; showLayer(1); };
    menu.onmouseout = function() { menu.hit = false; showLayer(0); };
    menu.onclick = function() { menu.hit = false; showLayer(0); };

    function showLayer(over) {
        if (over == 1) {
            menu.style.display = 'block';
			btn.onmouseover = function() { menu.hit = true; showLayer(1); };
			btn.onmouseout = function() { menu.hit = false; showLayer(0); };
            if (!d.getElementById(showID)) return;
            d.getElementById(showID).style.display = "none";			
			
        }
        else {
            setTimeout(function() {
                if (!menu.hit) {
                    menu.style.display = 'none';
					btn.onmouseover = null;
					btn.onmouseout = null;
                    if (!d.getElementById(showID)) return;
                    d.getElementById(showID).style.display = "block";
                }
            }, 10);
        }
    }
}

function myHKTabInit()
{
	if(!d.getElementById('tabContainer')) return;
	var tabDiv = d.getElementById('tabContainer');
	var tabDivArray = tabDiv.getElementsByTagName('A');
	
	if(!d.getElementById('tabItem')) return;
	var tabItem = d.getElementById('tabItem');
	var tabItemArray = getElementsByClassName(tabItem,'tabContent');
	
	for(var i=0; i<tabDivArray.length; i++)
	{
		tabDivArray[i].num = i;
		tabDivArray[i].onclick = function()
		{
			for(var j=0; j<tabItemArray.length; j++)
			{
				tabDivArray[j].className = tabDivArray[j].className.replace('active', '');
				tabItemArray[j].style.display = 'none';
			}
			tabItemArray[this.num].style.display = 'block';
			this.className += ' active';
		};
	}
}

var breadcrumbsObj = new Object();

function genBreadcrumbs()
{
	if (!d.getElementById('breadcrumb'))	return;
	var breadcrumb = d.getElementById('breadcrumb');
	var links = getElementsByClassName(breadcrumb, 'links')[0];
	
	var a = createA('javascript:parent.goHome();', '_self', 'Home');
	links.appendChild(a);
	
	for (var i=0; i<levelArray.length; i++)
	{
		var obj = levelArray[i];
		var btn;
		var txt;
		var link;
		
		if (i < 2)
		{
			links.innerHTML += '<span>&gt;</span>';
			
			var container = parent.d.getElementById('mainNav');
			var id = levelArray[i];
			btn = getElementsByClassName(container, id  + ' ', true)[0];
			if (!btn)	continue;
			txt = (isIE)?	btn.innerText:btn.textContent;
			
			a = createA('javascript:breadcrumbsLink(\'' + id  + '\');', '_self', txt);
			a.url = (btn.url)?btn.url:btn.href;
			breadcrumbsObj[id] = new Object();
			breadcrumbsObj[id].btn = a;
			if (i == 0)	breadcrumbsObj[id].goSection = true;
			links.appendChild(a);
		}
		else
		{
			//btn = d.getElementById(levelArray[i]);
			if (!d.getElementsByTagName('h2').length)	continue;
			links.innerHTML += '<span>&gt;</span>';
		
			btn = d.getElementsByTagName('h2')[0];
			txt = btn.getElementsByTagName('img')[0].alt;
			links.innerHTML += txt;
		}
	}
}

function breadcrumbsLink(id)
{
	changeTopHash(breadcrumbsObj[id].btn);
	
	if (breadcrumbsObj[id].goSection)
	{
		parent.goSection(id);
	}
	else
	{
		location.href = breadcrumbsObj[id].btn.url;
	}
};

//Specify spectrum of different font sizes:
var szs = new Array('100%', '110%', '120%');
var startSz = sz = szs.length;

function getFontSize()
{
	if (getCookie('fontSize') == null)
	{
		startSz = 0;
	}
	else
	{ 
		startSz = getCookie('fontSize');
		if (startSz == "NaN")	startSz = 0;
	}
	if (parent.changeFontSize)	parent.changeFontSize(startSz, true);
}

function changeFontSize(inc, start)
{
	if (!d.getElementById('fontSize'))	return;
	var cEl = null,sz = eval(startSz),i,j,cTags;
	sz = inc;
	
	//Highlight Btn
	var fontSizeLink = d.getElementById('fontSize').getElementsByTagName('a');
	for (var i=0; i<fontSizeLink.length; i++)
	{
		fontSizeLink[i].className = fontSizeLink[i].className.replace(/active/, '');
		fontSizeLink[i].clicked = false;
	}
	fontSizeLink[sz].className = fontSizeLink[sz].className += ' active';
	fontSizeLink[sz].clicked = true;
	
	setCookie('fontSize', sz, nd, cpath, cdomain);
	
	if (!d.getElementById('mainIFrame')) return;
	var mainIFrame = d.getElementById('mainIFrame').contentWindow;
	if (!getElementsByClassName(mainIFrame.document.body, 'contentContainer', true)[0])	return;
	
	cEl = getElementsByClassName(mainIFrame.document.body, 'contentContainer', true)[0];
	cEl = getElementsByClassName(mainIFrame.document.body, 'contentContainer', true)[0];
	cEl.style.fontSize = szs[ sz ];	
}

var nd= new Date();
nd.setTime(nd.getTime()+(365*24*60*60*1000));
//cdomain = (location.domain) ? location.domain : null;
var cdomain = (location.domain) ? location.domain : null;
var cpath = "/";
/*end of change fontsize*/

function setCookie(name, value, expires, path, domain, secure)
{	
	var curCookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");

	document.cookie = curCookie;
}

function getCookie(name)
{
	if (document.cookie.indexOf(name) < 0)	return null;
	
	var startStr = document.cookie.indexOf(name) + name.length + 1;
	var endStr = document.cookie.indexOf(";", startStr);
	if (endStr == -1)	endStr = document.cookie.length;
	return unescape(document.cookie.substring(startStr, endStr));
}

function genFunFactsSwf()
{
	var flashvars = {};
	flashvars.path = "../../";
	flashvars.xmlPath = "../../xml/";
	flashvars.imagePath = "../../images/facts/";
	flashvars.swfPath = "../../swf/";
	flashvars.flvPath = "../../flv/";

	var params = {};
	params.allowscriptaccess = "sameDomain";
	params.allowFullScreen = true;

	var attributes = {};
	//attributes.id = "mainFlash";

	w = 300;
	h = 408;

	swfobject.embedSWF("../../swf/funFacts_preloader.swf", "mainFlash", w, h, "9.0.115", false, flashvars, params, attributes);
}

function setSitemapLink()
{
	if (!opener)	return;
	
	var container = d.body;
	var a = container.getElementsByTagName('a');
	
	for (var i=0; i<a.length; i++)
	{
		if (!a[i].parentNode.getElementsByTagName('strong')[0])	continue;
		var btn = a[i].parentNode.getElementsByTagName('strong')[0];
		
		a[i].navID = btn.className;
		a[i].subNavID = a[i].className;
		a[i].url = a[i].href;
		if (a[i].target == 'mainIFrame')	a[i].href = 'javascript:;';
		a[i].onclick = function()
		{
			if (!opener || opener.closed)	window.open(this.url);
			opener.goSection(this.navID, this.subNavID);
		}
	}
}


