var fotos = new Class({
	Implements: [Events, Options],
	
	options:{
		imagesLinks:'div.mini a',
		selectedCls:'selected',
		bigImg:'big'
	},

	initialize: function(options) {
		this.setOptions(options);
		
		this.imglist = $$( this.options.imagesLinks );

		if ( !document.getElementById(this.options.bigImg ) )
			return false;
		
		this.viewer = $( this.options.bigImg );
		this.size = this.viewer.getSize();
		
		this.img = this.viewer.getElement('img');
		this.podpis = this.viewer.getElement('span');
		
/*		this.img.onload = function()	{
			this.show.delay(100, this);
		}.bind(this);*/
		
		this.preloadimg = new Element('img', {
			events: {
				load: function(){
					this.show.delay(100, this)
				}.bind(this)
			}
		});

		this.preload = new Element('div', {});
		this.preload.addClass('preloader');
		this.preload.setStyle('opacity',0.4);
		
		// blind clicks
		this.imglist.each( function(e,i)	{
			e.addEvent('click', this.click.bind(this));
		}.bind(this));
		
		//this.preloadimg.set('src', this.imglist[0].href );
		this.imglist[0].addClass('selected');
		// click - preload - show
		this.clicked = this.imglist[0];
		this.preload.inject( this.viewer, 'top' );
	},
	
	click: function(e)	{
		e.stop();
		var el = e.target;
		if ( el.tagName != 'A' ) {
			el = el.parentNode;//getParent('a');
		}
		this.clicked = el;

		var src = el.href;

		if ( this.preloadimg.src == src )
			return false;
		
		this.preloadimg.set('src', src );
		
		var s = this.img.getSize();

		this.preload.setStyles({
			left:(this.size.x - s.x),
			width:s.x,
			height:s.y
		});
		
		this.preload.inject( this.viewer, 'top' );

		this.imglist.each( function(l,i)	{
			l.removeClass('selected');
		});
		el.addClass('selected');
	},

	show: function() {
		this.img.set('src', this.preloadimg.src);
		if ( this.preload )
			this.preload = this.preload.dispose();
		if ( this.clicked.get('title') ) {
			var tt = this.clicked.get('title');//.replace(/-nl-/,'<br />');
			this.podpis.set('html', tt );
		}
	}
});

window.addEvent('load', function()	{
	var f = new fotos();
});

