var ratetext = "太差劲了！,不怎样。,还可以。,真不错。,太棒了！".split(',');

var loginuser;  //观看者ID
var spaceid; //空间用户ID

function mn(x){
	for (var i=1; i<=x; i++) {
		document.getElementById("img" + i).src = "/image/dot_star.gif";
	}
	for (var i=x+1; i<=5; i++) {
		document.getElementById("img" + i).src = "/image/dot_empstar.gif";
	}
	document.getElementById("mt").innerHTML = "<span class=fs01>" + ratetext[x-1] + "</span>";
}

function mf(x){
	for (var i=1; i<=x; i++) {
		document.getElementById("img" + i).src = "/image/dot_star.gif";
	}
	for (var i=x+1; i<=5; i++) {
		document.getElementById("img" + i).src = "/image/dot_empstar.gif";
	}
}

$(document).ready(function() {
	loginuser = $("#loginuser").val();
	spaceid = $("#spaceid").val();
	var desheight = $("#describediv").height();
	if (desheight > 350) {
		$("#describediv").css("height", "350px");
		$("#desA").html("详细");
	}
});

function showbox(type, id) {
	var opusid = $("#opusid").val();
	if (type == "sharespace") {
		if (loginuser == '0') {
			alert("请先登录，再分享此作品。");
		} else {
			window.open('/opus/mi_halve.jsp?opusid=&userid=' + loginuser + "&spaceid=" + spaceid, 'Share', 'width=580,height=480,resizable=yes,scrollbars=yes,status=0');
		}
	} else if (type == "favorite") {
		if (loginuser == '0') {
			alert("请先登录，再收藏此作品。");
		} else {
			$("#joingroup").hide();
			$("#exposure").hide();
			$.getJSON("/ajaxaction.do?method=favoption", {"userid":loginuser}, function(json) {
				var favcount = json[json.length-1];
				$("#favcountid").html(favcount);
				
				var result = ["<select name='favsel' id='favselid'>",
			  		"<option value='0' selected>——请选择——</option>"];
				for (var i=0; i<json.length-1; i++) {
					result.push("<option value=" + json[i].opuslistid+ ">" + json[i].title + " (" + json[i].opuscount + "个视频)</option>");
				}
				result.push("<option value='new'>[ 新建节目单 ]</option>");
				result.push("</select>");
				$("#favselspanid").html(result.join(''));
				$("#favcheckid").attr("checked", true);
			});
			$("#" + type).show();
		}
	} else if (type == "share") {
		if (loginuser == '0') {
			alert("请先登录，再分享此作品。");
		} else {
			window.open('/opus/mi_halve.jsp?opusid=' + opusid + '&userid=' + loginuser, 'Share', 'width=580,height=480,resizable=yes,scrollbars=yes,status=0');
		}
	}  else if (type == "exposure") {
		if (loginuser == '0') {
			alert("请先登录，再举报此作品。");
		} else {
			$("#joingroup").hide();
			$("#favorite").hide();
			$.getJSON("/ajaxaction.do?method=expoption", {},function(json) {
				var result = ["<select name='expsel' id='expselid'>",
					"<option value='0' selected>——请选择——</option>"]
				for (var i=0; i<json.length; i++) {
					result.push("<option value=" + json[i].id+ ">" + json[i].title + "</option>");
				}
				result.push("</select>");
				$("#expselspanid").html(result.join(''));
			});
			$("#" + type).show();
		}
	} else if (type == "joingroup") {
		if (loginuser == '0') {
			alert("请先登录，再添加到群组。");
		} else {
			$("#exposure").hide();
			$("#favorite").hide();
			$.getJSON("/ajaxaction.do?method=groupoption", {"userid":loginuser}, function(json) {
				if (json.length == 0) {
					$("#joindivid").html("您还没有加入任何<a href='/teams/mygrouplist.do' class='blue'>群组</a>。");
				} else {
					var result = ["<h3>请选择群组</h3>", "<span id='groupspanid'>"];
					for (var i=0; i<json.length; i++) {
						result.push("<input name='group' id='groupid' type='checkbox' value='" + json[i].gid + "," + json[i].vStrategy + "," + json[i].isowner + "' />" + json[i].gname + "（视频：" + json[i].gcount + "）");
						if (json[i].isowner != "") {
							result.push("（<span style='color:#0066FF'>群主</span>）");
						}
						result.push("<br/>");
					}
					result.push("</span>");
					result.push("<div class='vspace'></div>")
					result.push("<div><input name='groupbutton' id='groupbuttonid' type='button' value='添加入群组' onclick='joingroup()'/></div>");
					result.push("<div class='clear_float'></div>");
					$("#joindivid").html(result.join(''));
				}
			});
			$("#" + type).show();
		}
	} else if (type == "diy") {
		if (loginuser == '0') {
			alert("请先登录，再收藏素材。");
		} else {
			$.getJSON("/ajaxaction.do?method=diy", {"opusid":opusid, "userid":loginuser}, function(e){
				$("#diyid").html("成功收藏为素材");
			});
		}
	} else if (type == "postcom") {
		if ($("#isactive").val() == '1') {
			$("#posttext2")[0].focus();
		} else {
			alert("请先登陆并激活帐号，再发表您的评论。");
		}
	} else if (type == "replycom") {
		if ($("#isactive").val() == '1') {
			if (id != null && (typeof id != 'undefined') && id != "") {
				$("#"+ id).css("display", "block");				
			}
		} else {
			alert("请先登陆并激活帐号，再发表您的评论。");
		}
	} else if (type == "subscribe") {
		if (loginuser == '0') {
			alert("请先登录，再订阅。");
		} else {
			$.getJSON("/ajaxaction.do?method=subscribe", {"loginuserid":loginuser, "authorid":spaceid}, function(json) {
				if (json.result) {
					$("#subscribeid").html("您已订阅成功。");
				}
			});
		}
	} else if(type == "message") {
		if (loginuser == '0') {
			alert("请先登录，再发送消息。");
		} else {
			var nickname = $("#nickname").val();
			location.href = "/user/messagesadd.do?nickname=" + nickname;
		}
	} else if(type == "blockuser") {
		if (loginuser == '0') {
			alert("请先登录，再阻止用户。");
		} else {
			$.getJSON("/spaceajax.do?method=blockuser", {"loginuserid":loginuser, "spaceid":spaceid}, function(json) {
				if (json.result) {
					$("#blockuserspan").html("您已阻止成功。");
				}
			});
		}
	} else if (type == "addfriend") {
		if (loginuser == '0') {
			alert("请先登录，再加好友。");
		} else {
			$.getJSON("/spaceajax.do?method=addfriend", {"loginuserid":loginuser}, function(json) {
				var result = ["<select name='friendsel' id='friendselid'>",
					"<option value=0>----</option>"];
				for (var i=0; i<json.length; i++) {
					result.push("<option value=" + json[i].fgroupid+ ">" + json[i].fgroupname + "</option>");
				}
				result.push("</select>");
				$("#friendtd").html(result.join(''));
				$("#addfrienddiv").show();
			});
		}
	} else if (type == "removefriend") {
		if (loginuser == '0') {
			alert("请先登录，再删除好友。");
		} else {
			$.getJSON("/spaceajax.do?method=removefriend", {"loginid":loginuser, "spaceid":spaceid}, function(json) {
				if (json.result) {
					$("#removefriendspan").html("您已删除好友。");
				}
			});
		}		
	} 
}

