$("ducument").ready(function(){
	var ded=[];
	$('.ding').each(function(i){
		$(this).click(function(){
			var obj=$(this);
			var id=obj.attr('id');
			if(!ded[id]){
				$.get('/ajax/ding.php',{id:id},function(data){
					obj.text('顶'+data);
					ded[id]=data;
				});
			}else
			$.weeboxs.open("感谢您的支持，一个用户只能添加一个有效点击！", {
			   title: '星碟抢号系统',
			   type:'alert'
			});
		});
	});
	var de1=1;
	$('#ding').click(function(){
		if(de1){
			$.get('/ajax/ding.php',{id:cid},function(data){
				$('#ding').text('顶'+data);
				de1=0;
			});
		}else
		$.weeboxs.open("感谢您的支持，一个用户只能添加一个有效点击！", {
		   title: '星碟抢号系统',
		   type:'alert'
		});
	});
	
	$('#bonus').click(function(){
		var ref  = top.location.href;
		if(uid>0){
		var url  = "/ajax/bonus.php?id="+cid+"&ref="+ref;
		$.weeboxs.open(url, {title:'领号' , type:'msg',contentType:'ajax',width:550});
		}else{
		$.weeboxs.open('<div style="width:247px; background-color:#f9f9f9; border:2px solid #cfcfcf; padding:10px;"><iframe frameborder="0" width="247px" height="110px" allowtransparency="true" src="http://u.ufojoy.com/passport/smart_login.php?u=http://ka.ufojoy.com/pages/success.html" scrolling="no"></iframe></div>', {
						   title: '投票领号（快速登录入口）',
						   width:295,
						   height:135,
						   okBtnName:'关闭',
						   type:'alert'
						});
		$('.weedialog .dialog-ok').hide();
		}
	});
	
	var tabs=$('.tabs>a');
	tabs.click(function(){
		var j=tabs.index($(this)[0]);
		tabs.eq(j).addClass('current').siblings().removeClass('current');
		$('#testable table').eq(j).show().siblings().hide();
	})
	if($('#pllist').val()!=1)
	showcomment();

});
var _title=document.title;
var _url=document.location.href;

function tosns(i){
	var sns='';
	switch (i){
		case 1://kaixin
			sns="http://www.kaixin001.com/repaste/share.php?rurl="+_url+"&amp;rcontent="+_url+"&amp;rtitle="+_title;
			break;
		case 2://qq
			sns="http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url="+_url;
			break;
		case 3://sina
			sns="http://v.t.sina.com.cn/widget/login.php?url="+_url;
			break;
		case 4://renren
			sns="http://share.renren.com/share/buttonshare.do?link="+_url+"&amp;title="+_title;
			break;
		case 5://tencent blog
			sns="http://v.t.qq.com/share/share.php?site=www.ufojoy.com&url"+_url+"&amp;title="+_title;
			break;
		case 6://douban
			sns="http://www.douban.com/recommend/?url="+_url+"&amp;title="+_title+"&amp;sel="+_url+"&amp;v=1";
			break;
		case 7://baidu http://apps.hi.baidu.com/share?url="+location.href+"&rcontent="+location.href+"&rtitle="+document.title
			sns="http://apps.hi.baidu.com/share?url="+_url+"&amp;title="+_title+"&amp;rcontent="+_url;
			break;
		default:
		return flase;
	}
	window.open(sns);
	
}

//comments
var pl_temp='<div class="name_nub bb_[no]" id="plid_[plid]"><div class="b_c_top"><em>[time]</em><span id=user_[plid]>[user]</span></div><div class="b_c_box">[quote]</div><p class="pl_c" id="pl_c_[plid]">[content]</p><div><a href="javascript:;" onclick="zccomment($(this),[zcnum],[plid])">支持[[zcnum]]</a><a href="javascript:;" onclick="replyid([plid])">回复</a></div></div>';
var rp_temp='<div class="huifu_con"><a href="javascript:creplyid();" class="d_w">回复</a><div class="discuss"><p class="close" onclick="creplyid()"></p><textarea id="ccomment1" class="text_con"></textarea><div class="submitted"><a href="javascript:checkcomment(1)" class="j_y">提交评论</a><a href="">登录</a><i>|</i><a href="">注册</a></div>	</div></div>';
var pl_replyid=0;
var ucomment=0;

