var cur_vid = 0;
var global_vodurl = "";
var global_vodname = "";
var global_from = "";
//评论分页
function show_page(reccount,page,pagesize){
        var pagestr = "";
        //位移
        offset = 3;
        //显示页数长度
        page_length = 5;
        prev_page = page-1;
        next_page = page+1;

        pages = Math.ceil(reccount / pagesize);
        if (page > 1) pagestr += "<span><a href ='javascript:getcomment("+prev_page+")'><</a></span> ";
        if (pages > offset){
                start = page - offset > 2 ? page - offset : 1;
                if (page > offset+2)
                        pagestr += "<span><a href='javascript:getcomment(1)'>1</a></span><span><a href='javascript:getcomment(2)'>2</a></span>...";
                for (i = start; i < (pages > start+page_length ? start+page_length : pages)+1; i++){
                        if (page == i)
                                pagestr += "<span id=curr>"+i+"</span>";
                        else
                                pagestr += "<span><a href='javascript:getcomment("+i+")'>"+i+"</a></span>";
                }
                if (page < pages - page_length) pagestr += "...<span><a href='javascript:getcomment("+pages+")'>"+pages+"</a></span>";
        }else if(pages > 1){
                for (i = 1; i <= pages; i++){
                        if (page == i)
                                pagestr += "<span id=curr>"+i+"</span>";
                        else
                                pagestr += "<span><a href='javascript:getcomment("+i+")'>"+i+"</a></span>";
                }
        }
        if (page < pages) pagestr += "<span><a href ='javascript:getcomment("+next_page+")'>></a></span>";
        pagestr = "<div class=page1>"+pagestr+"</div>";
        return pagestr;
}

//发表评论
function doComment(){
	var comment = $.trim($("#txt_comment").val());
	var nickname = $.trim($("#usernick").val());
	if (nickname.length <= 0){
		showMsgBox("<p>请输入昵称</p>");
		setTimeout("close_box()",3000);
		return;
	}
	if(nickname.length > 80){
		nickname = nickname.substr(0,80);
	}
	if (comment.length <= 0){
		showMsgBox("<p>请输入评论</p>");
		setTimeout("close_box()",3000);
		return;
	}
	if(comment.length > 480){
		comment = comment.substr(0,480);
	}
	$("#txt_comment").val("");
	$.getJSON(SW_URL_VOD+"handle/handlecomment.php?jsoncallback=?",{
		nickname: nickname,
		comment: encodeURI(comment),
		video_id: video_id,
		isguest: isguest,
		page: 1,
		useridurl: useridurl,
		email: email,
		ts: new Date().getTime()
	},function(d){
		switch (d){
			case "-1":
			var text = "<p>请输入昵称！</p>";	
			break;
			case "-2":
			var text = "<p>请输入评论！</p>";	
			break;
			case "-3":
			var text = "<p>评论提交失败，请重试！</p>";	
			break;
			default :
			c_total = d;
			if(site_id==2){
			var text = '<div class="msg1_content01"><h1 style="border-bottom:1px dashed #304b53; padding-bottom:10px;">感谢您的参与，评论已发表成功 </h1><h2 style="color:#33bef3; text-aline">与好友一起分享这个视频吧，留下联系方式，可以获得NBA中国赛门票哦~</h2><h2>姓名：<input name="input" id="idTrueName" type="text" size="12"  style="background:#314d55; border:none;color:#fff;"/>电话：<input name="input2" type="text"  id="idPhone" size="12"  style="background:#314d55; border:none;color:#fff; "/></h2><input name="Email" id="idEmail" type="hidden" value="'+email+'"/><h2  style="color:#969696; font-weight:lighter;">*请填写真实姓名与可靠联系方式，以便保证可以及时通知到获奖信息 鲨威承诺不将您的重要信息透露他人</h2></div><div class="msg1_content02" style="margin-left:140px;"><a style="cursor:pointer; text-decoration:none;" onclick="doSubmit()"><h3>确 定</h3></a></div>';	
			}else{
				var text = "<p>评论提交成功，感谢您的参与！</p>";	
			}
			break;
		}
		showMsgBox(text);
		getcomment(1);
	});
}

