//定义channel类
var channel=function(channelID){
this.id=channelID;
this.table=channelID;
this.newsLastUTime="1000000000000";
this.nids=localStorage.getItem(channelID+"n");
this.objs;
this.firstTime=true;
this.query_or=localStorage.getItem(channelID+"q");
this.query_must=localStorage.getItem("query_must");
}

//定义channels类
var c=function(){
this.channelA0= new channel("a0");
this.channelA1= new channel("a1");
this.channelA2= new channel("a2");
this.channelA3= new channel("a3");
this.channelA4= new channel("a4");
this.channelA5= new channel("a5");
this.channelA6= new channel("a6");
this.channelA7= new channel("a7");
}




	
//----------开始运行--------

	var c=new c();
	
	//开始运行channels
	main(c.channelA0);
	main(c.channelA1);
	main(c.channelA2);
	main(c.channelA3);
	main(c.channelA4);
	main(c.channelA5);
	main(c.channelA6);
	main(c.channelA7);

	setInterval(function(){main(c.channelA0)},20000);
	//setInterval(function(){showNewsNotify(waitShowObjs)},10000);

	//启动日志
	$.ajax(httpHeadUrl+"/Log/wlog/user_id/0/action/1/machine_id/"+MACHINE_ID+"/category/2");
	//bubujs.showNotifications(["title", "content","url"],"apps/notifications.html",300,115);         //右下角显示出来

//函数




function main(channel){

	url=httpHeadUrl+"/Ajax/getListInfo?page=0&pagesize=100&nid="+channel.nids+"&time="+channel.newsLastUTime+"&query_must="+channel.query_must+"&query_or="+channel.query_or;
	htmlobj=$.ajax({url:url,async:true,success:function(data){getAjaxInfo(data,channel)}  });
	console.log(url);
}

function getAjaxInfo(data,channel){
	//console.log("getAjaxInfo");
	//console.log(data+table);
		channel.objs=eval(data);
			if(channel.objs[0].id!=undefined){           //如果已定义
				channel.newsLastUTime=getLastTime(channel.objs);  //写入最后时间
				if(channel.firstTime){                               //清空等待加载标记
					var trCount=0;
					//$("#"+channel.table+" table tbody tr")[0].remove();
					//addToObjs(channel.objs);			
					insertNews(channel);          //插入表格
					channel.firstTime=false;
				}else{
					if(channel.id=="a0"){
						addToObjs(channel.objs);			//添加信息进消息提醒队列
						insertNews(channel);          //插入表格

					}
				}
				console.log(channel.objs);
			}else{
				console.log(data);
			}
}

//把数据添加到waitShowObjs里
function addToObjs(objNews){
	var i;
	 //for(i=0;i<=objNews.length-1;i++){            //把最新的push到数组最前面
		//waitShowObjs.push(objNews[i]);
	 //}
	 for(i=(objNews.length-1);i>=0;i--){           //把最新的push到数组最后面
		waitShowObjs.push(objNews[i]);  
	 }
}

//定时从waitShowObjs里删除并显示出来


 function showNewsNotify(objNews){
	//setTimeout("bubujs.showNotifications(['"+objNews[i].title+"', '"+objNews[i].url+"','"+objNews[i].name+"','"+objNews[i].time+"'],'apps/notifications.html',300,117);",6000*((objNews.length-1)-i));
	if(objNews.length>0){
	//bubujs.showNotifications([objNews[0].title,objNews[0].url,objNews[0].name,objNews[0].time],'apps/notifications.html',300,117);
	bubujs.showNotifications(objNews[0].name +" "+sTime(objNews[0].time) , objNews[0].title,300,117);
	objNews.shift()
	}
	//alert(5000*((objNews.length-1)-i));
 }


function getLastTime(objNews){
	var nLastTime;
	var unixTime;
	nLastTime=new Date(objNews[0].time);
	unixTime=nLastTime.getTime();
	return unixTime
}


//循环遍历插入信息
function insertNews(channel){
	var nids1=localStorage.getItem("a1n");
	var nids2=localStorage.getItem("a2n");
	var nids3=localStorage.getItem("a3n");
	var nids4=localStorage.getItem("a4n");
	var nids5=localStorage.getItem("a5n");
	var nids6=localStorage.getItem("a6n");
	var nids7=localStorage.getItem("a7n");


	nids1=nids1.split(",");
	nids2=nids2.split(",");
	nids3=nids3.split(",");
	nids4=nids4.split(",");
	nids5=nids5.split(",");
	nids6=nids6.split(",");
	nids7=nids7.split(",");
	
//判断当前的线程是否是a0,如果是的话，再判断是否第一次，如果是第一次，那么只插入到自己的队列里，如果不是第一次，则插入到其它频道的表格中
//如果不是a0，那么
	if(channel.id=="a0"){
		if(channel.firstTime){
			var i;
				for(i=(channel.objs.length-1);i>=0;i--){
				addNew(channel.objs[i],"a0");
				}
			}
		else{
			var i;
			for(i=(channel.objs.length-1);i>=0;i--){
				addNew(channel.objs[i],"a0");
				if(nids1.indexOf(channel.objs[i].nid)!=-1){
				addNew(channel.objs[i],"a1");
				}
				if(nids2.indexOf(channel.objs[i].nid)!=-1){
				addNew(channel.objs[i],"a2");
				}
				if(nids3.indexOf(channel.objs[i].nid)!=-1){
				addNew(channel.objs[i],"a3");
				}
				if(nids4.indexOf(channel.objs[i].nid)!=-1){
				addNew(channel.objs[i],"a4");
				}
				if(nids5.indexOf(channel.objs[i].nid)!=-1){
				addNew(channel.objs[i],"a5");
				}
				if(nids6.indexOf(channel.objs[i].nid)!=-1){
				addNew(channel.objs[i],"a6");
				}
				if(nids7.indexOf(channel.objs[i].nid)!=-1){
				addNew(channel.objs[i],"a7");
				}
			}
		}
	}else{
		for(i=(channel.objs.length-1);i>=0;i--){
			addNew(channel.objs[i],channel.table);
		}
	}
		
}
//插入信息
function addNew(objNew,table){
strNew='<tr><td width="70px">'+ sTime(objNew.time) +'</td><td><a target="_blank" href="'+objNew.url+'">'+ objNew.title+'</a>-'+ objNew.name+'</td></tr>';
$("#"+table+" table tbody").prepend(strNew);
}



