document.observe('dom:loaded', function() { PageController.init(); });

PageController = {
	
	baseURL: null,
	currentSection: null,
	
	view: null,
	subNavigationView: null,
	contentView: null,
	
	init: function() {
		this.baseURL = window.location.toString().gsub(/#.*/, '');
		this.baseURL = this.baseURL.gsub(/index.html/, '');
		
		this.view = $('page_controller_content');
		this.subNavigationView = this.view.down('.sub_navigation');
		this.contentView = this.view.down('.content_box');
	},
	
	show_page_with_section: function(params) {
		var that = this;
		
		if(this.currentSection != params.section) {
			document.body.className = params.section;
			
			new Ajax.Request(this.baseURL + 'pages/' + params.section + '/sub_navigation.html', {
				method: 'get',
				onComplete: function(transport) {
					that.subNavigationView.update(transport.responseText);
					
					// For some reason IE 8 insists on prefixing the links with the baseURL. This should fix that...
					if(Prototype.Browser.IE) {
						that.subNavigationView.select('a').each(function(element) {
							element.href = element.href.replace(that.baseURL, '');
						});
					}
					
					that.subNavigationView.down('a[href="' + params.route.request + '"]').addClassName('selected');
				}
			});
		}
		params.element.addClassName('selected');
		params.element.siblings().invoke('removeClassName', 'selected');
		
		new Ajax.Request(this.baseURL + params.route.request, {
			method: 'get',
			onComplete: function(transport) {
				that.contentView.update(transport.responseText + "<br /><br /><br />");
				that.contentView.scrollTop = 0;
			}
		});
		
		this.currentSection = params.section;
	}
};

