/*
 * Bootstrap Image Gallery 3.0.1
 * https://github.com/blueimp/Bootstrap-Image-Gallery
 *
 * Copyright 2013, Sebastian Tschan
 * https://blueimp.net
 *
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/MIT
 */
/*global define, window */
(function(factory) {
	'use strict';
	if (typeof define === 'function' && define.amd) {
		define([
			'jquery',
			'./blueimp-gallery'
		], factory);
	} else {
		factory(
			window.jQuery,
			window.blueimp.Gallery
		);
	}
}(function($, Gallery) {
	'use strict';

	$.extend(Gallery.prototype.options, {
		useBootstrapModal: true
	});

	var close = Gallery.prototype.close,
		imageFactory = Gallery.prototype.imageFactory,
		videoFactory = Gallery.prototype.videoFactory,
		textFactory = Gallery.prototype.textFactory;

	$.extend(Gallery.prototype, {

		modalFactory: function(obj, callback, factoryInterface, factory) {
			if (!this.options.useBootstrapModal || factoryInterface) {
				return factory.call(this, obj, callback, factoryInterface);
			}
			var that = this,
				modalTemplate = this.container.children('.modal'),
				modal = modalTemplate.clone().show()
				.on('click', function(event) {
					// Close modal if click is outside of modal-content:
					if (event.target === modal[0] ||
						event.target === modal.children()[0]) {
						event.preventDefault();
						event.stopPropagation();
						that.close();
					}
				}),
				element = factory.call(this, obj, function(event) {
					callback({
						type: event.type,
						target: modal[0]
					});
					modal.addClass('in');
				}, factoryInterface);
			modal.find('.modal-title').text(element.title || String.fromCharCode(160));
			modal.find('.modal-body').append(element);
			return modal[0];
		},

		imageFactory: function(obj, callback, factoryInterface) {
			return this.modalFactory(obj, callback, factoryInterface, imageFactory);
		},

		videoFactory: function(obj, callback, factoryInterface) {
			return this.modalFactory(obj, callback, factoryInterface, videoFactory);
		},

		textFactory: function(obj, callback, factoryInterface) {
			return this.modalFactory(obj, callback, factoryInterface, textFactory);
		},

		close: function() {
			this.container.find('.modal').removeClass('in');
			close.call(this);
		}

	});

}));