//评分
function dorate(rate) {
	var opusid = $("#opusid").val();
	$.getJSON("/ajaxaction.do?method=rating", {"rate":rate, "opusid":opusid}, function(json) {
		if (true == json.result) {
			var count = 1 + parseInt($("#ratecount").val());
			var result = [
				"<div id='mt' style='margin-right:0;_padding-top:3px; padding-bottom:6px;'><span class='fs01'>投票：" + count + "</span></div>",
				"<div class='mt_img' style='margin-right:0;'>"
			];
			for (var i=1; i<=rate; i++) {
				result.push("<img id='img' src='/image/dot_star.gif' />");
			}
			for (var i=rate+1; i<=5; i++) {
				result.push("<img id='img' src='/image/dot_empstar.gif' />");
			}
			result.push("</div>");
			$("#ratetd").html(result.join(''));
		}
	});
}

function cancelbox(elmid) {
	$("#" + elmid).hide();
}

//收藏
function dofavorite() {
	var opusid = $("#opusid").val();
	var isfav = $("#favcheckid").attr("checked");
	var opuslistid = $("#favselid").val();
	if (opuslistid == 0 && !isfav) {
		return ;
	}
	$.getJSON("/ajaxaction.do?method=favorite", {"opusid":opusid, "userid":loginuser, "isfav":isfav, "opuslistid":opuslistid}, function(json) {
		if (json.result) {
			if (opuslistid != "new") {
				cancelbox("favorite");
			} else {
				var url = $("#param").val();
				location.href = "/user/edit_playlist_info.do?cp_vid=" + opusid + "&url=" + url;
			}
		}
	});
}