function close_box(){
	$("#msg_box").fadeOut(200);
	$("#msg_box").remove();
	$("#popup_overlay").remove();
}

function vote(vid){
	$.get(SW_URL_VOD+"vote.php",{vid:vid,authcode:$("#input_code").val()},function (d){
		switch (d){
			case "-1":
			var text = '<p>验证码错误，请重新输入！</p>';
			showMsgBox(text);
			break;
			case "-2":
			var text = '<p>参数错误！</p>';
			showMsgBox(text);
			break;
			case "1":
			var text = '</p>动作太快，请稍后重试！</p>';
			showMsgBox(text);
			break;
			case "2":
			var text = '<div class="mes_box2"><h1>知道你对这个视频喜爱有加， <br/>不过一天一个视频只能投10票哦</h1></div><div class="mes_box1"><a  onclick="close_box()" style="cursor:pointer;"> <h1 >确 定</h1></a></div>';
			showMsgBox(text);
			break;
			case "3":
			$.getJSON("http://id.shawei.tom.com/info.do?jsoncallback=?",{			//判断是否填写过真实信息
				m: "getUserForSW",
				email: email
			},function(d){
				if((d.realName!=undefined)&&(d.mobile!=undefined)){
					var text = '<div class="mes_box3"><h1 >投票成功，<span><a href="#">多多分享</a></span>，就有可能获得NBA门票</h1></div><div class="mes_box1"><a style="cursor:pointer;" onclick="close_box()"><h1 >确 定</h1></a></div>';
					showMsgBox(text);
				}else{
					showMsgBox('<div class="mes_box3"><h1>投票成功，留下联系方式，就有可能获得NBA门票 </h1><h2 class="white">姓名：<input name="" type="text" id="idTrueName" size="12" class="input"/>电话：<input name="" type="text" size="12"  id="idPhone" class="input"/></h2><input name="Email" id="idEmail" type="hidden" value="'+email+'"/><h3>*请填写真实姓名与可靠联系方式，以便保证可以及时通知到获奖信息鲨威承诺不将您的重要信息透露他人</h3></div><div class="mes_box1"><a style="cursor:pointer;" onclick="doSubmit()"> <h1 >确 定</h1></a></div>');
				}
			});
			break;
		}
			
	});
	$.get(SW_URL_VOD+"getvote.php",{vid:vid},function (d){
		obj("showvod").innerHTML = d+"票";
	});
	
}

//分享到SWID
function copyswid(vid){
	$.getJSON('http://id.shawei.tom.com/swbookmark?format=json&jsonpCallback=?',{
				'type': 4,      
   				'title': t,
   				'url': cur_url
			}, function(json){
					if (json.status_code == 'success'){
						//sw_public_track(email,102,2,video_id,swid_detail);
						
						//判断是否填写过真实信息
						$.getJSON("http://id.shawei.tom.com/info.do?jsoncallback=?",{			
							m: "getUserForSW",
							email: email
						},function(d){
							if((d.realName!=undefined)&&(d.mobile!=undefined)){
								var text = '<div class="mes_box2"><h1>已成功分享到鲨威ID</h1></div><div class="mes_box1"><a style="cursor:pointer;" onclick="close_box()"><h1 >确 定</h1></a></div>';
								showMsgBox(text);
							}else{
								showMsgBox('<div class="mes_box3"><h1>已成功分享到鲨威ID </h1><h2>多多分享，可以获得NBA中国赛门票哦~</h2><h4 class="bolder">姓名：<input name="" type="text" id="idTrueName" size="12" class="input"/>电话：<input name="" type="text" size="12"  id="idPhone" class="input"/></h2><input name="Email" id="idEmail" type="hidden" value="'+email+'"/><h3>*请填写真实姓名与可靠联系方式，以便保证可以及时通知到获奖信息鲨威承诺不将您的重要信息透露他人</h3></div><div class="mes_box1"><a style="cursor:pointer;" onclick="doSubmit()"> <h1 >确 定</h1></a></div>');
							}
						});
						
					}else{
						showMsg("分享到鲨威ID失败！");
					}
        		}
    );	
}

