(function($) {
	$.b8 = {};
	$.b8.gallery_options = {};
	$.b8.gallery = function(options) {
		
		// Default thresholds & swipe functions
		var defaults = {
			slidesContainer:'#slides',
			buttonsContainer:'#buttons',
			autoplay:true,
			speed:1000,
			cufonRefreshSelector: '.cufon',	// added by a.foeder@iveins.de
			stop_autoplay: false,
			stop_immediate: false
		};
		
		var timer = null;
		
		$.b8.gallery_options = $.extend(defaults, options);
		
		if (!this) return false;

		var totWidth = 0;
		//var currentSlide = null;
		var positions = new Array();
		var obj = this;
		
		$($.b8.gallery_options.slidesContainer + ' .slide').each(function(i){
			/* Loop through all the slides and store their accumulative widths in totWidth */
		  positions[i]= totWidth;
		  totWidth += $(this).width();
		 
		  /* The positions array contains each slide's commulutative offset from the left part of the container */
		 
		  if(!$(this).width()) {
		  	alert("Please, fill in width & height for all your images!");
		    return false;
		  }
		  
		  /* add navigation to slide */
		  
		  if ($(this).next().html() != null) {
		  	$('<div class="nav_arrow"></div>').appendTo($(this));
		  }
		});
		
		$($.b8.gallery_options.slidesContainer).width(totWidth);
		/* Change the container div's width to the exact width of all the slides combined */
		 
		$($.b8.gallery_options.buttonsContainer + ' ul li').click(function(e){
			/* On a thumbnail click */
		  $('li.menuItem').removeClass('act').addClass('inact');
			$(this).removeClass('inact').addClass('act');
		 
		  var pos = $(this).prevAll('.menuItem').length;
		  
			Cufon.refresh($.b8.gallery_options.cufonRefreshSelector, {});
		
			$('.nav_arrow').stop().fadeOut();
			
			if (obj.timer) {
				$.b8.gallery_options.stop_immediate = true;
			}
		 
		 	// hide navigation on image
		 	//$($.b8.gallery_options.slidesContainer).trigger('hideImageNavigation', [$(this)]);

		 	// reset subslides
		 	$($.b8.gallery_options.slidesContainer).find('.sublides').each(function(idx, obj) {
				$.b8.gallery.resetSubslide($(obj));
		 	});
		 	
		  // start sliding animation and show navigation on image when finished
		  $($.b8.gallery_options.slidesContainer).stop().animate({marginLeft:-positions[pos]+'px'},$.b8.gallery_options.speed, function(){$($.b8.gallery_options.slidesContainer).trigger('showImageNavigation', [$($.b8.gallery_options.slidesContainer + ' .slide:nth-child(' + (pos + 1) + ')')]);});
		  
		  e.preventDefault();
		  /* Prevent the default action of the link */
		});
		
		function next(slide) {
			if (!$.b8.gallery_options.stop_immediate) {
				if ($.b8.gallery_options.stop_autoplay) {
					clearTimeout(obj.timer);
					obj.timer = null;
					$.b8.gallery_options.stop_autoplay = false;
					$.b8.gallery_options.stop_immediate = false;
				}
				/* On a thumbnail click */
			  var pos = $(slide).prevAll().length;
			  //currentSlide = $($.b8.gallery_options.slidesContainer + ' .slide:nth-child(' + (pos) + ')');
	
			  $('li.menuItem').removeClass('act').addClass('inact');
			  $('li.menuItem:nth-child(' + (pos + 2) + ')').addClass('act');
			  
				Cufon.refresh($.b8.gallery_options.cufonRefreshSelector, {});
			 
				$('.nav_arrow').stop().fadeOut();
	
			 	// hide navigation on image
			 	//$($.b8.gallery_options.slidesContainer).trigger('hideImageNavigation', [$($.b8.gallery_options.slidesContainer + ' .slide:nth-child(' + (pos) + ')')]);
			 	
			 	// reset subslides
			 	$($.b8.gallery_options.slidesContainer).find('.sublides').each(function(idx, obj) {
					$.b8.gallery.resetSubslide($(obj));
			 	});
			 	
			  // start sliding animation and show navigation on image when finished
			  $($.b8.gallery_options.slidesContainer).stop().animate({marginLeft:-positions[pos+1]+'px'},$.b8.gallery_options.speed, function(){$($.b8.gallery_options.slidesContainer).trigger('showImageNavigation', [$($.b8.gallery_options.slidesContainer + ' .slide:nth-child(' + (pos + 2) + ')')]);});
			  
				if ($.b8.gallery_options.autoplay && !$.b8.gallery_options.stop_autoplay && $($.b8.gallery_options.slidesContainer).children().size() > pos + 2) {
					obj.timer = setTimeout(function() {
						next($($.b8.gallery_options.slidesContainer + ' .slide:nth-child(' + (pos + 2) + ')'));
					}, 5000);
				} else {
					$.b8.gallery_options.autoplay = false;
				}
			} else {
				clearTimeout(obj.timer);
				obj.timer = null;
				$.b8.gallery_options.stop_autoplay = false;
				$.b8.gallery_options.stop_immediate = false;
			}
		}
		
		$('.nav_arrow').click(function(e){
		  /* Prevent the default action of the link */
		  e.preventDefault();
			$.b8.gallery_options.stop_autoplay = true;
			next($(this).parent());
		});
		/*
		$($.b8.gallery_options.slidesContainer).bind('hideImageNavigation', function(event, param1) {
		  $('.nav_arrow', param1).hide();
		});
		*/
		$($.b8.gallery_options.slidesContainer).bind('showImageNavigation', function(event, param1) {
		  $('.nav_arrow', param1).stop().fadeIn();
		});
		
		//$('#menu ul li.menuItem:first').addClass('act').siblings().addClass('inact');
		
		if ($.b8.gallery_options.autoplay) {
			obj.timer = setTimeout(function() {
				next();
			}, 4000);
		}
		
	};
	
	$.b8.gallery.nextSubslide = function(currentSubslide) {
		var imgHeight = 401;
		var currentSubslideItem = $('#' + currentSubslide);
		var sublideContainer = $('#' + currentSubslide).parent().parent();
		var newMargin = parseInt(sublideContainer.css('marginTop')) - imgHeight;
	  sublideContainer.stop().animate({marginTop:newMargin + 'px'},1000, function(){});
	  $.b8.gallery_options.stop_immediate = true;
	};
	
	$.b8.gallery.prevSubslide = function(currentSubslide) {
		var imgHeight = 401;
		var currentSubslideItem = $('#' + currentSubslide);
		var sublideContainer = $('#' + currentSubslide).parent().parent();
		var newMargin = parseInt(sublideContainer.css('marginTop')) + imgHeight;
	  sublideContainer.stop().animate({marginTop:newMargin + 'px'},1000, function(){});
	  $.b8.gallery_options.stop_immediate = true;
	};

	$.b8.gallery.resetSubslide = function(sublideContainer) {
		if (sublideContainer) {
			if (parseInt(sublideContainer.css('marginTop')) != 0) {
		  	sublideContainer.stop().animate({marginTop:0 + 'px'},1000, function(){});
		  }
	  }
	};
	
})(jQuery);