function replyid(plid){
	$('.huifu_con').remove();
	pl_replyid=plid;
	$('#plid_'+plid).append(rp_temp);
}
function creplyid(){
	$('.huifu_con').remove();
	pl_replyid=0;
}
function zccomment(obj,zcnum,plid){
	obj.attr('onclick','');
	$.post('/ajax/pl_add.php?d='+new Date().getTime(),{id:$('#cid').val(),comment:'1',replyid:plid},function(data){
		if(data=='1'){
			zcnum++;
			obj.html('已支持['+zcnum+']');
			obj.addClass('gray');
		}
	});
}
function checkcomment(f){
	if(ucomment!=0)return;
	var commentid='ccomment';
	if(f==1){
		commentid='ccomment1';
	}else if(f==2){
		commentid='ccomment2';
	}else{
		pl_replyid=0;
	}
	var tt='';
	if($('#'+commentid).val().length<2){
		tt='评论不能为空！';
	}
	if(tt!='')
	$.weeboxs.open('<div align=center>'+tt+'</div>', {
	   title: '评论',
	   type:'alert'
	});
	else{
		ucomment=1;
		$.post('/ajax/pl_add.php?d='+new Date().getTime(),{id:$('#cid').val(),comment:$('#'+commentid).val(),replyid:pl_replyid},function(data){
			ucomment=0;
			if(data=='2'){
				$.weeboxs.open("请不要在30秒内连续发言", {
				   title: '评论',
				   type:'alert'
				});
				return;
			}
			data=eval('['+data+']');
			var cd=data[0];
			if(cd){
				
				$.weeboxs.open("您的评论已发布成功。", {
				   title: '评论',
				   type:'alert'
				});
				
				var html=pl_temp;
				cd['no']=1;
				html=relacetag(html,cd);
				html+=$('#pl').html();

				$('#pl').html(html);
				$('#'+commentid).val('');
				creplyid()
			}else{
				$.weeboxs.open("服务器忙，请稍后评论。", {
				   title: '评论',
				   type:'alert'
				});
			}
		});
	}
	
}
function showmorec(plid){
	$('#pl_c_'+plid).html(commentss[plid]);
}
function relacetag(html,cd){
	commentss[cd['plid']]=cd['saytext'];
	var dd=cd['saytext'];
	var mx=200;
	dd=dd.replace(/\&nbsp;/g,' ');
	dd=dd.replace(/\&lt;/g,'<');
	dd=dd.replace(/\&gt;/g,'>');
	dd=dd.replace(/\&quot;/g,'"');
	dd=dd.replace(/\&amp;/g,'&');
	if(dd.length>mx){
		var cc=dd.substr(0,mx);
		cc=cc.replace(/ /g,'&nbsp;');
		cc=cc.replace(/</g,'&lt;');
		cc=cc.replace(/>/g,'&gt;');
		cc=cc.replace(/"/g,'&quot;');

		cd['saytext']=cc+'...<a href="javascript:showmorec('+cd['plid']+')">[详细]</a>';
	}
	html=html.replace(/\[plid\]/g,cd['plid']);
	html=html.replace('[no]',cd['no']);
	html=html.replace('[time]',cd['saytime']);
	html=html.replace('[user]',cd['sayip']);
	html=html.replace('[content]',cd['saytext']);
	html=html.replace('[quote]',cd['quote']);
	html=html.replace(/\[zcnum\]/g,cd['zcnum']);
	return html;
}
function showcomment(){
	$.post('/ajax/pl_list.php',{id:$('#cid').val()},function(data){
		data=eval(data);
		var htm='';
		$('#plnum').text(data[1]);
		data=data[0];
		var htm='';
		for(var i=0;i<data.length;i++){
			var html=pl_temp;
			data[i]['no']=i+1;
			html=relacetag(html,data[i]);
			htm+=html;

			//htm+='<dt><span class="t_g_right">'+data[i]['saytime']+'</span><span>'+data[i]['sayip']+'</span></dt><dd><p>'+data[i]['saytext']+'</p></dd>';
		}
		$('#pl').html(htm);
	});
}
function toptocomment(){
	$('#huifu_cc').show();
}
function topcomment(){
	pl_replyid=0;
	checkcomment(2);
}
function ctopcomment(){
	$('#huifu_cc').hide();
	$('#ccomment2').val('');
}
