var Navigator = new Class({   
	
	initialize: function(menu, historyManager, morphList) {
		var that = this;
		this.historyManager = historyManager;
		this.morphList = morphList;
		this.spinner = new Spinner();
		this.menu = $(menu);
		this.menuitems = this.menu.getChildren();
		this.menuitems.addEvent('click', function(ev){ that.click(ev, this) });       
		this.historyKey = "go";
		this.title = "Rubikon Quartet";
		this.history = this.historyManager.register( this.historyKey,	[0],	function(values) {that.to(parseInt(values[0]));	},		function(values) {return [that.historyKey, '(', values[0], ')'].join('');	}, this.historyKey + '\\((\\d+)\\)');
	},          

	click: function(ev, item) {
		ev.stop();
		this.page = item.getParent().getChildren().indexOf(item);
		this.history.setValue(0, this.page);		
		this.navigate(item);
	},
	
	to: function(index) {
		if (!this.page && index == 0) return; // Don't run on direct root access
		
		var item = this.menuitems[index];
		this.morphList.morphTo(item);
		this.morphList.setCurrent(item);
		this.navigate(item);
	},
	
	navigate: function(item) {
		$$('title').set('text', this.title + " ~ " + item.getChildren().get('href'));	
		
		new Request.HTML({
			url: item.getChildren().get('href')+'&ajaxload=1',
			update: $('content'),
			useSpinner: true,
			spinnerTarget: 'container'
		}).send();
	}

});
