var Album = new Class({
	
	reduce: 1.4,
	
	Implements: [Options, Events],
	options:{
		alb:null
	},
	
	initialize:function(options) {
		this.setOptions(options);
		debug('Album:initialize');
		
		this.current_load = 0;
		this.current_left = 0;
		this.current_move = 0;
		this.current_mouse = 0;
		this.current_big = null;
		
		this.speed_fast = 6;
		this.speed_slow = 10;

		this.setRequest();
		
		this.container = $$('.text_wrapper')[0];
		
		this.accroche = new Element('div').inject(this.container);
		this.accroche.addClass('accroche');
		this.accroche_fx = new Fx.Morph(this.accroche, {
			transition: Fx.Transitions.Sine.easeOut,
			link:'cancel'
		});
		
		this.accroche_fx_dest = {
			'left': this.accroche.getCoordinates(this.accroche.getParent()).left,
			'opacity': 1
		};

		this.accroche_fx_src = {
			'left': this.accroche_fx_dest.left - 60,
			'opacity': 0
		};

		this.accroche_fx.set(this.accroche_fx_src);
		
		this.slider_cont = new Element('div').inject($('bkg_container'));
		this.slider_cont.addClass('nav_images');
		this.slider_cont.setStyles({
			'overflow': 'hidden',
			'position': 'absolute',
			width:window.getSize().x,
			top:180,
			'z-index':400,
			height:210
		});
		this.slider = new Element('ul').inject(this.slider_cont);
		
		this.resize();
		
		this.slider.setStyles({
			'position': 'absolute',
			'top':0,
			'left': this.slide_left
		});
		this.slider_fx = new Fx.Tween(this.slider, {
			//transition: Fx.Transitions.Quad.easeInOut,
			link:'cancel'
		})
		
		this.slider_width = 0;
		
		this.bound_slider_enter = this.enterSlider.bind(this);
		this.bound_slider_leave = this.leaveSlider.bind(this);
		this.bound_slider_mouse = this.mouseSlider.bind(this);
		
		this.slider_cont.addEvents({
			'mouseenter': this.bound_slider_enter,
			'mouseleave': this.bound_slider_leave
		});
		
		this.bound_image_complete = this.imageComplete.bind(this);
		
		window.addEvent('resize', this.resize.bind(this));
	},
	
	setRequest:function() {
		this.request = new Request({
			url:'./cmn/request.php',
			method:'post',
			evalScripts:true,
			data:'action=album_xml&alb='+this.options.alb+'&lang='+LANG+'&type='+this.options.alb,
			onSuccess:function(responseText, responseXML) {
				this.requestComplete(responseXML);
			}.bind(this)
		}).send();
		
	},
	
	requestComplete:function(responseXMl) {
		
		var albums = responseXMl.documentElement.getElementsByTagName("album");
		this.albums = new Array();
		for(var i = 0; i < albums.length; i++) {
			var obj = {
				id: albums[i].getAttribute('id'),
				libelle: albums[i].getAttribute('libelle'),
				accroche: albums[i].getAttribute('accroche'),
				image: albums[i].getAttribute('image'),
				image_width_mini: albums[i].getAttribute('image_width_mini'),
				image_height_mini: albums[i].getAttribute('image_height_mini'),
				image_width_big: albums[i].getAttribute('image_width_big'),
				image_height_big: albums[i].getAttribute('image_height_big'),
				text: albums[i].getAttribute('text')
			};
			this.albums.push(obj);
			if(i == 0) {
				//this.slider_cont.setStyle('height', parseInt(this.albums[i].image_height_mini));
			}
			this.slider_width += parseInt(this.albums[i].image_width_mini) / this.reduce;
		}
		this.accroche_text = responseXMl.documentElement.getElementsByTagName("accroche")[0].getAttribute('text');
		
		this.accroche_text = this.accroche_text.replace(/_br_/g, '<br />');
		
		loader.close();
		
		this.resize();
		
		this.loadElements();
		this.loadProfils();
	},
	
	loadElements:function() {
		this.accroche.set('html', this.accroche_text);
		this.accroche_fx.start(this.accroche_fx_dest);
	},
	
	loadProfils:function() {
		this.slider.setStyle('width', this.slider_width);
		this.profils = new Array();
		this.loadProfil();
	},
	
	loadProfil:function() {
		if(this.albums[this.current_load]) {
			var profil = new Profil({
				infos: this.albums[this.current_load],
				parent: this.slider,
				left: this.current_left,
				reduce:this.reduce,
				index:this.current_load,
				total:this.albums.length
			});
			this.profils.push(profil);
			
			profil.addEvents({
				'imageComplete': this.bound_image_complete,
				'deploy': function(e) {
					this.fireEvent('deploy');
					this.slider_fx.cancel();
					this.accroche_fx.start(this.accroche_fx_src);
					this.current_big = e.index;
					for(var i = 0; i < this.profils.length; i++) {
						this.profils[i].remove_events();
					}
					this.slider_cont.removeEvents({
						'mouseenter': this.bound_slider_enter,
						'mouseleave': this.bound_slider_leave
					});
					this.leaveSlider();
				}.bind(this),
				'closeBig': function() {
					this.fireEvent('closeBig');
					this.accroche_fx.start(this.accroche_fx_dest);
					this.current_big = null;
					for(var i = 0; i < this.profils.length; i++) {
						this.profils[i].add_events();
					}
					this.slider_cont.addEvents({
						'mouseenter': this.bound_slider_enter,
						'mouseleave': this.bound_slider_leave
					});
				}.bind(this),
				'prevBig': function(e) {
					this.current_big = e.index <= 0 ? this.albums.length - 1 : e.index - 1;
					this.profils[this.current_big].clickImage();
				}.bind(this),
				'nextBig': function(e) {
					this.current_big = e.index >= this.albums.length - 1 ? 0 : e.index + 1;
					this.profils[this.current_big].clickImage();
				}.bind(this)
			});
		}
	},
	
	imageComplete:function() {
		this.current_load++;
		if(this.albums[this.current_load]) {
			this.current_left += parseInt(this.albums[this.current_load].image_width_mini) / this.reduce;
			this.loadProfil();
		}
	},
	
	enterSlider:function() {
		this.slider_cont.addEvent('mousemove', this.bound_slider_mouse);
	},
	
	leaveSlider:function() {
		this.slider_cont.removeEvent('mousemove', this.bound_slider_mouse);
		this.smoothend();
	},
	
	mouseSlider:function(e) {
		if(this.current_mouse % 10 == 0) {
			var left = this.slider_cont.getCoordinates().left;
			var x = e.client.x - left;
			var width = this.slider_cont.getSize().x;
			if( x < width * 1 / 5) {
				this.moveSlider(1);
			}
			else if( x > width* 1 / 5 && x < width * 2 / 5) {
				this.moveSlider(3);
			}
			else if( x > width * 2 / 5 && x < width * 3 / 5) {
				this.moveSlider(3);
			}
			else if( x > width * 2 / 5 && x < width * 4 / 5) {
				this.moveSlider(3);
			}
			else if( x > width * 4 / 5) {
				this.moveSlider(5);
			}
		}
		this.current_mouse = this.current_mouse == 10000 ? 0 : this.current_mouse + 1;
	},
	
	moveSlider:function(index) {
		if(this.current_move != index) {
			this.current_move = index;
			var left = this.slider.getCoordinates(this.slider.getParent()).left;
			var width = this.slider.getSize().x;
			var width_cont = this.slider_cont.getSize().x;
			if(this.current_move == 1) {
				this.slider_fx.options.duration = - left * this.speed_fast;
				this.slider_fx.start('left', this.slide_left);
				this.direction = 'backward';
			}
			if(this.current_move == 2) {
				this.slider_fx.options.duration = - left * this.speed_slow;
				this.slider_fx.start('left', this.slide_left);
				this.direction = 'backward';
			}
			if(this.current_move == 3) {
				this.smoothend();
				this.direction = "still";
			}
			if(this.current_move == 4) {
				this.slider_fx.options.duration = (width + left - width_cont) * this.speed_slow;
				this.slider_fx.start('left', this.slide_right);
				this.direction = 'forward';
			}
			if(this.current_move == 5) {
				this.slider_fx.options.duration = (width + left - width_cont) * this.speed_fast;
				this.slider_fx.start('left', this.slide_right);
				this.direction = 'forward';
			}
			//debug(this.slider_fx.options.duration);
		}
	},
	
	smoothend:function() {
		this.slider_fx.cancel();
		this.current_move = 0;
		/*
		return false;
		var left = this.slider.getCoordinates(this.slider.getParent()).left;
		max_left = 0;
		max_right = - ( this.slider.getSize().x - this.slider_cont.getSize().x);
		if(left < max_left && left > max_right) {
			debug('smoothend');
			switch(this.direction) {
				case 'forward':
					var dest = left - 60;
					if(dest < max_right) dest = max_right;
				break;
				case 'backward':
					var dest = left + 60;
					if(dest > max_left) dest = max_left;
				break;
			}
			this.slider_fx.options.duration = 1000;
			this.slider_fx.start('left', dest);
		}
		*/
	},
	
	close:function() {
		this.accroche_fx.addEvent('complete', function() {
			this.destroy();
		}.bind(this));
		this.accroche_fx.start(this.accroche_fx_src);
		for(var i = 0; i < this.albums.length; i++) {
			if(this.profils[i]) this.profils[i].close();
		}
	},
	
	destroy:function() {
		this.fireEvent('destroy');
		this.accroche.destroy();
		this.slider_cont.empty();
		this.slider_cont.destroy();
	},
	
	resize:function() {
		try {
			this.slide_left = (window.getSize().x - $('content').getSize().x) / 2;
	
			if(this.slider_cont.getSize()) var width_cont = this.slider_cont.getSize().x;
			
			this.slide_right = - this.slider_width + width_cont - this.slide_left - 14;
		}
		catch(e) { debug('!!! erreur resize album'); }
	}
	
});
