/**
 * @author Alain Duchesneau (aduchesneau à terrainmarketing point com)
 */
var ImagesGallery = new Class({
	Implements: [Options,Log],
	options:{
		'menu':false
	},
	
	thumbnails: null,
	inTransition: false,
	currentThumb: null,
	imagePlaceholder: null,
	
	PATH: '/medias/imagesGallery/',
	
    initialize: function(options){
		this.setOptions(options);
		this.enableLog();
		if(this.options.menu) {
			this.initMenuInteractivity();
			$('galleryMenu').addEvent('mouseover',hideMenu);
		} else {
			this.thumbnails = [];
			$$('#imagesGallery_photoFilm li img').each(function(img){
				var li = img.getParent('li');
				var a = img.getParent('a');
				this.thumbnails.push(img.inject(li));
				a.destroy();
				img.addEvent('click',this.clickListHandler.bind(this));
			}.bind(this));
			//this.resetCurrentView();
		}
		
		this.imagePlaceholder = $('imagesGallery_image').getElement('img');
		
		$('imagesGallery_content').addEvent('mouseover',hideMenu);
    },
	
	initMenuInteractivity:function() {
		
		$$('.materialChanger').each(function(e){
			e.addEvent('click', function(evt) {
				var ul = $(evt.target.id + '_types'); 
				if (evt.target.checked) {
					ul.removeClass('inv');
				} else {
					ul.addClass('inv');
					var changed = false;
					ul.getElements('input').each(function(el){
						if (el.checked) {
							el.checked = false;
							changed = true;
						}
					}.bind(this))
					if (changed) this.fetchData(); 
				}
			}.bind(this));
		}.bind(this));
		
		
		$$('.typeChanger').each(function(e){
			e.addEvent('click', function(evt) {
				this.fetchData();
			}.bind(this));
		}.bind(this));

		$$('.windowsMaterialChanger','.windowsTypeChanger','doorMaterialChanger').each(function(e){
			e.addEvent('click', this.fetchData.bind(this));
		}.bind(this));
		$('galleryImagesMenu').set('send', {
			url: '/services/imagesgallery.php',
			method: 'post',
			evalResponse: true,
			onComplete: this.onFetchDataComplete.bind(this)
		});
		$('galleryImagesMenu').send();
	},
	
	// Populate thumnails
	fetchData:function(event) {
		$('imagesGallery_photoFilm').getElement('ul').empty();
		$('imagesGallery_vignette').empty();
		$('galleryImagesMenu').send();				
	},
	
	onFetchDataComplete:function(data) {
		var datas = JSON.decode(data);
		var ul = $('imagesGallery_photoFilm').getElement('ul');
		
		if (datas) {
			ul.empty();
			$('imagesGallery_vignette').empty();
			
			if(datas.length == 0) {
				this.setVignette((langue=='fr')?'Votre sélection ne correspond à aucune des images.':'Your selection did not match any images.');
				if (!$('imagesGallery_image').hasClass('empty')) $('imagesGallery_image').addClass('empty');
				if (this.imagePlaceholder) this.imagePlaceholder.destroy();
			} else {
				 $('imagesGallery_image').removeClass('empty');
			}
			this.thumbnails = [];
			var height = 55;
			var width = 80;
			datas.each(function(el){
				if ($type(el) == 'object') {
					this.setVignette(el.text);
					var small_src = el.img.replace(/\.(jpg|gif|png)$/,'_small.$1');
					var li = new Element('li').inject(ul);
					var img = new Element('img', {
						'height': height,
						'width' : width,
						alt: el.text,
						src: this.PATH + small_src,
						longdesc: this.PATH + el.img
					}).inject(li);
					img.addEvent('click',this.clickListHandler.bind(this));
					this.thumbnails.push(img);
				}
			}.bind(this));
		
			this.resetCurrentView();
		}
		
	},
	
	resetCurrentView:function () {
		
		if (this.thumbnails.length > 0) {
			this.inTransition = true;
			this.currentThumb = this.thumbnails[0];
			
			var src = (this.currentThumb.longdesc) ? this.currentThumb.longdesc : this.currentThumb.get('longdesc') ;
			new Asset.image(src, {
				'alt': this.currentThumb.alt,
				'onload': this.onImageLoaded.bind(this)
			});
			this.setVignette(this.currentThumb.alt);
		}
	},
	
	clickListHandler:function(event) {
		
		if(!this.inTransition && event.target && event.target !== this.currentThumb) {
			this.inTransition = true;
			
			this.currentThumb = event.target;
			this.setVignette('');
			this.imagePlaceholder.get('tween', {property: 'opacity', duration: 250,'onComplete':this.fadeOutImageDone.bind(this)}).start(0);
		}
	},	
	
	setVignette: function(txt) {
		$('imagesGallery_vignette').set('text',txt);
	},
	
	fadeOutImageDone: function(target) {

		var src = (this.currentThumb.longdesc) ? this.currentThumb.longdesc : this.currentThumb.get('longdesc') ;
		new Asset.image(src, {
			'alt': this.currentThumb.alt,
			'onload': this.onImageLoaded.bind(this)
		});
		this.setVignette(this.currentThumb.alt);
	},
	
	onImageLoaded:function(target) {
		target.setStyle('opacity', '0');
		target.inject($('imagesGallery_image'));
		//target.injectAfter(this.imagePlaceholder.getParent());
		if (this.imagePlaceholder) this.imagePlaceholder.destroy();
		this.imagePlaceholder = target;
		this.imagePlaceholder.get('tween', {property: 'opacity', duration: 250,'onComplete':this.fadeInImageDone.bind(this)}).start(1);
	},
	
	fadeInImageDone: function(target) {
		this.inTransition = false;
		this.imagePlaceholder.set('tween', null);
	}
	
});