$.fn.sortableListWithPagination = function () {
	$ (this).each (function() {
		this.table = $ (this).find ('table') ;
		//console.log(this.table) ;
		zCurrentSortField = this.table.attr ('zCurrentSortField') ;
		zCurrentSortDirection = this.table.attr ('zCurrentSortDirection') ;
		src = this.table.attr ('src') ;
		iCurrentPage = this.table.attr ('iCurrentPage') ;
		iNbPage = this.table.attr ('iNbPage') ;
		if (zCurrentSortField==undefined || zCurrentSortDirection==undefined || src==undefined || iCurrentPage==undefined || iNbPage==undefined) {
			alert ("Le tag table doit avoir les attributs src, zCurrentSortField, zCurrentSortDirection, iCurrentPage et iNbPage") ;
			return true ;
		}

		//Gestion de la pagination
		if (iNbPage > 1) {
			pagination = $ (this).find ('.pagination') ;
			if (!pagination.length) {
				return false ;
			}
			pagination.empty () ;

			iCurrentPage = new Number (iCurrentPage) ;
			iNbPage = new Number (iNbPage) ;

			if (iCurrentPage > 1){
				lien = jQuery ("<a>voir les 10 précédents</a>") ;
				lien.attr ("href", '#') ;
				lien.addClass("precedente");
				pagination.append (lien) ;
				pagination.append ("&nbsp; &nbsp;") ;
			}

			if (iCurrentPage < iNbPage){
				lien = jQuery ("<a>voir les 10 suivants</a>") ;
				lien.addClass("suivants");
				lien.attr ("href", '#') ;
				pagination.append ("&nbsp; &nbsp;") ;
				pagination.append (lien) ;
			}


			$(this).find ('.pagination > A').click (function() {
			//$(".pagination > A", $(this)).click(function(){
				iPage = $(this).text () ;
				table = $(this).parents().find ('table') ;
				iCurrentPage = new Number (table.attr('iCurrentPage')) ;
				//console.log(iCurrentPage) ;
				if (iPage == iCurrentPage){ //on est d�j� sur la bonne page
					return false ;
				}
				if (iPage == 'voir les 10 précédents') {
					iPage=iCurrentPage-1 ;
				}
				if (iPage == 'voir les 10 suivants') {
					iPage=iCurrentPage+1 ;
				}
				table = $(this).parents ('.sortableListWithPagination').find ('table') ;
				src = table.attr('src') ;
				zSortField = table.attr ('zCurrentSortField') ;
				zSortDirection = table.attr ('zCurrentSortDirection') ;

				ajaxZone = $(this).parents ('.ajaxZone') ;
				ajaxZone.load (src, {zSortField:zSortField, zSortDirection:zSortDirection, iPage:iPage}, function() {
					doOnLoad ($(this)) ;
				}) ;

				return false ;
			}) ;

		}

		//Gestion du tri
		//Si il n'y a qu'une seule ligne dans le tableau, on ne fait rien
		if($(this).find ('tbody').find ('tr').length == 1) {
			return ;
		}
		//On applique les styles et �v�nements sur chaque th
		$(this).find ('th').each (function () {
			zSortField = $(this).attr ('zSortField') ;
			if (zSortField != undefined) {
				if (zSortField == zCurrentSortField) {
					if(zCurrentSortDirection == 'DESC') {
						$(this).addClass ('sortUp') ;
					} else {
						$(this).addClass ('sortDown') ;
					}
				} else {
					$(this).addClass ('sortable') ;
				}
				$(this).click (function() {
					zSortField = $(this).attr ('zSortField') ;
					table = $(this).parents ('.sortableListWithPagination').find('table') ;
					zCurrentSortField = table.attr ('zCurrentSortField') ;
					zCurrentSortDirection = table.attr ('zCurrentSortDirection') ;
					src = table.attr ('src') ;
					if (zSortField == zCurrentSortField) {
						if (zCurrentSortDirection == 'DESC') {
							zSortDirection = 'ASC' ;
						} else {
							zSortDirection = 'DESC' ;
						}
					} else {
						zSortDirection = 'ASC' ;
					}

					$(this).parents ('.ajaxZone').load (src, {zSortField:zSortField, zSortDirection:zSortDirection}, function(){
						doOnLoad ($(this)) ;
					}) ;

				}) ;
			}
		}) ;
	}) ;
} ;
