﻿var vp_ok_offset={left:0,top:0};
$(function(){
	$("#flashbg").createFlashPlayer();
	$.vs.setAnchorTarget("video", "_self");
	$.applyPicsAndUrls();
	
	$("#gozone").click(vp_gozone_check);
	$("#addgrp").click(vp_checkUserLogin);
	$("#com_con").click(vp_edit_firstClick);
	//$("img .vpic").lazyload();
	
	$(".tj").click(vp_enter_rec);
	
	window.setTimeout('vp_nextprocess("");',200);
	
	window.setTimeout('vp_floatWinProcess();',400);
	
	window.setTimeout('vp_otherProcess();',600);
});

function vp_edit_firstClick()
{
	if($(this).attr("s")!=1)
	{
		$(this).attr("s",1).text("").attr("style","color:#000");
	}
}

function vp_gozone_check()
{
	if(vp_checkUserLogin())
	{
		return true;
	}
	return false;
}

function vp_floatWinProcess()
{
	$(".recpic").click(recpicClick);
	
	tb_init('a.floatwin');
	$(".winclose").click(closeFloatWin);
	
	$(".sharecopy").click(vp_sharecopy);
	
	$("#jubaosure").click(vp_jubao);	
	$("#sc").click(vp_collect);
	$("#postsub").click(vp_postComment);
	$("#up").click(vp_up);
}

function vp_otherProcess()
{
	$(".dtree").click(vp_dtree);
	$("#tjsub").click(vp_tjsub);

	$(".pldel").click(vp_pldel);
	$(".next").click(vp_playNext);	
	$("#xh").click(vp_xh);
}

function vp_xh()
{
	var me=$(this);
	if(vp_loop)
	{
		vp_loop=false;
		me.attr("xh",0);
		me.attr("title","正在单向播放");
		me.text("正在单向播放");
	}
	else
	{
		vp_loop=true;
		me.attr("xh",1);
		me.attr("title","正在循环播放");
		me.text("正在循环播放");
	}
	return false;
}

function vp_nextprocess(pre)
{
	$(pre+".pladdall").click(vp_playAll);
	$(pre+".pladd").click(vp_pladd);
	$("#picaddr").attr("value",$.vs.getVideoPicUrl(vp_vid));
	
	$("#gozone").attr("href",$.vs.path+"/qzone/go?vid="+vp_vid+"&msg="+escape(vp_title));
	$("#addgrp").attr("href",$.vs.path+"/user/addgrp?"+$.param({"v":vp_vid,"t":vp_title}));
}

function vp_playAll()
{
	var me=$(this);
	var list=$("."+me.attr("vclass"));
	var vid="";
	var obj=new Object();
	
	for(var i=0;i<list.length;i++)
	{
		vid+="|"+list.eq(i).attr("vid");
	}
	if(vid=="")
	{
		alert("没有任何视频可以添加");
		return false;
	}
	vid=vid.substr(1);
	vp_ok_offset=$(this).offset();
	vp_ok_offset.left = vp_ok_offset.left-300;
	vp_playAddVideo(vid,0,vp_playAll_cb,me.attr("type"));
	return false;
}

function vp_playAll_cb(xml)
{
	if($.vs.checkXml(xml))
	{
		if(this.vpara==1)
		{
			//alert("添加播放列表成功");
			if ($.browser.msie)
			{
				$("#addsuclk").get(0).click();
			}
			else
			{
				alert("添加成功");
			}
		}
		else
		{
			if ($.browser.msie)
			{
				$("#vplistlk").get(0).click();
			}
			else
			{
				alert("添加成功");
			}
			
			window.location.href =$("#vplistlk").attr("href");
			
		}
	}
}

function preplay(vid)
{
	vp_anotherPlay(vid,1);
}

function thisplay(vid)
{
	vp_anotherPlay(vid,2);
}

function vp_anotherPlay(vid,type)
{
	var list=$(".next")
	var now=$("#"+vid);
	var n=list.index(now.get(0));
	if(type==0)
	{
		n++;
	}
	else if(type==1) 
	{
		n--;
	}
	if(vp_loop)
	{
		if(n<0)
		{
			n=list.length-1;
		}
		else
		{
			n=n%list.length;
		}
	}

	if(n<list.length)
	{
		vp_playNextVideo(list.eq(n),type);
	}
	else
	{
		now.find(".play").hide();
	}
}

