var Page = new Class({
	Implements: [Options, Events],
	options: {
		navId: 'mainNav'
	},
	initialize: function(selector, options) {
		this.setOptions(options);
		this.items = $$(selector);
		this.selector = selector;
		this.busy = false;
		this.slide = new Fx.Slide('horizontal_slide', {mode: 'horizontal'});
		this.checkCurrent();
		this.activateElements();
	},
	checkCurrent: function() {
		var href = top.location.href;
		var pos = href.indexOf('#') + 1;
		var page = (pos) ? href.substr(pos) : '';
		if(page)
		{
			this.items.each(function(el) {
				if( el.getProperty('rel') == '/'+page )
					this.showPage(page);
			}.bind(this));
		}
		else
		{
			this.showPage('home');
		}
	},
	showPage: function(page) {
		var myChain = this.slide.slideOut();
		
		var Req = new Request({
			url: "ajax/pageRequest.php?page="+page,
			evalScripts: true,
			onSuccess: function(resp){
				myChain.chain(function() { $('content').set('html',resp); this.slide.slideIn(); }.bind(this)).chain(function() { this.renewLinks(); }.bind(this));
				
				$$('#'+this.options.navId+' a').each(function(el) {
					el.removeClass('current');
					
					if(el.getProperty('rel') == '/'+page)
						el.addClass('current');
				});
			}.bind(this)
		}).send();
	},
	renewLinks: function() {
		this.items = $$(this.selector);
		this.busy = false;
		this.activateElements();
	},
	activateElements: function() {
		this.items.each(function(el) {
			el.removeEvents();
			el.addEvent('click', function(ev) {
				new Event(ev).stop();
				if (!this.busy)
				{
					this.busy = true;
					this.followLink(el);
				}
			}.bind(this));
		}.bind(this));
	},
	followLink: function(el) {
			var page = el.getProperty('rel').replace('/','');
			top.location.href = '#'+page;

			this.showPage(page);
	}
});