(function($) {
	
	$.extend($.fn, {
		BoatUSGallery: function(options)
		{
			var settings = {
				start: 0,						// Which slide to start on.
				transition: {
					type: 'diag_block',
					H_Blocks: 10,
					V_Blocks: 4,
					duration: .5,
					direction: 'fwd'
				},
				duration: 5,
				nav: true
			};
			if (options) $.extend(settings, options);
			return $(this).each(function() {
				var gallery = $(this);
				gallery.css('overflow', 'hidden');
				var slides = gallery.find('li');
				var curSlideIndex = settings.start;
				var nextSlideIndex = curSlideIndex + 1;
				if (nextSlideIndex > slides.length) nextSlideIndex = 0;
				var transBlocksWidth = 0;
				var transBlocksHeight = 0;
				var galleryWidth = gallery.width();
				var galleryHeight = gallery.height();
				var trans_completed = 0;
				var tCount = 0;
				var slide_timeout;
				var transitions = {
					diag_block: function(c, n, d) {
						trans_completed = 0;
						var slideBG = $(slides[n]).find('img').attr('src');
						gallery.find('.tBlock').css({ width: transBlockWidth * .5, height: transBlockHeight * .5, opacity: 0, display: 'block', 'background-image':  'url(' + slideBG + ')'});
						var delay, tb, x, y, xu, yu;
						for (y = 0; y < settings.transition.V_Blocks; y++) {
							for (x = 0; x < settings.transition.H_Blocks; x++) {
								xu = x;
								yu = y;
								if (settings.transition.direction === 'rev') {
									xu = settings.transition.H_Blocks - x - 1;
									yu = settings.transition.V_Blocks - y - 1;
								}
								tB = $('.trans_' + xu + '_' + yu, gallery);
								delay = (30 * x) + (30 * y);
								tB.delay(delay).animate({ width: transBlockWidth, height: transBlockHeight, opacity: 1 }, d, function() { transition_complete(); });
							}
						}
					},
					horiz_block: function(c, n, d) {
						trans_completed = 0;
						var slideBG = $(slides[n]).find('img').attr('src');
						gallery.find('.tBlock').css({ width: transBlockWidth * .5, height: transBlockHeight * .5, opacity: 0, display: 'block', 'background-image':  'url(' + slideBG + ')'});
						var delay, tb, i, j, iu, sT = $('.tBlock', gallery);
						for (i = 0, j = sT.length; i <= j; i++) {
							iu = i;
							if (settings.transition.direction === 'rev') {
								iu = j - i;
							}
							delay = (30 * i);
							$(sT[iu]).delay(delay).animate({ width: transBlockWidth, height: transBlockHeight, opacity: 1 }, d, function() { transition_complete(); });
						}
					},
					rand_block: function (c, n, d) {
						trans_completed = 0;
						var slideBG = $(slides[n]).find('img').attr('src');
						gallery.find('.tBlock').css({ width: transBlockWidth * .5, height: transBlockHeight * .5, opacity: 0, display: 'block', 'background-image':  'url(' + slideBG + ')'});
						var delay = 0, tb, i, j, sT = $('.tBlock', gallery);
						var randomSort = function(a,b) {
							return (Math.round(Math.random())-0.5);
						}
						sT = sT.sort(randomSort).sort(randomSort);
						for (i = 0, j = sT.length; i <= j; i++) {
							delay = ((Math.floor(Math.random()*20) + Math.floor(Math.random()*20)) * i) - d / tCount;
							$(sT[i]).delay(delay).animate({ width: transBlockWidth, height: transBlockHeight, opacity: 1 }, d, function() { transition_complete(); });
						}
					},
					drop_curtain: function (c, n, d) {
						trans_completed = 0;
						var slideBG = $(slides[n]).find('img').attr('src');
						gallery.find('.tBlock').css({ width: transBlockWidth, height: transBlockHeight, opacity: 0, display: 'block', 'background-image':  'url(' + slideBG + ')'});
						var delay, tb, x, y, xu, yu;
						for (y = 0; y < settings.transition.V_Blocks; y++) {
							for (x = 0; x < settings.transition.H_Blocks; x++) {
								xu = x;
								yu = y;
								if (settings.transition.direction === 'rev') {
									xu = settings.transition.H_Blocks - x - 1;
									yu = settings.transition.V_Blocks - y - 1;
								}
								tB = $('.trans_' + xu + '_' + yu, gallery);
								delay = (80 * x) + (50 * y);
								tB.delay(delay).animate({ opacity: 1 }, d, function() { transition_complete(); });
							}
						}
					},
					fade_curtain: function (c, n, d) {
						trans_completed = 0;
						var slideBG = $(slides[n]).find('img').attr('src');
						gallery.find('.tBlock').css({ width: transBlockWidth, height: transBlockHeight, opacity: 0, display: 'block', 'background-image':  'url(' + slideBG + ')'});
						var delay, tb, x, y, xu, yu;
						for (y = 0; y < settings.transition.V_Blocks; y++) {
							for (x = 0; x < settings.transition.H_Blocks; x++) {
								xu = x;
								yu = y;
								if (settings.transition.direction === 'rev') {
									xu = settings.transition.H_Blocks - x - 1;
									yu = settings.transition.V_Blocks - y - 1;
								}
								tB = $('.trans_' + xu + '_' + yu, gallery);
								delay = (80 * x);
								tB.delay(delay).animate({ opacity: 1 }, d, function() { transition_complete(); });
							}
						}
					},
					horiz_curtain: function (c, n, d) {
						trans_completed = 0;
						var slideBG = $(slides[n]).find('img').attr('src');
						gallery.find('.tBlock').css({ width: transBlockWidth, height: transBlockHeight, opacity: 0, display: 'block', 'background-image':  'url(' + slideBG + ')'});
						var delay, tb, x, y, xu, yu;
						for (y = 0; y < settings.transition.V_Blocks; y++) {
							for (x = 0; x < settings.transition.H_Blocks; x++) {
								xu = x;
								yu = y;
								if (settings.transition.direction === 'rev') {
									xu = settings.transition.H_Blocks - x - 1;
									yu = settings.transition.V_Blocks - y - 1;
								}
								tB = $('.trans_' + xu + '_' + yu, gallery);
								delay = (80 * y);
								tB.delay(delay).animate({ opacity: 1 }, d, function() { transition_complete(); });
							}
						}
					},
					fade: function (c, n, d) {
						trans_completed = settings.transition.H_Blocks * settings.transition.V_Blocks;
						var slideBG = $(slides[n]).find('img').attr('src');
						gallery.find('.tBlock').css({ width: transBlockWidth, height: transBlockHeight, opacity: 0, display: 'block', 'background-image':  'url(' + slideBG + ')'}).animate({ opacity: 1 }, d, function() { transition_complete(); });
					}
				};
				if (typeof transitions[settings.transition.type] !== 'function') { settings.transition.type = 'diag_block'; }
				function createGallery(t)
				{
					if (curSlideIndex < 0 || curSlideIndex > slides.length) curSlideIndex = 0;
					slides.hide();
					$(slides[curSlideIndex]).css('display', 'block');
					createTransition();
					slide_timeout = setTimeout(nextSlide, 5000);
				}
				function incrementCurrentSlide()
				{
					curSlideIndex++;
					if (curSlideIndex > slides.length - 1) curSlideIndex = 0;
					nextSlideIndex++;
					if (nextSlideIndex > slides.length - 1) nextSlideIndex = 0;
				}
				function set_next_slide_index(i)
				{
					nextSlideIndex = i;
					if (nextSlideIndex > slides.length - 1) nextSlideIndex = 0;
				}
				// Create all of the transition blocks to cover the image.
				function createTransition()
				{
					if (settings.transition.H_Blocks > 40) settings.transition.H_Blocks = 40;
					if (settings.transition.V_Blocks > 20) settings.transition.V_Blocks = 20;
					transBlockWidth = (galleryWidth / settings.transition.H_Blocks).toFixed(3);
					transBlockHeight = (galleryHeight / settings.transition.V_Blocks).toFixed(3);
					var transBlocks = '';
					var slideBG = $(slides[curSlideIndex]).find('img').attr('src');
					var tCount = gallery.find('.tBlock').length;
					for (var y = 0; y < settings.transition.V_Blocks; y++) {
						yPos = y * transBlockHeight;
						xBPos = 0;
						yBPos = 0;
						for (var x = 0; x < settings.transition.H_Blocks; x++) {
							xPos = x * transBlockWidth;
							transBlocks += '<div class="tBlock trans_' + x + '_' + y + '" style="z-index: 20; position: absolute; top: ' + yPos + 'px; left: ' + xPos + 'px; width: ' + (transBlockWidth) + 'px; height: ' + (transBlockHeight) + 'px; background-position: ' + (galleryWidth - xPos) + 'px ' + (galleryHeight - yPos + 1) + 'px; background-image: url(' + slideBG + '); display: none;"></div>';
						}
					}
					gallery.append(transBlocks);
				}
				function gotoSlide(i)
				{
					set_next_slide_index(i);
					clearTimeout(slide_timeout);
					transition_slides(curSlideIndex, nextSlideIndex, 300);
				}
				function transition_slides(c, n, d) {
					transitions[settings.transition.type](c, n, d);
				}
				function transition_complete()
				{
					trans_completed++;
					if (trans_completed >= settings.transition.H_Blocks * settings.transition.V_Blocks) {
						clearTimeout(slide_timeout);
						trans_completed = 0;
						$(slides[nextSlideIndex]).css('display', 'block');
						$(slides[curSlideIndex]).css('display', 'none');
						incrementCurrentSlide();
						gallery.find('.tBlock').css('display', 'none');
						slide_timeout = setTimeout(nextSlide, (settings.duration * 1000).toFixed(0));
					}
				}
				function nextSlide()
				{
					gotoSlide(nextSlideIndex);
				}
				createGallery($(this));
			});
		}
	});
})(jQuery);
