
var Gallery = function ( elementName ) {

	if ( $(elementName) ) {
		this.element = $(elementName);
		this.init();
	} else 
		return null;
}

Gallery.prototype.init = function() {
	
	if ( !$('large') ) {
		this.largeFrame = new Element('div', {'class': 'large'}).injectBefore(this.element);
		this.table = '<table><tr><td id="large"></td></tr></table>';
		this.largeFrame.setHTML(this.table);
		this.large = $('large');
	} 
	
	this.large = $('large');
	this.large.setHTML('');
	this.largeFrame.style.display = 'none';


	window.images = new Array();
	var links = $$('a');
	
	links.each ( function(link) {

		if ( link.rel && link.rel.indexOf('gallery') >= 0 ) {
			
			link.index = images.length;
			images.push(link.href);
			
			link.addEvent("click", function(e){ 
				
				link.blur();
				e = new Event(e).stop(); 
				window.gallery.Show(link.index);
				
			});
			
			link.rel = null;
			
			//	nie ładujemy wszystkich naraz
			//var img = new Asset.images(link.href);
			
		}
		
	});
}

Gallery.prototype.Show = function (index) {
	
	this.current = index;
	this.largeFrame.style.display = 'block';
	this.element.style.display = 'none';
	this.large.setHTML('');
	
	this.img = new Element('img', {'src': window.images[index], 'alt': ''});
	
	var finishedLoading = function(){   
		window.gallery.imgEffect.start(1);
	};

	var img = new Asset.images(window.images[index],{ onComplete: finishedLoading });

	this.imgEffect = this.img.effect('opacity',{duration: 600});
	this.imgEffect.set(0);
	
	this.img.injectInside(this.large);
	
	this.large.addEvent("click", function(event){ 
		var event = new Event(event);
	    var x = event.page.x-window.gallery.large.getPosition().x; 
	    if ( x > 130 && x < 600-80 )
	    	window.gallery.Close();
	});		
	
	if (!this.back ) {
		this.back = new Element('div', {'class': 'back', 'id': 'back' }).injectInside(this.largeFrame);		
		this.next = new Element('div', {'class': 'next', 'id': 'next' }).injectInside(this.largeFrame);		
		
		
		this.backEffect = this.back.effect('opacity',{duration: 200});
		this.backEffect.set(0.01);
		this.back.addEvent("click", function(e){ window.gallery.Back(); });
		this.back.addEvent("mouseenter", function(e){	
														window.gallery.nextEffect.stop();
														window.gallery.backEffect.stop();
														window.gallery.nextEffect.start(0.01);
														window.gallery.backEffect.start(0.3);	});
		this.back.addEvent("mouseleave", function(e){	
														window.gallery.backEffect.stop();
														window.gallery.backEffect.start(0.01);	});
		
		
		this.nextEffect = this.next.effect('opacity',{duration: 200});
		this.nextEffect.set(0.01);
		this.next.addEvent("click", function(e){ window.gallery.Next(); });
		this.next.addEvent("mouseenter", function(e){	
														window.gallery.nextEffect.stop();
														window.gallery.backEffect.stop();
														window.gallery.nextEffect.start(0.3);
														window.gallery.backEffect.start(0.01);	});
		this.next.addEvent("mouseleave", function(e){	
														window.gallery.nextEffect.stop();
														window.gallery.nextEffect.start(0.01);	});
	}
	
	var indexNext = this.current + 1;
	if ( indexNext >= window.images.length )
		indexNext = 0;
		
	var indexPrev = this.current - 1;
	if ( indexPrev < 0 )
		indexPrev = window.images.length - 1;
	
	var imgBuffNext = new Asset.images(window.images[indexNext]);
	var imgBuffPrex = new Asset.images(window.images[indexPrev]);
	
}

Gallery.prototype.Next = function () {
	var index = this.current + 1;
	if ( index < window.images.length )
		this.Show(index);
	else
		this.Show(0);
}

Gallery.prototype.Back = function () {
	var index = this.current - 1;
	if ( index >= 0 )
		this.Show(index);
	else
		this.Show(window.images.length-1);
}

Gallery.prototype.Close = function () {
	this.imgEffect.start(0);
	this.largeFrame.style.display = 'none';
	this.element.style.display = 'block';
}
