var AjaxManager = new Class({
	
	request_path: ROOT_URL+'cmn/request.php',
	
	Implements: [Options, Events],
	
	initialize: function(options) {
		this.setOptions(options);

		this.initFx();
		
		this.loadSessionInfos();
	},
	
	initFx: function() {
		this.content_fx = new Fxable({
			target: $('content'),
			type: 'opacity',
			src: 0,
			dest: 1,
			duration:300
		});
		
		this.bound_loadpage_close = this.loadPageCloseComplete.bind(this);
	},
	
	loadSessionInfos: function() {
		var request = new Request.JSON({
			url: this.request_path,
			onSuccess:function(responseJSON, responseText) {
				this.loadInfosComplete(responseJSON);
			}.bind(this)
		});
		var datas = "action=session";
		request.post(datas);
	},
	
	loadInfosComplete: function(responseJSON) {
		session_infos = responseJSON;
		this.fireEvent('infosloaded');
	},
	
	update: function(uri) {
		var request = new Request.JSON({
			url: this.request_path
		});
		var datas = uri+"&action=update";
		request.post(datas);
	},
	
	loadContact:function() {
		var uri = 'action=contact';
		var form = $$('.form')[0];
		if(form) {
			form.getElements('.uri').each(function(input) {
				uri += '&';
				switch(input.get('type')) {
					default:
						uri += input.get('name')+'='+input.get('value');
				}
			}, this);
		}
		this.loadPage(uri);
	},
	
	loadPage: function(uri) {
		this.uri = !uri ? 'init' : uri;
		this.content_fx.addEvent('complete', this.bound_loadpage_close);
		this.content_fx.close();
	},
	
	loadPageCloseComplete: function() {
		this.content_fx.removeEvent('complete', this.bound_loadpage_close);
		var request = new Request.HTML({
			url: this.request_path,
			update: $('content'),
			onSuccess:function(responseTree, responseElements, responseHTML, responseJavaScript) {
				this.loadPageComplete();
			}.bind(this)
		});
		var action = this.uri.search('action') < 0 ? "&action=page" : "";
		var datas = this.uri+action+"&ajax=1";
		request.post(datas);
	},
	
	loadPageComplete: function() {
		this.content_fx.open();
		this.fireEvent('pageloaded');
	}
});
