(function($)
{
	
	Swappr = {
		
		/**
		 * The current version of Swappr.
		 *
		 * @type    {String}
		 * @public
		 */
		version: "1.0",
		
		active_scene: undefined,
		active_image: undefined,
		
		prev_scene: undefined,
		prev_image: undefined,
		
		next_scene: undefined,
		next_image: undefined
	};
	
	
	Swappr.options = {
		animate: true,
		
		text_animate_in_duration: 800,
		text_animate_delay: 8000,
		text_animate_out_duration: 800,
		
		image_animate_in_duration: 2500,
		image_animate_delay: 2500,
		
		image_width: 640,
		image_height: 480
	};
	
	
	
	Swappr.initialize = function(scenes, options)
	{
		Swappr.options = $.extend(Swappr.options, options);
		
		Swappr.scenes = scenes;
		//Swappr.swappr_loader = $("<div id=\"swappr-loader\"></div>");
		Swappr.swappr_div = $("<div id=\"swappr\"></div>");
		//Swappr.swappr_div.hide();
		
		//$('body').prepend(Swappr.swappr_loader);
		$('body').prepend(Swappr.swappr_div);
		
		$.each(scenes, function(i)
		{
			var scene = $('<div id="scene_' + i + '"></div>');
			
			if(scenes[i].text)
			{
				var text = $('<span></span>');
				text.hide();
				text.text(dencodeEntities(scenes[i].text));
				text.appendTo(scene);
			}
			
			$.each(scenes[i].images, function(j)
			{
				var image = $('<img src="' + scenes[i].images[j] + '" id="scene_' + i + '_image_' + j + '" />');
				
				image.appendTo(scene);
				image.css({ 'opacity': 0 });
			});
			
			scene.appendTo(Swappr.swappr_div);
		});
		
		Swappr.resizeSwappr();
		Swappr.resizeImages();
		Swappr.swapScenes();
		
		$(window).resize(Swappr.resizeImages);
		$(window).resize(Swappr.resizeSwappr);
		$(window).scroll(Swappr.resizeSwappr);
	}
	
	
	
	Swappr.swapImages = function()
	{
		if(Swappr.prev_image)
		{
			Swappr.prev_image.removeClass('prev');
		}
		
		if(Swappr.active_image)
		{
			Swappr.active_image.removeClass('active');
			Swappr.prev_image = Swappr.active_image;
			Swappr.prev_image.addClass('prev');
			Swappr.prev_image.delay(Swappr.options.image_animate_in_duration);
			Swappr.prev_image.queue(function()
			{
				$(this).css({ 'opacity': 0 });
				$(this).dequeue();
			});
		}
		
		if(Swappr.next_image)
		{
			Swappr.next_image.removeClass('next');
			Swappr.active_image = Swappr.next_image;
			Swappr.active_image.addClass('active');
			
			if(Swappr.options.animate)
			{
				Swappr.active_image.animate({
					'opacity': 1
				}, {
					'duration': Swappr.options.image_animate_in_duration
				});
			}
			else
			{
				Swappr.active_image.css({
					'opacity': 1
				});
			}
		}
		
		
		if(Swappr.active_image.next().length > 0)
		{
			// If there ARE more images to display after this one.
			Swappr.next_image = Swappr.active_image.next();
			Swappr.next_image.addClass('next');
			
			setTimeout('Swappr.swapImages()', Swappr.options.image_animate_delay);
		}
		else
		{
			if($('span', Swappr.active_scene).length > 0)
			{
				$('span', Swappr.active_scene).delay(Swappr.options.image_animate_delay);
				$('span', Swappr.active_scene).fadeIn(Swappr.options.text_animate_in_duration);
				$('span', Swappr.active_scene).delay(Swappr.options.text_animate_delay);
				$('span', Swappr.active_scene).fadeOut(Swappr.options.text_animate_in_duration, function()
				{
					setTimeout('Swappr.swapScenes()', Swappr.options.image_animate_delay);
				});
			}
			else
			{
				setTimeout('Swappr.swapScenes()', Swappr.options.image_animate_delay);
			}
		}
		
		return false;
	}
	
	
	
	Swappr.swapScenes = function()
	{
		if($('div', Swappr.swappr_div).length > 1)
		{
			if(!Swappr.prev_scene)
			{
				Swappr.prev_scene = $('div:last', Swappr.swappr_div);
			}
			
			if(!Swappr.active_scene)
			{
				Swappr.active_scene = $('div:last', Swappr.swappr_div);
			}
			
			if(!Swappr.next_scene)
			{
				Swappr.next_scene = $('div:first', Swappr.swappr_div);
			}
			
			var prev_scene   = Swappr.prev_scene;
			var active_scene = Swappr.active_scene;
			var next_scene   = Swappr.next_scene;
			
			Swappr.prev_scene.removeClass('prev');
			Swappr.active_scene.removeClass('active');
			Swappr.next_scene.removeClass('next');
			
			Swappr.prev_scene   = Swappr.active_scene;
			Swappr.active_scene = Swappr.next_scene;
			Swappr.next_scene   = Swappr.active_scene.next().length > 0 ? Swappr.active_scene.next() : $('div:first', Swappr.swappr_div);
			
			Swappr.next_scene.addClass('next');
			Swappr.prev_scene.addClass('prev');
			Swappr.active_scene.addClass('active');
			
			Swappr.next_image = $('img:first', Swappr.active_scene);
			Swappr.next_image.addClass('next');
			
			Swappr.swapImages();
		}
		else if($('div', Swappr.swappr_div).length == 1)
		{
			Swappr.active_scene = $('div:first', Swappr.swappr_div);
			Swappr.active_scene.addClass('active');
			
			Swappr.active_image = $('img:first', Swappr.active_scene);
			Swappr.active_image.addClass('active');
			
			if(Swappr.options.animate)
			{
				Swappr.active_image.animate({
					'opacity': 1
				}, {
					'duration': Swappr.options.image_animate_in_duration
				});
			}
			else
			{
				Swappr.active_image.css({
					'opacity': 1
				});
			}
		}
		
		return false;
	}
	
	
	Swappr.resizeSwappr = function()
	{
		Swappr.swappr_div.height(($(window).height() + $(window).scrollTop()));
		Swappr.swappr_div.width(($(window).width() + $(window).scrollLeft()));
	}
	
	
	Swappr.resizeImages = function()
	{
		var s = $('div', Swappr.swappr_div);
		
		$(s).each(function()
		{
			var i = $('img', this);
			
			//Resize each image seperately
			$(i).each(function()
			{
				//Gather browser and current image size
				var imagewidth = i.width();
				var imageheight = i.height();
				var browserwidth = $(window).width();
				var browserheight = $(window).height();
				
				//Define image ratio
				var ratio = Swappr.options.image_height / Swappr.options.image_width;
				
				var offset;
				
				//Resize image to proper ratio
				if((browserheight / browserwidth) > ratio)
				{
					i.height(browserheight);
					i.width(browserheight / ratio);
				}
				else
				{
					i.width(browserwidth);
					i.height(browserwidth * ratio);
				}
				
				i.css('left', (browserwidth - i.width()) / 2);
				i.css('top', (browserheight - i.height()) / 2);
				
				return false;
			});
			
		});
	};
	
	
	function dencodeEntities(str)
	{
		return $("<div></div>").html(str).text();
	}
	
	
})(jQuery);
