var isIE = (navigator.appName.indexOf("Microsoft") > -1);
var d = document;

if (!levelArray)	var levelArray = new Array();


function init()
{
	if (levelArray.length == 0)	getLevel();
	
	if (levelArray.length > 0)	highlightTopNav();
	if (levelArray.length > 1)	highlightLeftNav();
	
//	leftNavOver();
	
	/*if (levelArray.length > 3)	highlightContentTab();*/
	if (levelArray.length > 0)	genBreadcrumbs();
	
	//setFieldFocus();
	//setBtnOver();
	getFontSize();
}

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.php')
	{
		levelArray[levelArray.length-1] = obj.replace('.php', '');
	}
	else
	{
		levelArray.pop();
	}
	//alert(levelArray);
}

function highlightTopNav()
{
	var obj = levelArray[0];
	//alert(obj);
	if (!d.getElementById(obj))	return;
	
	var btn = d.getElementById(obj);
	btn.className += ' active';
	
	if (!d.getElementById(obj + '_menu'))	return;
	
	var menu = d.getElementById(obj + '_menu');
	menu.style.display = 'block';
}

function highlightLeftNav()
{
	var obj = levelArray[1];
	
	if (!getElementByRel('leftNav', obj))	return;
	
	var btn = getElementByRel('leftNav', obj);
	btn.parentNode.clicked = true;
	
	if (!d.getElementById(obj + '_menu'))
	{
		btn.className += ' active';
		return;
	}
	else
	{
		btn.parentNode.className += ' active';
	}
	var menu = d.getElementById(obj + '_menu');
	menu.style.display = 'block';
	
	if (!levelArray[2])	return;
	var obj1 = levelArray[2];
	
	if (!getElementByRel('leftNav', obj1))	return;
	var btn1 = getElementByRel('leftNav', obj1);
	btn1.parentNode.className += ' active';
	btn1.parentNode.clicked = true;
}

function leftNavOver()
{
	var leftNav = d.getElementById('leftNav');
	var subNavList = getElementsByClassName(leftNav, 'subNavList');
	
	for (var i=0; i<subNavList.length; i++)
	{
		var li = setChildNodes(subNavList[i], 'LI');
		
		for (var j=0; j<li.length; j++)
		{
			li[j].onmouseover = function()
			{
				if (this.className.indexOf('active') >= 0)	return;
				if (this.clicked)	return;
				this.className += ' active';
			}
			
			li[j].onmouseout = function() 
			{
				if (this.clicked)	return;
				this.className = this.className.replace(/active/, '');
			}
		}
	}
}

function getElementsByClassName(p, c)
{
	var array = new Array();
	var tags = p.getElementsByTagName('*');
	
	for (var i=0; i<tags.length; i++)
	{
		if (!tags[i].className)	continue;
		if (tags[i].className == c)	array.push(tags[i]);
	}
	
	return array;
}

function getElementByRel(parent, obj)
{
	if (!d.getElementById(parent))	return;
	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 str = breadcrumbs.innerHTML;
	breadcrumbs.innerHTML = '';
	
	var a = createA('/chi/index.php', '_self', '首页');
	breadcrumbs.appendChild(a);
	
	for (var i=0; i<levelArray.length; 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;
		}
	}
	
	if (str)	breadcrumbs.innerHTML += str;
	
	/*function getLink(total)
	{
		var link = '/';
		
		for (var i=0; i<=total; i++)
		{
			link += levelArray[i] + '/';
		}
		
		return link;
	}*/
}


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;
}

function genRowOver(name)
{
	var table = d.getElementById(name);
	var rows = table.rows;
	
	for (var i=0; i<rows.length; i++)
	{
		//if (i%2 == 0)			rows[i].className = 'even';
		//if (i == rows.length-1)	rows[i].className += ' last';
		
		rows[i].onmouseover	= function() {this.className += ' active';}
		rows[i].onmouseout	= function() {this.className = this.className.replace('active', '');}
	}
}

//Specify spectrum of different font sizes:
var szs = new Array('0.85em', '1em', '1.2em');
//var szs = new Array('x-small', 'small', 'medium');
//var szs = new Array('1em', '1.05em', '1.10em', '1.15em');
var startSz = sz = 0;

function getFontSize()
{	
	if (getCookie('fontSize') == null)
	{		
		startSz = 0;
	}
	else
	{ 
		startSz = getCookie('fontSize');
		if (startSz == "NaN")	startSz = 0;
	}	
	changeFontSize(startSz, true);
}

function changeFontSize(inc, start)
{
	if (!document.getElementById) return;
	var d = document,cEl = null,sz = eval(startSz),i,j,cTags;
	
	if (!start)
	{
		sz += inc;
		
		if ( sz < 0 ) sz = 0;
		if ( sz > (szs.length-1) ) sz = (szs.length-1);
		startSz = sz;
	}
	else
	{
		sz = inc;
	}
	
	cEl = d.getElementsByTagName('body')[0];
	cEl.style.fontSize = szs[ sz ];	
	setCookie("fontSize", sz, nd, cpath, cdomain);

}

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 = "/";

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;
	}
	else
	{	
		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));
	}
}
/*end of change fontsize*/

/*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 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);
};