function doSubmit(){
		var sTrueName = $("#idTrueName").val();
		var sPhone    = $("#idPhone").val();
		var sEmail    = $("#idEmail").val();
		if(sTrueName==""){
			$("#idTrueName").focus();
			return false;
		}
		if(sPhone==""){
			$("#idPhone").focus();
			return false;
		}
		$.getJSON("http://id.shawei.tom.com/info.do?jsoncallback=?",{
                m: "updateUserForSW",
                email: sEmail,
                mobile:sPhone,
				realName:sTrueName
        },function(d){
			if(d.result=="success"){
				close_box();
			}
        });
		
	}


function refresh_code(){
	var r = Math.round(Math.random()*10000);
	$('#imgcode').attr('src','');
	$('#imgcode').attr('src',"auth_code.php?"+r);
}

function auth_code(vid){
	if (islogin()){
		var text = '<div class="mes_box3"><h5>验证码: <input id="input_code" type="text" name="authcode" class="input" size="16" /><img id="imgcode"  src="auth_code.php" title="点击刷新" style="cursor:pointer; margin-left:2px;" onclick="refresh_code()" width="87" height="19" /></h5><h4 class="juli"><!--<span class="green">验证码错误！</span>--><span class="hui02" onclick="refresh_code()">看不清，换一张</span></h4></div><div class="mes_box1"><a href="javascript:vote('+vid+')"><h1 >确 定</h1></a></div>';
		
		showMsgBox(text);
		refresh_code();
	}else{
		cur_vid = vid;
		sw_login("auth_code");
	}
}

function auth_copyswid(vid){
	if (islogin()){
		var text = '<div class="mes_box3"><h5>验证码: <input id="input_code" type="text" name="authcode" class="input" size="16" /><img id="imgcode"  src="auth_code.php" title="点击刷新" style="cursor:pointer; margin-left:2px;" onclick="refresh_code()" width="87" height="19" /></h5><h4 class="juli"><!--<span class="green">验证码错误！</span>--><span class="hui02" onclick="refresh_code()">看不清，换一张</span></h4></div><div class="mes_box1"><a href="javascript:copyswid('+vid+')"><h1 >确 定</h1></a></div>';
		
		showMsgBox(text);
		refresh_code();
	}else{
		cur_vid = vid;
		sw_login("auth_copyswid");
	}
}

//添加用户未登陆情况下，点击复制地址按钮弹出登陆框
function auth_copylink(txt,name){
	if (islogin()){
		copyToClipboard(txt,name);
	}else{
		sw_login("copyToClipboard");
		global_vodurl = txt;
		global_vodname = name;
		global_from = "copyurl";
		//copyToClipboard(txt,name);
	}
}

//添加用户未登陆情况下，点击文本区域
function auth_message(txt,name){
	if (islogin()){
		if(txt=="area"){
			return true;	
		}else{
			sendMessage();
		}
	}else{
		sw_login("sendMessage");
		global_vodurl = txt;
		global_vodname = name;
		global_from = "copyurl";
		//copyToClipboard(txt,name);
	}
}

//跟贴
function sendMessage(){
	var bbs_message = $.trim($("#bbs_message").val());
	var cid = $.trim($("#cid").val());
	var fid= $.trim($("#fid").val());
	
	if (bbs_message.length < 5){
		var text = '<div class="mes_box2"><h1>回复内容不可以少于10字节，多写点吧</h1></div><div class="mes_box1"><a  onclick="close_box()" style="cursor:pointer;"> <h1 >确 定</h1></a></div>';
		showMsgBox(text);
		return;
	}
	
	if (bbs_message.length > 10000){
		var text = '<div class="mes_box2"><h1>回复内容超过10000字节，请分多次发布</h1></div><div class="mes_box1"><a  onclick="close_box()" style="cursor:pointer;"> <h1 >确 定</h1></a></div>';
		showMsgBox(text);
		return;
	}
	
	$.get("testpost.php?jsoncallback=?",{
		fid:fid,
		tid:cid,
		message:bbs_message
	},function(d){	
		obj('viewArticle').innerHTML = d + obj('viewArticle').innerHTML;
		
	});
	document.getElementById("bbs_message").value="";
	
}


