var isIE = (navigator.appName.indexOf("Microsoft") > -1);
var d = document;

if (!levelArray)	var levelArray = new Array();
if (!jspType)		var jspType;


function init()
{
	getFontSize('0');
	if (levelArray.length == 0)	getLevel();
	if (levelArray.length > 0)	highlightTopNav();
	if (levelArray.length > 0)	highlightLeftNav();
	
	if (levelArray.length > 0)	genBreadcrumbs();
	setFieldFocus();
	if (d.getElementById('annouceNav'))	setPromotion.init('annouceNav', 'annouceContent');
	if (d.getElementById('fyResultsContainer'))	setFyYear.init('fyYearPulldown', 'fyResultsContainer');
}

function getLevel()
{
	var url = location.pathname;
	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.shift();
	levelArray.shift();
	
	var obj = levelArray[levelArray.length-1];
	if (obj != 'index.html' && obj != 'index.jsp')
	{
		if (obj.indexOf('.html') >= 0)	levelArray[levelArray.length-1] = obj.replace('.html', '');
		if (obj.indexOf('.jsp') >= 0)	levelArray[levelArray.length-1] = obj.replace('.jsp', '');
	}
	else
	{
		levelArray.pop();
	}
	//alert(levelArray);
	
	if (jspType)
	{
		levelArray.shift();
		levelArray.push(jspType);
	}
}

function showPulldown(btn, id)
{
	//if (btn.className.indexOf('active') >= 0)	return;
	
	var menu = d.getElementById(id + 'Pulldown');
	
	showLayer(1);
	btn.onmouseover		= function() {menu.hit = true;	showLayer(1);};
	menu.onmouseover	= function() {menu.hit = true;	showLayer(1);};
	btn.onmouseout		= function() {menu.hit = false;	showLayer(0);};
	menu.onmouseout		= function() {menu.hit = false;	showLayer(0);};
	
	function showLayer(over)
	{
		if (over == 1)
		{
			//if (btn.id != levelArray[0] && btn.className.indexOf('mouseon') < 0)	btn.className += ' mouseon';
			if (btn.className.indexOf('mouseon') < 0)	btn.className += ' mouseon';
			menu.style.display = 'block';
		}
		else
		{
			//if (btn.id != levelArray[0])	btn.className = btn.className.replace('mouseon', '');
			btn.className = btn.className.replace('mouseon', '');
			
			setTimeout(function()
			{
				if (!menu.hit)
				{
					menu.style.display = 'none';
				}
			}, 10);
		}
	}
}

function highlightTopNav()
{
	var obj = levelArray[0];
	//alert(obj);
	if (!d.getElementById(obj))	return;
	
	var btn = d.getElementById(obj);
	btn.className += ' active';
}

function highlightLeftNav()
{
	var obj = levelArray[0];
	
	if (!levelArray[0])	return;
	var obj1 = levelArray[1];
	
	if (!d.getElementById(obj + '_leftMenu'))	return;
	//alert(obj);
	//if (!getElementByRel('leftNav', obj1))	return;
	
	var menu = d.getElementById(obj + '_leftMenu');
	menu.style.display = 'block';
	
	//var btn1 = d.getElementById(obj1);
	if (!getElementByRel('leftNav', obj1))	return;
	var btn1 = getElementByRel('leftNav', obj1);
	btn1.className += ' active';
	//alert(btn1);
}

function getElementByRel(parent, obj)
{
	var a = d.getElementById(parent).getElementsByTagName('a');
	
	for (var i=0; i<a.length; i++)
	{
		if (a[i].rel == obj)
		{
			return a[i];
		}
	}
}

function genBreadcrumbs()
{
	if (!d.getElementById('breadcrumb'))	return;
	var breadcrumbs = d.getElementById('breadcrumb');
	
	var a = createA('/en/index.html', '_self', 'Home');
	breadcrumbs.appendChild(a);
	
	for (var i=0; i<levelArray.length; i++)
	{
			//alert (levelArray[i]);
		if (!levelArray[i])	break;
		
		var obj = levelArray[i];
		if (d.getElementById(levelArray[i]))
		{			
			var btn = d.getElementById(levelArray[i]);
			//alert(levelArray[i]);
			
		}
		else
		{		//alert(levelArray[i]); 
				var btn = getElementByRel('leftNav', levelArray[i]);
		}
		if (!btn) break;
		var txt = (isIE)	?	btn.innerText	:	btn.textContent;
		var link = btn.href;
		
		breadcrumbs.innerHTML += ' > ';
		
		if (i < levelArray.length-1)
		{
			//var a = createA(getLink(i), '_self', txt);
			var a = createA(link, '_self', txt);
			breadcrumbs.appendChild(a);
			
		}
		else
		{			
			breadcrumbs.innerHTML += txt;
		}
	}
}

