/**
*	Permet de paginer des elements
*	@author Stephane
*	@date 18/01/2010
*	@project Newsletters AEF, Club AEF
*	@version 1.2
*	
*/
$.fn.pagify=function(options){
	var defaults = {
		nb_page_elements:5,	//Numbers of elements by page
		type:'light-partial',
		slice:2,	//nb of elements to show before "slicing" 
		childrens:'li:not(.filtrify_hidden)',	//childrens to show/hide
		containers:{
			pagination:'.navigation',	//default container for navigation
			data:'.data'	//default container for data
		},
		ellipsis:{
			clickable:false
		},
		mode:'normal',	// normal|ajax
		ajax:{
			callback:function(){},
			total_elements:0
		},
		onPageChange:function(){},
		onPageClick:function(){}
	};
	
	var options = $.extend(defaults, options);

	var container_data=$(options.containers.data);
	
	var container_pagination=$(options.containers.pagination);
	//launch the build
	buildPagination(1);
	
	
	/**
	*	Method called in order to switch pages
	*/
	function buildPagination(pageIndex){
		var pageIndex=pageIndex-1;
		if(options.mode=='ajax'){
			var Ddata=options.ajax.callback(pageIndex);
			container_data.replaceWith(Ddata);
			container_data=$(options.containers.data);
		}
		initPagination(pageIndex);
		
	}
	
	function initPagination(pageIndex){
		//show good items
		if(options.mode!='ajax'){
			var total_elements=container_data.children(options.childrens).length;
		}else{
			var total_elements=options.ajax.total_elements;
		}
		if(options.mode!='ajax'){
			var start_page=pageIndex*options.nb_page_elements;
			var end_page=((pageIndex+1)*options.nb_page_elements);
		}else{
			var start_page=0;
			var end_page=options.nb_page_elements;
		}
		
		var total_pages=total_elements/options.nb_page_elements;
		
		if(total_pages-parseInt(total_pages)>0)
			total_pages=parseInt(total_pages)+1;
			
			
		//hide all elements, and show current page items
		container_data.children(options.childrens).hide();
		container_data.children(options.childrens).slice(start_page,end_page).show();

			
		
		//build navigation
		container_pagination.html('');
		//hide container during process
		container_pagination.hide();
		
		var rest=total_elements;
		var pagination_max=total_elements/options.nb_page_elements;
		pagination_max=Math.ceil(pagination_max);
		if(pagination_max>1){
			
			//add pagination elements
			for(i=1;i<=pagination_max;i++){
				//add class for active page item
				if(((i-1)==pageIndex)){
					var is_active="active";
					
				}else{
					var is_active="";
				}
				//add class for last item
				if((i+1)>pagination_max){
					is_active+=" last";
				}
				is_active="class='"+is_active+"'";
				container_pagination.append("<li "+is_active+">"+i+"</li>");
				rest-=options.nb_page_elements;
			}
			
			if(rest>0){	//should never occur with 30/03/2010 modification (ceil() on pagination_max)
				if(((i-1)==pageIndex))
					var is_active="class='active'";
				else
					var is_active="";
				container_pagination.append("<li "+is_active+">"+i+"</li>");
			}
			//show pagination container if pagination exist
			container_pagination.show();
			if(options.type=='partial'){
				
				container_pagination.children('li').hide();
				container_pagination.children('li.inactive').remove();
				
				//show the "start" interval
				container_pagination.children('li').slice(0,0+options.slice).show();
				
				
				//we add the item "..." in 'partial' mode after the first elements
				if(pageIndex>(options.slice+2)){
					container_pagination.children('li').eq(0+options.slice).after("<li class='inactive'>...</li>");
				}
				
				//define the "middle" interval in partial mode( items '...' around the interval)
				if(pageIndex-(options.slice+1)>0){
					var start_section_middle=pageIndex-options.slice+1;
				}else{
					var start_section_middle=0;
				}
				if(pageIndex+options.slice<=total_pages){
					var end_section_middle=pageIndex+options.slice;
				}else{
					var end_section_middle=total_pages;
				}
				//show "middle" interval
				container_pagination.children('li:not(.inactive)').slice(start_section_middle,end_section_middle).show();
				if(pageIndex+options.slice<total_pages-options.slice){	//if the "middle" interval is not aggregate with the "end" interval, show the inactive item
					container_pagination.children('li:not(.inactive)').eq(pageIndex+options.slice).after("<li class='inactive'>...</li>");
				}
				//show the "end" interval
				container_pagination.children('li:not(.inactive)').slice(total_pages-options.slice,total_pages).show();
				
			}
			/**
			 * 	Mode lightPartial 
			 */
			if(options.type=='light-partial'){
				container_pagination.children('li').hide();
				container_pagination.children('li.inactive').remove();
				
				//show the "start" interval
				//container_pagination.children('li').slice(0,2).show();
				
				/**
				*	Case : First page
				*/	
				if(pageIndex==0){
					
					//show pages 1,2,3
					container_pagination.children('li:not(.inactive)').slice(0,3).show();	
					
					//add ... (only if the last page isn't on the scope of the first)
					if(total_pages>4)
						container_pagination.children('li:not(.inactive)').eq(2).after("<li class='inactive'>...</li>");
					
					//show last page
					container_pagination.children('li:not(.inactive)').slice('-1').show();	//show last page
					
				}
				/**
				*	Case : Middle page
				*/				
				if(pageIndex>0 && pageIndex!=(total_pages-1)){
					//show page 1
					container_pagination.children('li:not(.inactive)').eq(0).show();	//show 1st page only
					
					//if page>=3 , add ... after page 1
					if((pageIndex-1)>1){
						container_pagination.children('li:not(.inactive)').eq(0).after("<li class='inactive'>...</li>");
					}

					//show pages near current page
					container_pagination.children('li:not(.inactive)').slice((pageIndex-1),(pageIndex+2)).show();
				
					//if middle top limit < total pages, add ... after top limit
					if((pageIndex+3)<total_pages){	
						container_pagination.children('li:not(.inactive)').eq(pageIndex+2).after("<li class='inactive'>...</li>");
					}
					
					//show last page
					container_pagination.children('li:not(.inactive)').slice('-1').show();
					
				}
				/**
				*	Case : Last page
				*/
				if(pageIndex==(total_pages-1)){
					//show page 1
					container_pagination.children('li:not(.inactive)').eq(0).show();
					//add ... (only if the last page isn't on the scope of the first)
					if(total_pages>4)
						container_pagination.children('li:not(.inactive)').eq(0).after("<li class='inactive'>...</li>");
					//show the 3 last pages
					container_pagination.children('li:not(.inactive)').slice(pageIndex-2,total_pages).show();

					
				}
			}
		}else{
			container_pagination.hide();
		}
		
		//on pagination item click, build new pagination and show results
		container_pagination.children('li:not(.inactive)').click(function(){
			var pageNumber=$(this).html();
			options.onPageClick(pageNumber);

			buildPagination(pageNumber);
			
			options.onPageChange(pageNumber);
			
		});
		//option : clickable ellipsis
		if(options.ellipsis.clickable){
			container_pagination.children('li.inactive').click(function(){
				
				var active_item=container_pagination.children('li.active');
				if($('#membre_pagination li').index($(this))<$('#membre_pagination li').index(active_item)){
					//previous ellipsis
					options.onPageClick();
					buildPagination(parseInt($(this).nextAll('li:visible').html())-1);
					options.onPageChange();
				}else{
					//after the current active item
					options.onPageClick();
					buildPagination(parseInt($(this).prevAll('li:visible').html())+1);
					options.onPageChange();
				}
				
			});
		}
	}
}