//登陆框
function sw_login(callback){
	 var text = '<div class="mes_box4"><h1><img src="'+SW_URL_IMG+'/event/courtside/logo1.jpg" /></h1><span>电子邮箱: <input type="text" id="sw_email" class="input001" /></span><span class="jl1">密码: <input type="password" class="input001" id="sw_password"  onkeydown="if(event.keyCode==13)swlogin(\''+callback+'\');" /></span></div><div class="mes_box11" style="width:220px"><div class="mes_box101"><a href="javascript:swlogin(\''+callback+'\')"><h1 >快速登陆</h1></a></div><span class="font002"><a href="http://pass.tom.com/safeRecover.php?action=no" target="_blank" onclick="close_box()">忘记密码</a></span><span class="green"><a href="javascript:register()">注册鲨威ID</a></span></div>';

	showMsgBox(text);
}


//登陆
function swlogin(callback) {

	var email = $("#sw_email").val();
	var password =$("#sw_password").val();
	if (!Utils.isEmail(email)) {
		var text = '<div class="mes_box2"><h1>Email错误!</h1></div><div class="mes_box1"><a  onclick="close_box()" style="cursor:pointer;"> <h1 >确 定</h1></a></div>';
		showMsgBox(text);
		return;	
	}else if (!Utils.isPassword(password)) {  
		var text = '<div class="mes_box2"><h1>密码错误!</h1></div><div class="mes_box1"><a  onclick="close_box()" style="cursor:pointer;"> <h1 >确 定</h1></a></div>';
		showMsgBox(text);
		return;
	} else {
        $.getJSON('http://id.shawei.tom.com/user.do?m=login&format=json&jsonpCallback=?', {
            'email' :email,
            'password' :password
        }, function(json) {
            if (json.result == 'success') {
            	showUserCard();
				close_box();
				if (callback != "undefined"){
					return true;
				}
            } else {
            	var text = '<div class="mes_box2"><h1>用户名或密码错误!</h1></div><div class="mes_box1"><a  onclick="close_box()" style="cursor:pointer;"> <h1 >确 定</h1></a></div>';
		showMsgBox(text);
		return;
            }
        });
    }
}



//显示定制消息框
function showMsgBox(text){
	close_box();
	var msg = '<div class="msg09" id="msg_box"><div class="msg1_top"><div class="msg1_top1" id=cls><img src="'+SW_URL_IMG+'/event/courtside/chinagame_r1_c2.png" style="cursor:pointer;" onclick="close_box()" /></div></div><div class="msg1_c">'+text+'</div><div class="msg1_bottom"></div></div>';
	$("body").append('<div id="popup_overlay"></div>');
	$("#popup_overlay").css({
		position: 'absolute',
		zIndex: 99998,
		top: '0px',
		left: '0px',
		width: '100%',
		height: $(document).height(),
		background: '#fff',
		opacity: '0'
	});
	$("body").append(msg);
	var h = document.body.scrollTop;
	if(h == 0){
		h = document.documentElement.scrollTop;	
	}
	$("#msg_box").css({
		position: 'absolute',
		zIndex: 99999,
		left:Math.floor(($(document).width() - $("#msg_box").width())/2)+'px',
		top: (document.documentElement.clientHeight/2+h-$("#msg_box").height()/2)+'px'
	}).fadeIn(200);
}

function format_dd(dateline){
	var s = Math.round((new Date().getTime()/1000)) - dateline;
	var str = "";
	if (s > 0){
		var dn = new Array("1","2","3","4","5","6","7","8","9","10","11","12");
		d = Math.floor(s/86400);
		if (d > 0)
				str = dn[d]+"天前";
		if (d > 6)
				str = "一周前";
		if (d > 30)
				str = "一个月前";
		if (d == 0){
			h = Math.floor(s/3600);
			m = Math.floor((s-h*3600)/60);
			if (h > 0){ 
				str = h+"小时"+m+"分钟前";
			}else{
				str = m+"分钟前";
			}
		}
	}
	return str;
}


