var app=app || {};
var Route=Backbone.Router.extend({
 routes: {
	"":"index",// #index
	"index(/t:time)(/:group)":"index",    // #index
	"realtime/:theme(/p:page)(/t:time)":"realtime",    //#theme/全部
	"theme(/t:time)":"theme",
	"*other":"index"
		},

  index: function(t,group) {
  	console.log("index");
  	console.log('t:'+t,'group:'+group)
  	app.topModel=new TopModel({
  	  	
	  	});
  	//是否传了默认时间
  	if(t){
  		app.topModel.set("time",t);  		
  	}
  	
  	if(group) {
  		app.topModel.set("group",group);
  		$.cookie("group",group);
  	}else{
  		app.topModel.set("group",undefined)
  		$.cookie("group",undefined);
  	}

  	

  	var datas={
  			time:app.topModel.get("time"),
  			groupName:app.topModel.get("group")
  	};
  	
  	app.topModel.fetch({
  		data:datas,
  		success:function(model,res,opt){
	  			app.topView=new TopView({
	  		  		model:model
	  		  	});
			},
			error:function(err){
				console.error(err);
			}
  	});
  	
  },
  realtime: function(theme,p,t) {
	$(".wrap").html(app.tpls["loadTpl"]());
	var nowPage=p || 1;
	

  	//默认当前时间点
	var time=helper.formatTime(new Date().getTime());
	//如果带时间参数
	if(t){
		t=helper.getTimeStamp(t);
		console.log(t);
		time=helper.formatTime(t);
	}
	

  	var datas={
  		"theme":theme,
  		"time":time,
  		"loadPage":"1",
  		"page":nowPage,
  		"groupName":$.cookie("group")
  	};
  	

  	if(t){
  		app.realTimeModel=new RealTimeModel({
  	 		theme:theme,
  	 		realtime:time
  	 	});
  		app.themeModel=new ThemeModel({
  	  		active:theme,
  	  		realtime:time
  	  	});
  	}else{
  		app.realTimeModel=new RealTimeModel({
  	 		theme:theme
  	 	});
  	  	app.themeModel=new ThemeModel({
  	  		active:theme
  	  	});
  	}
  	
 
 	

 	app.realTimeCollection=new RealTimeCollection();
 	app.realTimeCollection.reset();
 	app.realTimeCollection.fetch({
 		data:datas,
 		success:function(collections,res,opt){
				app.realTimeView=new RealTimeView({
			 		collection:collections,
			 		model:app.realTimeModel
			 	});
 		}
 	});
 

  },
  theme:function(t){
  	console.log("theme");
  	if(!app.themeModel){
  			if(t){
  				t=helper.getTimeStamp(t);
  				var time=helper.formatTime(t);
  				app.themeModel=new ThemeModel({
  					realtime:time
  		  	  	});
  			}else{
  				app.themeModel=new ThemeModel({
  					
  		  	  	});
  			}
  		
  	}
  	
  		var datas = {
  				groupName:$.cookie("group")
  		}
  	
  		app.themeModel.fetch({
  			data:datas,
  			success:function(model,res,opt){
  				app.themeView=new ThemeView({
  			 		model:model
  			 	});
  			},
  			error:function(err){
  				console.error(err);
  			}
  		});
  	
 	
  }

});