// JavaScript Document
var gSidebarNav = null;
var gNavLinks = [
	{label:"Home",label_cn:"首页",link:"home.html"},
	{label:"Who are we?",label_cn:"我们是谁？",link:"who.html"
	  ,children:[
		  {label:"The working team",label_cn:"工作团队",link:"team.html"}					 
	  ]
	},
	{label:"What is our plan?",label_cn:"我们的计划是什么？",link:"plan.html"},
	{label:"Why donate?",label_cn:"为什么捐款？",link:"why.html"},
	{label:"How do we monitor?",label_cn:"我们如何监督？",link:"monitor.html"},
	{label:"How to donate?",label_cn:"如何捐款？",link:"donate.html"},
	{label:"How to reach us?",label_cn:"如何联络我们？",link:"contact_us.html"},
	{label:"What's new?",label_cn:"进度更新",link:"news.html"
	  ,children:[
			{label:"Fundraising progress",label_cn:"筹款进度",link:"progress_fundraising.html"},
			{label:"School construction progress",label_cn:"学校建设进度",link:"progress_construction.html"},
			{label:"Reports and photos",label_cn:"进度汇报和图片",link:"progress_reports.html"}
		]
	}
];

function SidebarNav(parentNodeName) {
  this.currentPage = this.getFilenameFromPathname(document.location.pathname);	
	this.parentNodeName = parentNodeName;
	this.parentNode = window.document.getElementById(this.parentNodeName);
}

SidebarNav.prototype.IS_CURRENT_PAGE = true;

SidebarNav.prototype.getCurrentPage = function() {
	return this.currentPage;
}

SidebarNav.prototype.getFilenameFromPathname = function(pathname) {
  if (typeof pathname == "undefined" || pathname == null) return "";
	return pathname.substring(pathname.lastIndexOf('/')+1); 
}

SidebarNav.prototype.buildDom = function() {
	for (i=0; i < gNavLinks.length; i++) {
		var navElem = gNavLinks[i];
		var div = null;
		var styleClass = "sbh1";
		var label = navElem.label;
		var label_cn = navElem.label_cn;
		var link = navElem.link;
		var isCurrentPage = (this.currentPage == link);
		div = this.createNavNode(styleClass, label, label_cn, link, isCurrentPage);
		this.parentNode.appendChild(div);
		if (navElem.children) {
			styleClass = "sbh2";
			for (j = 0; j < navElem.children.length; j++ ) {
				label = navElem.children[j].label;
				label_cn = navElem.children[j].label_cn;
				link = navElem.children[j].link;
				isCurrentPage = (this.currentPage == link);
				div = this.createNavNode(styleClass, label, label_cn, link, isCurrentPage);
			  this.parentNode.appendChild(div);
			}
		}
  }
}

SidebarNav.prototype.createNavNode = function(styleClass, label, label_cn, link, isCurrentPage) {
	var divNode = document.createElement("div");
	var labelNode = document.createTextNode(label);
	var brNode = document.createElement("br");
	var labelCnNode = document.createTextNode(label_cn);
	if (isCurrentPage == this.IS_CURRENT_PAGE) {
		// use class="className1 className2" to overlay different classes
		divNode.setAttribute("class", styleClass + " " + styleClass + "sel"); // this doesn't work for IE but does work for FireFox
	  divNode.setAttribute("className", styleClass + " " + styleClass + "sel"); // this works for IE and FireFox
		divNode.appendChild(labelNode);
		divNode.appendChild(brNode);
		divNode.appendChild(labelCnNode);
	} else {
	  divNode.setAttribute("class", styleClass); // this doesn't work for IE but does work for FireFox
	  divNode.setAttribute("className", styleClass); // this works for IE and FireFox
		var linkNode = document.createElement("a");
	  linkNode.setAttribute("href", link);
	  linkNode.appendChild(labelNode);
	  linkNode.appendChild(brNode);
	  linkNode.appendChild(labelCnNode);
	  divNode.appendChild(linkNode);
	}
	return divNode;  
}

function initSidebarNav() {
	gSidebarNav = new SidebarNav("side_bar");
  //gSidebarNav.getCurrentPage();
	gSidebarNav.buildDom();
}

addEvent(window, 'load', initSidebarNav);