function nextplay(vid)
{
	vp_anotherPlay(vid,0);
}

function vp_playNextVideo(me,type)
{
	var vid=me.attr("vid");
	var flashObj=document.getElementById("player");
	var title=me.find(".vt").text();
	var list=$(".next");
	var now=$("#"+vid);
	var n=list.index(me.get(0));
	var ln=-1;
	var nn=-1;
	var ltitle="";
	var ntitle="";
	if(n<=0)
	{
		if(vp_loop)
		{
			ln=list.length-1;
		}
	}
	else
	{
		ln=n-1;
	}
	if(n>=list.length-1)
	{
		if(vp_loop)
		{
			nn=0;
		}
	}
	else
	{
		nn=n+1;
	}
	
	if(ln>=0)
	{
		ltitle=list.eq(ln).attr("vtitle");
	}
	
	if(nn>=0)
	{
		ntitle=list.eq(nn).attr("vtitle");
	}
	if(flashObj)
	{
		if(type==2)
		{
			flashObj.reText(ltitle,ntitle);
			return false;
		}
		flashObj.startplay(vid,ltitle,ntitle);
	}
	
	$("#videoTitle").text(title);
	document.title="QQVideo--"+title;
	vp_vid=vid;
	vp_title=title;
	
	$(".next").find(".play").hide();
	me.find(".play").show();
	
	$.get("videopl/next",
	   {
		   "v":vid
	   },
		vp_playNext_cb, "text");
	return false;
}

function vp_playNext()
{
	vp_playNextVideo($(this));
}

function vp_playNext_cb(text)
{
	$("#fb").hide();
	$("#fb").appendTo($("body"));
	$("#plnext").get(0).innerHTML=text;
	$("#fb").appendTo($("#fbp"));
	$("#fb").show();
	
	var dataEm=$("#setupnum");
	var up=$("#up");
	var upnum=dataEm.attr("upnum");
	if(!upnum)
	{
		upnum=0;
	}
	up.attr("upnum",upnum);
	up.text("("+upnum+")");
	
	$.applyPicsAndUrls("plnext");
	vp_nextprocess("#plnext ");
	
}

function vp_pldel()
{
	if(window.confirm("确定将该视频移出播放列表吗？"))
	{
		vp_playAddVideo($(this).attr("vid"),1,vp_pldel_cb);
	}
	return false;
}

function vp_pldel_cb(xml)
{
	if($.vs.checkXml(xml))
	{
		$("#"+this.vid).remove();
		var el=$("#listtitle");
		var n=el.attr("n");
		n--;
		el.attr("n",n);
		el.text("播放列表("+n+")");
		
	}
}

function vp_pladd()
{
	vp_ok_offset=$(this).offset();
	vp_ok_offset.left = vp_ok_offset.left-300;
	vp_playAddVideo($(this).attr("vid"),0,vp_pladd_cb);
	return false;
}

function vp_playAddVideo(vid,t,cb,param)
{
	$.ajax({
			type: "POST",
			url: "videopl/pladd",
			data:{
				   "t":t,
				   "vid":vid
			  	 },
			success: cb,
			dataType: "xml",
			vid:vid,
			vpara:param
		   });
}

function vp_pladd_cb(xml)
{
	if($.vs.checkXml(xml))
	{
		//alert("添加播放列表成功");
		if ($.browser.msie)
		{
			$("#addsuclk").get(0).click();
		}
		else
		{
			alert("添加成功");
		}
	}
}

function vp_tjsub()
{
	var chks=$("#tjhy").find("input");
	var uins="";
	var em;
	for(var i=0;i<chks.length;i++)
	{
		em=chks.eq(i);
		if(em.attr("checked")&&em.attr("uin"))
		{
			uins+=em.attr("uin")+"|";
		}
	}
	if(!uins)
	{
		alert("你还没有选择任何推荐好友");
		return false;
	}
	$.post("videopl/rec",
   {
	   "vid":vp_vid,
	   "ru":$.trim(uins)
   },
	vp_tjsub_cb, "xml");
	$("#tjhy").hide();
}

function vp_tjsub_cb(xml)
{
	if($.vs.checkXml(xml))
	{
		alert("推荐成功，您好友的QQ邮箱将会收到一封视频推荐邮件。");
	}		
}


