// minor setting variables
var pageExtension = ".cfm";
var homepageurl = "index.cfm";


// method for dynamic menu generation

function menuItem(name, subItems) {
	this.name = name;
	this.subItems = subItems;
}

var menuItems = [];
menuItems[0] = new menuItem("about",["board","history","strategic","corporate"]);
menuItems[1] = new menuItem("programs",["cesep","mentorship"]);
menuItems[2] = new menuItem("sports",null);
menuItems[3] = new menuItem("grants",["who","how"]);
menuItems[4] = new menuItem("ambassadors",null);
menuItems[5] = new menuItem("culture",null);
menuItems[6] = new menuItem("news",null);
menuItems[7] = new menuItem("get",null);
menuItems[8] = new menuItem("contact",null);
menuItems[9] = new menuItem("blog",null);

// get the current page name (used to tailor showing of submenu items)
var sPath = window.location.pathname;
//var sPage = sPath.substring(sPath.lastIndexOf('\\') + 1);
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1).split(".")[0];

// find the currentSection according to page name
function getCurrentSection() {
	for (var i=0;i<menuItems.length;i++) {
		if (menuItems[i].name == sPage) {
			return menuItems[i];
		}
		if (menuItems[i].subItems) {
			for (var j=0;j<menuItems[i].subItems.length;j++) {
				if (menuItems[i].subItems[j] == sPage) {
					return menuItems[i];
				}
			}
		}
	}
	return null;
}

var currentSection = getCurrentSection();


function setupPage() {

	// set the home button url
	document.getElementById("home_button").href = homepageurl;
	
	// set athelete image
	if (currentSection != null) {
		document.getElementById("athelete").style.backgroundImage = "url(images/athelete_" + currentSection.name + ".png)";
	}
	
	// set header image
	if (document.getElementById("content_header")) {
		document.getElementById("content_header").style.backgroundImage = "url(images/header_" + sPage + ".png)";
	}
	
	for (var i=0;i<menuItems.length;i++) {
	
		if (menuItems[i].name == "news") {
			document.getElementById("menu").appendChild(document.createElement("P"));
		}
	
		var tempHeader = document.createElement("IMG");
		tempHeader.id = menuItems[i].name;
		tempHeader.className = "menuHeader";
		tempHeader.onclick = function() {
			location.href = this.id + ".cfm";
		}
		tempHeader.onmouseover = function() {
			this.src = this.oversrc;
		}
		tempHeader.onmouseout = function() {
			this.src = this.origsrc;
		}
		
		if (menuItems[i] == currentSection) {
			tempHeader.src = "images/" + menuItems[i].name + "_on.png";
			tempHeader.origsrc = tempHeader.src;
			tempHeader.oversrc = "images/" + menuItems[i].name + "_hov.png";
	
			document.getElementById("menu").appendChild(tempHeader);
			
			// append the subsection items
			if (menuItems[i].subItems) {
				for (var j=0;j<menuItems[i].subItems.length;j++) {
					var tempItem = document.createElement("IMG");
					tempItem.id = menuItems[i].subItems[j];
					if (menuItems[i].subItems[j] == sPage) {
						tempItem.className = "current";
					}
					tempItem.src = (menuItems[i].subItems[j] == sPage)?"images/" + menuItems[i].subItems[j] + "_hov.png":"images/" + menuItems[i].subItems[j] + "_on.png";
					tempItem.origsrc = tempItem.src;
					tempItem.oversrc = "images/" + menuItems[i].subItems[j] + "_hov.png";
					
	
					tempItem.onclick = function() {
						location.href = this.id + ".cfm";
					}
					tempItem.onmouseover = function() {
						this.src = this.oversrc;
					}
					tempItem.onmouseout = function() {
						this.src = this.origsrc;
					}
					
					document.getElementById("menu").appendChild(tempItem);
				}
			}
		} else {
			tempHeader.src = "images/" + menuItems[i].name + "_off.png";
			tempHeader.origsrc = "images/" + menuItems[i].name + "_off.png";
			tempHeader.oversrc = "images/" + menuItems[i].name + "_hov.png";
			document.getElementById("menu").appendChild(tempHeader);
		}
	}
}