function Gallery() {};

Gallery.pictures = new Array();
Gallery.opened = false;
Gallery.current = null;

Gallery.init = function(containerClass) {
	document.getElements('.'+containerClass).each( function(item) {
		item.getElements('img').each( function(item) {
			Gallery.pictures[item.uid] = item;
			item.style.cursor = 'pointer';
			item.onclick = function() {	Gallery.show(item.uid);	};
			document.onkeyup = function(e) {
				var key = (window.event) ? event.keyCode : e.keyCode;
				if(key == 27)
					Gallery.close();
				else if(key == 37)
					Gallery.previous();
				else if(key == 39)
					Gallery.next();
			};
		});
	});
};

Gallery.open = function() {
	var div, img, i=1;
	Gallery.mask.inject(document.getElements('body')[0], 'top');
	Gallery.container1.inject(document.getElements('body')[0], 'top');
	Gallery.container2.inject(document.getElements('body')[0], 'top');

	Gallery.picture.inject(Gallery.container1, 'top');
	Gallery.picture.onclick = function() {	if(!Gallery.next()) Gallery.close(); };
	
	Gallery.pictures.each( function(item) {
		div = new Element('div', { });
		img = new Element('img', { 'src': item.src, 'alt': item.alt });
		span = new Element('span', { 'text': i });
		img.onclick = function() { Gallery.show(item.uid); };
		img.inject(div, 'top');
		span.inject(div, 'top');
		div.inject(Gallery.container2, 'bottom');
		i++;
	});
	
	Gallery.opened = true;
};

Gallery.close = function() {
	Gallery.mask.destroy();
	Gallery.container1.destroy();
	Gallery.container2.destroy();
	
	Gallery.opened = false;
};

Gallery.show = function(index) {
	if(!Gallery.opened)
		Gallery.open();
	
	var morphObject = new Fx.Morph(Gallery.picture, {
		link: 'chain',
		duration : 100,
		onComplete: function(item) {
			Gallery.picture.src = Gallery.pictures[index].src;
			window.setTimeout(function() {
				var ContainerH = Gallery.container1.offsetHeight;
				var PictureH = Gallery.picture.offsetHeight;
				var MarginTop = Math.floor((ContainerH - PictureH) / 2);
				Gallery.picture.setStyle('margin-top', MarginTop+'px');
				Gallery.picture.fade(1);
			}, 10);
		}
	});
	morphObject.start({	'opacity': 0 });

	Gallery.current = index;
	return true;
};

Gallery.previous = function() {
	var previousUid = null;
	var bool = false;
	Gallery.pictures.each(function(item) {
		if(item.uid == Gallery.current) {
			if(previousUid != null)
				bool = Gallery.show(previousUid);
		}
		previousUid = item.uid;
	});
	return bool;
};

Gallery.next = function() {
	var bool = false;
	var uid = null;
	Gallery.pictures.each(function(item) {
		if(bool)
			uid = item.uid;
		bool = (item.uid == Gallery.current);
	});
	if(uid != null)
		Gallery.show(uid);
	return (uid != null);
};


Gallery.mask 		= new Element('div', {	'class'	:	'galleryMask'				});
Gallery.container1 	= new Element('div', {	'class' :	'galleryPictureContainer'	});
Gallery.container2 	= new Element('div', {	'class' :	'galleryListContainer'		});
Gallery.picture 	= new Element('img', {	});
