// heirarchical nav menus
var globalnav = {
	name: "",
	isloaded: false,
	menus: [], // top-level array for each menu group that holds config/style & content arrays (ie. menus[1][0] holds the array with definitions for the 1st menu group, the [0] in menus[1][0] is literally the value that holds the array, while the [1] is the number of the menu - starts at 1 instead of 0, menus[2][1] looks like it would hold the style definition (bgcolor, etc...) for menu group #2, menu item #1
	count: 0,
	activeMenu: null, // visible menu group/containers
	timer: 0,
	timerS: 0, // another timer value; reset? start? setTimer?
	hideTimeout: 200,
	
	init: function(args) {
		this.count = this.menus.length;
		this.name = args.name;
		this.build(args);
	},
	
	add: function(args) {		
		// these properties mostly refer to the whole menu group (item styles further down page)
		this.menus[this.count] = args;
		this.count++;
		console.log("count = "+this.count);
	},
	
	getMenuNum: function(menuId) {
		for (var i=0; i <= this.count; i++){
			if (menuId == this.menus[i].id) break;
		}
		return i;
	},

	build: function(args) {	
		for (var j=0; j<this.count; j++) {
			this.menus[j].displayNodeId = this.name + "_module_" + this.menus[j].id;
			this.menus[j].menuAttachNodeId = this.name + "_" + this.menus[j].id;
			this.menus[j].hdAttachNodeId = this.name + "_hd_on_" + this.menus[j].id;
			var m = this.menus[j];
			var hasMenu = (m.items ? true : false); // true: top link has drop-down menu
			
			// item/link list
			if (hasMenu) {
				var ms = this.buildItems(m, args);
				var containerId = "nav_container_" + m.id;
		
				// items/container
				/*
				var mnode = document.createElement("div");
				mnode.id = this.name + "_" + m.id;
				mnode.className = args.menuClass;
				mnode.style.left = m.left + "px"; 
				*/
				var containerNode = getById(containerId);
				if (containerNode) {
					containerNode.innerHTML = ms;
				}	
			}
			
			this.attachHandlers(m, hasMenu);
			
			if (j == (this.count - 1)){
				var self = this;
				//var setLoadedState = function() {
					self.isloaded = true;
				//}
				//setTimeout(setLoadedState, 200);
			}
		}
	},

	buildItems: function(menu, args) {
		var ms = "";
		var ls = "";
		for(var i=0; i<menu.items.length; i++) {
			var item = menu.items[i];
			ls += "<a href='" + docroot + item.url + "'><div>" + item.title + "</div></a>";
		}
		ms += "<div id='" + menu.displayNodeId +"' class='nav_menu_module'>"
		+ "<a href=" + docroot + menu.url + "><div class='nav_hd_on' id='" + menu.hdAttachNodeId + "'><img src='" + docroot + "images/nbug_on.gif' alt='' class='nav_arrow' height='10' width='8'>" + menu.title + "</div></a>"
		+ "<div class='nav_menu' id='" + menu.menuAttachNodeId +"'>"
		+  ls
		+ "</div></div>";
		return ms;
	},
	
	attachHandlers: function(menu, hasMenu) {
		var self = this;
						
		// top nav hd/links
		var mlink = getById("nav_hd_" + menu.id);
		if (mlink) {
			if (hasMenu) {
				var mlinkOn = getById(menu.hdAttachNodeId);
				// show the drop-down for either the default link, or overlay version
				mlink.onmouseover = mlinkOn.onmouseover = function(e) {
					self.show(menu, e);
				}
				mlink.onmouseout = mlinkOn.onmouseout = function() {
					self.startHide();
				}
			} else {
				// just swap the arrow
				var mimg = "navimg_" + menu.id;
				mlink.onmouseover = function(e) {
					self.swapArrow(mimg, 1);
				}
				mlink.onmouseout = function() {
					self.swapArrow(mimg);
				}
			}
		}
		
		if (hasMenu) {
			// drop-down menu (not including hd/link)	
			var mnode = getById(menu.menuAttachNodeId); 
			if (mnode) {
				mnode.onmouseover = function() {
					self.keepMenu();
				}
				mnode.onmouseout = function() {
					self.startHide();
				}
			}
		}
	},
	
	
	// Functions for showing/hiding items

	show: function(menu, e){
		if (!this.isloaded) return;
		
		clearTimeout(this.timer);
		if(this.timerS){
			clearTimeout(this.timerS);
			this.timerS = 0;
		}
				
		var o, s;
		o = getById(menu.displayNodeId);
		s = o.style;
		if (s.visibility.toLowerCase() == "visible") return;
		
		this.finishHide();
		this.activeMenu = o;

		s.visibility = "visible";		
	},

	keepMenu: function() {
		clearTimeout(this.timer);
	},
	
	startHide: function() {
		if (!this.isloaded) return;
		if (!browser.KON) this.keepMenu();
		var self = this;
		var hide = function() {
			self.finishHide();
		}
		this.timer = setTimeout(hide, this.hideTimeout);
	},
	
	finishHide: function() {
		if(this.timerS){
			clearTimeout(this.timerS);
			this.timerS = 0;
		}
		if (this.activeMenu) {
			this.activeMenu.style.visibility = "hidden";
		}
		this.aactiveMenu = null;
		//aActiveItem=[""];
	},

	swapArrow: function (imgObj,imgState) {	
		if(document.images){
			if (imgState == 1) {
				state = "_on";
			} else {
				state = "";
			}
			document.images[imgObj].src = "" + docroot + "images/nbug" + state + ".gif";
		}
	}
}

