var Application = new Class({
	
	displays: null,
	current_display:null,
	tags: null,
	
	initialize:function() {
		this.wrap = new Element('div.wrap').inject($('content'));
		
		this.ajax_manager = new AjaxManager({
			
		});
		
		this.ajax_manager.addEvent('infosloaded', this.ajaxManagerInfosLoaded.bind(this));
		this.ajax_manager.addEvent('pageloaded', this.pageLoaded.bind(this));
		
		this.clickHandlers();
		
		this.ajax_manager.loadPage(false);
	},
	
	clickHandlers: function() {
		this.displays = new Object();
		this.bound_display = this.clickDisplay.bind(this);
		$$('#top ul li').each(function(el, index) {
			var anchor = new Anchor({
				container: el,
				index: el.id,
				is_select: el.hasClass('select')
			});
			if(el.hasClass('select')) this.current_display = el.id;
			anchor.addEvent('click', this.bound_display);
			eval("this.displays."+el.id+" = anchor;");
		}, this);
		
		this.tags = new Array();
		this.bound_tag = this.clickTag.bind(this);
		$$('#tags ul li').each(function(el, index) {
			var anchor = new Anchor({
				container: el,
				index: index,
				is_select: el.hasClass('select'),
				is_tag:true
			});
			anchor.addEvent('click', this.bound_tag);
			this.tags.push(anchor);
		}, this);
		
	},
	
	clickHandlersContent: function() {
		var statics = $$('.vignets li a.goto, .vignets li a.link, .zoom .navig a');
		statics.each(function(el, index) {
			el.addEvent('click', function(e) {
				e.stop();
				this.ajax_manager.loadPage(e.target.search.replace('?', ''));
			}.bind(this));
		}, this);
	},
	
	clickDisplay:function(e) {
		this.changeDisplay(e);
		
		this.ajax_manager.loadPage(e.uri);
	},
	
	changeDisplay:function(e) {
		if(this.current_display != null) {
			eval("this.displays."+this.current_display+".unselect();");
		}
		this.current_display = e.index;
		eval("this.displays."+this.current_display+".select();");
	},
	
	clickTag:function(e) {
		if(e.is_select) 
			e.target.unselect();
		else
			e.target.select();
		
		this.ajax_manager.loadPage(e.uri);
	},
	
	ajaxManagerInfosLoaded: function(e) {
		//debug(session_infos.display.value);
	},
	
	unserializeURI: function(uri) {
		uri = uri.replace('?', '');
		var gets = uri.split('&');
		var ret = new Object();
		for(var i = 0; i < gets.length; i++) {
			var t = gets[i].split('=');
			eval('ret.'+t[0]+' = "'+ t[1]+'";');
		}
		return ret;
	},
	
	pageLoaded: function() {
		this.clickHandlersContent();
		var new_index = 'display_'+session_infos.display.value;
		if(this.current_display != new_index) {
			this.changeDisplay({index: new_index});
		}
		
		$$('ul.vignets li').each(function(el) {
			var image = new ImageMini({
				container: el.getFirst('.apercu'),
				infos: session_infos.works[el.id].images,
				work_id: session_infos.works[el.id].id
			});
			image.addEvent('imageloaded', function(e) {
				this.ajax_manager.update(e.uri);
			}.bind(this));
		}, this);
		
		$$('div.zoom .image').each(function(el) {
			var image = new ImageFull({
				container: el,
				infos: session_infos.works[el.id].images,
				work_id: session_infos.works[el.id].id
			});
			image.addEvent('imageloaded', function(e) {
				this.ajax_manager.update(e.uri);
			}.bind(this));
		}, this);
		
		$$('.form').addEvent('submit', function(e) {
			e.stop();
			this.ajax_manager.loadContact();
		}.bind(this));
	}
	
	
});
