var Ajaxifier = new Class({
	
	bkg_path: "./assets/images/visus/",
	
	Implements: [Options, Events],
	options:{
		container:null,
		action: 'rubrique',
		form:false,
		upload_file:false
	},
	
	initialize:function(options) {
		this.setOptions(options);
		debug('Ajaxifier.initialize '+this.options.action);
		
		this.initFxs();
		
		this.initEvents();
		
		if(!this.options.upload_file) this.setRequest();
		
		this.initBkg();
		
	},
	
	initFxs:function() {
		this.move_fx = new Fx.Morph(this.options.container, {
			duration:500,
			link:'cancel',
			transition: Fx.Transitions.Quad.easeOut
		});
				
		this.move_left = {
			'margin-left': -60,
			opacity: 0
		};
		this.move_right = {
			'margin-left': 60,
			opacity: 0
		};
		this.move_center = {
			'margin-left': 0,
			opacity: 1
		};
		
		this.bound_move_left_complete = this.moveLeftComplete.bind(this);
		this.bound_move_right_complete = this.moveRightComplete.bind(this);
		this.bound_open_complete = this.openComplete.bind(this);
		
		if(this.options.upload_file) this.bound_upload = this.doRequestUpload.bind(this);
	},
	
	initEvents:function() {
		this.bound_goto = this.goto.bind(this);
		this.addEvent('goto', this.bound_goto);
	},
	
	initBkg:function() {
		this.bkg = $('image_bg');
		
		this.bkg_fx = new Fxable({
			target: this.bkg,
			'type': 'opacity',
			'dest':0,
			'src':1
		});
		
		this.bound_bkg_add = this.bkgAddComplete.bind(this);
		this.bound_bkg_remove = this.bkgRemoveComplete.bind(this);
		
	},
	
	goto:function(e) {
		this.gets = new Object();
		for(var k in e) {
			if(k != 'index' || k != 'old_index') {
				eval('this.gets.'+k+' = '+e[k]);
			}
		}
		debug('Ajaxifier:goto');
		if(e.old_index == null) this.is_before = true;
		else this.is_before = e.index > e.old_index ? false : true;
		this.close();
		this.removeBkg();
	},
	
	setRequest:function() {
		this.request = new Request.HTML({
			method: 'post',
			evalScripts: true,
			evalResponse: true,
			url: './cmn/request.php',
			update: this.options.container
		});
		this.request.addEvent('success', this.requestComplete.bind(this));
	},
	
	doRequest:function() {
		debug('Ajaxyfy:doRequest');
		this.fireEvent('request:start');
		this.removeEvent('goto', this.bound_goto);
		if(!this.options.upload_file) {
			this.setDatas();
			this.request.send();
		}
		else {
			document.form_contact.submit();
			$('myFrame').addEvent('load', this.bound_upload);
			
		}
	},
	
	doRequestUpload:function() {
		$('myFrame').removeEvent('load', this.bound_upload);
		this.request = new Request.HTML({
			method: 'post',
			evalScripts: true,
			url: './cmn/request.php',
			update: this.options.container,
			data:"action=iframe&ajax=1&iframe=1&lang="+LANG
		});
		this.request.addEvent('success', this.requestComplete.bind(this));
		this.request.send();
	},
	
	setDatas:function() {
		var datas = "";
		if(!this.options.form) {
			datas = 'action='+this.options.action+"&lang="+LANG+"&ajax=1";
			for(var k in this.gets) {
				datas += "&"+k+"="+this.gets[k];
			}
		} 
		else {
			datas = 'action='+this.options.action+"&ajax=1";
			this.options.container.getElements('.uri').each(function(input) {
				datas +="&";
				switch(input.get('type')) {
					default:
						datas += input.get('name')+"="+input.get('value');
					break;
				}
			});
		}
		this.request.options.data = datas;
	},
	
	requestComplete:function() {
		debug('Ajaxify:requestComplete');
		this.fireEvent('request:complete');
		this.addEvent('goto', this.bound_goto);
		app.initOutils();
		app.initAlbum();
		app.initRecrutementAnnonce();
		app.initRecrutementForm();
		app.initContactForm();
    ScrollBar.init();
		this.open();
	},
	
	close:function() {
		if(this.is_before) this.goRight();
		else this.goLeft();
	},
	
	open:function() {
		if(this.is_before) this.setLeft();
		else this.setRight();
		this.goCenter();
	},
	
	goLeft:function() {
		this.goMove(this.move_left, this.bound_move_left_complete);
	},
	
	setLeft:function() {
		this.setMove(this.move_left);
	},
	
	goRight:function() {
		this.goMove(this.move_right, this.bound_move_right_complete);
	},
	
	setRight:function() {
		this.setMove(this.move_right);
	},
	
	goCenter:function() {
		this.goMove(this.move_center, this.bound_open_complete);
	},
	
	setCenter:function() {
		this.setMove(this.move_center);
	},
	
	goMove:function(obj, bound) {
		this.addMoveComplete(bound);
		this.move_fx.start(obj);
	},
	
	setMove:function(obj) {
		this.move_fx.set(obj);
	},
	
	moveLeftComplete:function() {
		this.removeMoveComplete(this.bound_move_left_complete);
		this.closeComplete();
	},
	
	moveRightComplete:function() {
		this.removeMoveComplete(this.bound_move_right_complete);
		this.closeComplete();
	},
	
	openComplete:function() {
		this.removeMoveComplete(this.bound_open_complete);
	},
	
	closeComplete:function() {
		this.doRequest();
	},
	
	addMoveComplete:function(bound) {
		this.move_fx.addEvent('complete', bound);
	},
	
	removeMoveComplete:function(bound) {
		this.move_fx.removeEvent('complete', bound);
	},
	
	addBkg:function() {
		this.bkg_fx.addEvent('complete', this.bound_bkg_add);
		this.bkg_fx.open();
	},
	
	removeBkg:function() {
		var paths = this.bkg.getStyle('background-image').match(/([^\/\\]+\.\w+)/);
		var old_imagename = paths[1];
		this.imagename = BKG_FILES[this.gets.rub] ? BKG_FILES[this.gets.rub] : "simple.jpg";
		
		if(old_imagename != this.imagename) {
			this.bkg_fx.addEvent('complete', this.bound_bkg_remove);
			this.bkg_fx.open();
		}
	},
	
	bkgAddComplete:function() {
		this.bkg_fx.removeEvent('complete', this.bound_bkg_add);
		this.bkg.setStyle('background-image', 'url('+this.bkg_path+this.options.imagename+')');
		this.bkg_fx.close();
	},
	
	bkgRemoveComplete:function() {
		this.bkg_fx.removeEvent('complete', this.bound_bkg_remove);
		this.bkg.setStyle('background-image', 'url('+this.bkg_path+this.imagename+')');
		this.bkg_fx.close();
	}
	
});
