/*
	Galbox
	by Fluid Creativity 2007
	Based on Slimbox v1.22 - The ultimate lightweight Lightbox clone
	by Christophe Beyls (http://www.digitalia.be) - MIT-style license.
	Inspired by the original Lightbox v2 by Lokesh Dhakar.
*/

var Lightbox = {

	init: function(options) {
		this.options = Object.extend({
			resizeDuration: 400,	// Duration of height and width resizing (ms)
			initialWidth: 640,	// Initial width of the box (px)
			initialHeight: 480	// Initial height of the box (px)
		}, options || {});
		
		this.anchors = [];
		$$('a').each(function(el){
			if(el.rel && el.href && el.rel.test(/^lightbox/i)) {
				if (el.rel != 'lightbox' && !el.hasClass("mainImage")) el.setStyles({
					display: 'none',
					height: 0,
					width: 0
				});
				el.onclick = this.click.pass(el, this);
				this.anchors.push(el);
			}
		}, this);
		this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);
		this.eventPosition = this.position.bind(this);
		
		this.overlay = new Element('div', {id: 'lbOverlay'}).injectInside(document.body);

		this.container = new Element('div', {
			id: 'lbContainer',
			styles: {
				display: 'none',
				marginLeft: (this.options.initialWidth + 28) / -2
			}
		}).injectInside(document.body);
		
		this.head = new Element('div', {
			id: 'lbHead',
			styles: {display: 'none'}
		}).injectInside(this.container);
		
		this.body = new Element('div', {
			id: 'lbBody',
			styles: {display: 'none'}
		}).injectInside(this.container);
		
		this.heading = new Element('h2', {id: 'lbHeading'}).injectInside(this.body);
		
		this.closer = new Element('a', {
			id: 'lbCloseLink',
			href: '#'
		}).injectInside(this.heading);
		
		this.closer.onclick = this.overlay.onclick = this.close.bind(this);
		
		this.center = new Element('div', {
			id: 'lbCenter',
			styles: {
				width: this.options.initialWidth,
				height: this.options.initialHeight,
				display: 'none'
			}
		}).injectInside(this.body);

		this.image = new Element('div', {id: 'lbImage'}).injectInside(this.center);

		this.prevLink = new Element('a', {
			id: 'lbPrevLink',
			href: '#',
			styles: {display: 'none'}
		}).injectInside(this.image);
		this.nextLink = this.prevLink.clone().setProperty('id', 'lbNextLink').injectInside(this.image);
		this.prevLink.onclick = this.previous.bind(this);
		this.nextLink.onclick = this.next.bind(this);

		this.thumbList = new Element('ul', {
			id: 'lbThumbList',
			styles: {display: 'none'}
		}).injectInside(this.body);

		this.bottom = new Element('div', {
			id: 'lbBottom',
			styles: {display: 'none'}
		}).injectInside(this.body);

		this.caption = new Element('div', {id: 'lbCaption'}).injectInside(this.bottom);
		this.number = new Element('div', {id: 'lbNumber'}).injectInside(this.bottom);
		
		this.foot = new Element('div', {
			id: 'lbFoot',
			styles: {display: 'none'}
		}).injectInside(this.container);

		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: 500}).hide(),
			image: this.image.effect('opacity', {
				duration: 500,
				onComplete: nextEffect
			})
		};
		
		this.preloadPrev = new Image();
		this.preloadNext = new Image();
	},

	click: function(link) {
		if (link.rel == 'lightbox') return this.show(link.href, link.title);
		
		var j, imageNum, images = [];
		this.anchors.each(function(el){
			if (el.rel == link.rel) {
				for (j = 0; j < images.length; j++)
					if (images[j][0] == el.href) break;
				if (j == images.length) {
					images.push([el.href, el.title, $E('img', el).src]);
					if(el.href == link.href) imageNum = j;
				}
			}
		}, this);
		return this.open(images, imageNum);
	},

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

	open: function(images, imageNum) {
		$ES('*', this.thumbList).each(function(el){ el.remove(); });
		this.images = images;
		this.setupThumbs();
		this.position();
		this.setup(true);
		this.top = window.getScrollTop() + (window.getHeight() / 15);
		this.container.setStyles({
			top: this.top,
			display: ''
		});
		this.center.setStyle('display', '');
		this.fx.overlay.custom(0.8);
		return this.changeImage(imageNum);
	},

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

	setup: function(open) {
		var elements = $$('object');
		elements.extend($$((window.ActiveXObject ? 'select' : 'embed')));
		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;
	},
	
	setupThumbs: function() {
		this.images.each(function(image, i){
			new Element('a', {href: '#'}).adopt(
				new Element('img', {src: image[2].replace(/[a-z]+-\d+x\d+/, 'standard-64x48')})
			).injectInside(
				new Element('li').injectInside(this.thumbList)
			).onclick = function(){
				this.changeImage(i);
			}.bind(this);
		}.bind(this));
	},

	keyboardListener: function(event) {
		switch(event.keyCode) {
			case 27: case 88: case 67: this.close(); break;
			case 37: case 80: this.previous(); break;	
			case 39: case 78: this.next();
		}
	},

	previous: function() {
		return this.changeImage(this.activeImage - 1);
	},

	next: function() {
		return this.changeImage(this.activeImage + 1);
	},

	changeImage: function(imageNum) {		
		if(this.step || (imageNum < 0) || (imageNum >= this.images.length)) return false;
		this.step = 1;
		this.activeImage = imageNum;

		this.prevLink.style.display = this.nextLink.style.display = 'none';
		this.bottom.setStyle('opacity', 0);
		this.fx.image.hide();
		this.center.className = 'lbLoading';

		this.preload = new Image();
		this.preload.onload = this.nextEffect.bind(this);
		this.preload.src = this.images[imageNum][0];
		return false;
	},

	nextEffect: function() {
		switch(this.step++) {
		case 1:
			this.center.className = '';
			this.image.setStyles({
				backgroundImage: 'url(' + this.images[this.activeImage][0] + ')',
				width: this.preload.width
			});
			this.image.style.height = this.prevLink.style.height = this.nextLink.style.height = this.preload.height + 'px';

			this.heading.setHTML(this.images[0][1] || '');
			this.closer.injectTop(this.heading);
			this.number.setHTML((this.images.length == 1) ? '' : 'Image ' + (this.activeImage + 1) + ' of ' + this.images.length);

			if (this.activeImage != 0) this.preloadPrev.src = this.images[this.activeImage - 1][0];
			if (this.activeImage != (this.images.length - 1)) this.preloadNext.src = this.images[this.activeImage + 1][0];
			if (this.center.clientHeight != this.image.offsetHeight) {
				this.center.setStyle('height', this.image.offsetHeight);
				this.nextEffect();
				break;
			}
			this.step++;
		case 2:
			if(this.center.clientWidth != this.image.offsetWidth) {
				this.center.setStyle('width', 640);
				this.container.setStyle('marginLeft', -334);
				this.nextEffect();
				break;
			}
			this.step++;
		case 3:
			this.thumbList.setStyles({
				width: this.image.style.width,
				display: ''
			});
			this.bottom.setStyles({
				width: this.image.style.width,
				display: ''
			});
			this.head.setStyle('display', '');
			this.body.setStyle('display', '');
			this.foot.setStyle('display', '');
			
			this.fx.image.start(0, 1);
			break;
		case 4:
			this.bottom.setStyles({
				opacity: 1,
				height: this.bottom.scrollHeight
			});
		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].clearTimer();
		this.center.style.display = this.thumbList.style.display = this.bottom.style.display = this.head.style.display = this.body.style.display = this.foot.style.display = this.container.style.display = 'none';
		this.fx.overlay.chain(this.setup.pass(false, this)).custom(0);
		return false;
	}
};

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