function vp_dtree()
{
	var uin=0;
	if(uin=vp_checkUserLogin())
	{
		var me=$(this);
		var stype=me.attr("stype");
		var url;
		var callback;
		if(stype==1 || stype==2)
		{	
			if(stype==1)
			{
				url="videopl/rf";
				callback=vp_dtree_cb_rf;
			}
			else
			{
				url="videopl/qqf";
				callback=vp_dtree_cb_qqf;
			}
			
			$.get(url,
			   {
				   "uin":uin
			   },
				callback, "text");
		}
	}
}

function vp_dtree_cb_rf(text)
{
	if(text)
	{
		vp_dtree_cb(text,1);
	}
}

function vp_dtree_cb_qqf(text)
{
	if(text)
	{
		vp_dtree_cb(text,2);
	}
}

function vp_dtree_cb(text,type)
{
	var p;
	var img;
	if(type==1)
	{
		p=$("#rctjhy");
		img=p.find(".dtree");
		img.attr("src",$.vs.static+"/img/minus.gif");
	}
	else
	{
		p=$("#qqtjhy");
		img=p.find(".dtree");
		img.attr("src",$.vs.static+"/img/minusbottom.gif");
	}
	p.after(text);
	$(".dtimg").click(vp_dtImgClick);
	$(".checkall").click(vp_checkall);
	img.attr("open",1);
	img.attr("stype",0);
		
	img.click(vp_dtImgClick);
}

function vp_checkall()
{
	var n=$(this).attr("n");
	var cks=$(".dtul"+n).find("input");
	cks.attr("checked",$(this).attr("checked")?true:false);
}

function vp_dtImgClick()
{
	var n=$(this).attr("n");
	var open=$(this).attr("open");
	var p=$(".dtul"+n);
	var last=$(this).attr("last");
	var src;
	var bottom="";
	if(open==1)
	{
		src="plus";
		p.css("display","none");
		$(this).attr("open",0);
	}
	else
	{
		src="minus";
		p.css("display","block");
		$(this).attr("open",1);
	}
	if(last==1)
	{
		bottom="bottom";
	}
	src=$.vs.static+"/img/"+src+bottom+".gif";
	$(this).attr("src",src);
}

function vp_up()
{
	if(vp_checkUserLogin())
	{
		$.post("videopl/up",
		   {
			   "vid":vp_vid
		   },
			vp_vote_cb, "xml");
	}
};

function vp_vote_cb(xml)
{
	$.vs.xmlErrorCode["259"]="该视频您已投票";
	if($.vs.checkXml(xml))
	{
		alert("投票成功");
		var up=$("#up");
		var upnum=up.attr("upnum");
		upnum++;
		up.attr("upnum",upnum);
		up.text('('+upnum+')');
	}
}

function vp_postComment()
{
	var con=$.trim($("#com_con").attr("value"));		
	if(con=="" || con==$.vs.pinlunWarn)
	{
		alert("评论内容不能为空");
		return false;
	}
	if(vp_checkUserLogin())
	{
		$.post("videopl/post",
		   {
			   "vid":vp_vid,
			   "c":con
		   },
			vp_postComment_cb, "xml");
	}
};

function vp_postComment_cb(xml)
{
	$.vs.xmlErrorCode["2503"]="评论字数过长";
	if($.vs.checkXml(xml))
	{
		var uin=$.vs.checkUin($.cookie("uin"));
		var name=$.vs.getSafeHTML(($.cookie("name")));
		var cn=$.vs.getSafeHTML($("#com_con").attr("value"));
		var htm='<div class="pl" id="newpost">'+
					'<span class="icon"><a class="uinfo" uin="'+uin+'" ><img class="upic" uin="'+uin+'" alt="" /></a></span>'+
					'<span>'+
						'<ul>'+
							'<li class="names"><a class="uinfo" bt="1" uin="'+uin+'" >'+name+'</a></li>'+
							'<li class="times">刚刚</li>'+
							'<li class="say">'+cn+'</li>'+
						'</ul>'+
					'</span>'+
				'</div>';
		$("#newpost").attr("id","");
		$(htm).insertBefore("#fb"); 
		
		$.applyPicsAndUrls("newpost");
	
		$("#com_con").attr("value","");	
		var comNum = $("#com_num").attr("comNum");
		if (!!comNum)
		{
			comNum++;
		}
		else
		{
			comNum = 1;
		}
		$("#com_num").attr("comNum", comNum);
		$("#com_num").text(comNum);
	}
}