function regSWID(){
	var idEmail = $("#idEmail").val();
	var idPassword = $("#idPassword").val();
	var idNickName = $("#idNickName").val();
	var pwdconfirm = $("#pwdconfirm").val();
	
	var code = $("#input_code").val();
	$.get('verify.php', {authcode:code}, function(d) {
		if (d == "1"){
			if (pwdconfirm == idPassword){
				if(idEmail!=""&&idPassword!=""&&idNickName!=""){
					regID(idEmail,idPassword,idNickName,function (result){
						if (result.result == 'success') {
							showUserCard();
							close_box();
					     	var text = '<div class="mes_box2"><h1>注册成功！</h1></div><div class="mes_box1"><a  onclick="close_box()" style="cursor:pointer;"> <h1 >确 定</h1></a></div>';
							showMsgBox(text);
						}else{
					     	var text = '<div class="mes_box2"><h1>注册失改！</h1></div><div class="mes_box1"><a  onclick="close_box()" style="cursor:pointer;"> <h1 >确 定</h1></a></div>';
							showMsgBox(text);
						}
					});
				}
			}else{
				$("#errmsg").html("<font color='red'>两次输入的密码不一致，请重新输入！</font>");
				setTimeout("clear_error()",2000);
			}
		}else{
			$("#errmsg").html("<font color='red'>验证码错误，请重新输入！</font>");
			setTimeout("clear_error()",2000);
			return false;
		}
	});
	
}

function clear_error(){
	$("#errmsg").html("");
}

function verify_code(){
	var code = $("#input_code").val();
	$.get(SW_URL_VOD+'verify.php', {authcode:code}, function(d) {
		if (d == "1"){
			return true;
		}else{
			$("#errmsg").html("<font color='red'>验证码错误，请重新输入！</font>");
			setTimeout("clear_error()",2000);
			return false;
		}
	});
}

function validateSWIDEmail(){
	var idEmail = $("#idEmail").val();
	if(idEmail!=""){
		validateEmail(idEmail,function (result){
			if (result.result == 'success') {
		           $("#emailmsg").html("<font color='green'>Email可以使用</font>");
		      }else{
		      	   $("#emailmsg").html("<font color='red'>Email已被占用</font>");
		      }
		}) ;
	}
}
function confirmpwd(){
	var idPassword = $("#idPassword").val();
	var pwdconfirm = $("#pwdconfirm").val();
	if (pwdconfirm != idPassword){
		$("#errmsg").html("<font color='red'>两次输入的密码不一致，请重新输入！</font>");
		setTimeout("clear_error()",2000);
	}
}

function register(){
	
	var text = '<div class="mes_box4">	<h1><img src="'+SW_URL_IMG+'event/courtside/logo.jpg" /></h1><span class="yh">电子邮箱: <input id="idEmail" type="text" class="input001" onblur="validateSWIDEmail()" /><b id="emailmsg"></b></span><span class="jl">密码: <input id="idPassword" type="password" class="input001" /></span><span class="yh">重复密码: <input id="pwdconfirm" type="password" class="input001" onblur="confirmpwd()"  /></span><span class="jl">昵称: <input id="idNickName"  type="text" class="input001"  /></span><span class="jl2">验证码: <input id="input_code"  name="authcode" type="text" class="input" size="10"  onblur="return verify_code()"  /> <img id="imgcode" src="auth_code.php" title="点击刷新" width="80" height="24" onclick="refresh_code()" style="cursor:pointer;" /></span><span id="errmsg"></span></div><div class="mes_box1"><a href="javascript:regSWID()"><h1 >确 定</h1></a></div>';
	
	showMsgBox(text);
	refresh_code();
}


function sw_upload(){
	if(islogin()) {
		window.open("http://vod.shawei.tom.com/vodUpload.php?ref=2");
	}else{
		sw_login("sw_upload");
	}
}

function islogin(){
	var id_user_cookie = Cookie.get("id_user");
	var id_4_sw_user_cookie = Cookie.get("id_4_sw_user");
	if(id_4_sw_user_cookie != null) {
		return true;
	}else{
		return false;
	}
}


		
function sw_note(callback){
	
	var text = '<div class="mes_box2"><h1>感谢您的参与，本次活动已经结束了*_*去<br><b> <a href="http://shawei.tom.com/event/courtside/award_list.php">获奖页面</a> </b>看看是否获得门票吧</h1></div>';
	showMsgBox(text);
}