//转发侃球
var reTalk = function(tid,uid){
	$.ajax({url:"/index.php?c=main&a=reTalk",type:"POST",dataType:"html",data:"tid="+tid+"&uid="+uid,
		success:function(v) {
			if(v=="0"){
				alert("服务器忙，请稍等~！");
			}else if(v=="-1"){
				ajaxLoginFloat();
			}else if(v=="-2"){
				alert("不能转发自己的侃球哦~！");
			}else if(v=="-3"){
				alert("您已经转发过这条侃球了~！");
			}else{
				if($("#init_rt_num_"+tid).html() == ""){
					$("#init_rt_num_"+tid).html("(1)");
				}else{
					addNum("rt_num_"+tid);
				}
				getAllRt(tid);
			}
		}
	});
};
//取转发列表
var getAllRt = function(tid){
	$.ajax({url:"/index.php?c=user&a=ajaxRt",type:"POST",dataType:"html",data:"tid="+tid,
		success:function(v) {
			if(v=="0"){
				alert("服务器忙，请稍等~！");
			}else if(v=="-1"){
				ajaxLoginFloat();
			}else{
				//样式切换
				$("#rt_num_"+tid).removeClass();
				$("#rt_num_"+tid).addClass("num_sel");
				$("#reply_num_"+tid).removeClass();
				$("#reply_num_"+tid).addClass("num");
				//内容切换
				$("#reply_list_"+tid).html("");
				$("#rt_list_"+tid).html(v);
			}
		}
	});
};
//取某条侃球的最新几条评论
var getComment = function(tid,fid,str){
	$.ajax({url:"/index.php?c=main&a=getComment",type:"POST",dataType:"html",data:"tid="+tid+"&fid="+fid,
		success:function(v) {
			if(v=="0"){
				alert("服务器忙，请稍等~！");
			}else if(v=="-1"){
				ajaxLoginFloat();
			}else{
				//样式切换
				$("#rt_num_"+tid).removeClass();
				$("#rt_num_"+tid).addClass("num");
				$("#reply_num_"+tid).removeClass();
				$("#reply_num_"+tid).addClass("num_sel");
				//内容切换
				$("#rt_list_"+tid).html("");
				$("#reply_list_"+tid).html(v);
				if(str == "word"){
					$("#reply_area_"+tid).show();
					$("#reply_input_"+tid).hide();
					$("#reply_con_"+tid).focus();
				}
			}
		}
	});
};
//单条侃球页取所有评论
var getAllComment = function(tid,fid,str,n){
	$.ajax({url:"/index.php?c=user&a=ajaxComment",type:"POST",dataType:"html",data:"tid="+tid+"&fid="+fid+"&n="+n,
		success:function(v) {
			if(v=="0"){
				alert("服务器忙，请稍等~！");
			}else if(v=="-1"){
				ajaxLoginFloat();
			}else{
				//样式切换
				$("#rt_num_"+tid).removeClass();
				$("#rt_num_"+tid).addClass("num");
				$("#reply_num_"+tid).removeClass();
				$("#reply_num_"+tid).addClass("num_sel");
				//内容切换
				$("#rt_list_"+tid).html("");
				$("#reply_list_"+tid).html(v);
				if(str == "num"){
					$("#reply_area").hide();
					$("#reply_input").show();
				}else{
					$("#reply_con").focus();
				}
			}
		}
	});
};
//显示更多评论
var showMoreComment = function(tid,num){
	$(".s_comment_"+tid).show();
	$("#more_comment_"+tid).hide();
	if(num>13)	$("#all_comment_"+tid).show();
};
//显示回复文本框
var showReplyArea = function(tid){
	$("#reply_area_"+tid).show();
	$("#reply_input_"+tid).hide();
	$("#reply_con_"+tid).focus();
};
//点回复某人
var preReply = function(tid,fid,cid,name){
	$("#reply_tid").val(tid);
	$("#reply_fid").val(fid);
	$("#reply_cid").val(cid);
	$("#reply_area_"+tid).show();
	$("#reply_input_"+tid).hide();
	$("#reply_con_"+tid).focus();
	$("#reply_con_"+tid).val("回复"+name+":"+$("#reply_con_"+tid).val());
	$("#comm_btn_"+tid).html("回复");
};
//回复提交
var doReply = function(tid,fid){
	$.ajax({url:"/index.php?c=main&a=ajaxIsLogin",type:"POST",dataType:"html",
		success:function(v) {
			if(v=="0"){
				alert("服务器忙，请稍等~！");
			}else if(v=="-1"){
				ajaxLoginFloat();
			}else{
				if(!validReply(tid,$("#reply_cid").val())) return false;
				$("#reply_tid").val(tid);
				$("#reply_fid").val(fid);
				$("#reply_content").val($("#reply_con_"+tid).val());
				preSubTip("kq_sub_div");
				document.reply_form.submit();
			}
		}
	});
};
//回复验证
var validReply = function(tid,cid){
	var str = cid == 0 ? "评论" : "回复";
	var v = $.trim($("#reply_con_"+tid).val());
	if(v==""){		
		showError("reply_info_"+tid,str+"的文字不能为空哦~！");
		return false;
	}else if(v.length > 160){
		showError("reply_info_"+tid,str+"的文字不能超过160个字~！");
		return false;
	}else{
		showOk("reply_info_"+tid);
		return true;
	}
};
//回复提交返回
var replyListReturn = function(tid,fid,cid){
	$("#kq_sub_div").hide();
	(cid == "0") ? successTip("comment_success") : successTip("reply_success");
	if($("#init_reply_num_"+tid).html() == ""){
		$("#init_reply_num_"+tid).html("(<em class='num' id='reply_num_"+tid+"'>1</em>)");
	}else{
		addNum("reply_num_"+tid);
	}
	//init
	$("#reply_tid").val("");
	$("#reply_fid").val("");
	$("#reply_cid").val("");
	$("#reply_content").val("");
	$("#reply_con_"+tid).val("");
	getComment(tid,fid,'num');
};
//删除侃球
var delTalk = function(tid){
	if (confirm("确定删除该条侃球吗？")){
		$.ajax({url:"/index.php?c=main&a=delTalk",type:"POST",dataType:"html",data:"tid="+tid,
			success:function(v) {
				if(v=="0"){
					alert("服务器忙，请稍等~！");
				}else if(v=="-1"){
					ajaxLoginFloat();
				}else if(v=="-2"){
					alert("不能删除其它人的侃球哦~！");
				}else{
					$("#talk_"+tid).fadeOut(500);
					successTip("del_success");
				}
			}
		});
	}
};
//删除评论
var delComment = function(tid,fid,cid){
	$.ajax({url:"/index.php?c=main&a=delComment",type:"POST",dataType:"html",data:"tid="+tid+"&fid="+fid+"&cid="+cid,
		success:function(v) {
			if(v=="0"){
				alert("服务器忙，请稍等~！");
			}else if(v=="-1"){
				ajaxLoginFloat();
			}else if(v=="-2"){
				alert("不能删除其它人的评论哦~！");
			}else{
				cutNum("reply_num_"+tid);
				$("#comment_"+cid).fadeOut(500);
				//getComment(tid,fid,'num');
				successTip("del_success");
			}
		}
	});
};
var doGood = function(tid,uid){
	$.ajax({url:"/index.php?c=main&a=doGood",type:"POST",dataType:"html",data:"tid="+tid+"&uid="+uid,
		success:function(v) {
			if(v=="0"){
				alert("服务器忙，请稍等~！");
			}else if(v=="-1"){
				ajaxLoginFloat();
			}else if(v=="-2"){
				alert("您已经赞过此条信息罗~！");
			}else{
				addNum("good_num_"+tid);
				$("#good_word_"+tid).html("已赞");
			}
		}
	});
};
var doFav = function(tid,uid){
	$.ajax({url:"/index.php?c=main&a=doFav",type:"POST",dataType:"html",data:"tid="+tid+"&uid="+uid,
		success:function(v) {
			if(v=="0"){
				alert("服务器忙，请稍等~！");
			}else if(v=="-1"){
				ajaxLoginFloat();
			}else if(v=="-2"){
				alert("您已经收藏过此条信息罗~！");
			}else{
				addNum("fav_num_"+tid);
				$("#fav_word_"+tid).html("已收藏");
			}
		}
	});
};
var addFollow = function(fid,from){
	$.ajax({url:"/index.php?c=user&a=addFollow",type:"POST",dataType:"html",data:"fid="+fid,
		success:function(v) {
			if(v=="0"){
				alert("服务器忙，请稍等~！");
			}else if(v=="-1"){
				ajaxLoginFloat();
			}else if(v=="-2"){
				alert("不能关注自己啊~！");
			}else if(v=="-3"){
				alert("您已经关注过该用户了~！");
			}else if(v=="-4"){
				alert("喔唷，转眼您的关注已经超过1000人，达到了普通用户关注上限啦，\n快升级成为VIP用户，获得更多关注权限！");
			}else if(v=="-5"){
				alert("天那！VIP用户关注2000人上限已经被您攻破，\n为了您消除信息噪音、舒适侃球，建议减少关注。");
			}else{
				if(from=="main"){
					addNum("follower_num");
					$("#main_follow_"+fid).html('<font class="s_b s_b_6_v">已关注</font>');
				}else if(from == "fans_me"){
					$("#fans_me_"+fid).html('<font class="s_b s_b_3_v">相互关注</font>');
				}else if(from=="fans_other" || from=="follow_other"){
					$("#"+from+"_"+fid).html('<font class="s_b s_b_3_v">已关注</font>');
				}else{//access,follower,bestlove,recommend
					$("#"+from+"_follow_"+fid).html('<font class="fans_added">已关注</font>');
				}
			}
		}
	});
};
var cutNum = function(id){
	var t_num = parseInt($("#"+id).text());
	$("#"+id).text(t_num-1);
};
var addNum = function(id){
	var t_num = parseInt($("#"+id).text());
	$("#"+id).text(t_num+1);
};
var showError = function(id,msg){
	$("#"+id).removeClass();
	$("#"+id).addClass("input_bad");
	//$("#"+id).css("display","block");
	$("#"+id).html(msg);
};
var showOk = function(id){
	$("#"+id).removeClass();
	$("#"+id).html("");
	$("#"+id).addClass("input_good");
};
//复制文本框内容
var copyToClipboard = function(txt) {   
	if(window.clipboardData) {   
		window.clipboardData.clearData();   
		window.clipboardData.setData("Text", txt); 
		alert("邀请地址已被复制到粘贴板,敬请广泛传播！") ; 
		$('#share_button').val('分享');
		$('#share_con').hide('600');
	} else if (window.netscape) {   
		try {   
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");   
		} catch (e) {   
			alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将 'signed.applets.codebase_principal_support'设置为'true'");   
		};   
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);   
		if (!clip)   
			return;   
		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);   
		if (!trans)   
			return;   
		trans.addDataFlavor('text/unicode');   
		var str = new Object();   
		var len = new Object();   
		var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);   
		var copytext = txt;   
		str.data = copytext;   
		trans.setTransferData("text/unicode",str,copytext.length*2);   
		var clipid = Components.interfaces.nsIClipboard;   
		if (!clip)   
			return false;   
		clip.setData(trans,null,clipid.kGlobalClipboard);   
		alert("邀请地址已被复制到粘贴板,敬请广泛传播！") ; 
		$('#share_button').val('分享');
		$('#share_con').hide('600');
	 }  else {
		alert("被浏览器拒绝！劳烦您手动复制！") ;
	}
};
var videoPubOpen = function(id){
	$("#video_small_"+id).hide(200);
	$("#video_big_"+id).show();
};
var videoPubClose = function(id){
	$("#video_small_"+id).show();
	$("#video_big_"+id).hide(200);
};
var imgPubOpen = function (id){
	smoothAchor(document.getElementById("img_a_"+id));
	$("#img_small_"+id).hide();
	$("#img_big_"+id).show(300);
};
var imgPubClose = function(id){
	smoothAchor(document.getElementById("img_a_"+id));
	$("#img_small_"+id).show();
	$("#img_big_"+id).hide(300);
};
var smoothAchor = function (obj) {
	if (location.pathname.replace(/^\//, '') == obj.pathname.replace(/^\//, '') && location.hostname == obj.hostname) {
		var $target = $(obj.hash);
		$target = $target.length && $target || $('[name=' + obj.hash.slice(1) + ']');
		if ($target.length) {
			var targetOffset = $target.offset().top;
			$('html,body').animate({scrollTop: targetOffset},400);
        }
	}
};
var getTeamViewHtml = function(){
	$.ajax({url:"/index.php",dataType:"html",data:"c=ajax&a=teamSel&f=view",
		error:function(){alert("对不起,服务器暂时忙!");},
		success:function(v){
			$("#sel_team_view").html(v);
		}
	});
};
var getPlayerViewHtml = function(){
	$.ajax({url:"/index.php",dataType:"html",data:"c=ajax&a=playerSel&f=view",
		error:function(){alert("对不起,服务器暂时忙!");},
		success:function(v){
			$("#sel_player_view").html(v);
		}
	});
};
var getPostAll = function(u,pid){
	$.ajax({url:"/index.php",dataType:"html",data:"c=main&a=getPostAll&u="+u+"&pid="+pid,
		error:function(){alert("对不起,服务器暂时忙!");},
		success:function(v){
			$("#post_show").html(v);
		}
	});
};
var getCookie = function(cookie_name){
    var allcookies = document.cookie;
    var cookie_pos = allcookies.indexOf(cookie_name);
    if(cookie_pos != -1){
        cookie_pos += cookie_name.length + 1;
        var cookie_end = allcookies.indexOf(";", cookie_pos);
        if(cookie_end == -1){
            cookie_end = allcookies.length;
        }
        var value = unescape(allcookies.substring(cookie_pos, cookie_end));
    }
    return value;
};
//提示信息
var noticePopIn = function(id){
	$('#'+id).delay(400).slideDown('400');
};
var noticePopOut = function(id){
	$('#'+id).slideUp('400');
};
var delRemind = function(type){
	$.post("/ajax_delRemind.html",{t:type});
};
var preSubTip = function(id){
	topV = $(window).height()/2 + $('#'+id).height()/2 + 50;
	$('#'+id).css('margin-top',-topV).fadeIn(300);
};
var successTip = function(id){
	topV = $(window).height()/2 + $('#'+id).height()/2 + 50;
	$('#'+id).css('margin-top',-topV).fadeIn(300).delay(800).fadeOut(300);
};
var getNewMsgHtml = function(tid){
	$.ajax({url:"/index.php",dataType:"html",data:"c=msg&a=create&tid="+tid,
		error:function(){alert("对不起,服务器暂时忙!");},
		success:function(v){
			if(v == "-1"){
				ajaxLoginFloat();;
			}else{
				$("#new_msg_div").html(v);
			}
		}
	});
};
var getUnreadMsgHtml = function(type){
	var urlData = type=='' ? "/index.php?c=msg&a=ajaxUnread" : "/index.php?c=Ajax&a=ajaxUnread";
	$.ajax({url:urlData,type:"POST",dataType:"html",
		error:function(){alert("对不起,服务器暂时忙!");},
		success:function(v){
			if(v == "-1"){
				ajaxLoginFloat();;
			}else{
				$("#unread_msg_div").html(v);
			}
		}
	});
};
var getUnreadMsgNum = function(){
	$.ajax({url:"/index.php?c=msg&a=ajaxUnreadNum",type:"POST",dataType:"html",global:false,
		error:function(){},
		success:function(v){
			v = parseInt(v);
			if(v>0){
				$("#unread_msg_span").show();
				$("#unread_msg_num").html(v);
			}else{
				$("#unread_msg_span").hide();
			}
		}
	});
};
window.setInterval("getUnreadMsgNum()",120000);

var getUserDetailHtml = function(fid){
	$.ajax({url:"/index.php?c=user&a=ajaxDetail",type:"POST",dataType:"html",data:"d="+fid,
		error:function(){alert("对不起,服务器暂时忙!");},
		success:function(v){
			$("#user_detail_div").html(v);
		}
	});
};
var showUserDetail = function(obj,fid){
	var p = obj.position();
	var l = Math.floor(p.left) - 50;
	var t = Math.floor(p.top) - 50;
	$("#user_detail_div").css({"left":l,"top":t,"display":"block"});
	getUserDetailHtml(fid);
};
$(function(){
	$('a#inviteButton').click(function(){
		var copyLink = $('#inviteText').val();
		copyToClipboard(copyLink);
	});
	//菜单下拉
	$("#button_setting").click(function(){
		$("#list_setting").slideDown();
	});
	$('a#button_view,ul#list_view').hover(function () 
		{$('#list_view').show();
		},function(){$('#list_view').hide();
	});
	$('a#button_view_1,ul#list_view_1').hover(function ()
		{$('#list_view_1').show();
		},function(){$('#list_view_1').hide();
	});
	$("#team_view").click(function(){
		showDialog("sel_team_view");
		$("#sel_team_div").empty();
		getTeamViewHtml();
		return false;
	});
	$("#palyer_view").click(function(){
		showDialog("sel_player_view");
		$("#sel_player_div").empty();
		getPlayerViewHtml();
		return false;
	});
	$("#main").click(function(){
		$("#list_setting").hide();
		$("#unread_msg_div").hide();
	});
	//用户详情
	$("#user_detail_div").hover(
		function(){},
		function(){
			$(this).html("");
		}
	);
	// SNS左菜单展开、收缩
	var tools = $("#tools > li > ol");
	$("#tools > li:last > a").toggle(
		function(){
			tools.slideDown(300);
			$(this).html('<span class="icons icon_80"></span>更少'); 
		},function(){
			tools.slideUp(300); 
			$(this).html('<span class="icons icon_06"></span>更多'); 
		}
	);
});
// 直播动作播放
var liveActionShowTimeID = '';
var liveActionShow = function(swfName){
	var swfData = '<embed src="http://van.qiumi.cc/images/flash/live/'+swfName+'.swf" quality="high" width="100%" height="100%" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" wmode="transparent"></embed>';
	$("#live_action_box").html(swfData).show();
	// 表情关闭
	var actionTime = actionTimeByName(swfName);
	liveActionShowTimeID = window.setInterval("closeFace()", actionTime);
}
// 关闭直播动作
var closeFace = function(){
	$("#live_action_box").hide();
	window.clearInterval(liveActionShowTimeID);
}
// 动作时间
var actionTimeByName = function(name){
	var time = '';
	switch(name){
		case 'dacaipang':
			time = 6250;
			break;
		case 'jieshoubizui':
			time = 6670;
			break;
		case 'jiaolianxiake':
			time = 5400;
			break;
	}
	return time;
}

// 顶赛程中的球队(弹出的赛程直播tip)
var matchDigg = function(mid, homeaway){
	$.ajax({url:"/index.php?c=Ajax&a=matchDigg",type:"GET",dataType:"html",data:"mid="+mid+"&homeaway="+homeaway,
		error:function(){alert("对不起,服务器暂时忙!");},
		success:function(v){
			if(v == -1){
				alert("对不起,服务器暂时忙!");
			}else{
				addNum(homeaway+"_digg");
				$("#"+homeaway+"_digg_text").html("已顶");
			}
		}
	});
}
function ShowCountDown(spanId,yy,mm,dd,hh,ii) {
	var now = new Date();
	var endDate = new Date(yy,mm,dd,hh,ii,"00"); // 对应的年月日时分秒，注意，月份是从0开始，06表示7月
   
	var leftTime = endDate.getTime() - now.getTime();
	var leftsecond = parseInt(leftTime / 1000);
	var day0 = parseInt(leftsecond / (24 * 60 * 60));
	var day1 = Math.floor(leftsecond / (60 * 60 * 24));
	var hour = Math.floor((leftsecond - day1 * 24 * 60 * 60) / 3600);
	var minute = Math.floor((leftsecond - day1 * 24 * 60 * 60 - hour * 3600) / 60);
	var second = Math.floor(leftsecond - day1 * 24 * 60 * 60 - hour * 3600 - minute * 60);
	var day = day0==0 ? '' : day0 + "天";
	document.getElementById(spanId).innerHTML = day + hour + "时" + minute + "分" + second+"秒";
}