function vp_collect()
{
	if(vp_checkUserLogin())
	{
		$.post("videopl/collect",
		   {
			   "vid":vp_vid
		   },
			vp_collect_cb, "xml");
	}
	return false;
}
	
function vp_collect_cb(xml)
{
	$.vs.xmlErrorCode["2001"]="收藏成功";
	var res = $.vs.checkXml(xml,true);
	if(res=="o" || res=="2001")
	{
		$("#sc").hide();
		$("#sc2").show();
	}
	else
	{
		alert("收藏失败");
	}
}

function vp_jubao()
{
	var cks=$(".jubaock");
	var reason="";
	for(var i=0;i<cks.length;i++)
	{
		if(cks.get(i).checked)
		{
			reason+=(i+1)+"|";
		}
	}
	reason=$.trim(reason);
	if(reason=="")
	{
		alert("请选择举报理由");
		return false;
	}
	
	$.post("videopl/jubao",
   {
	   "vid":vp_vid,
	   "cls":reason
   },
	vp_jubao_cb, "xml");
}

function vp_jubao_cb(xml)
{
	var res = $.vs.checkXml(xml,true);
	if( $.vs.checkXml(xml))
	{
		alert("举报成功");
	}
}

function vp_sharecopy()
{
	var iptext=$(this).siblings(".copyinput");
	
	iptext.get(0).select();
	if(window.clipboardData&&window.clipboardData.setData)
	{
		if(!window.clipboardData.setData('text', iptext.attr("value")))
		{
			alert("复制失败");
		}
	}
}

function vp_checkUserLogin()
{
	var uin=$.vs.checkUin($.cookie("uin"));
	var skey=$.cookie("skey");
	var qvkey=$.cookie("qvkey");
	if(uin&&(skey!="")&&(qvkey!=""))
	{		
		return uin;
	}
	else
	{
		vp_openLoginPage();
		return false;
	}
}

function vp_openLoginPage()
{
  	openLogin('f_url=loginerroralert&low_login=0');
  	window.scrollTo(0,0);
//	if(window.confirm("登录操作会打断当前的播放进程，确定登录吗？"))
//	{
//		window.location.href =$.vs.path+"/login.htm?u="+encodeURIComponent(vp_pageName);
//	}
}


function closeFloatWin(){
	var lay=$("#"+$(this).attr("wid"));
	lay.hide();
}

function moveElement(jq, x, y)
{
	jq.css({left:x+"px",top:y+"px"});
}

function isPointInRect(x, y, left, top, width, height)
{
	return (x >= left && x < (left + width))
			&& (y >= top && y < (top + height));
}

function tb_init(domChunk){
	
	$(domChunk).each(function(){
	var lay=$("#"+$(this).attr('inlineId'));
	var cap=lay.find(".title");
	
	lay.attr("first",1);
	cap.mousemove(function(){
		if(this.bDragging)
		{
			moveClip(this,lay);
		}
	});
	cap.mousedown(function(){
		if(isPointInRect(event.offsetX , event.offsetY, 0,0, this.offsetWidth , this.offsetHeight ))
		{
			this.lastX = event.screenX;
			this.lastY = event.screenY;
			this.bDragging = true;
			this.setCapture();
		}
	});
	cap.mouseup(function(){
		if(this.bDragging)
		{
			this.releaseCapture();
		}
		this.bDragging=false;
	});
	cap.mouseover(function(){
		$(this).addClass("move");
	});
	cap.mouseout(function(){
	    $(this).removeClass("move");
	});
	});
	
	$(domChunk).click(function(){
	var id = $(this).attr("inlineId");
	tb_show(id,this);
	this.blur();
	return false;
	});
}

function moveClip(el,p)
{
	var me=$(el);
	var now_x = event.screenX;
	var now_y = event.screenY;
	var x;
	var y;
	if(el)
	{
		x = now_x - el.lastX + p.get(0).offsetLeft;
		y = now_y - el.lastY + p.get(0).offsetTop;
		el.lastX = now_x;
		el.lastY = now_y;
		moveElement(p, x, y);
	}
}

