﻿(function($){
	$.fn.jpaging = function(options){
				
		var options = $.extend({
			n: 10,
			pages_step: 5,
			all_items_num: 0,
			callback: ''
		},options);

		return this.each(function() {			
			var hndl = this;			
			
			this.get_pages_num = function(){
				return eval(Math.ceil(options.all_items_num/options.n));
			};			
			
			//init
			this.div = $(this);
//			this.div.html('<div class="panel"></div><div class="info"></div>');
			this.div.html('<div class="panel"></div>');
			this.div.addClass("jpaging");
			this.panel = this.div.find("div.panel");
//			this.info = this.div.find("div.info");
			
			this.current_page = 1;
			this.pages_num = this.get_pages_num(); 
			this.start_index = 0;
			this.end_index = 0;
			
			this.prev_pages_num = 0;
			this.prev_start_page = 1;
			this.next_pages_num = 0;
			this.next_last_page = 1;
			this.description = '';
			
			//arrows
			this.first_arrow_visible = false;
			this.prev_arrow_visible = false;
			this.last_arrow_visible = false;
			this.next_arrow_visible = false;	

			this.first = '';
			this.last = '';
			this.next = '';
			this.prev = '';
			
			this.getPrevIndex = function(){
				if(hndl.current_page <= 1){
					return 1;
				}
				else{
					return hndl.current_page - 1;
				}
			};
			
			this.getNextIndex = function(){
				if(hndl.current_page >= hndl.pages_num){
					return hndl.pages_num;
				}
				else{
					return eval(hndl.current_page) + 1;
				}
			};
			
			this.get_link = function(index, text, class_name, link_title){
				return '<a href="#" title="' + link_title + '" class="' + class_name + '">' + text + '</a>';
			};
						
			this.update_class = function(){				
				hndl.div.find('a').removeClass('current');
				hndl.div.find('a.page' + hndl.current_page).addClass('current');
			};
			
			this.set_visibility = function(){
			
				//prev and next index in class
				hndl.prev.removeClass().addClass("page" + hndl.getPrevIndex() + " prev");
				hndl.next.removeClass().addClass("page" + hndl.getNextIndex() + " next");
				
				//first arrow				
				if(hndl.first_arrow_visible){
					//hndl.first.css("visibility", "visible");	
//					hndl.first.removeClass("hidden");
				}
				else{
					//hndl.first.css("visibility", "hidden");	
//					hndl.first.addClass("hidden");
				}
				
				//prev
				if(hndl.prev_arrow_visible){
					//hndl.prev.css("visibility", "visible");
					hndl.prev.removeClass("hidden");					
				}
				else{
					//hndl.prev.css("visibility", "hidden");	
					hndl.prev.addClass("hidden");
				}
				
				//next
				if(hndl.next_arrow_visible){
					//hndl.next.css("visibility", "visible");	
					hndl.next.removeClass("hidden");
				}
				else{
					//hndl.next.css("visibility", "hidden");	
					hndl.next.addClass("hidden");
				}
				
				//last
				if(hndl.last_arrow_visible){
					//hndl.last.css("visibility", "visible");
//					hndl.last.removeClass("hidden");					
				}
				else{
					//hndl.last.css("visibility", "hidden");
//					hndl.last.addClass("hidden");
				}
				
			};
			
			this.draw_paging = function(){
					
				//empty
				hndl.panel.html('');
//				hndl.info.html('');
				
				if(hndl.pages_num > 1){
				
					//first arrow
//					hndl.panel.append(hndl.get_link(1, '&laquo;', 'page1 first', 'First page'));
//					hndl.first = hndl.panel.find(".first");
					
					//prev
					hndl.panel.append(hndl.get_link(hndl.getPrevIndex(), '<p></p>上一页', 'page' + hndl.getPrevIndex() + ' prev', 'Previous page'));
					hndl.prev = hndl.panel.find(".prev");
					
					//prev pages (left side of the current page)
					for(var i=hndl.prev_start_page; i<eval(hndl.prev_start_page) + hndl.prev_pages_num; i++){
						hndl.panel.append(hndl.get_link(i, i, 'page' + i, 'Page ' + i));
					}
					
					//current page
					hndl.panel.append(hndl.get_link(hndl.current_page, hndl.current_page, 'page' + hndl.current_page + ' current', 'Page ' + i));
					
					//next pages (right side of the current page)
					for(var i=eval(hndl.current_page) + 1; i<=eval(hndl.current_page) + hndl.next_pages_num; i++){
						hndl.panel.append(hndl.get_link(i, i, 'page' + i, 'Page ' + i));
					}
					
					//next
					hndl.panel.append(hndl.get_link(hndl.getNextIndex(), '<p></p>下一页', 'page' + hndl.getNextIndex() + ' next', 'Next page'));
					hndl.next = hndl.panel.find(".next");
										
					//last arrow
//					hndl.panel.append(hndl.get_link(hndl.pages_num, '&raquo;', 'page' + hndl.pages_num + ' last', 'Last page'));
//					hndl.last = hndl.panel.find(".last");
										
					//description
//					hndl.info.append(hndl.description);
					
					//init onclick
					hndl.panel.find('a').unbind('click').click(function(){
						var index = $.trim($(this).attr("class").replace('page','').replace('current','').replace('next','').replace('prev','').replace('first','').replace('last',''));
						hndl.current_page = index;
						hndl.update();							
						hndl.update_class();
						hndl.draw_paging();
						hndl.set_visibility();
						
						//callback
						if($.isFunction(options.callback)){
							//alert($.isFunction(options.callback));
							options.callback(index, options.n);
						}	

						return false;
					});
				}
			};
			
			this.update = function(){			
			
				//start and last index
//				hndl.start = eval((hndl.current_page - 1) * options.n) + 1;
//				hndl.end = hndl.current_page * options.n - (options.n - (options.all_items_num % options.n));  //1*10 - (10-(12 mod 10)) -> 2
				hndl.start = hndl.current_page;
				hndl.end = hndl.pages_num;
				
				//get 'left' pages number
				if(hndl.current_page - options.pages_step <= 0){
					hndl.prev_pages_num = hndl.current_page - 1;
					hndl.prev_start_page = 1;           
				}
				else{
					hndl.prev_pages_num = options.pages_step;
					hndl.prev_start_page = hndl.current_page - options.pages_step;           
				}
          				
				//get 'right' pages number				
				if(eval(hndl.current_page) + options.pages_step >= hndl.pages_num){					
					hndl.next_pages_num = hndl.pages_num - hndl.current_page;
					hndl.next_last_page = hndl.pages_num; 									
				}
				else{
					hndl.next_pages_num = options.pages_step;
					hndl.next_last_page = eval(hndl.current_page) + options.pages_step;           
				}
								
				//first and prev arrows visibility
				if(hndl.current_page == 1){
//					hndl.first_arrow_visible = false;
					hndl.prev_arrow_visible = false;						
				}
				else{
//					hndl.first_arrow_visible = true;
					hndl.prev_arrow_visible = true;
				}

				//last and next arrows visibility
				if(hndl.current_page == hndl.pages_num){
//					hndl.last_arrow_visible = false;
					hndl.next_arrow_visible = false;   
				}
				else{
//					hndl.last_arrow_visible = true;
					hndl.next_arrow_visible = true;   
				}
				
				//description ------------
				hndl.start_index = (hndl.current_page - 1)*options.n + 1;
				
				if(hndl.current_page*options.n <= options.all_items_num){
					hndl.end_index = hndl.current_page*options.n;
				}
				else{
					hndl.end_index = options.all_items_num;
				}
				
				hndl.description = hndl.start_index + " - " + hndl.end_index + " of " + options.all_items_num; 
//				hndl.info.html(hndl.description);
				//end of description -----											
								
			};
			
			if(options.n < options.all_items_num){
				this.update();
				this.draw_paging();
				this.set_visibility();
			}
		});
	};
})(jQuery); 
