(function($) {
	$.navigationBar = {
			section: 'default',
			options: {
				currentPage: 1,
				lastPage: 10,
				pagesToShow: 10,
				call: function(numPage) {
					alert("Call this page : " + numPage);
				}
			},
			list:{},
			settings:{},
			init: function(params, section) {
				if(params != undefined) {
					var settings = $.extend({}, this.options, params);
					if(section == undefined)
						this.settings[this.section] = settings;
					else
						this.settings[section] = settings;
				}
			},
			ajaxInit: function(obj) {
				if(obj != undefined) {
					if(obj.section == undefined)
						obj.section = this.section;
					if(obj.pages.length > 1) {
						var oldLastPage = this.settings[obj.section].lastPage;
						var newLastPage = Math.ceil(obj.nbElements / obj.elementsNbByPage);
						this.settings[obj.section].lastPage = newLastPage;
						this.settings[obj.section].pagesToShow = obj.pagesToShow;
						if(oldLastPage < newLastPage) {
							this.addNumberPage(obj.section);
						}
						this.setCurrentPage(obj.currentPage, obj.section);
						this.list[obj.section].show();
					}
					else {
						if(this.list[obj.section] != undefined && this.list[obj.section].length > 0)
							this.list[obj.section].hide();
					}
				}
			},
			addNumberPage: function(sect) {
				var list = this.list[sect].children("li.page, li.current");
				var min = parseInt($(list[0]).children("a").text()) - 1;
				var max = parseInt($(list[list.length-1]).children("a").text()) + 1;
				for(var i = min; i > 0; i--) {
					this.list[sect].each(
							function() {
								$(this).children("li.previous")
									.after("<li class='separator'>|</li>")
									.next().hide()
									.after("<li class='page'><a href='#'>" + i + "</a></li>")
									.next().hide();
							}
					);
				}
				for(var i = max; i <= $.navigationBar.settings[sect].lastPage; i++) {
					this.list[sect].each(
							function() {
								$(this).children("li.next")
									.before("<li class='separator'>|</li>")
									.prev().hide()
									.before("<li class='page'><a href='#'>" + i + "</a></li>")
									.prev().hide();
							}
					);
				}
			},
			resetClass: function(sect) {
				this.list[sect].each(
						function() {
							$(this).children("li.current").each(
									function() {
										$(this).removeClass("current")
												.addClass("page");
									}
							);
						});
			},
			setCurrentPage: function(num, section) {
				if(num == undefined)
					num = this.currentPage;
				if(section == undefined)
					section = this.section;

				this.resetClass(section);
				this.list[section].each(
						function() {
							// Previous link
							if(num == 1) {
								$(this).children("li.separator:first").hide();
								$(this).children("li.previous").hide();
							}
							else {
								$(this).children("li.separator:first").show();
								$(this).children("li.previous").show();
							}
							// Next link
							if(num == $.navigationBar.settings[section].lastPage) {
								$(this).children("li.next").hide();
								$(this).children("li.separator:last").hide();
							}
							else {
								$(this).children("li.next").show();
								$(this).children("li.separator:last").show();
							}
							// Current page
							$(this).children("li.page").each(
									function() {
										var around = Math.round(($.navigationBar.settings[section].pagesToShow - 1)/2);
										var min = 1;
										var max = $.navigationBar.settings[section].lastPage;
										var minPad = (num - max + around > 0)?num - max + around:0;
										var maxPad = (around - num + 1 > 0)?around - num + 1:0;
										if(num - around - minPad < 1 || num <= around)
											min = 1;
										else
											min = num - around - minPad;
										if(num + around + maxPad > max)
											max = max;
										else
											max = num + around + maxPad;

										var numPage = parseInt($(this).children("a").text());
										if(numPage == num) {
											$(this).removeClass("page")
													.addClass("current");
										}
										if(numPage >= min && numPage <= max) {
											if(min == 1)
												$(this).prev().show();
											$(this).show().next().show();
										}
										else {
											$(this).hide().next().hide();
										}
									}
							);
						});
				this.settings[section].currentPage = num;
			}

	};

	$.fn.navigationBar = function(func, section) {
		if (section == undefined)
			section = $.navigationBar.section;

		if ($.isFunction(func))
			$.navigationBar.settings[section].call = func;

		$.navigationBar.list[section] = $(this);

		if($(this).children("li.previous").length == 0) {
			$(this).each(
					function() {
						$(this).children("li.separator:first")
							.after("<li class='previous'><a href='#'><img src='" + BASE_URL + "skin/img/back.jpg' alt='Previous.'/></a></li>")
							.next().hide()
							.after("<li class='separator'>|</li>")
							.next().hide();
					}
			);
		}
		if($(this).children("li.next").length == 0) {
			$(this).each(
					function() {
						$(this).children("li.separator:last")
							.before("<li class='next'><a href='#'><img src='" + BASE_URL + "skin/img/next.jpg' alt='Next.'/></a></li>")
							.prev().hide()
							.before("<li class='separator'>|</li>")
							.prev().hide();
					}
			);
		}

		// On ajoute les numéros manquants
		if($.navigationBar.settings[section].pagesToShow < $.navigationBar.settings[section].lastPage)
			$.navigationBar.addNumberPage(section);

		// On affecte les fonctions à chaque enfant
		$(this).children("li").each(
				function() {
					$(this).data('section', section);
					switch ($(this).attr("class")) {
						case "first":
							$(this).click(
									function() {
										var numPage = 1;
										$.navigationBar.settings[$(this).data('section')].call(numPage);
										return false;
									}
							);
							break;
						case "previous":
							$(this).click(
									function() {
										var numPage = $.navigationBar.settings[$(this).data('section')].currentPage - 1;
										$.navigationBar.settings[$(this).data('section')].call(numPage);
										return false;
									}
							);
							break;
						case "next":
							$(this).click(
									function() {
										var numPage = $.navigationBar.settings[$(this).data('section')].currentPage + 1;
										$.navigationBar.settings[$(this).data('section')].call(numPage);
										return false;
									}
							);
							break;
						case "last":
							$(this).click(
									function() {
										var numPage = $.navigationBar.settings[$(this).data('section')].lastPage;
										$.navigationBar.settings[$(this).data('section')].call(numPage);
										return false;
									}
							);
							break;
						case "current":
						case "page":
							$(this).click(
									function() {
										var numPage = parseInt($(this).children("a").text());
										$.navigationBar.settings[$(this).data('section')].call(numPage);
										return false;
									}
							);
							break;
					}
				}
		);

		return $(this);
	};

})(jQuery);