function tb_show(id,em){
	var me=$("#"+id);
	var first=me.attr("first");
	if(first==1)
	{
		if(id!="addsuc")
		{
			me.attr("first",0);
			me.css($(em).offset());
		}
		else
		{
			me.css(vp_ok_offset);
		}
		if(id=="qqface")
		{
			vp_addfaces();
		}
	}
	if(id=="zhuantie")
	{
		$(".copy1").each(function(){$(this).attr("value",$(this).attr("value0").replace(/v=replace/g,"v="+vp_vid).replace(/vid=replace/g,"vid="+vp_vid))});
	}
	$("#"+id).show();
}

function vp_addfaces()
{
	var str="";
	var i=0;
	for(i=0;i<44;i++)
	{
		str+="<img class='bbsface'  n='[A"+i+"]' src='"+$.vs.static+"/faces/a"+i+".gif'>";
	}
				
	$("#facediv").html(str);
	$(".bbsface").click(vp_insertFace);
	$(".bbsface").mouseover(function(){$(this).css("border","2px solid #626262");});
	$(".bbsface").mouseout(function(){$(this).css("border","2px solid #ededed");});
	
}

function vp_insertFace()
{
	var value=$("#com_con").attr("value");
	if(!value)
	{
		value="";
	}
	value+=$(this).attr("n");
	$("#com_con").attr("value",value);
	$("#qqface").hide();
}

// 打开推荐窗口
function vp_enter_rec()
{
	var uin = vp_checkUserLogin();
	if ( !uin ){return;}
	
	var rec_board = $("#rec_div");
	if ( rec_board.css("display") != "block" )
	{	
		var shadow_wnd = $("#shadow_div");
		var height = document.documentElement.scrollHeight + "px";
		shadow_wnd.css("height", height);
		shadow_wnd.css("display", "block");
		
		rec_board.css("display", "block");
		
		var offset = $(".tj").offset();
		var y = offset.top - 465;
		if ( y < 0 )
		{ 
			y = 0;
		}
		window.scrollTo(0, y);
		
		var rm = Math.random();
		var src = "videopl/qqf?rm=" + rm + "&vid=" + vp_vid;
		rec_frame.location.href = src;
	}	
}

function recpicClick()
{
	var lk=this.parentElement;
	var i=0;
	while((lk.tagName!="A") && (i<4))
	{
		i++;
		lk=lk.parentElement;
	}
	lk.click();
}

$.fn.createFlashPlayer = function() 
{
	return this.each(function() {
		var me = $(this);
		var vid = me.attr("vid");
		var swf = me.attr("swf");
		var width = me.attr("w") || me.css("width");
		var height = me.attr("h") || me.css("height");
		var skin = me.attr("skin");
		var title = me.attr("title");

		var str = "";
		var listcgi="";
		var param;
		var type;
		var list=2;
		if(vp_pageType==0)
		{
			list=1;
			
			if(vp_plt&&vp_plt.length>=3)
			{
				param=vp_plt.substring(0,vp_plt.length-2);
				type=parseInt(vp_plt.substr(vp_plt.length-1,1));
				if(type==0)
				{
					listcgi="&listcgi="+$.vs.path+"/view/xml?type="+param;
				}
				else
				{
					listcgi="&listcgi="+$.vs.path+"/rank/xml?type="+param;
				}
			}
		}

		
		if ($.browser.msie)
			str = 
			"<object id='player' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' " +
			"        codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' " +
			"        width='" + width + "' height='" + height + "'>" +
			"    <param name='movie' value='" + swf + "'/>" +
			//"    <param name='wmode' value='Opaque'/>" +
			"    <param name='wmode' value='window'/>" +
			"    <param name='allowscriptaccess' value='always'/>" +
			"    <param name='allowFullScreen' value='true'/>" +
			"    <param name='flashvars' value='vid=" + vid + "&skin="+skin+"&autoplay=1&gourl=http://video.qq.com/v1/videopl?v=&list="+list+listcgi+"&title=" + title +"'/>" +
			"    <param name='quality' value='high'/>" +
			"</object>";
		else
			str = 
			"<embed id='player' type='application/x-shockwave-flash' " +
			"       src='" + swf + "' " +
			"       width='" + width + "' height='" + height + "' " +
			"       quality='high' " +
			"       allowScriptAccess='always'" +
			"       wmode='Opaque' " +
			"		allowFullScreen='true' "+
			"       flashvars='vid=" + vid + "&skin="+skin+"&autoplay=1&gourl=http://video.qq.com/v1/videopl?v=&list="+list+listcgi+"&title=" + title +"'>" +
			"</embed>";
		this.innerHTML = str;
		return this;
	});
};
