var engine = { s : 0, r : null, target : null, busy : false, render : function(url, thumb, name){ return '<a href="'+crtr_url('/'+url+'/',this.s)+'"><img src="/'+url+'/t'+thumb+'.jpg" alt="'+name+'" /></a>'; }, init : function(r, target){ if (!r) return; this.r = r; if (!target) return; this.target = $(target); var that = this; $(window).scroll(function(){ if ( $(document).height() - $(window).height() <= $(window).scrollTop() + 50 ) { that.scrollPosition = $(window).scrollTop(); that.get(); } }); }, append : function(data){ var that = this; $.map(data,function(d,url){ that.target.append(that.render(url,d.thumb,d.name)); }); if (this.scrollPosition !== undefined && this.scrollPosition !== null) { $(window).scrollTop(this.scrollPosition); } }, get : function() { if (!this.r || !this.target || this.busy) return; this.s++; this.setBusy(true); var that = this; $.post( '/', { s:this.s, r:this.r }, function(data){ that.append(data); that.setBusy(false); }, 'json' ); }, showLoading : function(bState){ var loading = $('#loading'); if (bState) { loading.show('slow'); } else { loading.hide(); } }, setBusy : function(bState){ this.showLoading(this.busy = bState); } };
