(function($) {
	$.fn.lArditoGaleryPagination = function(settings) {
		var config = {};
		settings = $.extend(true,config,settings);
		return $.lArditoGaleryPagination.impl.init(this,settings);
	};
	
	$.lArditoGaleryPagination = function(elem,settings) {
		return $(elem).lArditoGaleryPagination(settings);
	};
	
	$.lArditoGaleryPagination.impl = {
		settings: null,	
		$e: null,
		width: null,
		height: null,
		images: null,
		$images: null,
		curImage: 0,
		curLine: 0,
		i: 0,
		
		init: function(e,settings) {
			
			this.curImage = this.curLine = this.i = 0;
			$('.ArditoPagination .prev').unbind().removeClass('prev-enabled');
			$('.ArditoPagination .next').unbind().removeClass('next-enabled');
			
			var self = this;
			this.$e = $e = e;
			this.width = $e.width();
			this.height = $e.height();
			settings.images.height(settings.height);
			this.$images = $("#les-images .container-img:nth-child(n+"+settings.start+") .Ardito");
			
			if( settings.lines == 'auto' ) {
				settings.lines = this.NbLines(settings);
			}
			
			this.settings = settings;
			
			this.initResizer();
			this.drawContainer();
			this.drawLines(0);
			
			return e;
		},
		
		drawLines: function(i) {
			var self = this;
			var widthLine = 0;
			self.i = i;
			var widthFirstImage;
			
			
			for( var j=this.curImage; j<this.$images.length; j++ ) {
				var imgWidth = $(this.$images[j]).width()+this.settings.margin;
				if( widthLine + imgWidth > this.width ) {
					break;
				}
				widthLine += (imgWidth);
			}
			
			var ie = ($.browser.msie);
			var ie6 = ($.browser.msie && $.browser.version == '6.0');
			if( ie6 ) {
				widthLine += 10;
			}
			
			var line = 
			"<div id='line-"+i+"' style='" +
				"position: absolute; overflow: hidden; clear: both;" +
				"height: "+(this.settings.height)+"px;" +
				"top: "+(i == 0 ? this.settings.margin : i*(this.settings.height+this.settings.margin) + this.settings.margin)+"px;" +
				"left: "+(this.width)/2+"px;"+
				"width: "+"0px;"+
			"'>";
			
			$("#Ardito-container").append(line);
			
			this.curLine = i;
			
			
			for( var k=this.curImage; k<j; k++ ) {
				var clone = $("#line-"+i)
					.append($(this.$images[k])
					.parents('.container-img')
					.clone(false));
				clone.find(".Ardito").fadeIn(this.settings.fadeInDuration);
			}
			
			this.curImage = j;
			
			$("#line-"+i)
			.animate( {
				width: widthLine,
				left: "-="+(widthLine)/2
			}, {
				duration: this.settings.speed,
				easing: 'swing',
				complete: function() {
					if( self.i < self.settings.lines-1 ) {
						self.placeContainer(self.i+1);
						self.drawLines(self.i+1);
					} else {
						
						$("#diaporama").find('a.fancy').click( function(e) {
							e.preventDefault();
							self.settings.onClick.apply($(this));
						} );
						
						self.makePagination(self.settings.start,self.curImage+self.settings.start-1);
					}
				} 
			} );
			
		},
		
		NbLines: function(settings) {
			var curImage = 0;
			var widthLine = 0;
			var nbLines = 0;
			
			
			var n = 0;
			while(true) {
				n++;
				for( var j=curImage; j<this.$images.length; j++ ) {
					var imgWidth = $(this.$images[j]).width()+settings.margin;
					if( widthLine + imgWidth > this.width ) {
						break;
					}
					widthLine += imgWidth;
				}
				widthLine = 0;
				curImage = j;
				
				if( (settings.height+settings.margin)*(nbLines+1) > this.height ) {
					break;
				}
				if( curImage == this.$images.length) {
					nbLines++;
					break;
				}
				nbLines++;
			}
			
			return nbLines;
		},
		
		drawContainer: function() {
			var container = 
			"<div id='Ardito-container'" +
				"style='" +
					"position: absolute;" +
					"top: "+(((this.height-(this.settings.height))/2)-this.settings.margin)+"px;"+
					"height: "+(this.settings.height+this.settings.margin)+"px;" +
					"width: 100%;" +
				"'" +
			"></div>";
			
			this.$e.append(container);
		},
		
		placeContainer: function(i) {
			var n = i+1;
			var height = (this.settings.height+this.settings.margin)*n;
			
			
			$("#Ardito-container").animate({
				top: ((this.height-height-this.settings.margin)/2)
			},{
				duration: 200,
				easing: 'swing'
			} );
			$("#Ardito-container").css( {
				height: height+"px"
			} );
		},
		
		makePagination: function(firstImage,lastImage) {
			var self = this;
			var nbAllImages = this.$images.length+firstImage-1;
			
			if( firstImage > 1 ) {
				$('.ArditoPagination .prev').addClass('prev-enabled');
			}
			
			if( lastImage < nbAllImages ) {
				$('.ArditoPagination .next').addClass('next-enabled');
			}
			
			$(".next-enabled")
			.bind('click', function() {
				self.drawGaleryPage(lastImage+1);
			} );
			
			$(".prev-enabled")
			.bind('click', function() {
				self.previousPage(firstImage-1);
			} );
		},
		
		previousPage: function(lastImage) {
			var firstImage = 0;
			var widthLine = 0;
			
			var j = lastImage-1;
			var lines = this.maxNbLines();
			for( var i=0; i<lines; i++ ) {
				while(true) {
					if( j <= 0 ) {
						j = -1;
						break;
					}
					var imgWidth = $(this.settings.images[j]).width()+this.settings.margin;
					if( widthLine + imgWidth > this.width ) {
						widthLine = 0;
						break;
					}
					widthLine += imgWidth;
					j--;
				}
			}
			
			
			this.drawGaleryPage(j+2);
		},
		
		maxNbLines: function() {
			var height = 0;
			var lines = 0;
			
			while( height <= this.height ) {
				height += this.settings.height+this.settings.margin;
				lines++;
			}
			
			return lines-1;
		},
		
		drawGaleryPage: function(firstImage) {
			var newSettings = this.settings;
			newSettings.start = firstImage;
			newSettings.lines = 'auto';
			
			this.$e.empty();
			this.$e.lArditoGaleryPagination(newSettings);
		},
		
		initResizer: function() {
			var self = this;
			$(window).resize(function() {
				self.IWantToRedraw();
			} );
		},
		
		IWantToRedraw: function() {
			if( window.timerResizeWindow == null ) {
				var self = this;
				window.timerResizeWindow = setTimeout(function(){self.redraw();},300);
			}
		},
		
		redraw: function() {
			window.timerResizeWindow = null;
			this.drawGaleryPage(this.settings.start);
		}
	};
	
} )(jQuery);


timerResizeWindow = null;













