//My PHP DropBox Gallery Lightbox. Copyright (c) 2009-2010 Jonathan Ochej <http://2boandco.com>, MIT Style License. This file is part of My PHP DropBox Gallery.
var DB_Lightbox = new Class({

	Implements: [Events, Options],

	options: {
		overlay: 0.7,
		lightbox_play_text : 'Play Slideshow',
		lightbox_stop_text : 'Stop Slideshow'
	},

	initialize: function(options){
		this.setOptions(options);

		var links = $$('a').filter(function(el) {
			return el.rel && el.rel.test(/^lightbox/i);
		});

		// Si il y a des images de lightbox
		if(links.length > 0) {
			this.showLightbox = false;

			// Text traduits dans la bonne langue
			if($('lightbox_play_text') && $('lightbox_play_text').get('text')) this.options.lightbox_play_text = $('lightbox_play_text').get('text');
			if($('lightbox_stop_text') && $('lightbox_stop_text').get('text')) this.options.lightbox_stop_text = $('lightbox_stop_text').get('text');

			this.currentShow = 0;
			this.currentSlideShow = false;

			this.lightbox = new Element('div', {
				'id': 'lightbox',
				'styles': {
					'display': 'none'
					//'width': window.getScrollSize().x + 'px' // Pas obligatoire, width:100% marche très bien!
				}
			});
			$(document.body).adopt(this.lightbox);

			// Overlay
			this.overlay = new Element('div', {
				'id': 'overlay',
				'styles': {
					'opacity': this.options.overlay.toFloat(),
					'height': window.getScrollSize().y + 'px'
				}
			}).addEvent('click', this.close.bind(this));

			// Windows Resize Event
			window.addEvent('resize', function() { this.overlay.setStyle('height', window.getScrollSize().y + 'px'); }.bind(this));

			// Barre de navigation
			this.navigation = new Element('div', {'class': 'navigation'});
			// Titre de l'image
			this.title = new Element('div', {'class': 'title'});
			// Btn Next, Prev, Close
			this.nextLink = new Element('a', {'class': 'nextLink', 'href': '#next'}).addEvent('click', function(e) { e.stop(); this.showNext(); }.bind(this));
			this.prevLink = new Element('a', {'class': 'prevLink', 'href': '#prev'}).addEvent('click', function(e) { e.stop(); this.showPrev(); }.bind(this));
			this.closeLink = new Element('a', {'class': 'closeLink', 'href': '#close'}).addEvent('click', function(e) { e.stop(); this.close(); }.bind(this));
			// SlideShow
			this.slideshowDiv = new Element('div', {'class': 'slideshow'});
			this.slideshowLink = new Element('a', {'class': 'slideshowLink play', 'href': '#slideshow', 'text': this.options.lightbox_play_text}).addEvent('click', function(e) { e.stop(); this.slideShow(); }.bind(this));
			this.slideshowCount = new Element('span', {'class': 'counter'});

			// Ajoute les éléments
			this.lightbox.adopt(this.overlay);
			this.navigation.adopt(this.title).adopt(this.nextLink).adopt(this.prevLink).adopt(this.closeLink).adopt(this.slideshowDiv.adopt(this.slideshowLink).adopt(this.slideshowCount));
			this.lightbox.adopt(this.navigation);

			// Image
			this.image = new Element('div', {'class': 'image'});
			this.lightbox.adopt(this.image);

			// Prépare les liens qui sont en lightbox
			this.linksHref = [];
			this.linksTitle = [];
			this.linksDownload = [];

			$$(links).each(function(link, index){
				link.removeEvents('click').addEvent('click', function(e) {
					e.stop();
					this.showLink(link.get('href'));
				}.bind(this));
				this.linksHref.include(link.get('href'));
				this.linksTitle.include(link.get('title'));
				this.linksDownload.include(link.get('rel').replace(/lightbox[/gi,'').replace(/[[]|]/gi,''));
			}.bind(this));

			// Ajoute les event du clavier
			document.addEvent('keydown', this.keyDown.bind(this));
		}
	},

	getIndex: function(index) {
		if(index >= this.linksHref.length)
			index = 0;
		else if(index < 0)
			index = (this.linksHref.length - 1);

		return index;
	},

	keyDown: function(event) {
		if(this.showLightbox) {
			switch(event.code) {
				case 27:	// Esc
				case 88:	// 'x'
				case 81:	// 'q'
				case 67:	// 'c'
					this.close();
					return false;
					break;
				case 37:	// Left arrow
				case 80:	// 'p'
					this.showPrev();
					return false;
					break;
				case 39:	// Right arrow
				case 78:	// 'n'
				case 32:	// Space Bar
				case 13:	// Enter
					this.showNext();
					return false;
			}
		}
	},

	showLink: function(link) {
		this.currentShow = this.linksHref.indexOf(link);
		this.show();
	},

	close: function() {
		this.slideShow('stop');
		this.lightbox.setStyle('display', 'none');
		this.showLightbox = false;
	},

	showNext: function() {
		this.currentShow++;
		this.show();
	},

	showPrev: function() {
		this.currentShow = (this.currentShow - 1);
		this.show();
	},

	slideShow: function(action) {
		if(action == 'stop' || (!action && this.currentSlideShow)) { // Stop SlideShow
			this.slideshowLink.removeClass('stop').addClass('play').set('text', this.options.lightbox_play_text);
			$clear(this.periodicalSlideShow);
			this.currentSlideShow = false;
		}
		else { // Play SlideShow
			this.slideshowLink.removeClass('play').addClass('stop').set('text', this.options.lightbox_stop_text);
			this.showNext();
			this.periodicalSlideShow = (function() { this.showNext(); }.bind(this)).periodical(5000);
			this.currentSlideShow = true;
		}
	},

	show: function() {
		this.showLightbox = true;
		this.currentShow = this.getIndex(this.currentShow);

		// Nouvelle image
		this.imageCurrent = new Element('img', {'src': this.linksHref[this.currentShow]}).addEvent('click', this.showNext.bind(this));

		// Préload les images suivante et précédente
		this.imagePrevIndex = this.getIndex((this.currentShow - 1));
		this.imagePrev = new Element('img', {'src': this.linksHref[this.imagePrevIndex]}).setStyle('display', 'none');
		this.imageNextIndex = this.getIndex((this.currentShow + 1));
		this.imageNext = new Element('img', {'src': this.linksHref[this.imageNextIndex]}).setStyle('display', 'none');

		this.image.empty(); // Vidage
		this.image.adopt(this.imageCurrent).adopt(this.imageNext).adopt(this.imagePrev);

		this.title.empty(); // Vidage

		// Lien de téléchargement
		if(this.linksDownload[this.currentShow] && this.linksDownload[this.currentShow] != '')
			this.title.adopt(new Element('a', {'html': this.linksTitle[this.currentShow], 'class': 'downloadLink', 'href': this.linksDownload[this.currentShow]}));
		else
			this.title.adopt(new Element('span', {'html': this.linksTitle[this.currentShow]}));

		this.slideshowCount.set('text', '(' + (this.currentShow + 1) + '/' + this.linksHref.length + ')');

		// Affiche la lightbox
		this.lightbox.setStyle('display', 'block');
	}
});
window.addEvent('domready', function(){ new DB_Lightbox(); });
