/*
**
**	Gallerie Photo v 0.1
**	12/03/2008
**
**	CANSII (http://www.cansii.com)
**	Cédric ANGENIOL
**	
**	Cette librairie est distribuée sous les termes de la licence CeCILL v 1
**
**	Cette librairie nécéssite la librairie Mootools (http://mootools.net/) version 1.11
*/

var Gallerie = {};

Gallerie.Photo = new Class
({
	options:
	{
		attributs: {},
		imageAutoCreate: true,
		zoomDo: true,
		zoomMargeTop: 10,
		zoomMargeLaterale: 50,
		zoomMargeBottom: 40,
		zoomZIndexBase: 90,
		petitZoomDo: true,
		elementParent: null,
		zoomTransition: Fx.Transitions.Expo.easeInOut,
		legende: '',
		preview: '',
		album: null
	},
	
    initialize: function(options, options2)
    {
	    var		photo = this;
	    
	    switch($type(options))
	    {
		case 'object':
		    this.setOptions(options);
		    this.Img = null;
		    break;
		case 'element':
	    	options2 = $merge
	    	(
	    		options2,
	    		{
		    		'elementParent':options.getParent(),
		    		'attributs':
		    		{
			    		'src':options.src,
			    		'alt':options.alt,
			    		'style':options.style
				    }
				}
			);
			this.setOptions(options2);
			this.Img = options;
		    this.Img.addEvent('click', function() {photo.Zoom();});
		    this.Img.addEvent('mouseover', function() {photo.PetitZoom();});
			break;
		}
	    this.ZoomTransition = null;
	    this.ZoomImage = null;
	    this.ZoomFond = null;
	    this.ImageLoader = null;
	    this.ZoomLegende = null;
	    this.ZoomImageSuivante = null;
	    this.ZoomImagePrecedente = null;
	    this.PetitZoomImage = null;
	    this.Preloader = null;
	    if (this.options.imageAutoCreate)
	    	this.getImage();
    },
    
    Delete: function()
    {
	    if (this.Img)
	    {
		    //this.Img.empty();
		    this.Img.remove();
		}
    },
    
    getImage: function()
    {
	    var		photo = this;
	    var		attributs;
	    
	    if (!this.Img)
	    {
		    if (this.options.preview != '')
		    	attributs = $merge(this.options.attributs, {'src':this.options.preview});
		    else
		    	attributs = this.options.attributs;
		    this.Img = new Element('img', attributs)
		    this.Img.injectTop(this.options.elementParent ? $(this.options.elementParent) : $(document.body));
		    this.Img.setProperty('alt', attributs.alt ? attributs.alt : this.options.legende);
		    this.Img.setStyles(attributs.style);
		    this.Img.addEvent('click', function() {photo.Zoom();});
		    this.Img.addEvent('mouseover', function() {photo.PetitZoom();});
	    }
	    return this.Img;
    },
    
    PetitZoom: function()
    {
	    var		photo = this;
		var		x, y, w, h;
		var		parent;

		if (!this.options.petitZoomDo || this.ZoomImage)
			return;
		w = this.Img.getCoordinates().width.toInt();
		h = this.Img.getCoordinates().height.toInt();
		x = this.Img.getCoordinates().left.toInt() - w * .15;
		y = this.Img.getCoordinates().top.toInt() - h * .15;
		parent = $(this.options.elementParent);
		while (parent && (parent.tagName != 'BODY') && (parent.getStyle('position') != 'absolute'))
			parent = parent.getParent();
		if (parent)
		{
			x -= parent.getCoordinates().left.toInt();
			y -= parent.getCoordinates().top.toInt();
		}
		w *= 1.3;
		h *= 1.3;
		this.PetitZoomImage = this.Img.clone().injectAfter(this.Img);
		this.PetitZoomImage.setStyles
		(
			{
				'position':'absolute',
				'top':y,
				'left':x,
				'width':w,
				'height':h
			}
		);
		this.PetitZoomImage.addEvent('mouseout', function() {photo.UnPetitZoom();});
		this.PetitZoomImage.addEvent('click', function() {photo.UnPetitZoom(); photo.Zoom();});
    },
    
    UnPetitZoom: function()
    {
	    if (this.PetitZoomImage != null)
	    {
		    this.PetitZoomImage.remove();
		    this.PetitZoomImage = null;
	    }
    },
    
    Zoom: function()
    {
		var		photo = this;

		if (!this.options.zoomDo)
			return;
		this.ZoomFond = new Element('div', {'style':{'position':'absolute'}});
		this.ZoomFond.injectTop($(document.body));
		this.ZoomFond.setStyles
		(
			{
				'position':'absolute',
				'top':0,
				'left':0,
				'right':0,
				'bottom':0,
				'z-index':this.options.zoomZIndexBase,
				'background-color':'black',
				'opacity':0
			}
		);
		this.ZoomImage = new Element
		(
			'img',
			{			
				'style':
				{
					'position':'absolute',
					'opacity':0
				},
				'events':
				{
					'click':function() {photo.UnZoom();}
				}
			}
		);	
		if (photo.options.preview != '')
		{
			this.ImageLoader = new Element('img', {'src':'ajax-loader.gif', 'style':{'position':'absolute'}});
			this.ImageLoader.injectTop(this.ZoomFond);
			this.ZoomFond.setStyles
			(
				{
					'position':'absolute',
					'top':0,
					'left':0,
					'right':0,
					'bottom':0,
					'z-index':this.options.zoomZIndexBase,
					'background-color':'black',
					'opacity':.8
				}
			);
			this.ImageLoader.setStyles
			(
				{
					'position':'absolute',
					'z-index':this.options.zoomZIndexBase + 3,
					'top':window.getHeight() / 2,
					'left':window.getWidth() / 2,
					'opacity':1
				}
			);
			this.ZoomImage.addEvent('load', function() {photo.ZoomLoaded();});
			this.ZoomImage.src = photo.options.attributs.src;
		}
		else
		{
			this.ZoomImage.src = photo.options.attributs.src;
			this.ZoomLoaded();
		}
	},
		
    ZoomLoaded: function()
    {
		var		x, y, w, h;
		var		photo = this;

		if (this.ImageLoader)
		{
			this.ImageLoader.remove();
			this.ImageLoader = null;
		}	
		this.ZoomFond.setStyles
		(
			{
				'position':'absolute',
				'top':0,
				'left':0,
				'right':0,
				'bottom':0,
				'z-index':this.options.zoomZIndexBase,
				'background-color':'black',
				'opacity':0
			}
		);
		this.ZoomImage.injectTop($(document.body));
		w = this.ZoomImage.getStyle('width').toInt();
		h = this.ZoomImage.getStyle('height').toInt();
		if (w > (window.getWidth() - 2 * this.options.zoomMargeLaterale))
		{
			h *= (window.getWidth() - 2 * this.options.zoomMargeLaterale) / w;
			w = (window.getWidth() - 2 * this.options.zoomMargeLaterale);
		}
		if (h > (window.getHeight() - this.options.zoomMargeBottom - this.options.zoomMargeTop))
		{
			w *= (window.getHeight() - this.options.zoomMargeBottom - this.options.zoomMargeTop) / h;
			h = (window.getHeight() - this.options.zoomMargeBottom - this.options.zoomMargeTop);
		}
		x = (window.getWidth() - w) / 2;
		y = (window.getHeight() - h - this.options.zoomMargeBottom) / 2;
		this.ZoomTransition = new Fx.Elements
		(
			[this.ZoomImage, this.ZoomFond],
			{
				duration: 500,
				transition: photo.options.zoomTransition,
				onComplete:function()
				{
					photo.ZoomLegende = new Element('div', {'position':'absolute'});
					photo.ZoomLegende.setText(photo.Img.alt);
					photo.ZoomLegende.injectTop(photo.ZoomFond);
					photo.ZoomLegende.setStyles
					(
						{
							'color':'white',
							'text-align':'center',
							'position':'absolute',
							'bottom':Math.max(0, (window.getHeight() - y - h - photo.ZoomLegende.getStyle('height').toInt()) / 2),
							'left':x,
							'width':w
						}
					);
					if (photo.options.album)
					{
						photo.ShowNextInZoom();
						photo.ShowPreviousInZoom();
					}
					photo.ZoomTransition = null;
				},
				onCancel:function()
				{
					photo.ZoomImage.remove();
					photo.ZoomFond.remove();
					photo.ZoomTransition = null;
				}
			}
		);
		this.ZoomImage.setStyles
		(
			{
				'position':'absolute',
				'z-index':this.options.zoomZIndexBase + 1,
				'top':this.Img.getTop(),
				'left':this.Img.getLeft(),
				'width':this.Img.getCoordinates().width,
				'height':this.Img.getCoordinates().height,
				'opacity':1
			}
		);
		this.ZoomTransition.start
		({
			'0':
			{
				'top':[y],
				'left':[x],
				'width':[w],
				'height':[h]
			},
			'1':
			{
				'opacity':[.8]
			}
		});
    },
    
    ShowNextInZoom: function()
    {
		var		photo = this;
	    var 	album;
	    var		index;
		var		x, y;
	    
	    if (!this.options.album)
	    	return;
	    album = this.options.album.options.photos;
	    index = album.indexOf(this);
		if (index < (album.length - 1))
		{
			this.ZoomImageSuivante = album[index + 1].getImage().clone();
			this.ZoomImageSuivante.injectInside(this.ZoomFond);
			x = 1.5 * this.ZoomImage.getStyle('left').toInt() + this.ZoomImage.getCoordinates().width - this.ZoomImageSuivante.getCoordinates().width / 2;
			y = this.ZoomImage.getStyle('top').toInt() + this.ZoomImage.getCoordinates().height / 2 - this.ZoomImageSuivante.getCoordinates().height / 2;
			this.ZoomImageSuivante.setStyles
			({
				'position':'absolute',
				'left':x,
				'top':y
			});
			this.ZoomImageSuivante.addEvent('click', function() {photo.GoToNextInZoom()});
			album[index + 1].StartPreload();
		}
    },
    
    GoToNextInZoom: function()
    {
		var		photo = this;
	    var 	album;
	    var		index;
		var		x, y, w, h;
		var		xPrev, yPrev, hPrev, wPrev;
		var		xNext, yNext, hNext, wNext;
		var		photoSuivante;
		var		transition;
	    
	    if (!this.options.album)
	    	return;
	    album = this.options.album.options.photos;
	    index = album.indexOf(this);
		photoSuivante = album[index + 1];
		xPrev = .5 * this.ZoomImage.getStyle('left').toInt() - photoSuivante.getImage().getCoordinates().width / 2;
		yPrev = this.ZoomImage.getStyle('top').toInt() + this.ZoomImage.getCoordinates().height / 2 - photoSuivante.getImage().getCoordinates().height / 2;
		hPrev = photoSuivante.getImage().getCoordinates().height;
		wPrev = photoSuivante.getImage().getCoordinates().width;
		xNext = this.ZoomImageSuivante.getStyle('left').toInt();
		yNext = this.ZoomImageSuivante.getStyle('top').toInt();
		hNext = this.ZoomImageSuivante.getCoordinates().height;
		wNext = this.ZoomImageSuivante.getCoordinates().width;
		if (this.ZoomImageSuivante)
		{
			this.ZoomImageSuivante.remove();
			this.ZoomImageSuivante = null;
		}
		this.ZoomTransition = new Fx.Elements
		(
			[photo.ZoomImage, photo.ZoomFond],
			{
				duration: 500,
				transition: photo.options.zoomTransition,
				onComplete:function()
				{
					photo.ZoomImage.setStyle('opacity', 0);
					photo.UnZoom();
					photo.ZoomTransition = null;
				},
				onCancel:function()
				{
					photo.ZoomImage.setStyle('opacity', 0);
					photo.UnZoom();
					photo.ZoomTransition = null;
				}
			}
		);
		if (photo.ZoomImagePrecedente)
			this.ZoomTransition.elements[this.ZoomTransition.elements.length] = $(photo.ZoomImagePrecedente);
		transition =
		{
			'0':
			{
				'top':[yPrev],
				'left':[xPrev],
				'width':[wPrev],
				'height':[hPrev],
				'opacity':[.8]
			},
			'1':
			{
				'opacity':[0]
			}
		};
		if (photo.ZoomImagePrecedente)
		{
			transition = $merge
			(
				transition,
				{
					'2':
					{
						'top':[Math.floor(window.getHeight() / 2)],
						'left':[Math.floor(window.getWidth() / 2)],
						'width':[0],
						'height':[0],
						'opacity':[0]
					}
				}
			);
		}
		photoSuivante.ComeFromInZoom(xNext, yNext, wNext, hNext);
		this.ZoomTransition.start(transition);
    },
    
    GoToPreviousInZoom: function()
    {
		var		photo = this;
	    var 	album;
	    var		index;
		var		x, y, w, h;
		var		xPrev, yPrev, hPrev, wPrev;
		var		xNext, yNext, hNext, wNext;
		var		photoPrecedente;
		var		transition;
	    
	    if (!this.options.album)
	    	return;
	    album = this.options.album.options.photos;
	    index = album.indexOf(this);
		photoPrecedente = album[index - 1];
		xPrev = 1.5 * this.ZoomImage.getStyle('left').toInt() + this.ZoomImage.getCoordinates().width - photoPrecedente.getImage().getCoordinates().width / 2;
		yPrev = this.ZoomImage.getStyle('top').toInt() + this.ZoomImage.getCoordinates().height / 2 - photoPrecedente.getImage().getCoordinates().height / 2;
		hPrev = photoPrecedente.getImage().getCoordinates().height;
		wPrev = photoPrecedente.getImage().getCoordinates().width;
		xNext = this.ZoomImagePrecedente.getStyle('left').toInt();
		yNext = this.ZoomImagePrecedente.getStyle('top').toInt();
		hNext = this.ZoomImagePrecedente.getCoordinates().height;
		wNext = this.ZoomImagePrecedente.getCoordinates().width;
		if (this.ZoomImagePrecedente)
		{
			this.ZoomImagePrecedente.remove();
			this.ZoomImagePrecedente = null;
		}
		this.ZoomTransition = new Fx.Elements
		(
			[photo.ZoomImage, photo.ZoomFond],
			{
				duration: 500,
				transition: photo.options.zoomTransition,
				onComplete:function()
				{
					photo.ZoomImage.setStyle('opacity', 0);
					photo.UnZoom();
					photo.ZoomTransition = null;
				},
				onCancel:function()
				{
					photo.ZoomImage.setStyle('opacity', 0);
					photo.UnZoom();
					photo.ZoomTransition = null;
				}
			}
		);
		if (photo.ZoomImageSuivante)
			this.ZoomTransition.elements[this.ZoomTransition.elements.length] = $(photo.ZoomImageSuivante);
		transition =
		{
			'0':
			{
				'top':[yPrev],
				'left':[xPrev],
				'width':[wPrev],
				'height':[hPrev],
				'opacity':[.8]
			},
			'1':
			{
				'opacity':[0]
			}
		};
		if (photo.ZoomImageSuivante)
		{
			transition = $merge
			(
				transition,
				{
					'2':
					{
						'top':[Math.floor(window.getHeight() / 2)],
						'left':[Math.floor(window.getWidth() / 2)],
						'width':[0],
						'height':[0],
						'opacity':[0]
					}
				}
			);
		}
		photoPrecedente.ComeFromInZoom(xNext, yNext, wNext, hNext);
		this.ZoomTransition.start(transition);
    },
    
    ComeFromInZoom: function(xTarget, yTarget, wTarget, hTarget)
    {
		var		photo = this;

		this.ZoomFond = new Element('div', {'style':{'position':'absolute'}});
		this.ZoomFond.injectTop($(document.body));
		this.ZoomFond.setStyles
		(
			{
				'position':'absolute',
				'top':0,
				'left':0,
				'right':0,
				'bottom':0,
				'z-index':this.options.zoomZIndexBase,
				'background-color':'black',
				'opacity':0
			}
		);
		this.ZoomImage = new Element
		(
			'img',
			{			
				//'src':photo.Img.src,
				//'src':photo.options.attributs.src,
				'style':
				{
					'position':'absolute',
					'opacity':0
				},
				'events':
				{
					'click':function() {photo.UnZoom();}
				}
			}
		);
		if (photo.options.preview != '')
		{
			this.ImageLoader = new Element('img', {'src':'ajax-loader.gif', 'style':{'position':'absolute'}});
			this.ImageLoader.injectTop(this.ZoomFond);
			this.ZoomFond.setStyles
			(
				{
					'position':'absolute',
					'top':0,
					'left':0,
					'right':0,
					'bottom':0,
					'z-index':this.options.zoomZIndexBase,
					'background-color':'black',
					'opacity':.8
				}
			);
			this.ImageLoader.setStyles
			(
				{
					'position':'absolute',
					'z-index':this.options.zoomZIndexBase + 3,
					'top':window.getHeight() / 2,
					'left':window.getWidth() / 2,
					'opacity':1
				}
			);
			this.ZoomImage.addEvent('load', function() {photo.ComeFromInZoomLoaded(xTarget, yTarget, wTarget, hTarget);});
			this.ZoomImage.src = photo.options.attributs.src;
		}
		else
		{
			this.ZoomImage.src = photo.options.attributs.src;
			this.ComeFromInZoomLoaded(xTarget, yTarget, wTarget, hTarget);
		}
	},
		
	ComeFromInZoomLoaded: function(xTarget, yTarget, wTarget, hTarget)
	{
		var		x, y, w, h;
		var		photo = this;
		
		if (this.ImageLoader)
		{
			this.ImageLoader.remove();
			this.ImageLoader = null;
		}	
		this.ZoomImage.injectTop($(document.body));
		w = this.ZoomImage.getStyle('width').toInt();
		h = this.ZoomImage.getStyle('height').toInt();
		if (w > (window.getWidth() - 2 * this.options.zoomMargeLaterale))
		{
			h *= (window.getWidth() - 2 * this.options.zoomMargeLaterale) / w;
			w = (window.getWidth() - 2 * this.options.zoomMargeLaterale);
		}
		if (h > (window.getHeight() - this.options.zoomMargeBottom - this.options.zoomMargeTop))
		{
			w *= (window.getHeight() - this.options.zoomMargeBottom - this.options.zoomMargeTop) / h;
			h = (window.getHeight() - this.options.zoomMargeBottom - this.options.zoomMargeTop);
		}
		x = (window.getWidth() - w) / 2;
		y = (window.getHeight() - h - this.options.zoomMargeBottom) / 2;
		this.ZoomTransition = new Fx.Elements
		(
			[this.ZoomImage, this.ZoomFond],
			{
				duration: 500,
				transition: photo.options.zoomTransition,
				onComplete:function()
				{
					photo.ZoomLegende = new Element('div', {'position':'absolute'});
					photo.ZoomLegende.setText(photo.Img.alt);
					photo.ZoomLegende.injectTop(photo.ZoomFond);
					photo.ZoomLegende.setStyles
					(
						{
							'color':'white',
							'text-align':'center',
							'position':'absolute',
							'bottom':Math.max(0, (window.getHeight() - y - h - photo.ZoomLegende.getStyle('height').toInt()) / 2),
							'left':x,
							'width':w
						}
					);
					if (photo.options.album)
					{
						photo.ShowNextInZoom();
						photo.ShowPreviousInZoom();
					}
					photo.ZoomTransition = null;
				},
				onCancel:function()
				{
					photo.ZoomImage.remove();
					photo.ZoomFond.remove();
					photo.ZoomTransition = null;
				}
			}
		);
		this.ZoomImage.setStyles
		(
			{
				'position':'absolute',
				'z-index':this.options.zoomZIndexBase + 1,
				'top':yTarget,
				'left':xTarget,
				'width':wTarget,
				'height':hTarget,
				'opacity':1
			}
		);
		this.ZoomTransition.start
		({
			'0':
			{
				'top':[y],
				'left':[x],
				'width':[w],
				'height':[h]
			},
			'1':
			{
				'opacity':[.8]
			}
		});
    },
    
    ShowPreviousInZoom: function()
    {
	    var 	album;
	    var		index;
		var		x, y;
   	    var		photo = this;
	    

	    if (!this.options.album)
	    	return;
	    album = this.options.album.options.photos;
	    index = album.indexOf(this);
		if (index > 0)
		{
			this.ZoomImagePrecedente = album[index - 1].getImage().clone();
			this.ZoomImagePrecedente.injectInside(this.ZoomFond);
			x = .5 * this.ZoomImage.getStyle('left').toInt() - this.ZoomImagePrecedente.getCoordinates().width / 2;
			y = this.ZoomImage.getStyle('top').toInt() + this.ZoomImage.getCoordinates().height / 2 - this.ZoomImagePrecedente.getCoordinates().height / 2;
			this.ZoomImagePrecedente.setStyles
			({
				'position':'absolute',
				'left':x,
				'top':y
			});
			this.ZoomImagePrecedente.addEvent('click', function() {photo.GoToPreviousInZoom()});
			album[index - 1].StartPreload();
		}
    },
    
	UnZoom: function()
	{
		var		photo = this;
		
		if (photo.ZoomLegende)
		{
			photo.ZoomLegende.remove();
			photo.ZoomLegende = null;
		}
		if (photo.ZoomImageSuivante)
		{
			photo.ZoomImageSuivante.remove();
			photo.ZoomImageSuivante = null;
		}
		if (photo.ZoomImagePrecedente)
		{
			photo.ZoomImagePrecedente.remove();
			photo.ZoomImagePrecedente = null;
		}
		this.ZoomTransition = new Fx.Elements
		(
			[this.ZoomImage, this.ZoomFond],
			{
				duration: 500,
				transition: photo.options.zoomTransition,
				onComplete:function()
				{
					photo.ZoomFond.remove();
					photo.ZoomImage.remove();
					photo.ZoomTransition = null;
					photo.ZoomImage = null;
				},
				onCancel:function()
				{
					photo.ZoomFond.remove();
					photo.ZoomImage.remove();
					photo.ZoomTransition = null;
					photo.ZoomImage = null;
				}
			}
		);
		this.ZoomTransition.start
		({
			'0':
			{
				'top':[this.Img.getTop()],
				'left':[this.Img.getLeft()],
				'width':[this.Img.getCoordinates().width],
				'height':[this.Img.getCoordinates().height]
			},
			'1':
			{
				'opacity':[0]
			}
		});
	},
	
    StartPreload: function()
    {
		var	src;
		var	photo = this;
		
		if (this.Preloader != null)
			return;
		src = this.options.attributs.src;
		this.Preloader = new Element
		(
			'img',
			{
				'src':src,
				'events':
				{
					'load': function() {this.Preloader = null;}
				}
			}
		);
    }    
});

