/**
	 * MooDDG3
	 * Display images from an array of urls with a smooth effect (puts all the images in the cache when launched and then beggins to display)
	 * Version : 1.0 - Last update: December 3rd 2009
	 * 
	 * Author: Arnaud Lejosne
	 * Site: http://creations.arnaudlejosne.com
	 * 
	 * Further informations: http://www.jonathan-petitcolas.com/
	 * 
	 * This file is released under Creative Commons (with Attribution, NonCommercial 
	 * and NoDerivs specifications) licence. You can read the legal code on:
	 * http://creativecommons.org/licenses/by-nc-nd/3.0/legalcode
	 * 
	 * You can use this script for every non-commercial project. You just have to
	 * put a link on www.jonathan-petitcolas.com within the resulting website. For all
	 * commercial projects, please contact me at <contact at arnaudlejosne dot com>.
**/


var MooDDG3 = new Class({
	version:1.0,
	Implements: [Events, Options],
	options: {
		delay: 2000,
		shuffle:false,
		newWindow:true
	},
	initialize: function(element, imagesHash, options) {
		if($chk(options))	this.setOptions(options);
		this.imagesHash = this.options.shuffle?this.shuffle(imagesHash):imagesHash;
		this.imagesToLoad = new Array(this.imagesHash.length);
		$each(this.imagesHash,function(imageHash,key){	this.imagesToLoad[key] = imageHash.image;	}.bind(this));
		this.el = element;
		this.el.setStyle('cursor','pointer');
		this.curIndex = 1;
		element.empty();
		var loadedImages = new Array(this.imagesHash.length);
		new Asset.images(this.imagesToLoad,{
			onProgress:function(counter, key){
				this.setStyle('border','none');
				loadedImages[key] = this;
			},
			onComplete:function(){
				$each(loadedImages,function(img, key){
					img.set('alt',this.imagesHash[key].titre);
					img.set('title',this.imagesHash[key].titre);
					img.addEvent('click',function(){
						if(this.options.newWindow)
							window.open(this.imagesHash[key].lien,'_blank');
						else
							window.location.assign(this.imagesHash[key].lien);
					}.bind(this));
				}.bind(this));
				this.loadedImages = loadedImages;
				this.loadedImages[0].setStyle('opacity',0);
				this.el.adopt(this.loadedImages[0]);
				this.curFxMorph2 = new Fx.Morph(this.loadedImages[0],{
					onComplete:function(){
						setTimeout(function(){this.showImages(this.loadedImages[this.curIndex]);}.bind(this),this.options.delay);
					}.bind(this)
				}).start({'opacity':1});
			}.bind(this)
		});
	},
	showImages: function(load){
		var oldImg = this.el.getFirst('img');
		new Fx.Morph(oldImg,{
			onComplete:function(){
				load.setStyle('opacity',0);
				this.el.adopt(load);
				oldImg.destroy();
				new Fx.Morph(load,{
					onComplete:function(){
						if(this.curIndex == this.loadedImages.length-1)		this.curIndex = -1;
						setTimeout(function(){this.showImages(this.loadedImages[++this.curIndex]);}.bind(this),this.options.delay);
					}.bind(this)
				}).start({'opacity':1});
			}.bind(this)
		}).start({'opacity':0});
	},
	shuffle: function(o){
		for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
		return o;
	}
});