PageManager = Class.create({
	initialize: function (customHandlers) {
		this.menuItems = [];
		this.customHandlers = customHandlers;
		Event.observe (window, 'load', this.load.bindAsEventListener(this));
	},
	
	load: function (event) {
		this.menu = $('menu');
		this.main = $('main');
		this.menu.select ('a').each (function (a, i) {
			var isCurrent = a.up ('li').hasClassName('current');
			
			doDefault = false;
			if (this.customHandlers) {
				if (this.customHandlers[i]=='default') {
					var doDefault = true;
				}
			}
			
			this.menuItems.push (new MenuItem(this, isCurrent, a, i, doDefault));
		}, this);
	},
	
	loadUrl: function (url) {
		new Ajax.Request(url, {
			method: 'get',
			parameters: {},
			onSuccess: this.onContentLoaded.bind(this),
			onFailure: this.requestFailed.bind(this)
		});
	},
	
	unsetCurrent: function () {
		this.menu.select ('a').each (function (a, i) {
			var isCurrent = a.up ('li').hasClassName('current');
			if (isCurrent) {
				a.select('img')[0].src = 'themes/parking/images/menu/'+a.className+'.png';
				a.up ('li').removeClassName('current');
			}
		});
	},
	
	goTo: function (target) {
		target.setCurrent();
		
		if (this.customHandlers) {
			if (this.customHandlers[target.index]) {
				this.customHandlers[target.index] ();
				return;
			}
		}
		
		new Ajax.Request(target.target, {
			method: 'get',
			parameters: {},
			onSuccess: this.onContentLoaded.bind(this),
			onFailure: this.requestFailed.bind(this)
		});
	},
	
	onContentLoaded: function (t) {
		var content = t.responseText;
		this.main.innerHTML = content;
		FLIR.replace('span.fontReplaceBold' , new FLIRStyle({ cFont:'sf_speedwaystar_bold'}) );//mode:'quickeffects', qe_Stroke: '2,006400'
		FLIR.replace('span.fontReplace' , new FLIRStyle({ cFont:'sf_speedwaystar'}) );//mode:'quickeffects', qe_Stroke: '2,006400'
		FLIR.replace('h1.fontReplace' , new FLIRStyle({ cFont:'sf_speedwaystar'}) );//mode:'quickeffects', qe_Stroke: '2,006400'
		var script = this.main.select ("script.load");
		if (script.size()>0) {
			eval (script[0].innerHTML);
		}
	},
	
	requestFailed: function (t) { alert ('Sorry! We failed to request this page! Please come back later!');}
});

MenuItem = Class.create({
	initialize: function (parent, isCurrent, a, i, doDefault) {
		this.parent = parent;
		this.isCurrent = isCurrent;
		this.target = a.href;
		this.obj = a;
		this.index = i;
		this.doDefault = doDefault;
		this.pageType = a.className;
		a.observe ('click', this.onClick.bindAsEventListener(this));
	},
		
	onClick: function (event) {
		if (! this.doDefault) {
			this.obj.blur();
			this.parent.unsetCurrent();
			this.parent.goTo (this);
			event.stop ();
		}
	},
	
	
	setCurrent: function () {
		this.obj.up('li').addClassName ('current');
		this.obj.up('li').select ('span')[0].addClassName ('current');
		this.obj.select('img')[0].src = 'themes/parking/images/menu/'+this.pageType+'_selected.png';
	}
});

window.isLoaded = false;
Event.observe (window, 'load', function () {
	window.isLoaded = true;
});
runWhenLoaded = function (f) {
	if (window.isLoaded) {
		f();
	} else {
		Event.observe (window, 'load', f);
	}
}