//举报
function doexposure() {
	var opusid = $("#opusid").val();
	var reason = $("#expselid").val();
	if (reason == 0) {
		return ;
	}
	$.getJSON("/ajaxaction.do?method=exposure", {"opusid":opusid, "userid":loginuser, "reason":reason}, function(e){
		cancelbox("exposure");
		$("#expspanid").html("举报");
	});
}

function joingroup() {
	var opusid = $("#opusid").val();
	var joingroup = ["["];
	$("#joindivid").find("input[@type=checkbox]").each(function(i) {
		if ($(this).attr("checked")) {
			var value = $(this).val().split(",");
			if (joingroup.join('') != "[") {
				joingroup.push(",");
			}
			joingroup.push("{'gid':" + value[0] + ", 'vStrategy':" + value[1] + ", 'isowner':'" + value[2] + "'}");
		}		
	});
	joingroup.push("]");
	if (joingroup.join('') == "[]") {
		return;
	}
	$.getJSON("/ajaxaction.do?method=joingroup", {"opusid":opusid, "group":joingroup.join('')}, function(json){
		if (json.result) {
			alert("添加成功");
			cancelbox("joingroup");
		}
	});
}

function important(value) {
	var opusid = $("#opusid").val();
	$.getJSON("/ajaxaction.do?method=important", {"opusid":opusid, "value":value}, function(json) {
		$("#important").html("推荐到首页(" + json.important + ")");
		$("#unimportant").html("反对上首页(" + json.unimportant + ")");
	});
}

//加朋友
function doaddfriend() {  
	var fgid = $("#friendselid").val();
	$.getJSON("/spaceajax.do?method=doaddfriend", {"loginid":loginuser, "fgid":fgid, "spaceid":spaceid}, function(json) {
		if (json.result != -1) {
			$("#addfrienddiv").hide();
			$("#addfriendspan").html("已发送邀请");
		}
	});	
}

function getlist(json) {
	var result = new Array();
	for (var i=0; i<json.length; i++) {
		var opusmap = json[i];
		if (i%3==0 && i!=0) {
			result.push("</ul>");
			result.push("<ul class='il_517_4'>");
		}
		result.push("<li>");
		result.push("<div class='image baseBorder'><a title='" + opusmap.title + "' href='" + opusmap.opusurl + "' target='_blank'><img width='112' height='84' title='" + opusmap.title + "' alt='" + opusmap.title + "' src='" + opusmap.picurl + "'/></a></div>");
		result.push("<div class='text' style='width:112px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;'>");
		result.push("<a title='" + opusmap.title + "' href='" + opusmap.opusurl + "' target='_blank'>" + opusmap.title + "</a>");
		result.push("</div>");
		result.push("</li>");
	}
	return result;
}

