
var LightFrame = {

	init: function(options) {
		this.options = Object.extend({
			resizeTransition: Fx.Transitions.sineInOut,
			resizeDuration: 400,	// Duration of height and width resizing (ms)
			initialWidth: 650,		// Initial width of the box (px)
			initialHeight: 550,		// Initial height of the box (px)
			animateCaption: false	// Enable/Disable caption animation
		}, options || {});
		
		this.anchors = [];
		$each(document.links, function(el){
			if (el.rel && el.rel.test(/^lightframe/i)){
				el.onclick = this.click.pass(el, this);
				this.anchors.push(el);
			}
		}, this);
		this.eventPosition = this.position.bind(this);

		this.overlay = new Element('div').setProperty('id', 'lbOverlay').injectInside(document.body);
		
		this.center = new Element('div').setProperty('id', 'lbCenter').setStyles({width: this.options.initialWidth+'px', height: this.options.initialHeight+'px', marginLeft: '-'+(this.options.initialWidth/2)+'px', display: 'none'}).injectInside(document.body);
		this.frame = new Element('iframe').setProperty('id', 'frameContainer').setStyles({width: (this.options.initialWidth-50)+'px', height: (this.options.initialHeight-75)+'px', margin: '20px 20px 10px 20px', display: 'none'}).injectInside(this.center);
		
		this.bottom = new Element('div').setProperty('id', 'lbBottom').setStyle('display', 'block').injectInside(this.center);
		new Element('a').setProperties({id: 'lbCloseLink', href: '#'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);
		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, onComplete: nextEffect }),
			bottom: this.bottom.effects({ duration: 400, onComplete: nextEffect })
		};

	},

	click: function(link) {

		return this.show(link.href, link.title);
		
	},

	show: function(url, title) {
		return this.open([[url, title]], 0);
	},

	open: function(images, imageNum) {
		this.frames = 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);

		this.frame.setStyles({display: 'block'});
		this.frame.src = this.frames[imageNum][0];
		this.bottom.setStyles({margin: '10px 20px 10px 0'});

		return false;
	},

	position: function() {
		this.overlay.setStyles({top: window.getScrollTop()+'px', height: window.getHeight()+'px'});
	},

	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);
		this.step = 0;
	},

	nextEffect: function() {
		switch(this.step++) {
		case 1:
			this.center.className = '';
			this.frame.setStyles({backgroundImage: 'url('+this.frames[this.activeImage][0]+')', width: this.preload.width+'px'});
			
			if(this.activeImage != 0) this.preloadPrev.src = this.frames[this.activeImage - 1][0];
			if(this.activeImage != (this.frames.length - 1)) this.preloadNext.src = this.frames[this.activeImage + 1][0];
			if(this.center.clientHeight != this.frame.offsetHeight) {
				this.fx.resize.custom({height: [this.center.clientHeight, this.frame.offsetHeight]});
				break;
			}
			this.step++;
		case 2:
			if(this.center.clientWidth != this.frame.offsetWidth) {
				this.fx.resize.custom({width: [this.center.clientWidth, this.frame.offsetWidth], marginLeft: [-this.center.clientWidth/2, -this.frame.offsetWidth/2]});
				break;
			}
			this.step++;
		case 3:
			this.bottom.setStyles({top: (this.top + this.center.clientHeight)+'px', width: this.image.style.width, marginLeft: this.center.style.marginLeft, display: ''});
			this.fx.image.custom(0, 1);
			break;
		case 4:
			if(this.options.animateCaption) {
				this.fx.bottom.custom({opacity: [0, 1], height: [0, this.bottom.scrollHeight]});
				break;
			}
			this.bottom.setStyles({opacity: '1', height: this.bottom.scrollHeight+'px'});
		case 5:
			if(this.activeImage != 0) this.prevLink.style.display = '';
			if(this.activeImage != (this.images.length - 1)) this.nextLink.style.display = '';
			this.step = 0;
		}
	},

	close: function() {
		if(this.step < 0) return;
		this.step = -1;
		if (this.preload){
			this.preload.onload = Class.empty;
			this.preload = null;
		}
		for (var f in this.fx) this.fx[f].stop();
		this.center.style.display = 'none';
		this.fx.overlay.chain(this.setup.pass(false, this)).start(0);
		this.frame.src = '';
		return false;
	}
};

window.addEvent('domready', LightFrame.init.bind(LightFrame));
