// JavaScript Document

var discuz = {
	deletethread : function(fid,tid) {
		SMS.closepage();
		setTimeout(function(){
			$('.currentbody #thread_'+tid).remove();
			var threads =parseInt($('#threads_'+fid).text())-1;
			$('#threads_'+fid).text(threads);		
			SMS.deleteitem('discuz.php?mod=forum&fid='+fid);
		},300);
	},
	deletepost : function(tid,pid) {
		$('.currentbody #reply_'+pid).remove();
		var replys=parseInt($('#replyscount_'+tid).text())-1;
		$('#replyscount_'+tid).text(replys);
		SMS.deleteitem('discuz.php?mod=view&tid='+tid);
	},
	settop : function(id,val,type) {
		if(type=='thread'){
			var text=val==1?'取消置顶':'置顶';
			if(val==1){
				$('.currentbody .article_title').prepend('<span class="b_c8 c3">置顶</span>');
			}else{
				$('.currentbody .article_title .b_c8').remove();
			}
			$('#settop_'+id).text(text);	
		}else{
			if(val==0){
				$('.currentbody #reply_'+id+' .reply_content h3 span.b_c8').remove();
			}else{
				$('.currentbody #reply_'+id+' .reply_content h3').append('<span class="b_c8 c3">置顶</span>');
			}
		}
		SMS.deleteitem('discuz.php?mod=view&tid='+id);
	},
	setbest : function(id,val) {
		var text=val==1?'取消推荐':'推荐';
		if(val==1){
			$('.currentbody .article_title').prepend('<span class="b_c1 c3">推荐</span>');
		}else{
			$('.currentbody .article_title .b_c1').remove();
		}
		$('#setbest_'+id).text(text);
		SMS.deleteitem('discuz.php?mod=view&tid='+id);
	},
	praise : function(id,val,tid) {
		if(tid){
	    $('.currentbody #reply_'+id+' .icon-praise').addClass('c1').html('<span class="s13 pl5">+'+val+'</span>');
			SMS.deleteitem('discuz.php?mod=view&tid='+tid,true,true);
		}else{
			SMS.upcount('#threadpraise_'+id);
			$('#threadpraise_'+id+' a').addClass('c1');
			SMS.deleteitem('discuz.php?mod=view&tid='+id,true,true);
		}
	},
	praiselist : function(id,val) {
		$('#thread_'+id+' .icon-praise').addClass('c1');
		$('#thread_'+id+' .threadpraise').text(val);
	},
	rate : function(tid,res) {
		var s = $.parseJSON(res);
		SMS.closepage();
		setTimeout(function(){
			var html='<div class="b_c5 p10 mb5 cl">';
					html +='<img src="'+s.avatar+'" class="avatar" onerror="this.onerror=null;this.src=\'./ui/avatar_middle.jpg\'">';
					html +='<h4><span class="r c9 s13">+'+s.score+s.title+'</span><a href="user.php?dzuid='+s.uid+'" class="load">'+s.username+'</a></h4>';
					html +='<p class="c4 s15">'+s.reason+'</p>';
					html +='</div>';
			$('.currentbody #rates_'+tid).append(html);
			SMS.deleteitem('discuz.php?mod=view&tid='+id,true,true);
		},300);
	},
	
	fav : function(id,type) {
		if(type=='tid'){
			$('#threadfavbtn_'+id).addClass('c1');
			SMS.deleteitem('discuz.php?mod=view&tid='+id);
		}else if(type=='fid'){
			$('#forumbtn_'+id).removeClass('weui-btn weui-btn_mini load').addClass('icon icon-openside').text('');
			$('#forumbtn_'+id).attr('href','javascript:SMS.openside()');
			$('#forumfav_'+id).remove();
			SMS.deleteitem('discuz.php?mod=forum&fid='+id,true,true);
		}else{
			$('#newsfavbtn_'+id).addClass('c1');
			SMS.deleteitem('discuz.php?mod=news&aid='+id,true,true);
		}
	},
	setactivitytime : function(obj) {
		if(obj.checked){
			var html ='<div class="weui-cell"><div class="weui-cell__hd"><label for="" class="weui-label s15">开始时间</label></div><div class="weui-cell__bd">';
			    html +='<input class="weui-input" type="datetime-local" name="special[starttimefrom]" value=""></div></div>';
					html +='<div class="weui-cell"><div class="weui-cell__hd"><label for="" class="weui-label s15">结束时间</label></div><div class="weui-cell__bd">';
					html +='<input class="weui-input" type="datetime-local" name="special[starttimeto]" value=""></div></div>';
		}else{
			var html ='<div class="weui-cell"><div class="weui-cell__hd"><label for="" class="weui-label s15">活动时间</label></div><div class="weui-cell__bd">';
			    html +='<input class="weui-input" type="datetime-local" name="special[starttimefrom]" value=""></div></div>';
		}
		$('#setactivitytime').html(html);
	},
	addthread : function(tid,fid) {
		SMS.ajax('discuz.php?mod=get&tid='+tid,'.currentbody .themeslist','prepend');
		var threads =parseInt($('#threads_'+fid).text())+1;
		$('#threads_'+fid).text(threads);
		SMS.deleteitem('discuz.php?mod=forum&fid='+fid);
	},
	editthread : function(tid,fid,subject) {
		$('#subject_'+tid).text(subject);
		SMS.ajax('discuz.php?mod=get&show=c&tid='+tid,'.currentbody .weui-article','replace');
		SMS.deleteitem('discuz.php?mod=view&tid='+tid);
	},
	addreply : function(pid,tid) {
		SMS.ajax('discuz.php?mod=get&pid='+pid,'.currentbody #replylist');
		var replys =parseInt($('#replyscount_'+tid).text())+1;
		$('.currentbody #replyscount_'+tid).text(replys);
		setTimeout(function(){$(window).scrollTop($("body").height());},100);			
		SMS.deleteitem('discuz.php?mod=view&tid='+tid);
	},
	editreply : function(pid,tid) {
		SMS.ajax('discuz.php?mod=get&show=c&pid='+pid,'.currentbody #replycontent_'+pid,'replace');
		SMS.deleteitem('discuz.php?mod=view&tid='+tid);
	},
	insertpic : function(aid) {
		$('#discuz_post_form').append('<input type="hidden" name="pics[]" value="'+aid+'">');
	},
	setansres : function(tid,pid) {
		$('#rewardarea_'+tid).html('<p class="notice">本悬赏已解决</p>');
		$('#reply_'+pid).addClass('b_c9');
		$('#reply_'+pid+' .reply_content h3').append('<span class="b_c1 c3">最佳答案</span>');
	},
	setans : function(tid,pid) {
		SMS.open('确定要将本回复采用为答案吗','confirm','discuz.php?mod=action&&ac=setans&tid='+tid+'&pid='+pid);
	},
	delpic : function(aid) {
		$('.currentbody #img_'+aid).remove();
		$('#discuz_post_form').append('<input type="hidden" name="delpics[]" value="'+aid+'">');
	},
	editcomment : function(cid,aid) {
		SMS.ajax('discuz.php?mod=get&cid='+cid+'&ac=edit','.currentbody #commentcontent_'+cid,'replace');
		SMS.deleteitem('discuz.php?mod=news&aid='+aid);
	},
	addcomment : function(cid,aid) {
		SMS.ajax('discuz.php?mod=get&cid='+cid,'.currentbody #replylist');
		var replys =parseInt($('#replyscount_'+aid).text())+1;
		$('.currentbody #replyscount_'+aid).text(replys);
		setTimeout(function(){$(window).scrollTop($("body").height());},100);			
		SMS.deleteitem('discuz.php?mod=news&aid='+aid);
	},
	click3 : function(aid) {
		SMS.upcount('#newsclick3_'+aid);
		$('#newsclick3_'+aid+' a').addClass('c1');
		SMS.deleteitem('discuz.php?mod=news&aid='+aid,true,true);
	},
	deletecomment : function(aid,cid) {
		$('.currentbody #comment_'+cid).remove();
		var replys=parseInt($('#replyscount_'+aid).text())-1;
		$('#replyscount_'+aid).text(replys);
		SMS.deleteitem('discuz.php?mod=news&aid='+aid);
	},
	option : function(res) {
		var s = JSON.parse(res);
		var src=s.type=='smsot'?SITEURL+s.src:s.src;
		$('.currentbody #sp_'+s.optionid+'_area').html('<img src="'+s.src+'">');
		$('.currentbody #option_'+s.optionid).val(src);
	},
	pollimg : function(res) {
		var s = JSON.parse(res);
		$('.currentbody #poll_'+s.pollid+' .upload').removeClass('icon icon-pic').html('<img src="'+s.src+'">');
		$('.currentbody #pollimg'+s.pollid).val(s.aid);
	},
	activitypic : function(res) {
		var s = JSON.parse(res);
		$('.currentbody #activitypic_img').html('<img src="'+s.src+'">');
		$('.currentbody #special_aid').val(s.aid);
		$('.currentbody #activitypic_inp').html('<input type="hidden" name="pics[]" value="'+s.aid+'">');
		if(s.del){
			$('#discuz_post_form').append('<input type="hidden" name="delpics[]" value="'+s.del+'">');
		}
	},
	checkopt : function(id,maxcheck){
    var len = $(".currentbody input[name='opid[]']:checked").length;
		if($(".currentbody input[name='opid[]']:checked").length>maxcheck){
			$(".currentbody #"+id).prop("checked",false);
			SMS.open('最多只能选'+maxcheck+'项','alert');
		}
	},
	addpollitem : function() {
		pollid++;
		var poll='<div class="weui-cell" id="poll_'+pollid+'"><div class="weui-cell__hd"></div><div class="weui-cell__bd">';
			 poll+='<input class="weui-input" type="text" name="poll['+pollid+']" placeholder="投票项目">';
			 poll+='</div><div class="weui-cell__ft"><a class="upload icon icon-pic" href="javascript:;" additional="'+pollid+'" name="pollitem"></a><a href="javascript:$(\'#poll_'+pollid+'\').remove()" class="icon icon-close c9"></a></div><input type="hidden" name="pollimg['+pollid+']" value="" id="pollimg'+pollid+'"/></div>';
		$('.currentbody #pollitems').append(poll);
	},
	delpollitem : function(id,type) {
		var type=type||'';
		$('.currentbody #poll_'+id).remove();
		if(type=='edit'){
			$('.currentbody #pollitems').append('<input type="hidden" name="delpoll[]" value="'+id+'">');
		}
	},
	poll : function(tid) {
		SMS.ajax('discuz.php?mod=get&&pollid='+tid,'.currentbody #viewpoll_'+tid,'replace');
		SMS.deleteitem('discuz.php?mod=view&tid='+tid);
	},
	activity : function(tid) {
		SMS.closepage();
		setTimeout(function(){
			SMS.ajax('discuz.php?mod=get&&activityid='+tid,'.currentbody #viewactivity_'+tid,'replace');
			SMS.deleteitem('discuz.php?mod=view&tid='+tid);
		},300);
	},
	activityapply : function(applyid,tid,ac) {
		if(ac=='delete'){
			$('.currentbody #applylist_'+applyid).remove();
		}else if(ac=='apply'){
			$('.currentbody #verified_'+applyid).text('已通过');
			$('.currentbody #apply_'+applyid).remove();
		}else{
			$('.currentbody #verified_'+applyid).text('需完善');
			$('.currentbody #replenish_'+applyid).remove();
		}
		SMS.deleteitem('discuz.php?mod=view&tid='+tid);
	},
	makepicker : function(opid,v1,v2,v3) {
		var v1=v1||0,v2=v2||0,v3=v3||0;
		$.ajax({
			type: 'GET',
			url: 'discuz.php?mod=get&opid='+opid,
			dataType: 'text',
			success: function(s) {
				var res = JSON.parse(s)		
				var pickervar=eval(res.picker);
        var indexvar=objtoarray(res.index);
				weui.picker(
					pickervar,  
					{
						defaultValue: [v1,v2,v3],
						onChange: function (result) {console.log(indexvar);},
						onConfirm: function (result) {
							if(result[2]){
								var resid=result[2];
								var text=indexvar[result[0]]+','+indexvar[result[1]]+','+indexvar[result[2]];
							}else if(result[1]){
								var resid=result[1];
								var text=indexvar[result[0]]+','+indexvar[result[1]];
							}else{
								var resid=result[0];
								var text=indexvar[resid];
							}
							$('.currentbody #option_'+opid).val(resid);
						  $('.currentbody #picker_'+opid).text(text);		
						} 
				});
			},
			error: function(data) {
				return false;
			}
		});
	},
};