/**
 * Menu
 * 
 * Adds a sliding menu to the top of the page.
 *
 * Usage:
 *
 *	window.addEvent('domready', function() {
 *		new Menu();
 *	});
 *
 * @license	MIT License
 * @author	stensi
 * @link	http://stensi.com
 */
var Menu = new Class({
 
	// implements
	Implements: [Options],
 
	// options
	options: {
		basepath: '',
		pagespath: '',
		last: false
	},
	
	menu: [		
		[
			{ url: "../index", name: "User Guide Home" },
			{ url: "toc", name: "Table of Contents Page" },		
					"Installation",
						
			{ url: "downloading-shortie", name: "Downloading Shortie" },
						
			{ url: "installation-instructions", name: "Installation Instructions" },
								"Shortie",
						
			{ url: "user-guide-home", name: "User Guide Home" },
							],
				[
					"Basic Info",
						
			{ url: "change-log", name: "Change Log" },
						
			{ url: "licence-agreement", name: "Licence Agreement" },
							],
				[
					"General Topics",
						
			{ url: "shorten-url", name: "Shorten URL" },
							],
				[
				],
	],
	
	makeMenu: function() {
		var m = '<table cellpadding="0" cellspaceing="0" border="0" style="width:98%"><tr>';
		var listStarted, item;
		for(var i=0; i<this.menu.length; i++) {
			m += '<td valign="top">';
			
			listStarted = false;
			for(var j=0; j<this.menu[i].length; j++) {
				item = this.menu[i][j];
				if(typeof item == "string") {
					if(listStarted) {
						m += '</ul>'
						listStarted = false;
					}
					m += '<h3>'+item+'</h3>';
				} else {
					if(!listStarted) {
						listStarted = true;
						m += '<ul>';
					}
					m += '<li><a href="' + this.options.pagespath + item.url + '.html">'+item.name +'</a></li>';
				}
			}
			if(listStarted) {
				m += '</ul>';
			}
			
			m += '</td>';
		}
		m += '</table>';
		return m;
	},
 
	/**
	 * Constructur - Class.initialize
	 * 
	 * @param	object
	 * @return	void
	 */
	initialize: function(options) {
 
		this.setOptions(options);
 
 
		var mySlide = new Fx.Slide('nav_inner').hide();
 
		$('nav_toggle').addEvent('click', function() {
			mySlide.toggle();
		});
 
		var menu = this.makeMenu();
		$('nav_inner').set('html', menu );
		
		var el = $('toc_placeholder');
		if(el) {
			el.set('html', menu);
		}
		
		var url = document.location.pathname.replace(/^.*\/([\w]+)\.html$/, "$1");
		
		var prev = $('footer_previous');
		if(url && prev) {
			var next = $('footer_next');
			var lastItem = null;
			var found = false;
			var nextItem = null;
			for (var i = 0; i < this.menu.length; i++) {
				if(nextItem !== null) {
					break;
				}
				for (var j = 0; j < this.menu[i].length; j++) {
					var item = this.menu[i][j];
					if(typeof item == "string" || item.url == "../index" || item.url == "toc") {
						continue;
					}
					if(found) {
						nextItem = item;
						break;
					}
					if(item.url == url) {
						found = true;
					} else {
						lastItem = item;
					}
				}
			}
			if(this.options.last) {
				found = false;
				for (var i = 0; i < this.menu.length; i++) {
					for (var j = 0; j < this.menu[i].length; j++) {
						var item = this.menu[i][j];
						if(typeof item !== "string" && item.url == this.options.last ) {
							found = true;
							lastItem = item;
							break;
						}
					}
					if(found) {
						break;
					}
				}
			}
			if(found && lastItem !== null) {
				prev = prev.getFirst();
				prev.set('html', lastItem.name);
				prev.set('href', this.options.pagespath + lastItem.url + ".html");
			} else {
				prev.setStyle('display', 'none');
			}
			if(nextItem !== null) {
				next = next.getFirst();
				next.set('html', nextItem.name);
				next.set('href', this.options.pagespath + nextItem.url + ".html");
			} else {
				next.setStyle('display', 'none');
			}
		}
		
	}
});