function checkURL(array)
{
	var url = window.top.location.href;
	var id;
	var va = [["/", "/"], ["=", ""]];
	
	for (var item=0; item<va.length; item++)
	{
		for (var i=0; i<array.length; i++)
		{
			if (url.indexOf((va[item][0] + array[i] + va[item][1])) >= 0)
			{
				id = array[i];
				break;
			}
		}
	}
	
	return id;
}

/*Change language*/
function changeLanguage(target)
{
	var array = new Array("en", "tc", "sc");
	var nowL = checkURL(array);
	var va = [["/", "/"], ["=", ""]];
	var url = window.top.location.href;
	for (var item=0; item<va.length; item++)
	{
		if (url.indexOf((va[item][0] + nowL + va[item][1])) >= 0)
		{
			url = url.replace((va[item][0] + nowL + va[item][1]), (va[item][0] + target + va[item][1]));
			break;
		}
	}
	if (url.substring((url.length-1), url.length) == '#')	url = url.substring(0, (url.length-1))
	
	window.top.location.href = url;
}

/*Popup a new window*/
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 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 setBtnOver()
{
	var img = d.getElementsByTagName('img');
	
	for (var i=0; i<img.length; i++)
	{
		if (img[i].className.indexOf('btnOver') < 0)	continue;
		if (img[i].src.indexOf('_over') >= 0)			img[i].clicked = true;
		
		img[i].onmouseover = function()
		{
			if (this.clicked)	return;
			var ext = this.src.substring(this.src.length-4, this.src.length);
			this.src = this.src.replace(ext, '_over' + ext);
		}
		
		img[i].onmouseout = function()
		{
			if (this.clicked)	return;
			var ext = this.src.substring(this.src.length-4, this.src.length);
			this.src = this.src.replace('_over' + ext, ext);
		}
	}
}

function setFieldFocus()
{
	var input = d.getElementsByTagName('input');
	
	for (var i=0; i<input.length; i++)
	{
		if (input[i].type != 'text')			continue;
		//if (input[i].className == 'routeField')	continue;
		
		input[i].onfocus = function()
		{
			if (this.value == this.defaultValue)	this.value = '';
		}
		
		input[i].onblur = function()
		{
			if (this.value == '')	this.value = this.defaultValue;
		}
	}
}

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;
}


/*Change font size*/
//Specify affected tags. Add or remove from list:
var tgs = new Array('p', 'h1', 'h2');

//Specify spectrum of different font sizes:
var szs = new Array('84%', '90%', '100%');
var startSz = sz = szs.length;

function getFontSize()
{
	if (getCookie('fontSize') == null)
	{
		startSz = 1;
	}
	else
	{ 
		startSz = getCookie('fontSize');
		if (startSz == "NaN")	startSz = 1;
	}	
	changeFontSize(startSz, true);
}

function changeFontSize(inc, start)
{
	if (!d.getElementById) return;
	var cEl = null,sz = eval(startSz),i,j,cTags;
	
	sz = inc;
	
	cEl = d.getElementsByTagName('body')[0];
	cEl.style.fontSize = szs[ sz ];	
	
	//Highlight Btn
	var fontSizeLink = d.getElementById('fontSizeLink').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);
}

var nd= new Date();
nd.setTime(nd.getTime()+(60*24*60*60*1000));
var cdomain = (location.domain) ? location.domain : null;
var cpath = "/";

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" : "");

  d.cookie = curCookie;
}

function getCookie(name)
{
	if (d.cookie.indexOf(name) < 0)
	{		
		return null;
	}
	else
	{	
		var startStr = d.cookie.indexOf(name) + name.length + 1;
		var endStr = d.cookie.indexOf(";", startStr);
		if (endStr == -1)	endStr = d.cookie.length;
		return unescape(d.cookie.substring(startStr, endStr));
	}
}

function showHideDiv(obj, num)
{
	var div = setChildNodes(d.getElementById(obj), 'DIV');
	
	for (var i=0; i<div.length; i++)
	{
		div[i].style.display = 'none';
	}
	
	div[num].style.display = 'block';
}

function showAll(obj)
{
	var div = setChildNodes(d.getElementById(obj), 'DIV');
	
	for (var i=0; i<div.length; i++)
	{
		div[i].style.display = 'block';
	}
}
function displayDropDownList(id) {
	var btnPull = d.getElementById(id);
	var menuPull = d.getElementById(id+"List");
	
	
	showLayer(1);
	btnPull.onmouseover		= function() {menuPull.hit = true;	showLayer(1);};
	menuPull.onmouseover	= function() {menuPull.hit = true;	showLayer(1);};
	btnPull.onmouseout		= function() {menuPull.hit = false;	showLayer(0);};
	menuPull.onmouseout		= function() {menuPull.hit = false;	showLayer(0);};
	
	function showLayer(over)
	{
		if (over == 1)
		{
			if (btnPull.className.indexOf('active') < 0)	btnPull.className += ' active';
			menuPull.style.display = 'block';
		}
		else
		{
			btnPull.className = btnPull.className.replace('active', '');
			setTimeout(function()
			{
				if (!menuPull.hit)
				{
					menuPull.style.display = 'none';
				}
			}, 10);
		}
	}
}
function showHideObj(obj, self, objType, num)
{
	var child = setChildNodes(d.getElementById(obj), objType);
	var btns = setChildNodes(d.getElementById(self), 'A');
	
	for (var i=0; i<child.length; i++)
	{
		child[i].style.display = 'none';
	}
	
	child[num].style.display = 'block';
	
	for (var i=0; i<btns.length; i++)
	{
		btns[i].className = btns[i].className.replace(/active/, '');
	}
	
	btns[num].className += ' active';
}

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 checkParam(v, id)
{
	if (!GetParam(v))	return;
	var q = GetParam(v) - 1;
	
	showHideDiv(id, q);
}

