$.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 () ;
			pagination.append ('PAGE :&nbsp; &nbsp; &nbsp;')

			iCurrentPage = new Number (iCurrentPage) ;
			iNbPage = new Number (iNbPage) ;

			if (iCurrentPage > 1){
				lien = jQuery ("<a><<</a>") ;
				lien.attr ("href", '#') ;
				pagination.append (lien) ;
				pagination.append ("&nbsp; &nbsp;") ;
			}

			iStart = 1 ;
			iStop = iNbPage ;
			if (iNbPage > 10){
				iStart = iCurrentPage - 5 ;
				iStop = iCurrentPage + 5 ;
				if(iStart<=0){
					iStop += -iStart+1 ;
					iStart=1 ;
				}
				if (iStop > iNbPage){
					iStart += iNbPage-iStop ;
					if(iStart<=0){
						iStart=1 ;
					}
					iStop = iNbPage ;
				}
			}

			for (i = iStart ; i <= iStop ; i++){
				lien = jQuery ("<a>"+i+"</a>") ;
				lien.attr("href", '#') ;
				if (i == iCurrentPage) {
					lien.addClass ("currentPage")
				}
				pagination.append (lien) ;
				//alert (iStop)  ;
				if (i != iStop){
					pagination.append(" - ") ;
				}
			}

			if (iCurrentPage < iNbPage){
				lien = jQuery ("<a>>></a>") ;
				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 == '<<') {
					iPage=iCurrentPage-1 ;
				}
				if (iPage == '>>') {
					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)) ;
					}) ;

				}) ;
			}
		}) ;
	}) ;
} ;