var Application = new Class({
	
	Implements: [Options, Events],
	options:{

	},
	
	initialize:function(options) {
		this.setOptions(options);
		debug('Application:initialize');
		
		this.menu = new Menu({
			container: $('menu')
		});
		
		this.bound_menu_goto = this.menuGoTo.bind(this);
		this.menu.addEvent('Menu:goto', this.bound_menu_goto.bind(this));
		
		if(!is_home) {
			this.initAjaxifier();
			this.initLoader();
			outil_container = new OutilContainer({
				'bkg': $('outil_container')
			});
			this.initOutils();
			this.initRecrutement();
			this.initRecrutementAnnonce();
			this.initRecrutementForm();
			this.initAlbum();
			this.initContact();
			this.initContactForm();
		}
		else {
			//this.initNewsImages();
			this.initMainTitle();
		}
		
		HM.start();
		
		HM.addEvent('hash:changed', function(e) {
			if(e.rub) {
				this.ajaxifier.fireEvent('goto', e);
				this.menu.goSelect(e);
			}
			if(e.emploi) {
				this.ajaxifier_recrut.fireEvent('goto', e);
				$$('.emploi_list a').each(function(el) {
					if(el.getParent().hasClass('select')) el.getParent().removeClass('select');
					var gets = getGets(el.get('href'));
					if(gets.emploi == e.emploi) el.getParent().addClass('select');
				}, this);
			}
		}.bind(this));
		
	},
	
	initAjaxifier:function() {
		
		this.ajaxifier = new Ajaxifier({
			container: $('ajax_cont'),
			action: 'rubrique'
		});
		
		this.ajaxifier.addEvents({
			'request:start': function() {
				loader.open();
			}.bind(this),
			'request:complete': function() {
				loader.close();
			}.bind(this)
		});
		
	},
	
	menuGoTo:function(e) {
		if(album_page) {
			album_page.close();
			album_page = null;
		}
		this.ajaxifier.fireEvent('goto', e);
		HM.set('hash', e);
	},
	
	initLoader:function() {
		loader = new Loader({
			'image': './assets/images/ajax-loader.gif',
			'parent': $('header')
		});
	},
	
	initOutils:function() {
		
		$$('.outil_link').each(function(el) {
			var outil = new Outil({
				target:el,
				id:el.get('href').replace('#', ''),
				parent:outil_container
				
			});
			outil.addEvents({
				'request:start': function() {
					loader.open();
					outil_container.open();
				}.bind(this),
				'request:complete': function() {
					loader.close();
					outil_container.openOutil();
				}.bind(this)
			});
			el.addEvent('click', function(e) {
				e.stop();
			});
		});
	},
	
	initRecrutement:function() {
		if($$('.recrutement').length > 0) {
			this.current_an = null;
			this.ajaxifier_recrut =  new Ajaxifier({
				container: $$('.contact .annonce_cont')[0],
				action: 'recutement_nav'
			});
			
			this.ajaxifier_recrut.addEvents({
				'request:start': function() {
					loader.open();
				}.bind(this),
				'request:complete': function() {
					loader.close();
				}.bind(this)
			});
			
			$$('.emploi_list a').each(function(el, index) {
				if(el.getParent().hasClass('select')) this.current_an = el;
				el.addEvent('click', function(event) {
					event.stop();
					if(this.current_an) this.current_an.getParent().removeClass('select');
					var link = el.get('href');
					var obj = getGets(link);
					this.ajaxifier_recrut.fireEvent('goto', obj);
					HM.set('hash', obj);
					this.current_an = el;
					this.current_an.getParent().addClass('select');
				}.bind(this));
			}, this);
			
			var my_frame = new IFrame({
				'name': 'myFrame',
				'id': 'myFrame',
				styles	: { 
					'position'	: 'absolute',
					'top'		: 0,
					'left'		: 0,
					'width'		: '1px', 
					'height'	: '1px',
					'visibility': 'hidden'
				}
			});
	
			my_frame.inject($$('.recrutement')[0]);
		}
	},
	
	initRecrutementAnnonce:function() {
		if($$('.recrutement').length > 0) {
			
			ajaxifier_contact = new Ajaxifier({
				container: $$('.contact .form_cont')[0],
				action: 'recrutement',
				form:true,
				upload_file:true
			});
			
			ajaxifier_contact.addEvents({
				'request:start': function() {
					loader.open();
				}.bind(this),
				'request:complete': function() {
					loader.close();
				}.bind(this)
			});
		}
		
	},
	
	initRecrutementForm:function() {
		if($$('.recrutement #form_contact').length > 0) {
			$('form_contact').set('target', 'myFrame');
			var input = new Element('input', {name:'ajax', value:'1', type:'hidden'}).inject($('form_contact'));
			
			$$('.submit')[0].addEvent('click', function(event) {
				event.stop();
				var index = Math.floor(Math.random()*11);
				var e = {
					index: index,
					old_index: index + 1
				};
				ajaxifier_contact.fireEvent('goto', e);
				HM.set('hash', e);
			});
		}

		$$('input[type=file]').each(function(el) {
			var upload = new Upload({
				target:el
			});
		}, this);
	},
	
	initNewsImages:function() {
		$$('#news li').each(function(el) {
			var img = el.getFirst('img');
			if(img) {
				img.setStyle('display', 'block');
				img.setStyles({
					'top': -img.getSize().y - 24
				});
				var fx = new Fxable({
					target:img,
					'type': 'opacity',
					dest: 1,
					src: 0,
					duration:500
				});
				fx.setClose();
				el.store('fx', fx);
				el.addEvents({
					'mouseenter': function(e) {
						el.retrieve('fx').open();
					}.bind(this),
					'mouseleave':function(e) {
						el.retrieve('fx').close();
					}
				});
			}
		}, this);
	},
	
	initMainTitle:function() {
		this.current_title = 0;
		this.titles = new Array();
		$$('.main_title_cont div').each(function(el) {
			var title = new Title({
				container:el
			});
			this.titles.push(title);
		}, this);
		
		this.moveTitle.periodical(5000, this);
		
		this.titles[this.current_title].goCenter();
		
	},
	
	moveTitle:function() {
		this.titles[this.current_title].goLeft();
		this.current_title++;
		if(this.current_title >= this.titles.length) this.current_title = 0;
		this.titles[this.current_title].goCenter();
	},
	
	initAlbum:function() {
		if($$('.album').length > 0) {
			album_page = new AlbumPage({
			
			});
			album_page.goto({alb: 10});
		}
	},
	
	initContact:function() {
		if($$('.contact').length > 0 && $$('.recrutement').length <= 0) {
			ajaxifier_contact = new Ajaxifier({
				container: $$('.contact .form_ajax')[0],
				action: 'contact',
				form:true
			});
			ajaxifier_contact.addEvents({
				'request:start': function() {
					loader.open();
				}.bind(this),
				'request:complete': function() {
					loader.close();
				}.bind(this)
			});	
		}
	},
	
	initContactForm:function() {
		if($$('.contact #form_contact').length > 0 && $$('.recrutement').length <= 0) {
			$('form_contact').addEvent('submit', function(event) {
				event.stop();
				var index = Math.floor(Math.random()*11);
				var e = {
					index: index,
					old_index: index + 1
				};
				ajaxifier_contact.fireEvent('goto', e);
				HM.set('hash', e);
			});
		}
	}
								
	
});
