
var ImageGallery = Class.create({

	initialize: function(className) 
	{
		this.list = $$('a.' + className);

		this.count = this.list.length;
		this.visible = false;
		
		this.list.each(function(element){
			element.onclick = function(){ return false };
			element.observe('click', function(event){
				this.index = this.list.indexOf(element);
				this.show(this.index);
				this.play();
			}.bindAsEventListener(this, element));
		}.bind(this));
	
	}, 
	
	show: function(index)
	{
		if (!this.visible)
		{
			this.visible = true;
			
			this.overlay = new Element('div', {'id': 'igOverlay'});
			this.overlay.style.height = document.viewport.getHeight() + 'px';

			this.container = new Element('div', {'id': 'igContainer'});
			this.container.style.height = document.viewport.getHeight() + 'px';

			if (Prototype.Browser.IE && !navigator.appVersion.match(/\b7.0\b/)) { // Preparing IE 6 for showing modalbox
				window.scrollTo(0,0);
			}
			
			containerLeft = Math.round((document.viewport.getWidth() - 842) / 2);
			
			this.container.style.left = containerLeft + 'px';

			this.closeX = new Element('div', {'id': 'igCloseX'}).update(new Element('img', {src: 'http://ps.ingatlanonki.hu/img/imagegallery/closeX.gif'}));
			this.closeX.observe('click', function(){this.close()}.bind(this));
			this.container.insert(this.closeX);

			this.image = new Element('img', {'id': 'igImage'});
			this.container.insert(this.image);

			this.caption = new Element('div', {'id': 'igCaption'}).hide();
			this.container.insert(this.caption);
	
			$(document.body).insert({'Top': this.overlay});

			$(document.body).insert({'Top': this.container});


			this.buttons = new Element('div', {id: 'igButtons'});
	
			this.backBtn = new Element('a', {'id': 'igBack', 'class': 'igButton'}).update(new Element('img', {'src': 'http://ps.ingatlanonki.hu/img/imagegallery/back.gif'}));
			this.backBtn.observe('click', function(){if (this.pe) this.stop(); this.back()}.bind(this));
	
			this.playBtn = new Element('a', {'id': 'igPlay', 'class': 'igButton'}).update(new Element('img', {'src': 'http://ps.ingatlanonki.hu/img/imagegallery/play.gif'}));;
			this.playBtn.observe('click', function(){this.play()}.bind(this));

			this.stopBtn = new Element('a', {'id': 'igStop', 'class': 'igButton', 'style': 'display:none'}).update(new Element('img', {'src': 'http://ps.ingatlanonki.hu/img/imagegallery/pause.gif'}));;
			this.stopBtn.observe('click', function(){this.stop()}.bind(this));
	
			this.forwardBtn = new Element('a', {'id': 'igForward', 'class': 'igButton'}).update(new Element('img', {'src': 'http://ps.ingatlanonki.hu/img/imagegallery/forward.gif'}));;
			this.forwardBtn.observe('click', function(){if (this.pe) this.stop(); this.forward()}.bind(this));

			if (this.print)
			{
				this.printBtn = new Element('a', {'id': 'igPrint', 'class': 'igButton'}).update(new Element('img', {'src': 'http://ps.ingatlanonki.hu/img/imagegallery/print.gif'}));;
				this.printBtn.observe('click', function(){this.print()}.bind(this));
			}

			this.closeBtn = new Element('a', {'id': 'igClose', 'class': 'igButton'}).update(new Element('img', {'src': 'http://ps.ingatlanonki.hu/img/imagegallery/close.gif'}));;
			this.closeBtn.observe('click', function(){this.close()}.bind(this));
	
			this.buttons.insert(this.backBtn);
			this.buttons.insert(this.playBtn);
			this.buttons.insert(this.stopBtn);
			this.buttons.insert(this.forwardBtn);
			if (this.printBtn)
				this.buttons.insert(this.printBtn);
			this.buttons.insert(this.closeBtn);
	
			this.container.insert(this.buttons);

		}
		
		if (this.list[index].title)
		{
			this.caption.update(this.list[index].title);
			Effect.Appear(this.caption, {duration: 0.2});
		}
		else
		{
			this.caption.update();
			Effect.Fade(this.caption, {duration: 0.2});
		}

		this.image.src = this.list[index];
		
		// Következő kép gyorstárazása:
		nextIndex = (this.index + 1) % this.count;
		img = new Image;
		img.src = this.list[nextIndex];

		// Előző kép gyorstárazása:
		prevIndex = (this.index - 1 + this.count) % this.count;
		img = new Image;
		img.src = this.list[prevIndex];
		
		this.index = index;
		
		Event.observe(window, 'resize', function(){
			this.overlay.setStyle({height: document.viewport.getHeight() + 'px'});
			this.container.setStyle({
				height: document.viewport.getHeight() + 'px',
				left: Math.round((document.viewport.getWidth() - 842) / 2) + 'px'
			});
		}.bindAsEventListener(this));
	},


	setPrintFunc: function(printFunc) {
		this.print = function() {
			this.stop();
			printFunc();
		}
	},


	forward: function() {
		this.index = ++this.index % this.count;
		this.show(this.index);
	},


	back: function() {
		this.index = (--this.index + this.count) % this.count;
		this.show(this.index);
	},


	play: function() {
		this.playBtn.hide();
		this.stopBtn.show();
		this.pe = new PeriodicalExecuter(function() {
			this.forward();
		}.bind(this), 3)
	},
	
	
	stop: function() {
		this.pe.stop();
		this.playBtn.show();
		this.stopBtn.hide();
	},


	close: function() {
		if (this.pe) 
			this.pe.stop();
		this.overlay.hide();
		this.container.hide();
		this.visible = false;
	},
	
	
	currentImage: function() {
		return this.image.src;
	},


	currentCaption: function() {
		return this.caption.innerHTML;
	}

});

