var LightboxIframe = {

	init: function(options){
		this.options = Object.extend({
			resizeDuration: 400,
			resizeTransition: Fx.Transitions.sineInOut,
			initialWidth: 250,
			initialHeight: 250,
			animateCaption: true
		}, options || {});
		
		this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);
		this.eventPosition = this.position.bind(this);

		this.overlay = new Element('div').setProperty('id', 'lbiOverlay').injectInside(document.body);

		this.center = new Element('div').setProperty('id', 'lbiCenter').setStyles({width: this.options.initialWidth+'px', height: this.options.initialHeight+'px', marginLeft: '-'+(this.options.initialWidth/2)+'px', display: 'none'}).injectInside(document.body);       
        this.image = new Element('div').setProperty('id', 'lbiImage').injectInside(this.center);
		this.iFrame = new Element('iframe').setProperty('id', 'lbiIframe');
		this.iFrame.setProperty('height', 100 );
		this.iFrame.setProperty('width',  100 );
		this.iFrame.setProperty('frameBorder', 0 );
		this.iFrame.setProperty('scrolling', 'no' );
		this.iFrame.setProperty('src', '');
        this.iFrame.setStyle('opacity', 0);
		this.iFrame.injectInside(this.image);

		this.bottomContainer = new Element('div').setProperty('id', 'lbiBottomContainer').setStyle('display', 'none').injectInside(document.body);
		this.bottom = new Element('div').setProperty('id', 'lbiBottom').injectInside(this.bottomContainer);
		new Element('a').setProperties({id: 'lbiCloseLink', href: '#'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);
		this.caption = new Element('div').setProperty('id', 'lbiCaption').injectInside(this.bottom);
		this.number = new Element('div').setProperty('id', 'lbiNumber').injectInside(this.bottom);
		new Element('div').setStyle('clear', 'both').injectInside(this.bottom);

		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: 500}).hide(),
			resize: this.center.effects({duration: this.options.resizeDuration, transition: this.options.resizeTransition, onComplete: nextEffect}),
			image: this.image.effect('opacity', {duration: 500, onComplete: nextEffect}),
			bottom: this.bottom.effect('margin-top', {duration: 400, onComplete: nextEffect})
		};
	},
	
	position: function(){
		this.overlay.setStyles({top: window.getScrollTop()+'px', height: window.getHeight()+'px'});
	},
	
	show: function(url, title, winW, winH){
		return this.open([[url, title, winW, winH]], 0);
	},
	
	setup: function(open){
		var elements = $A(document.getElementsByTagName('object'));
		if (window.ie) elements.extend(document.getElementsByTagName('select'));
		elements.each(function(el){ el.style.visibility = open ? 'hidden' : ''; });
		var fn = open ? 'addEvent' : 'removeEvent';
		window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
		document[fn]('keydown', this.eventKeyDown);
		this.step = 0;
	},
	
	open: function(images, imageNum){
		this.images = images;
		this.position();
		this.setup(true);
		this.top = window.getScrollTop() + (window.getHeight() / 15);
		this.center.setStyles({top: this.top+'px', display: ''});
		this.fx.overlay.start(0.8);
		return this.changeImage(imageNum);
	},
	
	changeImage: function(imageNum){
		if (this.step || (imageNum < 0) || (imageNum >= this.images.length)) return false;
		this.step = 1;
		this.activeImage = imageNum;
		this.fx.image.hide();
		this.center.className = 'lbiLoading';
		this.nextEffect();
		return false;
	},
	
	keyboardListener: function(event){
		switch (event.keyCode){
			case 27: case 88: case 67: this.close(); break;
		}
	},
	
	nextEffect: function(){
		switch (this.step++){
		case 1:
			this.center.className = '';
            
            this.iFrame.setProperty('height', this.images[this.activeImage][3]+'px' );
		    this.iFrame.setProperty('width',   this.images[this.activeImage][2]+'px' );
            this.iFrame.setProperty('src', this.images[this.activeImage][0] );
            this.iFrame.setStyle('opacity', 0);
			this.image.style.width = this.bottom.style.width = this.images[this.activeImage][2]+'px';
			this.image.style.height = this.images[this.activeImage][3]+'px';

			this.caption.setHTML(this.images[this.activeImage][1] || '');
			this.number.setHTML((this.images.length == 1) ? '' : 'Image '+(this.activeImage+1)+' of '+this.images.length);

			if (this.center.clientHeight != this.image.offsetHeight){
				this.fx.resize.start({height: this.image.offsetHeight});
				break;
			}
			this.step++;
		case 2:
			if (this.center.clientWidth != this.image.offsetWidth){
				this.fx.resize.start({width: this.image.offsetWidth, marginLeft: -this.image.offsetWidth/2});
				break;
			}
			this.step++;
		case 3:
			this.bottomContainer.setStyles({top: (this.top + this.center.clientHeight)+'px', height: '0px', marginLeft: this.center.style.marginLeft, display: ''});
			this.fx.image.start(1);
			break;
		case 4:
			this.iFrame.setStyle('opacity',1);
            if (this.options.animateCaption){
				this.fx.bottom.set(-this.bottom.offsetHeight);
				this.bottomContainer.style.height = '';
				this.fx.bottom.start(0);
				break;
			}
			this.bottomContainer.style.height = '';
		case 5:
			this.step = 0;
		}
	},
	close: function(){
		this.iFrame.setProperty('src', this.images[this.activeImage][0] );
        if (this.step < 0) return;
		this.step = -1;
		for (var f in this.fx) this.fx[f].stop();
		this.center.style.display = this.bottomContainer.style.display = 'none';
		this.fx.overlay.chain(this.setup.pass(false, this)).start(0);
		return false;
	}
};

window.addEvent('domready', LightboxIframe.init.bind(LightboxIframe));