globalnav.menus = [
	{
		id: 'products',
		url: 'services/',
		title: 'Products and Services',
		items: [
			{url: 'services/products.html', title: 'Products'},
			{url: 'services/services.html', title: 'Services'},
			{url: 'services/tys.html', title: 'TYS School Behavioral Program'},
			{url: 'services/products.html#PLSI', title: 'Paragon Learning Style Inventory'}
		]
	},
	{
		id: 'assessment',
		url: 'assessment/',
		title: 'Assessment',
		items: [
			{url: 'assessment/#system_comparison', title: 'Assessment System'},
			{url: 'assessment/scai_protocol.html', title: 'Assessment Protocol'},
			{url: 'assessment/school_survey.html', title: 'School Climate Inventory'},
			{url: 'assessment/classroom_survey.html', title: "Classroom Climate Inventory"}
		]
	},
	{
		id: 'research',
		url: 'research/',
		title: 'Research',
		items: [
			{url: 'research/#assc_research', title: 'ASSC Research'},
			{url: 'research/reliability_validity.html', title: 'Reliability and Validity'},
			{url: 'research/#climate_research', title: 'School Climate Research'}
		]
	},
	{
		id: 'resources',
		url: 'resources/',
		title: 'Resources/AERA SIG',
		items: [
			{url: 'resources/change_from_inside.html', title: 'Change from the Inside'},
			{url: 'resources/#school_climate_resources', title: 'School Climate Resources'},
			{url: 'resources/#super_report', title: "CA Superintendent's Report"}
			/*{url: 'resources/', title: 'AERA SIG'} */
		]
	},
	{
		id: 'assc',
		url: 'assc/',
		title: 'ASSC',
		items: [
			{url: 'assc/', title: 'About us'},
			{url: 'assc/contact.html', title: 'Contact us'},
			{url: 'assc/team.html', title: 'Our Team'},
			{url: 'assc/mission.html', title: 'Mission'}
		]
	},
	{
		id: 'contact'
	}
];

/*
Products and Services
- Products
- Services
- TYS School Behavioral Program
- Paragon Learning Style Inventory
 
Assessment
- ASSC Assessment System
- School Climate Inventory
- Classroom Climate Inventory
 
Research
- ASSC Research
- Reliability and Validity 
- School Climate Research
 
Resources/AERA SIG
- Change from the Inside
- School Climate Resources
- CA Superintendent's Report
- AERA SIG

ASSC
- About us
- Contact us
- Our Team
- Mission

Contact us (stand alone and also in the ASSC Section too)
*/