var Clientes_lista_mini=function(){
	this.baseUrl=null;
	this.windowId=null;
	this.clientes=null;
	this.fields={id:null,nombre:null,dui:null,nit:null,nrc:null,sender:null};
	this.initializeEnviroment=function(url,winId){
		var _this=this;
		this.baseUrl=url;
		this.windowId=winId;
		this.initializeGUIComponets();
		this.initializeTools();
		this.initializeListeners();
		this.clientes.cargarHTML(true,0);
	},
	/**/
	this.setFields=function(id,nombre,dui,nit,nrc,sender){
		this.fields.id=id;
		this.fields.nombre=nombre;
		this.fields.dui=dui;
		this.fields.nit=nit;
		this.fields.nrc=nrc;
		this.fields.sender=sender;
	},
	/*Inicializar componentes de la interfaz*/
	this.initializeGUIComponets=function(){
		$("#"+this.windowId+" #clientes-search-str").focus();
	},
	/*Inicializar clase de apoyo*/
	this.initializeTools=function(){
		this.clientes=new Clientes(); 
		this.clientes.setUrl(this.baseUrl);
	},
	/*Eventos*/
	this.initializeListeners=function(){
		var _this=this;
		/*TEXTBOX buscar cliente*/
		$("#"+this.windowId+" #clientes-search-str").keyup(function(e){
			if(e.which==38 || e.keyCode==38){
				e.preventDefault();
				_this.goTop();
			}else if(e.which==40 || e.keyCode==40){
				e.preventDefault();
				_this.goBottom();
			}else if(e.which==13 || e.keyCode==13){
				e.preventDefault();
				var currentSel=$("#"+_this.windowId+" #clientes-lista-table > tbody > tr.miracle-selected-table-item");
				if(currentSel.length){
					_this.setCliente(currentSel.attr("id").split("-")[1],currentSel.find("td h4").text(),currentSel.attr("data-dui"),currentSel.attr("data-nit"),currentSel.attr("data-nrc"),currentSel.attr("data-clasificacion"),currentSel.attr("data-credito"));
				}
			}else{
				_this.clientes.cargarHTML(true,0);
			}			
		});
		/*BOTON nuevo cliente*/
		 $("#"+this.windowId+" #clientes-nuevo-btn").click(function(){
			Custombox.open({
				target:_this.baseUrl+"clientes/nuevo?mode=create", 
				effect: 'fadein',
				overlayClose:false,
				complete:function(){
					$("#nuevo-cliente-modal input:text").first().focus();
				}
			});
		});
		/*PANEL scroll*/
		$("#"+this.windowId+" #clientes-lista-wrapper").scroll(function(){
		   if($(this).scrollTop()+$(this).height()+25 >= $("#"+_this.windowId+" #clientes-lista-table").height()){
			   _this.clientes.cargarHTML(false,$("#"+_this.windowId+" #clientes-lista-table > tbody tr").size()-1);
		   }
		});
		/*TABLA clic celda de producto*/
		$("#"+this.windowId+" #clientes-lista-table").on("click","tbody tr",function(){
			$("#"+_this.windowId+" #clientes-lista-table > tbody tr.miracle-selected-table-item").removeClass("miracle-selected-table-item");
			$(this).addClass("miracle-selected-table-item");
		});
		/*TABLA doble clic celda de producto*/
		$("#"+this.windowId+" #clientes-lista-table").on("dblclick","tbody tr",function(){
			_this.setCliente($(this).attr("id").split("-")[1],$(this).find("td h4").text(),$(this).attr("data-dui"),$(this).attr("data-nit"),$(this).attr("data-nrc"),$(this).attr("data-clasificacion"),$(this).attr("data-credito"));
		});
		/*DOCUMENT tecla arriba*/
		$(document).bind('keydown', 'up', function(e){
			if($("#"+_this.windowId).is(":visible")){
				e.preventDefault();
				_this.goTop();
			}
		});
		/*DOCUMENT tecla abajo*/
		$(document).bind('keydown', 'down', function(e){
			if($("#"+_this.windowId).is(":visible")){
				e.preventDefault();
				_this.goBottom();
			}
		});
		/*DOCUMENT tecla enter*/
		$(document).bind('keydown', 'return', function(e){
			if($("#"+_this.windowId).is(":visible")){
				e.preventDefault();
				var currentSel=$("#"+_this.windowId+" #clientes-lista-table > tbody tr.miracle-selected-table-item");
				if(currentSel.length){
					_this.setCliente(currentSel.attr("id").split("-")[1],currentSel.find("td h4").text(),currentSel.attr("data-dui"),currentSel.attr("data-nit"),currentSel.attr("data-nrc"),currentSel.attr("data-clasificacion"),currentSel.attr("data-credito"));
				}
			}
		});
	},
	/*FUNCIONES*/
	this.setCliente=function(id,nombre,dui,nit,nrc,clasificacion,credito){
		if(this.fields.id)
			this.fields.id.val(id);
		if(this.fields.nombre){
			this.fields.nombre.val(nombre);
			this.fields.nombre.attr('data-type',clasificacion);
			this.fields.nombre.attr('data-credito',credito);
			this.fields.nombre.prop('readonly','true');
		}
		if(this.fields.dui)
			this.fields.dui.val(dui);
		if(this.fields.nit)
			this.fields.nit.val(nit);
		if(this.fields.nrc)
			this.fields.nrc.val(nrc);
		if(this.fields.sender){
			this.fields.sender.removeClass('btn-info');
			this.fields.sender.find('i').removeClass('fa-search');
			this.fields.sender.addClass('btn-default');
			this.fields.sender.find('i').addClass('fa-times');
			this.fields.sender.attr('data-function','unset');
		}
		Custombox.close();
	},
	this.goTop=function(){
		if($("#"+this.windowId).is(":visible")){
			var trows=$("#"+this.windowId+" #clientes-lista-table > tbody tr");
			if(trows.size()>1){
				var currentSel = $("#"+this.windowId+" #clientes-lista-table > tbody tr.miracle-selected-table-item");
				var prevSel=currentSel.prev();
				if(prevSel.length > 0){
					currentSel.removeClass("miracle-selected-table-item");
					prevSel.addClass("miracle-selected-table-item");
					/*AUTO SCROLL*/								
					 if(((prevSel.index()+1)*prevSel.height())<=$("#"+this.windowId+" #clientes-lista-wrapper").scrollTop()){
						 $("#"+this.windowId+" #clientes-lista-wrapper").scrollTop($("#"+this.windowId+" #clientes-lista-wrapper").scrollTop()-prevSel.height());
					 }
				}
			}
		}
	},
	this.goBottom=function(){
		if($("#"+this.windowId).is(":visible")){
			var trows=$("#"+this.windowId+" #clientes-lista-table > tbody tr");
			if(trows.size()>1){
				var currentSel=$("#"+this.windowId+" #clientes-lista-table > tbody tr.miracle-selected-table-item");
				var nextSel=currentSel.next();
				if(nextSel.length>0){
					currentSel.removeClass("miracle-selected-table-item");
					nextSel.addClass("miracle-selected-table-item");
					 if((((nextSel.index())*nextSel.height())+nextSel.height())>=($("#"+this.windowId+" #clientes-lista-wrapper").scrollTop()+$("#"+this.windowId+" #clientes-lista-wrapper").height())){
						 $("#"+this.windowId+" #clientes-lista-wrapper").scrollTop($("#"+this.windowId+" #clientes-lista-wrapper").scrollTop()+nextSel.height());
					 }
				}
			}
		}
	}	
}