window.addEvent('domready', function() {
var scroll2 = new Scroller('gallery-bar-content', {
	area: 100,
	velocity: 0.5
}
);

var myElements = $('gallery-bar-content-inside').getChildren();
i = 0;
myElements.each(function(el, i)
{
	el.style.MozOpacity = '0.3';
  el.style.opacity = '0.3';
  el.style.khtmlopacity = '0.3';
  el.firstChild.style.filter = 'alpha(opacity=30)';
  
	if (i == 0)
	{
		el.ClassName = 'clicked';
		el.style.MozOpacity = '0.99';
  	el.style.opacity = '0.99';
  	el.style.khtmlopacity = '0.99';
  	el.firstChild.style.filter = 'alpha(opacity=99)';
  	showFoto(el.href);
	}
	
  el.onmouseover = function()
  {
  	this.style.MozOpacity = '0.99';
  	this.style.opacity = '0.99';
  	this.style.khtmlopacity = '0.99';
  	this.firstChild.style.filter = 'alpha(opacity=99)';
	};
	el.onmouseout = function(){
		if (this.ClassName !== 'clicked')
		{
    	this.style.MozOpacity = '0.2';
  		this.style.opacity = '0.2';
  		this.style.khtmlopacity = '0.2';
  		this.firstChild.style.filter = 'alpha(opacity=20)';
  	}
	};
	el.onclick = function(){
		this.ClassName = 'clicked';
    this.style.MozOpacity = '0.99';
  	this.style.opacity = '0.99';
  	this.style.khtmlopacity = '0.99';
  	this.firstChild.style.filter = 'alpha(opacity=99)';
  	showFoto(this.href);
  	return false;
	};
	i = i+1;
});

// Mousemove
$('gallery-bar').addEvent('mouseover', scroll2.start.bind(scroll2));
$('gallery-bar').addEvent('mouseout', scroll2.stop.bind(scroll2));
});

function showFoto (img)
{
	var busy = false, timer, loadedImages = [], gallery = $('gallery'); 
	var images = [img];
			
	gallery.setStyle('background', 'url(/style/img/gallery-progress.gif) 50% 50% no-repeat');
	//var galleryImgs = $$('#gallery img');
	var galleryImgs = document.getElementById('gallery');
	while (galleryImgs.childNodes[0]) {
    galleryImgs.removeChild(galleryImgs.childNodes[0]);
	}
	if (!busy)
	{
		busy = true;
		gallery.setStyle('display', 'block');
		new Asset.images(images, {
			onProgress: function(i) {
				this.setStyles({
					'position': 'absolute',
					'opacity': 0,
					'left': 0,
					'top': 0
				});
				loadedImages[i] = this;
			},
			onComplete: function() {
				//var fx = $('gallery').efect('opacity').start(1);
				timer = 0;
				loadedImages.each(function(image, i) {
					timer += 500;
					image.inject(gallery);
					fx = function() {
						gallery.setStyle('background', 'transparent');
						var imgEffect = image.effect('opacity', {duration: 1500});
						imgEffect.start(1).chain(function() {
							if (i < loadedImages.length - 2) {
								this.start(0).chain(function() {
									//image.remove();
								});
							}
						});
						
					}.delay(timer * 2);
				});
			}
		});
		
	}
}