function turnpage(p, type) {
	$.getJSON("/spaceajax.do?method=turn" + type, {"page":p, "userid":spaceid}, function(json) {
		var result = new Array();
		var jlen = json.length-1;
		for (var i=0; i<jlen; i++) {
			var opusmap = json[i];
			if (i%3==0 && i!=0) {
				result.push("</ul>");
				result.push("<ul class='il_517_4'>");
			}
			result.push("<li>");
			result.push("<div class='image baseBorder'><a title='" + opusmap.title + "' href='" + opusmap.opusurl + "'><img width='112' height='84' title='" + opusmap.title + "' alt='" + opusmap.title + "' src='" + opusmap.picurl + "'/></a></div>");
			result.push("<div class='text' style='width:112px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;'>");
			result.push("<a title='" + opusmap.title + "' href='" + opusmap.opusurl + "'>" + opusmap.title + "</a>");
			result.push("</div>");
			result.push("</li>");
		}
		$("#"+type+"ul").html(result.join(''));
		$("#"+type+"cur").html(p);
		$("#"+type+"page").html(json[jlen].page);
		initwatchbtn("#"+type+"ul .image");
	});
}

function showpanel( vid,type )//type 1 show more  0 show less
{
	if(type==1)
	{
		$('#less_'+vid).hide();
		$('#MorevidDescy_'+vid).hide();
		$('#more_'+vid).show();
		$('#LessidDescy_'+vid).show();
	}
	else{
		$('#less_'+vid).show();
		$('#MorevidDescy_'+vid).show();
		$('#more_'+vid).hide();
		$('#LessidDescy_'+vid).hide();
	}
}

function postcomment(id, type, pid) {
	var content = $("#" + id).val();
	var devalue = $("#" + id).attr("defaultValue");
	if (content == "" || content == devalue) {
		return ;
	}
	$("#" + id + "btn").attr("disabled","true");
	var commenterid = loginuser;
	var parentid;
	if (type == "post") {
		parentid = 0;
	} else {
		parentid = pid;
	}
	var param = {"userid": spaceid, "commenterid": commenterid, 
				 "parentid": parentid, "content": content};
	$.post("/spaceajax.do?method=postcomment", param, function(xml) {
	     if ($(xml).text() == -1) {
	     	alert("您的评论被拒绝。");
	     } else if ($(xml).text() == 0) {
	     	alert("您的评论提交失败，请重试。");
	     } else {
	     	alert("您的评论已提交，请等待管理员审核。");
	     	location.reload();
	     }
   	});
   	
   	location.reload();
}

function turnpagecom(p) {
	$.getJSON("/spaceajax.do?method=turncompage", {"page":p, "userid":spaceid}, function(json) {
		var jlen = json.length-1;
		//更新页面评论
		var result = new Array();
		for (var i=0; i<jlen; i++) {
			var comment = json[i];
			result.push("<div class='textbox tbtypevideo'>");
			result.push("<div class='image baseBorder'><a title='" + comment.commentername + "' href='" + comment.spaceurl + "'><img width='120' height='90' src='" + comment.commentlogo + "'/></a></div>");
			result.push("<p class='room2'><a title='" + comment.commentername +"' href='" + comment.spaceurl + "'>" + comment.commentername + "</a><br/>");
			result.push("<span>时间：<b>" + comment.createtime + "</b></span><br/>");
			result.push("<span>" + comment.content + "</span>");
			result.push("</p>");
			result.push("<div class='clear_float'></div>");
			result.push("</div>");
		}
		
		$("#commentdiv").html(result.join(''));
		
		//更新翻页
		$("#compagecur").html(p);
		$("#compage1").html(json[jlen].page);
		$("#compage2").html(json[jlen].page);
	});
}

function seemoreless() {
	if ($("#desA").html() == "详细") {
		$("#describediv").height("auto");
		$("#desA").html("简约");
	} else {
		$("#describediv").css("height", "350px");
		$("#desA").html("详细");
	}
}

function plshowpanel( vid,type ) { //type 1 show more  0 show less 
		if(type==1)	{
			$('#plless_'+vid).hide();
			$('#plMorevidDescy_'+vid).hide();
			$('#plmore_'+vid).show();
			$('#plLessidDescy_'+vid).show();
		} else {
			$('#plless_'+vid).show();
			$('#plMorevidDescy_'+vid).show();
			$('#plmore_'+vid).hide();
			$('#plLessidDescy_'+vid).hide();
		}
}