// Drop Down Menu Class

var Menu = new function() {
	var timeout			= 500;
	var closetimer		= null;
	var ddmenuitem		= null;
	var root = 'http://www.csehy.org/';
	
	// json format
	var menu = [
		{"name": "Home", "url": ""},
		{"name": "Student Life", "url": "DisplayMenu.php?menu=Student+Life", 
			"menu": [
			{"name": "Life at Csehy", "url": "StudentLife/LifeAtCsehy.html"},
			{"name": "Typical Daily Schedule", "url": "StudentLife/TypicalDailySchedule.html"},
			{"name": "Counselors", "url": "StudentLife/Counselors.html"},
			{"name": "Chapel Messages", "url": "StudentLife/ChapelMessages.html"},
			{"name": "Music Faculty", "url": "StudentLife/MusicFaculty.html"},
			{"name": "Musical Instruction", "url": "StudentLife/MusicalInstruction.html"},
			{"name": "Camp Photos", "url": "StudentLife/CampPhotos.php"},
			{"name": "T-Shirt Design Contest", "url": "StudentLife/TShirtDesignContestWinner.html"}
		]},
		{"name": "Steps To Get Here", "url": "DisplayMenu.php?menu=Steps+To+Get+Here",
			"menu": [
			{"name": "Register Now", "url": "StepsToGetHere/RegisterNow.html"},
			{"name": "Pay Online", "url": "Store/Store.php"},
			{"name": "Rates", "url": "StepsToGetHere/Rates.html"},
			{"name": "Directions To Csehy", "url": "StepsToGetHere/DirectionsToCsehy.html"},
			{"name": "Share a Ride", "url": "StepsToGetHere/ShareARide.html"},
			{"name": "Parent Accommodations", "url": "StepsToGetHere/ParentAccommodations.html"}
		]},
		{"name": "Alumni", "url": "DisplayMenu.php?menu=Alumni",
			"menu": [
			{"name": "Alumni Listing", "url": "Alumni/AlumniListing.php"},
			{"name": "Countries Represented", "url": "Alumni/CountriesRepresented.html"},
			{"name": "Store", "url": "Store/Store.php"}
		]},
		{"name": "About Us & News", "url": "DisplayMenu.php?menu=About+Us+%26+News",
			"menu": [
			{"name": "Statement Of Faith", "url": "AboutUs/StatementOfFaith.html"},
			{"name": "Non-discrimination Policy", "url": "AboutUs/Non-discriminationPolicy.html"},
			{"name": "From The Director", "url": "AboutUs/FromTheDirector.html"},
			{"name": "From The Artistic Director", "url": "AboutUs/FromTheArtisticDirector.html"},
			{"name": "Board of Directors", "url": "AboutUs/BoardOfDirectors.html"},
			{"name": "Upcoming Events", "url": "News/UpcomingEvents.html"},
			{"name": "Newsletter", "url": "News/Newsletter.html"},
			{"name": "2010 Brochure", "url": "AboutUs/2010Brochure.html"},
			{"name": "Merchandise", "url": "Store/Store.php"},
			{"name": "Resources", "url": "AboutUs/Resources.html"},
			{"name": "Contact Us", "url": "AboutUs/ContactUs.html"}
		]}
	];
	
	// open hidden layer
	this.expand = function(el) {	
		// cancel close timer
		Menu.cancelCloseTime();
	
		// close old layer
		if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
	
		// get new layer and show it
		ddmenuitem = el;
		ddmenuitem.style.visibility = 'visible';
	}
	
	// close showed layer
	this.hide = function() {
		if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
	}
	
	// go close timer
	this.closeTime = function() {
		closetimer = window.setTimeout(Menu.hide, timeout);
	}
	
	// cancel close timer
	this.cancelCloseTime = function() {
		if(closetimer) {
			window.clearTimeout(closetimer);
			closetimer = null;
		}
	}
	
	this.init = function() {
		var mainList = document.createElement('ul');
		mainList.id = 'sddm';
		for(var i in menu) {
			var li = document.createElement('li');
			var a = document.createElement('a');
			var div = document.createElement('div');
			var id = 'menu' + menu[i].name.replace(/\s/g, '');
			
			li.appendChild(a);
			a.appendChild(document.createTextNode(menu[i].name));
			a.setAttribute('href', ((typeof menu[i].url == 'undefined') ? '#' : root + menu[i].url));
			a.onmouseover = Menu.hide;
			
			if(typeof menu[i].menu != 'undefined') {
				(function(){var el = div; a.onmouseover=function(){Menu.expand(el);}})(); // define a scope for the parameter
				a.onmouseout = Menu.closeTime;
				
				div.id = id;
				div.onmouseover = Menu.cancelCloseTime;
				div.onmouseout = Menu.closeTime;
			
				for(var j in menu[i].menu) {
					var a = document.createElement('a');
					a.setAttribute('href', (typeof menu[i].menu[j].url == 'undefined') ? '#' : root +  menu[i].menu[j].url);
					a.appendChild(document.createTextNode(menu[i].menu[j].name));
					div.appendChild(a);
				}
				li.appendChild(div);
			}
			mainList.appendChild(li);
			
			document.onclick = Menu.hide; // close layer when click-out
		}
		
		document.getElementById('mainMenu').appendChild(mainList);
	}
	
	this.getSubMenu = function() {
		return menu;
	}
}