/**
 * WebPoint Lightbox
 *
 * Copyright (c) 2010 Por Design (pordesign.eu)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

(function($)
{
	$.fn.lightbox = function(options)
	{
		options = $.extend
		({
			id: 'lightbox',
			path: '',
			speed: 500,
			single: false
		}, options);
		
		lb = {};

		$this = $(this);
		
		lb = 
		{
			data: null,
			index: 0,
			initialized: false,
			
			hide: function()
			{
				lb.data = null;
				
				$('#' + options.id + '-wrapper').fadeOut(options.speed, function()
				{
					$(this).remove();
				});
				
				$('#' + options.id + '-container').fadeOut(options.speed, function()
				{
					$(this).remove();
				});
			},
			
			show: function(element)
			{
				options = element.data('options');

				if ($('#' + options.id).length == 0)
				{
					$template = $('<div id="' + options.id + '-wrapper" style="background: #222;"></div>' +
					'<div id="' + options.id + '-container" style="background: transparent;">' +
						'<img src="' + options.path + 'images/loading.gif" class="loading" alt="Loading" />' +
						'<div id="' + options.id + '" class="lightbox">' +
							'<ul class="images">' +
								'<li>' +
								'</li>' +
							'</ul>' +
								
							'<h2></h2>' +
							'<ul class="nav">' +
								'<li class="prev"><a href="#prev">prev</a></li>' +
								'<li class="next"><a href="#next">next</a></li>' +
							'</ul>' +
							'<p class="total"></p>' +
							'<a class="close" href="#close">close</a>' +
						'</div>' +
					'</div>');
					$('body').append($template);
					$('#' + options.id).hide();
				
					lb.show_image(element);

					$('#' + options.id + '-wrapper').hide().fadeTo(options.speed, 0.8);
					$('#' + options.id + '-container').hide().fadeIn(options.speed);
					$('#' + options.id + ' ul.nav, #' + options.id + ' p.total, #' + options.id + ' h2').hide();
					
					$('#' + options.id + '-wrapper, #' + options.id + '-container').css
					({
						'position': 'absolute',
						'top': $(window).scrollTop(),
						'left': $(window).scrollLeft(),
						'width': $(window).width(),
						'height': $(window).height(),
						'z-index': 99999
					}).click( function()
					{
						lb.hide();
					});
					
					$('#' + options.id).css
					({
						'margin-left': $(window).width() / 2 - $('#' + options.id).outerWidth() / 2,
						'margin-top': $(window).height() / 2 - $('#' + options.id).outerHeight() / 2
					});

					$(window).resize( function()
					{
						$('#' + options.id + '-wrapper, #' + options.id + '-container').css
						({
							'width': $(window).width(),
							'height': $(window).height()
						});
						
						$('#' + options.id).css
						({
							'margin-left': $(window).width() / 2 - $('#' + options.id).outerWidth() / 2,
							'margin-top': $(window).height() / 2 - $('#' + options.id).outerHeight() / 2
						});
					});
					
					$(window).scroll( function()
					{
						$('#' + options.id + '-wrapper, #' + options.id + '-container').css
						({
							'top': $(window).scrollTop(),
							'left': $(window).scrollLeft()
						});
					});
					
					if ( ! options.single)
					{
						$('#' + options.id + ' ul.nav li.prev').click( function()
						{
							lb.index--;
							
							if (lb.index < 0)
							{
								lb.index = lb.data.length-1;
							}
							
							image = lb.data.eq(lb.index);
							
							lb.show_image(image);
							
							return false;
						});
						
						$('#' + options.id + ' ul.nav li.next').click( function()
						{
							lb.index++;
							
							if (lb.index > lb.data.length-1)
							{
								lb.index = 0;
							}

							image = lb.data.eq(lb.index);
							
							lb.show_image(image);
							
							return false;
						});
					}
					
					
					lb.initialized = true;
				} else
				{
					lb.show_image(element);
				}
			},
			
			show_image: function(element)
			{
				options = element.data('options');
				
				src = element.attr('href');

				$('#' + options.id + '-container img.loading').css
				({
					'position': 'absolute',
					'left': $(window).width() / 2 - 63,
					'top': $(window).height() / 2
				});
				
				if (lb.initialized)
				{
					$('#' + options.id).fadeOut(options.speed);
					$('#' + options.id + ' h2').slideUp(options.speed);
					
					if ( ! options.single)
					{
						$('#' + options.id + ' ul.nav:visible').fadeOut(options.speed);
						$('#' + options.id + ' p.total:visible').slideUp(options.speed);
					}
				}

				$('#' + options.id + ' ul.images li img').fadeOut(options.speed, function()
				{
					$(this).remove();
				});
				
				$('#' + options.id + ' ul.images li').append
				(
					$('<img />').load( function()
					{
						$('#' + options.id).fadeIn(options.speed);
						$img = $(this);
						
						// fix for ie7
						$img.parent('li').show().children('img').hide();
						
						
						$('#' + options.id + ' ul.images').animate
						({
							height: $img.height(),
							width: $img.width()
						},
						{
							duration: options.speed,
							complete: function()
							{
								$img.fadeIn(options.speed);
								
								$('#' + options.id).css
								({
									'margin-left': $(window).width() / 2 - $('#' + options.id).outerWidth() / 2,
									'margin-top': $(window).height() / 2 - $('#' + options.id).outerHeight() / 2
								});
								
								$('#' + options.id + ' p.total').text((lb.index+1) + ' of ' + lb.data.length);
								
								if (element.nextAll('div').children('h3').length > 0)
								{
									title = element.nextAll('div').children('h3').html();
									url = element.nextAll('div').children('a').attr('href');
									
									$('#' + options.id + ' h2').html(title).wrap($('<a />').attr('href', url));
								} else
								{
									$('#' + options.id + ' h2').html('');
								}
								
								$('#' + options.id + ' h2').slideDown(options.speed);

								if ( ! options.single)
								{
									$('#' + options.id + ' ul.nav').fadeIn(options.speed);
									$('#' + options.id + ' p.total').slideDown(options.speed);
								}
							},
							step: function()
							{
								$('#' + options.id).css
								({
									'margin-left': $(window).width() / 2 - $('#' + options.id).outerWidth() / 2,
									'margin-top': $(window).height() / 2 - $('#' + options.id).outerHeight() / 2
								});
							}
						});
					}).attr('src', src + '?' + new Date().getTime())
				);
				
				$('#' + options.id + ' ul.images li:last').hide();
			}
		};
		
		$this.each( function(index)
		{
			if (options.single)
			{
				$(this).data('data', $(this));
			} else
			{
				$(this).data('data', $this);
			}
			
			$(this).data('options', options);
			
			$(this).click( function()
			{
				lb.data = $(this).data('data');
				options = $(this).data('options');
				
				if (lb.data.length == 1)
				{
					options.single = true;
				}
				
				lb.index = lb.data.index(this);
				lb.show($(this));
					
				return false;
			});
		});
		
		return this;
	}
})(jQuery);