Gallerie.Photo.implement(new Options);

Gallerie.Album = new Class
({
	options:
	{
		photos: [],
		widthMax: 100,
		heightMax: 100,
		left: 0,
		top: 0,
		width: 0,
		height: 0,
		elementParent: null
	},
	
    initialize: function(options, elementParent)
    {
	    this.options.photos = [];
	    switch($type(options))
	    {
		case 'object':
	    	this.setOptions(options);
	    	if (elementParent)
	    		this.options.elementParent = elementParent;
	    	this.Create();
	    	break;
	    case 'string':
	    	if (elementParent)
	    		this.options.elementParent = elementParent;
	    	this.Create();
	    	this.Unserialize(options)
	    	break;
	    default:
	    	if (elementParent)
	    		this.options.elementParent = elementParent;
	    	this.Create();
	    	break;
    	}
    },
    
    Create: function()
    {
	    var		album = this;
		var		x, y, w, h;
		
	    this.Album = new Element('div', {'styles':{'position':'absolute'}});
	    y = this.options.top;
	    x = this.options.left;
	    if (this.options.elementParent)
	    {
	    	this.Album.injectTop($(this.options.elementParent));
		    w = (this.options.width > 0) ? this.options.width : ($(this.options.elementParent).getCoordinates().width - x);
		    h = (this.options.height > 0) ? this.options.height : ($(this.options.elementParent).getCoordinates().height - y);
    	}
    	else
    	{
	    	this.Album.injectTop($(document.body));
		    w = (this.options.width > 0) ? this.options.width : (window.getWidth() - x);
		    h = (this.options.height > 0) ? this.options.height : (window.getHeight() - y);
    	}
	    this.Album.setStyles
	    ({
		    'position':'absolute',
		    'top':y,
		    'left':x,
		    'width':w,
		    'height':h
    	});
    	window.addEvent('beforeunload', function() {album.Delete();});
    },
    
    Delete: function()
    {
	    var		i;
	    var		album = this;
	    
	    if (this.Album)
	    {
		    for (i = 0; i < this.options.photos.length; i++)
		    {
			    this.options.photos[i].Delete();
			}
		    this.Album.empty();
	    	this.Album.remove();
	    }
	    this.Album = null;
	    window.removeEvent('beforeunload', function() {album.Delete();});
	},
    
    Unserialize: function(url)
    {
		var theGallerie = this;
		var data;
		var gallerie;
		var photos;
		var i;
		var	src;
		var preview;
		var legende;
		
		data = new XHR
		(
			{
				method: 'get',
				async: false,
			    onSuccess: function(response, responseXml)
				{
					//try
					{
						gallerie = responseXml.getElementsByTagName('gallerie').item(0);
						photos = gallerie.getElementsByTagName('photo');
						for (i = 0; i < photos.length; i++)
						{
							src = photos.item(i).getAttribute('src') ? photos.item(i).getAttribute('src') : '';
							preview = photos.item(i).getAttribute('preview') ? photos.item(i).getAttribute('preview') : '';
							legende = photos.item(i).getAttribute('legende') ? photos.item(i).getAttribute('legende') : '';
							if (src != '')
								theGallerie.addPhoto(src.toString(), legende.toString(), preview.toString());
						}
					}
					//catch(ex)
					{
					}
				},
				onFailure: function(transport)
				{
					alert('Erreur : ' + url);
				}
			}
		);
		data.send(url);
    },
    
    addPhoto: function(attributs, legende, preview)
    {
	    var 	photo;
	    var		attrs;
	    var		colonne, ligne;
	    var		colonnes, lignes;
	    var		width, height;
	    
	    colonnes = Math.floor((this.Album.getCoordinates().width - this.options.widthMax * .15) / (this.options.widthMax * 1.15));
	    lignes = Math.floor((this.Album.getCoordinates().height - this.options.heightMax * .15) / (this.options.heightMax * 1.15));
	    colonne = Math.floor(this.options.photos.length % colonnes);
	    ligne = Math.floor(this.options.photos.length / colonnes);
	    //if (ligne < lignes)
	    {
		    switch($type(attributs))
		    {
			case 'object':
		    	width = this.options.widthMax;
		    	height = this.options.heightMax;
		    	attrs = $merge
		    	(
		    		attributs,
		    		{
			    		'elementParent':this.Album,
			    		'album':this,
			    		'attributs':
			    		{
				    		'style':
				    		{
					    		'position':'absolute',
						    	'width':width + 'px',
						    	'height':height + 'px',
					    		'left':Math.floor(this.options.widthMax * .15 + colonne * this.options.widthMax * 1.15) + 'px',
					    		'top':Math.floor(this.options.heightMax * .15 + ligne * this.options.heightMax * 1.15) + 'px'
					    	}
					    }
					}
				);
				if (legende)
			    	attrs = $merge(attrs, {'legende':legende});
			    photo = new Gallerie.Photo(attrs);
		    	break;
		    case 'string':
		    	if (!legende)
		    		legende = '';
		    	if (!preview)
		    		preview = '';
		    	width = this.options.widthMax;
		    	height = this.options.heightMax;
		    	attrs =
		    	{
			    	'elementParent':this.Album,
			    	'album':this,
			    	'legende':legende,
			    	'preview':preview,
			    	'attributs':
			    	{
				    	'src':attributs,
				    	'style':
				    	{
					    	'position':'absolute',
					    	'width':width + 'px',
					    	'height':height + 'px',
					    	'left':Math.floor(this.options.widthMax * .15 + colonne * this.options.widthMax * 1.15) + 'px',
					    	'top':Math.floor(this.options.heightMax * .15 + ligne * this.options.heightMax * 1.15) + 'px'
					    }
					}
				};
			    photo = new Gallerie.Photo(attrs);
		    	break;
		    case 'element':
		    	if (!legende)
		    		legende = '';
		    	if (!preview)
		    		preview = '';
		    	attrs =
		    	{
			    	'elementParent':this.Album,
			    	'album':this,
			    	'legende':legende,
			    	'preview':preview
				};
			    photo = new Gallerie.Photo(attributs, attrs);
		    	break;
	    	}
		    this.options.photos.include(photo);
	    }
    }
});

Gallerie.Album.implement(new Options);
