// JavaScript Document
//投票类
function VoteClass(){
	this.url='';//文章地址
	this.group='';//投票组名称
	this.value=1;//投票加1
	this.postURL='http://app5.dayoo.com/news_mood/vote.php';//提交地址
	this.sel_fun='';//查找后处理函数
	this.vote_fun='';//投票后处理函数
	this.vote_sel_fun='';//投票并查找后处理函数
	this.cookie_time='60';//投票cookie限时
	this.checkCookie=true;//是否进行投票限时检测
	this.cookie_fun='';//投票限时处理函数
		
	//查找
	this.sel=function(sel_fun){
		if(sel_fun==null || sel_fun=='')sel_fun=this.sel_fun;
		
		if(this.url=='' || this.group=='' || sel_fun==''){
			alert('plase set url&group&sel_fun first');
			return;
		}
		var params="action=sel&group="+encodeURIComponent(this.group)+"&url="+encodeURIComponent(dojo.string.trim(this.url));
		this.dojoBind(params,sel_fun);
	}
	//投票
	this.vote=function(name,value,vote_fun){
		if(value==null || value=='')value=this.value;
		if(vote_fun==null || vote_fun=='')vote_fun=this.vote_fun;	
		
		if(this.url=='' || name=='' || value=='' || vote_fun==''){
			alert('plase set url&name&value&vote_fun first');
			return;
		}

		if(this.checkCookie){if(!this.setCookie())return;}
		
		var params="action=vote&name="+encodeURIComponent(name)+"&value="+encodeURIComponent(value)+"&url="+encodeURIComponent(dojo.string.trim(this.url));
		this.dojoBind(params,vote_fun);
	}
	//投票并查找
	this.vote_sel=function(name,value,vote_sel_fun){
		if(value==null || value=='')value=this.value;
		if(vote_sel_fun==null || vote_sel_fun=='')vote_sel_fun=this.vote_sel_fun;	
		
		if(this.url=='' || name=='' || value=='' || this.group=='' || vote_sel_fun==''){
			alert('plase set url&name&value&group&vote_sel_fun first');
			return;
		}
		
		if(this.checkCookie){if(!this.setCookie())return;}
		
		var params="action=vote_sel&name="+encodeURIComponent(name)+"&value="+encodeURIComponent(value)+"&group="+encodeURIComponent(this.group)+"&url="+encodeURIComponent(dojo.string.trim(this.url));
		this.dojoBind(params,vote_sel_fun);
	}
	
	//一定时间内不能重复投票
	this.setCookie=function(cookie_time,fun){
		
		if(cookie_time==null || cookie_time=='')cookie_time=this.cookie_time;
		if(fun==null || fun=='')fun=this.cookie_fun;
		
		if(cookie_time==''){
			alert('plase set cookie_time first');
			return false;
		}
		
		if(fun==''){
			var cf=function(time){
				alert('place vote after '+time+' minutes');
			}
			fun='cf';
		}
		
		var url=dojo.string.trim(this.url.toString());
		var cookie_name=url.replace(/#/g,'');
		var dateObj=new Date();
		var dateT=dateObj.getTime();
	
		if(!dojo.io.cookie.get(cookie_name)){
			dojo.io.cookie.set(cookie_name,dateT);
			return true;
		}
		
		if(dateT-dojo.io.cookie.get(cookie_name)<0 || dateT-dojo.io.cookie.get(cookie_name)>cookie_time*60*1000){
			dojo.io.cookie.set(cookie_name,dateT);
			return true;
		}else{
			eval(fun+'('+this.cookie_time+')')
			return false;
		}
		
	}
	
	//检查cookie是否有限
	this.isVote=function(cookie_time){
		if(cookie_time==null || cookie_time=='')cookie_time=this.cookie_time;
		if(cookie_time==''){
			alert('plase set cookie_time first');
			return true;
		}
		var url=dojo.string.trim(this.url.toString());
		var cookie_name=url.replace(/#/g,'');
		var dateObj=new Date();
		var dateT=dateObj.getTime();
		
		if(!dojo.io.cookie.get(cookie_name)){
			return false;
		}

		if(dateT-dojo.io.cookie.get(cookie_name)<0 || dateT-dojo.io.cookie.get(cookie_name)>cookie_time*60*1000){
			return false;
		}
		return true;
	}
	
	this.dojoBind=function(params,handleFun){
		if(this.postURL==''){
			alert('plase set postURL first');
			return;
		}
		var transport = dojo.io.ScriptSrcTransport;
		var kw = {
		url: this.postURL,
		constantParams: params,
		transport: "ScriptSrcTransport",
		jsonParamName: "callback",
		preventCache: true,
		load: function (type, data, evt) {eval(handleFun+"(data)");transport.removeScripts();},
		timeout: function (){timeout();},
		timeoutSeconds: 30,
		error:function(type, error){/*alert('读取数据失败，请联系管理员！'+type+' ERROR:'+error.message);*/},
		mimetype: "text/javascript",
		preventCache:true,
		apiId: "vote"
		//sync: true
		};
		dojo.io.queueBind(kw);
	}
	
}

//获取投票类别类
function VoteListClass(){
	
	this.group='';
	this.type='';
	this.fun='';
	this.postURL='http://app5.dayoo.com/news_mood/voteList.php';//提交地址
	this.limit='10';
	this.date='';
	
	this.list=function(group,type,limit,fun){
		if(group==null || group=='')group=this.group;
		if(type==null || type=='')type=this.type;
		if(limit==null || limit=='')limit=this.limit;
		if(fun==null || fun=='')fun=this.fun;
		
		if( (group=='' && type=='') || fun==''){
			alert('plase set (url/type)&fun first');
			return;
		}

		
		var params="group="+encodeURIComponent(group)+"&type="+encodeURIComponent(type)+"&limit="+encodeURIComponent(limit)+"&date="+this.date;
		this.dojoBind(params,fun);
	}
	
	this.dojoBind=function(params,handleFun){
		if(this.postURL==''){
			alert('plase set postURL first');
			return;
		}
		var transport = dojo.io.ScriptSrcTransport;
		var kw = {
		url: this.postURL,
		constantParams: params,
		transport: "ScriptSrcTransport",
		jsonParamName: "callback",
		preventCache: true,
		load: function (type, data, evt) {eval(handleFun+"(data)");transport.removeScripts();},
		timeout: function (){timeout();},
		timeoutSeconds: 30,
		error:function(type, error){/*alert('读取数据失败，请联系管理员！'+type+' ERROR:'+error.message);*/},
		mimetype: "text/javascript",
		preventCache:true,
		apiId: "vote"
		//sync: true
		};
		dojo.io.queueBind(kw);
	}
	
}