/**
 * @author Poly 89490364a@gmail.com
 * @version 0.1
 * @projectDescription  
 */

(function($){
	
	/**
	 * 初始化分页控件
	 */
	$.fn.pageInit=function(o)
	{
		o=o||{};
		o.radius=o.radius||5;
		o.pageSize=o.pageSize||10;
		this.each(function(){
			var that=$(this);
			eval("var param="+that.attr("param"));
			if(param.recordCount<1)
			{return false}
			param.pageSize=parseInt(param.pageSize)||o.pageSize
			param.pageCount=Math.ceil(param.recordCount/param.pageSize);
			param.page=1;
			param.radius=param.radius||o.radius;
			this.p=param;
			this.prev=$(".prev",this);
			this.next=$(".next",this);
			this.pm=$(".pm",this);
			this.nm=$(".nm",this);
			this.num=$("span",this);
			that.bind("onTurn",turn).bind("recordAdd",addRecord).bind("reset",reset);
			this.fun=function(){
				var that=$(this);
				var event=new jQuery.Event("onTurn");
				event.page=parseInt(unParam(that.attr("href")).page)||1;
				event.pageSize=param.pageSize;
				that.trigger(event);
				return false;
			}
			$("a:not(.goTop)",this).bind("click",this.fun);
		});
		
		/**
		 * 复位到首页
		 */
		function reset()
		{
			var event=new jQuery.Event("onTurn");
			event.page=1;
			event.pageSize=this.p.pageSize;
			$(this).trigger(event);
		}
		
		/**
		 * 记录增加，重新计算分页参数
		 */
		function addRecord()
		{
			this.p.recordCount++;
			this.p.pageCount=Math.ceil(this.p.recordCount/this.p.pageSize);
		}		
		
		/**
		 * 翻页处理方法
		 */
		function turn(e)
		{
			var that=$(this);
			var p=this.p;
			this.num.empty();
			p.page=e.page||1;
			this.prev.attr("href","?page="+Math.max(1,p.page-1));
			this.next.attr("href","?page="+Math.min(p.pageCount,p.page+1));
			this.pm.attr("href","?page="+Math.max(1,p.page-10));
			this.nm.attr("href","?page="+Math.min(p.pageCount,p.page+10));
			p.lbound=Math.max(1,p.page-p.radius);
			p.ubound=Math.min(p.pageCount,p.page+p.radius);
			for(var i=p.lbound;i<=p.ubound;i++)
			{
				this.num.append($("<a href='?page="+i+"'>"+i+"</a>").bind("click",this.fun))
			}
			$("span a",this).each(function(){
				var that=$(this);
				if(parseInt(unParam(that.attr("href")).page)==p.page)
				{
					that.addClass("current");
				}
			});
		}
		return this;
	}
	
	$.fn.ajaxInit=function()
	{
		var that=this;
		this.each(function(){
			var that=$(this);
			this.render=$(".render",this).bind("dataChange",changeData);
			this.page=$(".page",this).pageInit();
			eval("this.param="+that.attr("param"));
			this.param.pageSize=this.page[0].p.pageSize||o.pageSize;
			that.bind("onTurn",ajaxProcess)
			$("a:eq(1)",this.page).click();
		});
		
		/**
		 * 渲染器数据更新处理程序
		 * @param {Object} e
		 */
		function changeData(e)
		{
			var that=$(this);
			var template=that.children().eq(0).clone(true);
			that.children().remove();
			for(var i=0;i<e.d.length;i++)
			{
				for(each in e.d[i])
				{
					$("[label="+each+"]",template).attr(e.d[i][each])
				}
				that.append(template.clone(true));
			}
			that.removeClass("loading");
		}
		
		/**
		 * 控件的onTurn事件处理程序。
		 * @param {Object} e
		 */
		function ajaxProcess(e)
		{
			var that=$(this);
			this.param.page=e.page||1;
			this.param.cookie=document.cookie;
			this.render.addClass("loading");
			$.ajax({
				url:that.attr("datasource"),
				render:this.render,
				data:this.param,
				success:function(data){
					var event=new jQuery.Event("dataChange");
					event.d=data;
					this.render.trigger(event);
				},
				error:function(XMLHttpRequest, textStatus, errorThrown){
					var event=new jQuery.Event("dataChange");
					event.d=[{content:{innerHTML:"error!<br/>"+"textStatus:"+textStatus+"<br/>errorThrown:"+errorThrown}}];
					this.render.trigger(event);
				}
			});
		}
		return this;
	}	
})(jQuery);