var setPromotion = 
{
	init:	function (obj, obj2)
	{
		this.leftContainer = d.getElementById(obj);
		this.container = d.getElementById(obj2);
		this.setA(this.leftContainer, this.container);
	},
	
	setA:	function (obj, obj2)
	{
		var leftDiv = setChildNodes (obj, 'A');
		var contentDiv = setChildNodes (obj2, 'DIV');
		
		//alert(leftDiv.length);
		for (var i=0; i<leftDiv.length; i++)
		{
			leftDiv[i].num = i;
			leftDiv[i].div = contentDiv[i];
			leftDiv[i].className = leftDiv[i].className.replace('active', '');
			
			leftDiv[i].onclick = function()
			{
				if (this.className.indexOf('active') >= 0)	return;
				setPromotion.changeA(this, true);
			}
		}
		
		for (var i=0; i<contentDiv.length; i++)
		{
			contentDiv[i].style.display = 'none';
		}
		
		setPromotion.highlightA = leftDiv[0];
		setPromotion.highlightDiv = contentDiv[0];
		
		setPromotion.changeA(leftDiv[0], false);
	},
	
	changeA:	function (obj, fadeIn)
	{
		var highlightA = setPromotion.highlightA;
		highlightA.className = highlightA.className.replace('active', '');
		obj.className += ' active';
		setPromotion.highlightA = obj;
		
		var div = obj.div;
		setPromotion.highlightDiv.style.display = 'none';
		div.style.display = 'block';
		setPromotion.highlightDiv = div;
		
		if (fadeIn)	startFading(div);
		//obj2.style.display = 'block';
	}
}

function emailAFriend()
{
	var emailMsg = new Array();
	emailMsg[0] = "Click the following to access the sent link:%0A%0A";
	emailMsg[1] = "You have received the following link to the China Gas website";
	var winEF = window.open("mailto:?body="+ emailMsg[0] + escape((window.top.location.href).replace("?","!")) + "&subject=" + emailMsg[1]);
}

var setFyYear = 
{
	init:	function (obj, obj2)
	{
		this.pulldown = d.getElementById(obj);
		this.container = d.getElementById(obj2);
		this.setA(this.pulldown, this.container);
	},
	
	setA:	function (obj, obj2)
	{
		var pulldownOption = setChildNodes (obj, 'OPTION');
		this.contentDiv = setChildNodes (obj2, 'DIV');
		
		//alert(leftDiv.length);
		for (var i=0; i<pulldownOption.length; i++)
		{
			pulldownOption[i].value = i;
			//pulldownOption[i].className = pulldownOption[i].className.replace('active', '');
		}
			
		this.pulldown.onchange = function()
		{
			setFyYear.changeA(this.value, true);
		}
		
		setFyYear.changeA(0, false);
	},
	
	changeA:	function (num, fadeIn)
	{
		if (setFyYear.highlightDiv)	setFyYear.highlightDiv.style.display = 'none';
		var div = setFyYear.contentDiv[num];
		div.style.display = 'block';
		setFyYear.highlightDiv = div;
		
		if (fadeIn)	startFading(div);
	}
}

function setGoverLevelSelect(obj, obj2)
{
	var self = this;
	
	this.init = function (obj, obj2)
	{
		this.pulldown = d.getElementById(obj);
		this.container = d.getElementById(obj2);
		this.setA(this.pulldown, this.container);
	};
	
	this.setA = function (obj, obj2)
	{
		var pulldownOption = setChildNodes (obj, 'OPTION');
		this.contentDiv = setChildNodes (obj2, 'DIV');
		
		//alert(leftDiv.length);
		for (var i=0; i<pulldownOption.length; i++)
		{
			pulldownOption[i].value = i;
		}
			
		this.pulldown.onchange = function()
		{
			self.changeA(this.value, true);
		}
		
		self.changeA(0, false);
	};
	
	this.changeA = function (num, fadeIn)
	{
		if (self.highlightDiv)	self.highlightDiv.style.display = 'none';
		var div = self.contentDiv[num];
		div.style.display = 'block';
		self.highlightDiv = div;
		
		if (fadeIn)	startFading(div);
	};
	
	this.init(obj, obj2);
}