;(function($){

	function techtwoGallery() {

		// Define variables
		this.$large,
		this.$loader,
		this.$nextButton,
		this.$prevButton,
		this.$container,
		this.$firstThumb,
		this.$firstLarge,
		this.$thumbList,
		this.$this;

		// Defaults
		this.currentPage 	= 1,
		this.perPage		= 4,
		this.pages 			= 1,
		this.items			= 1;
			
		this.activeImage;
		
		var self 			= this;

		this.init = function($this) {

			this.$container  	= $(this),

			this.$thumbList		= $('.ngg-galleryoverview a.techtwo_photoalbum'),
			this.$large 		= $('#techtwo-fotoalbum-image'),
			this.$loader 		= $('#techtwo-fotoalbum-loader'),

			this.$nextButton 	= $('#techtwo-fotoalbum-next'),
			this.$prevButton 	= $('#techtwo-fotoalbum-prev'),

			this.$firstThumb 	= $('.ngg-galleryoverview a.techtwo_photoalbum:first'),
			this.$firstLarge	= $('<img/>').attr('src', this.$firstThumb.attr('href')),

			this.bindEvents();
			this.initPaginator();

		}

		this.initPaginator = function() {

			this.items			= this.$thumbList.length,
			this.pages			= Math.ceil(this.items / this.perPage),
			this.currentPage	= 1;

			$('span#techtwo-fotoalbum-current-page').html(self.currentPage);
			$('span#techtwo-fotoalbum-total-pages').html(self.pages);
			this.gotoPage(this.currentPage);

		}

		this.showLoader = function() {
			this.$loader.show();
		}

		this.hideLoader = function() {
			this.$loader.hide();
		}

		this.gotoPage = function(page) {

			// Limit the paginator
			if ( page > this.pages || page < 1 ) {
				return false;
			}

			var lowerLimit = (page * this.perPage) - this.perPage;
			var upperLimit = (page * this.perPage) - 1;

			this.$large.find('img').animate({opacity: 0}, 1000, function(){
				$(this).remove();
			});

			$('.ngg-galleryoverview a.techtwo_photoalbum')
				.removeClass('active')
				.hide();

			for ( var i = lowerLimit; i <= upperLimit; i++ ) {
				$('.ngg-galleryoverview a.techtwo_photoalbum:eq(' + i + ')').show();
			}

			if (page < this.currentPage) {
				var showImage = upperLimit;
			} else {
				var showImage = lowerLimit;
			}
			
			this.createImage($('.ngg-galleryoverview a.techtwo_photoalbum:eq(' + showImage + ')').addClass('active'));

			this.currentPage = page;
			$('span#techtwo-fotoalbum-current-page').html(this.currentPage);
		}

		this.createImage = function($thumb) {
			self.showLoader();
			$thumb.data('large', $('<img/>')
				.attr('src', $thumb.attr('href'))
				.load(function(){
					$(this)
						.css({
							opacity: 	0,
							position:	'absolute',
							'z-index':		1000
						});
					self.showImage($thumb.data('large'));
				})
			);
		}

		this.showImage = function($image) {
			self.showLoader();
			self.activeImage = $image;
			$image
				.appendTo(self.$large)
				.css({
					left: (this.$large.width()/2) - ($image.width()/2)
				})
				.animate({opacity: 1}, 1000, function(){
					$image.css('z-index', 800);
					self.hideLoader();
			});
		}

		this.bindEvents = function() {

			$('.ngg-galleryoverview a.techtwo_photoalbum').click(function(){

				// Is the clicked thumbnail already active
				if ( self.$large.find('img').attr('src') == $(this).attr('href') ) {
					return false;
				}

				// Remove the current image
				self.$large.find('img').animate({opacity: 0}, 1000, function(){
					$(this).remove();
				});

				$('.ngg-galleryoverview a.techtwo_photoalbum').removeClass('active')
				$(this).addClass('active');

				// Show the image or create the image
				if ( $(this).data('large') ) {
					self.showImage($(this).data('large'));
				} else {
					self.createImage($(this));
				}

				return false;

			});

			$nextImage = $('#next-image');
			$prevImage = $('#prev-image');
			
			$nextImage.click(this.nextImage);
			$prevImage.click(this.prevImage);
			
			this.$nextButton.click(function(){
				self.currentPage++;
				self.gotoPage(self.currentPage);
				return false;
			});

			this.$prevButton.click(function(){
				self.currentPage--;
				self.gotoPage(self.currentPage);
				return false;
			});

		}

		this.nextImage = function()
		{
			$activeThumb = $('a[href="' + self.activeImage.attr('src') + '"]');
			$activeBox   = $activeThumb.parent().parent();

			activeIndex  = $('.ngg-galleryoverview>div').index($activeBox);
						
			if ((activeIndex+1)%4 == 0) {
				self.gotoPage(self.currentPage + 1);
			} else {
				$('.ngg-galleryoverview>div:eq(' + (activeIndex + 1) + ') a').trigger('click');	
			}
			
			return false;
		}
		
		this.prevImage = function()
		{
			$activeThumb = $('a[href="' + self.activeImage.attr('src') + '"]');
			$activeBox   = $activeThumb.parent().parent();

			activeIndex  = $('.ngg-galleryoverview>div').index($activeBox);
			
			console.log(activeIndex);
			
			if (activeIndex%4 == 0) {
				self.gotoPage(self.currentPage - 1);
			} else {
				$('.ngg-galleryoverview>div:eq(' + (activeIndex - 1) + ') a').trigger('click');	
			}
			
			return false;
		}
		
		
		
		this.init();
	}

	$.fn.techtwoGallery = function(){

		return this.each(function(){

			var gallery = new techtwoGallery($(this));

		});

	}


})(jQuery);
