(function($) { 
	
	$.fn.wtdgallery = function(options) {
		
		var opts = $.extend({}, $.fn.wtdgallery.defaults, options);
		
		if(this.length){
			return this.each(function(){
				setThumbs($(this), opts);				  
			});
		}
		
		function setThumbs($obj, opts){
			
			//$obj.css('overflow', 'hidden');
			$obj.find('div.gallery-images').css({
				'height': '272px'
			});
			$obj.css('padding-bottom', '20px');
			$obj.find('div.image-caption').css({ 
				'display': 'block'
			});
			$obj.find('div.gallery-thumbnails').css({ 
				'overflow': 'hidden',
				'position': 'relative',
				'display': 'block',
				'height': '60px',
				'width': '489px'
			});
			$obj.find('div.gallery-thumbnails').append('<ul>');
			
			$obj.find('div.gallery-thumbnails img').each(function(index, element){
				var imgThumb = $(element).attr('src');
				var imgThumbAlt = $(element).attr('alt');
				$obj.find('div.gallery-thumbnails ul').append('<li><a href=\'#\' title=\'' + imgThumbAlt + '\'><img src=\'' + imgThumb + '\' alt=\'' + imgThumbAlt + '\' width=\'97px\' height=\'60px\' class=\'carousel-thumb' + index + '\'/><span></span></a></li>');
				$(element).remove();
				
				$obj.find('div.gallery-thumbnails ul li a').click(function(e){															 
																		 
					$obj.find('div.gallery-thumbnails ul li').removeClass('active');
					$obj.find('div.gallery-thumbnails ul li img').css('opacity', '0.5');				
					$(this).parent().addClass('active');
					$(this).parent().find('img').css('opacity', '1');
					
					var imageID = $(this).parent().find('img').attr('class').substring(14);
					setLargeIMG($obj, opts, imageID)
	
					e.preventDefault();														 
				});
			}); 
			
			// Set all thumbnails to zero opacity initially
			$obj.find('div.gallery-thumbnails ul li img').css('opacity', '0.5');
			
			// Set first thumbnail to active state
			$obj.find('div.gallery-thumbnails ul li').eq(0).addClass('active');
			$obj.find('div.gallery-thumbnails ul li:first-child img').css('opacity', '1');
			
			setLargeIMG($obj, opts, 0);
			
			var totalItems = $obj.find('div.gallery-thumbnails ul li').length;
			setNavigation($obj, opts, totalItems);

		}
		
		function setLargeIMG($obj, opts, imageID){	
			$obj.find('div.gallery-images img').css('display', 'none');
			$obj.find('div.gallery-images img').eq(imageID).css('display', 'block');	
			var imageCaption = $obj.find('div.gallery-images img').eq(imageID).attr('alt');
			$obj.find('div.image-caption p').html(imageCaption);
		}
		
		function setNavigation($obj, opts, totalItems){	
		
			$obj.find('div.gallery-thumbnails ul').css('width', '2000px');
			$obj.find('div.gallery-thumbnails ul').css('position', 'absolute');
		
			$obj.append('<a href=\'#\' class=\'carousel-back\' title=\'Previous image\'>Previous image</a>');
			$obj.append('<a href=\'#\' class=\'carousel-forward\' title=\'Next image\'>Next image</a>');
			
			// Hide forward and back buttons initially
			//$obj.find('a.carousel-forward, a.carousel-back').css('opacity', '0');
			
			// Set events so that forward and back buttons fade in and out when user hovers over image feature
			//$obj.hover(function(){
    		//	$obj.find('a.carousel-forward, a.carousel-back').stop().animate({'opacity': '1'}, 1000, opts.easing);
  			//}, function(){
    		//	$obj.find('a.carousel-forward, a.carousel-back').stop().animate({'opacity': '0'}, 1000, opts.easing);
  			//});
			
			// Set events so that forward and back buttons fade in and out when they are in focus
			//$('a.carousel-forward, a.carousel-back').focus(function(){
    		//	$obj.find('a.carousel-forward, a.carousel-back').stop().animate({'opacity': '1'}, 1000, opts.easing);
  			//});
			//$('a.carousel-forward, a.carousel-back').blur(function(){
    		//	$obj.find('a.carousel-forward, a.carousel-back').stop().animate({'opacity': '0'}, 1000, opts.easing);
  			//});

			
			$obj.find('a.carousel-forward').click(function(e){
				
				// Find next image
				var nextImage = $obj.find('div.gallery-thumbnails ul li.active img').attr('class').substring(14);
				nextImage = parseInt(nextImage) + 1;
				
				var isLastItem = nextImage % 5 === 0;
				
				// Check whether there are any more images, if not stop movement forward
				if(nextImage >= totalItems){
					return false;
				}
				else if(isLastItem == true){
					var currentLeftPos = $obj.find('div.gallery-thumbnails ul').css('left');
					currentLeftPos = parseInt(currentLeftPos);
					var newLeftPos = -(nextImage * 98);
					//$obj.find('div.gallery-thumbnails ul').css('left', newLeftPos + 'px');
					$obj.find('div.gallery-thumbnails ul').animate({
						left: newLeftPos + 'px'
					}, opts.speed, opts.easing);

				}
				
				// Remove active state from previous thumbnail
				$obj.find('div.gallery-thumbnails ul li img').css('opacity', '0.5');
				$obj.find('div.gallery-thumbnails ul li').removeClass('active');
				
				// Add active state to next thumbnail
				$obj.find('div.gallery-thumbnails ul li').eq(nextImage).addClass('active');
				$obj.find('div.gallery-thumbnails ul li').eq(nextImage).find('img').css('opacity', '1');
				
				setLargeIMG($obj, opts, nextImage);
				e.preventDefault();	
			});
			
			$obj.find('a.carousel-back').click(function(e){
				
				// Find next image
				var lastImage = $obj.find('div.gallery-thumbnails ul li.active img').attr('class').substring(14);
				lastImage = parseInt(lastImage) - 1;
								
				var isLastItem = (lastImage+1) % 5 === 0;	
				
				// Check whether there are any more images, if not stop movement forward
				if(lastImage < 0){
					return false;
				}
				else if(isLastItem == true){
					var currentLeftPos = $obj.find('div.gallery-thumbnails ul').css('left');
					currentLeftPos = parseInt(currentLeftPos);
					var newLeftPos = currentLeftPos + (5 * 98);

					if(lastImage <= 4){
						$obj.find('div.gallery-thumbnails ul').animate({
							left: '0px'
						}, opts.speed, opts.easing);
					}
					else{
						//$obj.find('div.gallery-thumbnails ul').css('left', newLeftPos + 'px');
						$obj.find('div.gallery-thumbnails ul').animate({
							left: newLeftPos + 'px'
						}, opts.speed, opts.easing);
					}

				}
				
				// Remove active state from previous thumbnail
				$obj.find('div.gallery-thumbnails ul li img').css('opacity', '0.5');
				$obj.find('div.gallery-thumbnails ul li').removeClass('active');
				
				// Add active state to next thumbnail
				$obj.find('div.gallery-thumbnails ul li').eq(lastImage).addClass('active');
				$obj.find('div.gallery-thumbnails ul li').eq(lastImage).find('img').css('opacity', '1');
				
				setLargeIMG($obj, opts, lastImage);
				e.preventDefault();	
			});
		}
		
	};
	
	// Gallery Defaults
	$.fn.wtdgallery.defaults = {
		speed: 2000,
		easing: 'easeOutQuint'
	};
	
	
})(jQuery);
