jQuery.noConflict();
(function($) {
	$(function()
	{
		var galleryIndex=0;
		var gallerySlideSpeed=400;
		var images=new Array();
	
		$('.carouselgallery').each(function() {
			galleryInit($(this), galleryIndex);
			galleryIndex++;
		});
	
		function galleryInit(gallery, galleryIndex)
		{
			gallery.addClass('js').attr('id', 'carouselgallery_'+galleryIndex);
				
			//build the array of urls to image/thumbnail/loaded/alt text
			var imagesThisGallery=new Array();
			$('img', gallery).each(function() {
				var image=new Array();
				image[0]=$(this).attr('src');
				image[1]=$(this).parent().attr('href');
				image[2]=false;
				image[3]=$(this).attr('alt');
	
				imagesThisGallery.push(image);
			})
			images[galleryIndex] = imagesThisGallery;
			
			$('.item', gallery).wrapAll('<div class="carouselgallery-inner" id="carouselgallery-inner_'+galleryIndex+'"></div>');
	
			//set sliderWidth
			var sliderWidth=gallery.getItemWidth() * $('.item', gallery).length;
			$('.carouselgallery-inner', gallery).css('width', sliderWidth+'px');
			$('.carouselgallery-inner', gallery).css('left', 0);
	
			//prev/next controls
			gallery.append('<div class="carouselgallery-prev" id="carouselgallery-prev_'+galleryIndex+'" style="display:none">Prev</div>');
			gallery.append('<div class="carouselgallery-next" id="carouselgallery-next_'+galleryIndex+'" style="display:none">Next</div>');
	
			$('.carouselgallery-prev', gallery).bind('click', function() {
				showItem(gallery.getCurrentImageIndex()-1, gallery);
			});
	
			$('.carouselgallery-next, img.clickable', gallery).live('click', function() {
				showItem(gallery.getCurrentImageIndex()+1, gallery);
			});
	
			//build thumbs
			gallery.append('<div class="carouselgallery-thumbs" id="carouselgallery-thumbs_'+galleryIndex+'"><div class="carouselgallery-thumbsinner" id="carouselgallery-thumbsinner_'+galleryIndex+'"></div>');
			for (i=0; i<images[galleryIndex].length; i++)
			{
				var selectedClass='';
				if (0==i)
				{
					selectedClass=' class="selected"';
				}
				$('.carouselgallery-thumbsinner', gallery).
					append('<img src="'+images[galleryIndex][i][0]+'" alt="'+images[galleryIndex][i][3]+'"'+selectedClass+'/>');
			}
			$('.carouselgallery-thumbsinner', gallery).css('width', $('.carouselgallery-thumbsinner img', gallery).length * gallery.getThumbWidth());
			gallery.append('<div class="carouselgallery-prevthumb" id="carouselgallery-prevthumb_'+galleryIndex+'">Prev</div>');
			gallery.append('<div class="carouselgallery-nextthumb" id="carouselgallery-nextthumb_'+galleryIndex+'">Next</div>');
			moveThumbsPage(0, gallery);
	
			$('.carouselgallery-nextthumb', gallery).bind('click', function() {
				moveThumbsPage(1, gallery);
			});
	
			$('.carouselgallery-prevthumb', gallery).bind('click', function() {
				moveThumbsPage(-1, gallery);
			});
	
			$('.carouselgallery-thumbsinner img', gallery).bind('click', function() {
			   showItem($('.carouselgallery-thumbsinner img', gallery).index(this), gallery);
			});
	
			//load the images
			loadImage(0, gallery);
		}
	
		function loadImage(imageIndex, gallery)
		{
			if (imageIndex > images[gallery.getIndex()].length)
			{
				return;
			}
			
			var galleryItem=$('.carouselgallery-inner .item:nth('+imageIndex+')', gallery);
	
			var img=new Image();
			$(img).load(function() {
					if (0 != this.width && this.width / this.height > 1)
					{
						$(this).addClass('landscape');
					}
					else if (0 != $(this).width())
					{
						$(this).addClass('portrait');
					}
					
					$('.credit a', galleryItem).addClass('credit');
					$('a[class!=credit]', galleryItem).remove();
					
					images[gallery.getIndex()][imageIndex][2]=true;	
					
					setControls(gallery);
					
					gallery.addClass('ready');	
				})
				.appendTo(galleryItem)
				.wrap('<div class="image"></div>')
				.attr('src', images[gallery.getIndex()][imageIndex][1]);
				
			if (imageIndex != images[gallery.getIndex()].length - 1)
			{
				$(img).addClass('clickable');
			}
	
			if (imageIndex < (images[gallery.getIndex()].length-1))
			{
				loadImage(imageIndex+1, gallery);
			}
		}
	
		function setControls(gallery)
		{
			var galleryIndex=gallery.getIndex();
			var currentPosition = gallery.getCurrentImageIndex();
	
			if (currentPosition < (images[galleryIndex].length-1)
				&& images[galleryIndex][currentPosition+1][2])
			{
				$('.carouselgallery-next', gallery).show();
			}
			else
			{
				$('.carouselgallery-next', gallery).hide();
			}
	
			if (currentPosition > 0)
			{
				$('.carouselgallery-prev', gallery).show();
			}
			else
			{
				$('.carouselgallery-prev', gallery).hide();
			}
		}
	
		function moveThumbsPage(pageTransitionIndex, gallery)
		{
			var left=$('.carouselgallery-thumbsinner', gallery).css('left').toInt();
			var newLeft = (left + (0 - pageTransitionIndex * gallery.getThumbPageWidth()));
	
			if (pageTransitionIndex > 0)
			{
				//want to leave the last thumb on current page visible on new page
				newLeft+=gallery.getThumbWidth();
			}
			else if (pageTransitionIndex < 0)
			{
				newLeft-=gallery.getThumbWidth();
			}
	
			$('.carouselgallery-thumbsinner', gallery).animate({left: newLeft}, gallerySlideSpeed, 'linear');
	
			if (0==newLeft)
			{
				$('.carouselgallery-prevthumb', gallery).hide();
			}
			else
			{
				$('.carouselgallery-prevthumb', gallery).show();
			}
	
			if (gallery.getAllThumbsWidth() - Math.abs(newLeft) <= gallery.getThumbPageWidth())
			{
				$('.carouselgallery-nextthumb', gallery).hide();
			}
			else
			{
				$('.carouselgallery-nextthumb', gallery).show();
			}
		}
		
		function setSelectedThumb(imageIndex, gallery)
		{
			$('.carouselgallery-thumbs img', gallery).removeClass('selected');
			$('.carouselgallery-thumbs img:nth('+imageIndex+')', gallery).addClass('selected');
		}
	
		function showItem(imageIndex, gallery)
		{
			var left=parseInt($('.carouselgallery-inner', gallery).css('left').replace(/px/,''), 10);
	
			//if the left is not a multiple of itemWidth, we're actually sliding right now, so ditch out
			if (0 != Math.abs(left) % gallery.getItemWidth())
			{
				return;
			}
	
			var numberToJump=Math.abs(imageIndex - gallery.getCurrentImageIndex());
			var slideSpeed = numberToJump * gallerySlideSpeed * 0.75;
	
			var newLeft = 0 - (imageIndex * gallery.getItemWidth());
		
			$('.carouselgallery-inner', gallery).animate({left: newLeft}, slideSpeed, 'linear', function() {
				setControls(gallery);
			});
	
			setSelectedThumb(imageIndex, gallery);
	
			//if the rightmost thumb is selected, slide the thumb page
			if ((imageIndex < $('.item', gallery).length-1)
				&& ($('.carouselgallery-thumbs img.selected', gallery).offset().left + gallery.getThumbWidth() ==
				$('.carouselgallery-thumbs', gallery).offset().left + gallery.getThumbPageWidth()))
			{
				moveThumbsPage(+1, gallery);
			}
			//similarly if leftmost is selected, slide thumb page left
			else if (0 != imageIndex && $('.carouselgallery-thumbs img.selected', gallery).offset().left ==
				$('.carouselgallery-thumbs', gallery).offset().left)
			{
				moveThumbsPage(-1, gallery);
			}
		}
	});
	
	$.fn.getThumbsPerPage=function()
	{
		return Math.round($('.carouselgallery-thumbs', $(this)).css('width').toInt()
			/ $(this).getThumbWidth());
	}
	
	$.fn.getThumbPageWidth=function()
	{
		return $(this).getThumbWidth() * $(this).getThumbsPerPage();
	}
	
	$.fn.getAllThumbsWidth=function()
	{
		return parseInt($('.carouselgallery-thumbsinner', $(this)).css('width').replace(/px/,''));
	}
	
	$.fn.getThumbWidth=function()
	{
		return $('.carouselgallery-thumbsinner img:first', $(this)).css('width').toInt() +
			$('.carouselgallery-thumbsinner img:first', $(this)).css('margin-right').toInt() +
			$('.carouselgallery-thumbsinner img:first', $(this)).css('padding-left').toInt() +
			$('.carouselgallery-thumbsinner img:first', $(this)).css('padding-right').toInt();
	}
	
	$.fn.getCurrentImageIndex=function()
	{
		var left=$('.carouselgallery-inner', $(this)).css('left').toInt();
		return Math.abs(left/$(this).getItemWidth());
	}
	
	$.fn.getItemWidth=function()
	{
		return $('.item:first', $(this)).css('width').toInt();
	}
	
	$.fn.getIndex=function()
	{
		return $(this).attr('id').replace(/[^0-9]/gi, '');
	}
})(jQuery);

String.prototype.toInt = function()
{
    return parseInt(this.replace(/px/,''), 10);
}
