/**
 * AJAX分页
 * @param listObj 分页列表对象
 * @param pageSize 每页记录数
 * @param totalPage 总页数
 * @param totalNum 总记录数
 * @param url 执行记录url
 * @param param 参数json
 * @param pageNumName 分页对象名
 * @param showPageSize 显示翻页导航链接数  
 * @param type 特殊处理类型 1奖杯列表,2投票评论,3我的足迹
 * @param otherObj 奖杯页面特殊处理
 */
function pageNavAction(listObj,pageSize,totalPage,totalNum,url,param,pageNumName,showPageSize,type,otherObj) {		//listObj，需要翻页的列表对象
	var PER_PAGE_NUM = pageSize;		//每页记录数
	var SHOW_PAGE_SIZE = showPageSize;		//显示翻页导航链接数
	
	var father = listObj.parent();
	
	$('.pageNav').each(function() {
		var pageNav = $(this);
		var last_page_pos;			//点击之前的上一个页面数
		
		//总的记录条数
		var total_count = totalNum;
		var isDivide = (total_count%PER_PAGE_NUM == 0);			//判断是否整除
		
		var total_page_num = totalPage;		//总的页数
				
		var html = '';			//生成翻页导航的html代码
		
        html += '<li>总共<label class="total_page_num">' + total_page_num + '</label>页，当前第<label class="curr_page_num">1</label>页</li>';
		html += '<li class="first"><a href="#first">首页</a></li><li class="prev"><a href="#prev">上一页</a></li>';
		
		for(var i=1; i<=total_page_num; i++) {
			if(i==1) {
				html += '<li class="pages"><a href="#p' + i + '" class="curr_page">' + i + '</a></li>';
			} else if(i>=10) {
				html += '<li class="pages"><a href="#p' + i + '" class="w2">' + i + '</a></li>';
			} else if(i>=100) {
				html += '<li class="pages"><a href="#p' + i + '" class="w3">' + i + '</a></li>';
			} else if(i>=1000) {
				html += '<li class="pages"><a href="#p' + i + '" class="w4">' + i + '</a></li>';
			} else if(i>=10000) {
				html += '<li class="pages"><a href="#p' + i + '" class="w5">' + i + '</a></li>';
			} else if(i>=100000) {
				html += '<li class="pages"><a href="#p' + i + '" class="w6">' + i + '</a></li>';
			} else {
				html += '<li class="pages"><a href="#p' + i + '">' + i + '</a></li>';
			};
		};
		
		html += '<li class="next"><a href="#next">下一页</a></li><li class="last"><a href="#last">末页</a></li>';
		pageNav.removeClass('p_0').html(html);	
		var curr_page_num = $('.curr_page_num', this).text();			//当前页数	
				
		
		if(total_page_num > SHOW_PAGE_SIZE) {
			pageNav.find('li.pages').slice(SHOW_PAGE_SIZE, total_page_num).hide();		//加载后只显示预置的页数
			pageNav.find('li.pages:visible:last').after('<li class="more">...</li>');
		};
		
		pageNav.find('a').click(function() {
			last_page_pos = pageNav.find('.curr_page').text();				//记录上次点击的位置
			
			if($(this).parent().hasClass('first')) {						//首页
				curr_page_num = 1;
			} else if($(this).parent().hasClass('prev')) {				//上一页
				if(curr_page_num <= 1) {
					return false;
				};
				curr_page_num --;
			} else if($(this).parent().hasClass('next')) {//下一页
				if(total_page_num-curr_page_num <=0) {
					return false;
				};
				curr_page_num ++;
			} else if($(this).parent().hasClass('last')) {//末页
				curr_page_num = total_page_num;
			} else {											//根据点击数字翻页
				var clickPgae = $(this).text();
				curr_page_num = clickPgae;
			};
			
			if(curr_page_num == last_page_pos) {
				return false;
			};
			
			setCurrPage(pageNav, curr_page_num);
			
			checkPages(pageNav, curr_page_num, last_page_pos, total_page_num);
			
			removeCurrPageStyle(pageNav);		//设置当前页样式
			setCurrPageStyle(pageNav, curr_page_num);
			param[pageNumName] = curr_page_num;
			father.html('').addClass('loading');
			$.post(url,param,function(data){
				father.removeClass('loading');
				//father.children().not('#pageParams').remove();
				father.html(data);
				if($('#pageParams').size() <=1) {
					$('#pageParams:last').remove();
				};
				if(type==1) {
					if(otherObj!='') {
						listObj = $('.cupAll dd');
						listObj.each(function() {
							if(isMycup($('.cupId', this).text(),otherObj)) {
								$('.left', this).append('<img src="'+CTX+'/images/success_icon.gif" alt="已获得该奖杯" />');
							};
						});
					};
				};
				if(type==2) {
					if(otherObj!='') {
						otherObj.hoverPerson();
					};
				};
				if(type==3) {
					$('.r_list li:odd').addClass('even');
				};
				
			});
			return false;
		});
	});
	
	var isMycup = function(cupId,userCupIds) {
		var cupArr = userCupIds.split(',');
		for(var i=0;i<cupArr.length;i++) {
			if(cupArr[i]==cupId){
				return true;
			};
		};
		return false;
	};
	
	
	//设置当前页数
	var setCurrPage = function(pageNav, curr_page_num) {
		pageNav.find('.curr_page_num').text(curr_page_num);
	};
	
	//去除上一个页面的导航样式
	var removeCurrPageStyle = function(pageNav) {
		pageNav.find('a').removeClass('curr_page');
	};
	
	//设置当前页面的导航样式
	var setCurrPageStyle = function(pageNav, curr_page_num) {
		pageNav.find('li.pages a').eq(curr_page_num-1).addClass('curr_page');
	};
	
	//检查页面数量，根据点击显示前后的页面数
	var checkPages = function(pageNav, curr_page_num, last_page_pos, total_page_num) {
		var leftSize = pageNav.find('li.pages').eq(curr_page_num).prevAll('li.pages:visible').size() - 1;
		var rightSize = pageNav.find('li.pages').eq(curr_page_num).nextAll('li.pages:visible').size() + 1;
		var changePos = SHOW_PAGE_SIZE%2 == 0 ?  SHOW_PAGE_SIZE/2 : (SHOW_PAGE_SIZE - 1)/2;
		
		
		//只有但点击的页面的左边、或右边只有一个或0个链接时，才会加载新的链接
		if(last_page_pos-curr_page_num <0 ) {		//预查看的页数比上一页数大
			if(pageNav.find('li.pages:visible:last').text() == total_page_num) {
				pageNav.find('li.pages:visible:last').next('li.more').remove();
				return false;
			};
			if(curr_page_num == total_page_num) {	//若是末页
				pageNav.find('li.pages, li.more').hide();
				pageNav.find('li.pages').slice(curr_page_num - SHOW_PAGE_SIZE, curr_page_num).show();
				pageNav.find('li.pages:visible:first').before('<li class="more">...</li>');
				return false;
			};
			if(rightSize <= changePos - 1) {
				pageNav.find('li.pages:visible:first').hide();
				pageNav.find('li.pages:visible:last').nextAll('li.pages:hidden:first').show();
				
				setDotted(pageNav);
			};
		} else if(curr_page_num-last_page_pos<=0 ) {		//预查看的页数比上一页数小
			if(pageNav.find('li.pages:visible:first').text() == 1) {
				pageNav.find('li.pages:visible:first').prev('li.more').remove();
				return false;
			};
			if(curr_page_num == 1) {	//若是首页
				pageNav.find('li.pages, li.more').hide();
				pageNav.find('li.pages').slice(curr_page_num - 1, curr_page_num - 1 + SHOW_PAGE_SIZE).show();
				pageNav.find('li.pages:visible:last').after('<li class="more">...</li>');
				return false;
			};
			if(leftSize <= changePos - 1) {
				pageNav.find('li.pages:visible:last').hide();
				pageNav.find('li.pages:visible:first').prevAll('li.pages:hidden:first').show();
				
				setDotted(pageNav);
			};
		} else if(last_page_pos == curr_page_num) {			//预查看的页数等于上一页数，不做处理
			return false;
		};
	};
	
	//设置省略号
	var setDotted = function(pageNav) {
		pageNav.find('li.more').remove();
		pageNav.find('li.pages:visible:first').before('<li class="more">...</li>');
		pageNav.find('li.pages:visible:last').after('<li class="more">...</li>');
	};
};
