(function($){
	function JRuf(){
		this.conf={
			ancho: 100,
			alto:0,
			cssCab:"ui-widget-header",
			cssGrid:"ui-widget-content",
			nomGrid:"Datos de Grid",
			mnsLod:"Cargando datos...",
			formato:"",
			fileAjax:"",
			elemPag:20,
			pagAcv:1,
			pagNom:"Pagina",
			totPag:1,
			frmFil:"",
			botFil:"+ Filtros",
			cadFil:""
		};
	}
	$.extend(JRuf.prototype,{
		jq:null,
		lst:null,
		cabeza:null,
		cuerpo:null,
		pies:null,
		iniciar:function($this,setting){
			this.jq=$this;
			this.lst=$this.children();
			var nConf=setting;
			if(nConf.ancho!=undefined){this.conf.ancho=nConf.ancho;}
			if(nConf.alto!=undefined){this.conf.alto=nConf.alto;}
			if(nConf.cssCab!=undefined){this.conf.cssCab=nConf.cssCab;}
			if(nConf.cssGrid!=undefined){this.conf.cssGrid=nConf.cssGrid;}
			if(nConf.mnsLod!=undefined){this.conf.mnsLod=nConf.mnsLod;}
			if(nConf.nomGrid!=undefined){this.conf.nomGrid=nConf.nomGrid;}
			if(nConf.formato!=undefined){this.conf.formato=nConf.formato;}
			if(nConf.fileAjax!=undefined){this.conf.fileAjax=nConf.fileAjax;}
			if(nConf.elemPag!=undefined){this.conf.elemPag=nConf.elemPag;}
			if(nConf.pagNom!=undefined){this.conf.pagNom=nConf.pagNom;}
			if(nConf.frmFil!=undefined){this.conf.frmFil=nConf.frmFil;}
			if(nConf.botFil!=undefined){this.conf.botFil=nConf.botFil;}
			if(nConf._filtrando!=undefined){this._filtrando=nConf._filtrando;}
			this.cabeza=this.jq.children().filter("div").eq(0);
			this.cuerpo=this.jq.children().filter("div").eq(1);
			this.pies=this.jq.children().filter("div").eq(2);
			this.pintarInicial();
		},
		filtrar:function(){
			this.conf.pagAcv=1;
			this.loading();
			this.conf.cadFil=this._filtrando();
			this.cargar();
		},
		pintarInicial:function(){
			if(this.conf.alto>0){this.jq.css("height",this.conf.alto);}
			if (this.conf.ancho>0){this.jq.css("width", this.conf.ancho);}
			this.cabeza.addClass(this.conf.cssCab);
			var alto1=this.cabeza.height();
			var alto3=this.jq.children().filter("div").eq(2).height();
			var altoCuerpo=this.conf.alto-(alto1+alto3);
			this.cabeza.addClass(this.conf.cssCab);
			this.cabeza.html(this.conf.nomGrid);
			if (this.conf.frmFil.length > 0) {
				this.cabeza.append("<div id='menuFil'>" + this.conf.botFil + "</div>");
				this.cabeza.append("<div id='filtroJRuf' style='display:none'>" + this.conf.frmFil + "</div>");
				$("#menuFil").toggle(function(){
					$("#filtroJRuf").fadeIn("slow");
				}, function(){
					$("#filtroJRuf").fadeOut("slow");
				});
			}
			//cabecera configurada
			this.cuerpo.addClass(this.conf.cssGrid);
//			this.cuerpo.height(altoCuerpo);
			//pie configurado
			this.loading();
			this.pintarPie();
			this.cargar();
		},
		pintarPie:function(){
			this.pies.addClass(this.conf.cssCab);
			this.pies.html("<table><tr><td><div class='ui-state-default ui-corner-all'><span class='ui-icon ui-icon-circle-triangle-w' onclick='$.jRuf.cambioPagina("+(this.conf.pagAcv-1)+")'/></div></td>"+this.conf.pagNom+" "+this.conf.pagAcv+"<td></td><td><div class='ui-state-default ui-corner-all'><span class='ui-icon ui-icon-circle-triangle-e' onclick='$.jRuf.cambioPagina("+(this.conf.pagAcv+1)+")'/></div></td></tr></table>");
		},
		loading:function(){this.cuerpo.html(this.conf.mnsLod);},
		vaciarCuerpo:function(){this.cuerpo.html("");},
		cargar:function(){
			$.post(this.conf.fileAjax, 
			{topePag:this.conf.elemPag,pagAct:this.conf.pagAcv,cond:this.conf.cadFil},
			 function(data) {
				var total=data.registros.length;
				$.jRuf.conf.totPag=Math.ceil(data.total/$.jRuf.conf.elemPag);
				$.jRuf.vaciarCuerpo();
				for(var i=0;i<total && i<$.jRuf.conf.elemPag;++i){
					$.jRuf.addReg(data.registros[i]);
				}
			}, "json");
			this.pintarPie();
		},
		addReg:function(reg){
			var respuesta=this._traducirFormato(reg);
			this.cuerpo.append("<div>"+respuesta+"</div>");
		},
		cambioPagina:function(pag){
			if(pag>0 && pag<=this.conf.totPag){
				this.conf.pagAcv=pag;
				this.cargar();
			}
		},
		refrescar:function() {this.cargar();},
		_traducirFormato:function(reg){
			var frm=this.conf.formato;
			var vecFrm=frm.split("|");
			var totVec=vecFrm.length;
			var salida;
			for(i=0,salida="";i<totVec;++i) {
				if(vecFrm[i].charAt(0)=="{" && vecFrm[i].charAt(vecFrm[i].length-1)=="}"){
					campo=vecFrm[i].substr(1,vecFrm[i].length-2);
					eval("salida+=reg."+campo+";");
				}else{salida+=vecFrm[i];}
			}
			return salida;
		},
		_filtrando:function(){return "{filtro:val}";}
	});
	$.fn.JRuf=function(options){$.jRuf=new JRuf();$.jRuf.iniciar($(this),options);};
})(jQuery);
