(function(window,document,undefined){

function Lightbox(args){
	var that = this;
	
	that.settings = {
		container: '.featured-img',
		prefix: 'lightbox-',
		nextText: 'Next',
		prevText: 'Previous',
		separator: ''
	}
	for( var i in args) {
		that.settings[i] = args[i];
	}
	
	that.init= function(){
		var s = that.settings;
		// Create bg
		var $bg = $('<div id="' + s.prefix + 'bg"></div>');
		$bg
			.css({
				background: 'rgba(0,0,0,0.85)',
				position: 'fixed',
				top: 0,
				left: 0,
				right: 0,
				bottom: 0,
				"z-index": 5
			})
			.hide()
			.appendTo('body');
			
		// Create lightbox
		var $lightbox = $('<div id="' + s.prefix + 'main"></div>');
		$lightbox
			.css({
				position: 'absolute',
				top: '40px',
				left: '100px',
				"z-index": 6
			})
			.hide()
			.appendTo('body');
		
		// Create eventhandlers
		$(s.container + ' a').click(function(e){
			e.preventDefault();
			that.showLightbox(this, $(this).parent());
		});
	}
	
	that.showLightbox = function(elem,parent){
		var s = that.settings;
		that.showBg();
		
		// Insert all images into the lightbox
		$(parent).children('a').each(function(value,i){
			var print = '<div class="slide">'
			print += '<img src="' + $(this).attr('href') + '" />';
			print += '<div class="data">';
			print += '<p class="alt">' + $(this).children('img').attr('alt') + '</p>';
			print += '<p class="photographer">' + $(this).children('img').attr('title') + '</p>';
			print += '</div></div>';
			$('#' + s.prefix + 'main').append(print);
		});
		$('#' + s.prefix + 'main').show();
		
		// Set clicked image to active
		$('#' + s.prefix + 'main .slide')
			.eq($(elem).index(s.container + ' a'))
			.addClass('active');
			
		// Center the lightbox
		that.resize('#' + s.prefix + 'main', 20, 20);
		$('#' + s.prefix + 'main .active img').first().load(function(){
			that.resize('#' + s.prefix + 'main');
		});
		
		// Make lightbox to a slideshow
		that.currentLightbox = new Slideshow({
			container: '#' + s.prefix + 'main',
			prevText: s.prevText,
			nextText: s.nextText,
			autoPlay: false,
			separator: s.separator
		});
		
		// Button for removing lightbox
		$('<a class="close"></a>')
			.css({
				position: 'absolute',
				'z-index': 11,
				top: '-13px',
				right: '-10px'
			})
			.appendTo($('#' + s.prefix + 'main'))
			.click(function(e){
				that.hideLightbox();
			});
		
		// Eventhandler för updating width and height when changing image
		$('#' + s.prefix + 'main').resize(function(){
			that.resize(this);
		});
	};
	that.hideLightbox = function(){
		that.hideBg();
		$('#' + that.settings.prefix + 'main')
			.hide()
			.empty();
	};
	
	that.showBg = function(){
		$('#' + that.settings.prefix + 'bg').show();
		$('#' + that.settings.prefix + 'bg').live('click',function(e){
			that.hideLightbox();
		});
	};
	that.hideBg = function(){
		$('#' + that.settings.prefix + 'bg').hide();
	};
	
	that.resize = function(elem, x, y){
		elem = $(elem);
		x = x || 20;
		y = $(window).scrollTop() + (y || 20);
		elem.css({
			width: elem.children('.active').outerWidth(),
			height: elem.children('.active').outerHeight(),
			left: elem.children('.active').outerWidth() > 20 ? ($(window).width() - elem.children('.active').outerWidth()) / 2 : x,
			top: elem.children('.active').outerHeight() > 20 ? $(window).scrollTop() + ($(window).height() - elem.children('.active').outerHeight()) / 2 : y
		});
	};
	
	that.init();
}
window.Lightbox = Lightbox;

})(window,document);
