RealTimeView = Backbone.View.extend({
    el: ".wrap",
    template: app.tpls["realtimeTpl"],
    initialize: function() {
        this.render();
       	var self=this;
    },
    templateLi: app.tpls["realtimeLiTpl"],
    templateWrap: app.tpls["realtimeWrapTpl"],
    render: function() {
    	
    	var theme="全部";
    	if(app.themeModel){
    		var theme=app.themeModel.get("active");
    	}
    
        this.$el.html(this.template({
            data: this.collection.models,
            theme:theme,
            realtime:app.realTimeModel.get("realtime")
        }));
        
        console.log(this.collection);
        this.swiper();
        this.iscorller();
    },
    iscorller:function(){
        this.scorllers = {};
        var self = this;
        var h = $(".swiper-slide").height();
        var loading = '<div class="loading"></div>';
        var nomore = '<div class="nomore">没有更多数据</div>';

   
    	 
    	 _.each(this.collection.models, function(m) {
             e = m.toJSON();
             self.scorllers[m.cid] = new IScroll('.wrapper' + e.time.replace(" ","-"),{
            	 mouseWheel: true,
            	 click: true 
             });
             self.scorllers[m.cid].on('scrollEnd', function() {
                 if (this.y <= this.maxScrollY) {
                     if (!$(this.wrapper).find(".loading").length) {
                         $(this.wrapper).find(".events").append(loading);
                         var time = $(this.wrapper).attr("data-time"),
                             model = m.get(m.cid),
                             theme = m.get("theme"),
                             page = m.get("page") + 1;
                         	m.set("page", page);
                         if (page > 10) {
                             $(this.wrapper).find(".loading").remove();
                             if (!$(this.wrapper).find(".nomore").length) {
                                 $(this.wrapper).find(".events").append(nomore);
                             }
                             return false;
                         }
                         time=helper.getTime(time);
                         self.loadMore(theme, time, page, this.wrapper, self.scorllers[m.cid]);
                     }
                 }
             });
         }); 
    	 
        $(".wrapper").height(h - 37);
    },
    swiper: function() {
        var cache = [];
        var self = this;
        this.mySwiper = new Swiper('.swiper-container', {
            // Optional parameters
            initialSlide: 0,
            prevButton: '.swiper-button-prev',
            nextButton: '.swiper-button-next',
            onSlideChangeEnd: function(swiper) {
                console.log(swiper)
                console.log(swiper.activeIndex)
                console.log(swiper.slides.length)
                var time = $(".swiper-slide-next").find(".wrapper").attr("data-time");
                var id= $(".swiper-slide-next").find(".wrapper").attr("data-id");
                	if(!time){
                		return;
                	}
                
                if ((swiper.activeIndex == (swiper.slides.length - 2))) {

                    var model = self.collection.get(id);
                    	console.log(model)
                    var  theme = model.get("theme");
 
                    var datas = {
                        "theme": theme,
                        "time": helper.nextTime(time),
                        "loadPage":"1",
                        "page":1
                    };
                 
                    
            	if((id in cache)){
            		return false;
            	}

                   cache.push(id);
                   self.loadPage(datas);
                }
            }
        });
    },
    loadPage: function(datas) {
        var self = this;
      //  alert(datas.time);
        $.ajax({
            url: "servlet/Realtime.do",
//            url: "http://wr52b5.natappfree.cc/bjfzxWebapp/servlet/Realtime",
            type: "GET",
            data:datas,
            dataType: "json",
            success: function(data) {
                self.collection.add(data);
                console.log(self.collection)
                var html = [];
                for(var i=self.collection.models.length,j=i-data.length;j<i;j++){
                	 var tmp = self.templateWrap({
                         data: self.collection.models[j]
                     });
                     html.push(tmp);
                }
                self.mySwiper.appendSlide(html);
                self.iscorller();
            },
            error: function(e) {
                console.log(e)
            }
        });
    },
    loadMore: function(theme, time, page, target, iscroller) {
    	
        var datas = {
            "theme": theme,
            "time": time,
            "page": page,
            "loadPage":"0"
        };
        var nomore = '<div class="nomore">没有更多数据</div>';
        $(target).find(".events .nomore").remove();
        var model = this.collection.get(time);
        var self = this;
        $.ajax({
            url: "servlet/Realtime.do",
//            url: "http://wr52b5.natappfree.cc/bjfzxWebapp/servlet/Realtime",
            data: datas,
            type: "GET",
            dataType: "json",
            success: function(data) {
            	if(data.length){
            	     var html = self.templateLi({"data":data});
                     $(target).find(".events").append(html);
                     $(target).find(".loading").remove();
                     iscroller.refresh();
            	}else{
            		  $(target).find(".events").append(nomore);
                	  $(target).find(".loading").remove();
            	}
            	 iscroller.refresh();
            	
            },
            error: function(e) {
                console.log(e);
            }
        });
    }
});