//Function to loombox (lightbox) the page.
(function($)
{
	$.fn.loombox = function(direction, callback)
	{
		return this.each(function()
		{			
			//Make element visibility: hidden to get it's height.
			$('#lightBox').css({'display': 'inline',
								'visibility': 'hidden'});
								
			var left = $('#lightBox').css('left');
			
			//Hide the element again.
			$('#lightBox').css({'display': 'none',
								'visibility': 'visible'});

			
			//Slide the lightbox from the left
			if (direction == 'left')
			{
				$('#lightBox').css('left', '-' + ($('#lightBox').width() + 100) + 'px');
			}
			//Or the right
			else
			{
				$('#lightBox').css('left', ($(window).width() + 100) + 'px');
			}
			
			
			//If there are any divs with a class of 'swf', pull in their flash movies
			$('#lightBox .content .holder .html .swf').each(function()
			{
				$(this).flash({
								swf: $(this).text(),
								width: $(this).attr('width'),
								height: $(this).attr('height'),
								play: true
							  });
				
			});
			
			
			//Reset the html box which may have been dragged
			$('#lightBox .content .holder .html').css('left', '0px');


			//Show the box
			$('#lightBox').show();
			$('#lightBox').animate({'left': left}, 500, function()
			{
				//If there is a callback
				if (typeof callback == 'function')
				{
					callback.call(this);
				}
			});	
			
			//Enable the blockOut
			$('#blockOut').show();	
		});
	};
})(jQuery);



//Function to slide the loombox off the page in the given direction
(function($)
{
	$.fn.loomboxOut = function(direction, callback)
	{
		return this.each(function()
		{										
			var left = $('#lightBox').css('left');

			
			//Slide the lightbox from the left
			if (direction == 'left')
			{
				$('#lightBox').animate({'left': '-' + ($('#lightBox').width() + 100) + 'px'}, 500, function()
				{
					$('#lightBox').hide();
					$('#lightBox').css('left', left);
										
					//If there is a callback
					if (typeof callback == 'function')
					{
						callback.call(this);
					}
				});
			}
			//Or the right
			else
			{
				$('#lightBox').animate({'left': ($(window).width() + 100) + 'px'}, 500, function()
				{
					$('#lightBox').hide();
					$('#lightBox').css('left', left);
					
					//If there is a callback
					if (typeof callback == 'function')
					{
						callback.call(this);
					}
				});
			}
			
				
			
			//Hide the blockOut
			$('#blockOut').hide();	
		});
	};
})(jQuery);



//Function to initialize the loombox
(function($)
{
	$.fn.loomboxInit = function()
	{
		return this.each(function()
		{
			//Add a click event to the close button
			$('#lightBox .close').click(function()
			{
				$('#blockOut').hide();
				
				var left = $('#lightBox').css('left');
				$('#lightBox').show();
				$('#lightBox').animate({'left': ($(window).width() + 100) + 'px'}, 500, function()
				{
					//Remove the content
					$('#lightBox .content .html').html('');
					$(this).hide();
					$(this).css('left', left);
				});
				
				//Clear the hash
				window.location.hash = '';
				
				return false;
			});
			
			
			//Add an event listener to the loomboxBackground
			$('#blockOut').click(function()
			{				
				var left = $('#lightBox').css('left');
				$('#lightBox').show();
				$('#lightBox').animate({'left': ($(window).width() + 100) + 'px'}, 500, function()
				{
					//Remove the content
					$('#lightBox .content .html').html('');
					$(this).hide();
					$(this).css('left', left);
				});
				
				$(this).hide();

					
				//Clear the hash
				window.location.hash = '';
				
				return false;
			});
		});
	};
})(jQuery);
