$(document).ready(function(){
	$("#ChkAll").click(function(){
		if($(this).attr("checked")==true){
			$("input[id='S_ID']").each(function(){
				$(this).attr("checked",true);									 
			})							
		}else{
			$("input[id='S_ID']").each(function(){
				$(this).attr("checked",false);									 
			})							
		}
	})
	$("#search").click(function(){
		var keyword=$("#keyword")[0].value;
		keyword=encodeURI(keyword);
		if(keyword==''){
			$("#keyword").css("border","1px solid red");return false;	
		}else{
			$("#form1").attr("action",'admin_video.php?action=listbysc&keyword=' + keyword);
			$("#form1").submit();
		}
	})
	
	
	$("#torun").click(function(){
		if(confirm('确定执行此操作吗?')){return true;}else{return false;}
	})

	$("input[name=reg]").bind("click",regulate)
	$("span[id=topicsel]").bind("click",topicsel)
	$("span[id=remarksel]").bind("click",remarksel)
	
//被调函数	
	function regulate(){
		var id=$(this).attr("id");	
		var content=$("#S_Vdata"+id).attr("value");
		if(content.length==0){jAlert('请填写影片地址!',"操作提示");return false;}
		if(navigator.userAgent.indexOf("Firefox")>0){var contentarr=content.split("\n");}else{contentarr=content.split("\r\n");}
		var newadd="";
		for(var i=0;i<contentarr.length;i++){
			if(contentarr[i].length>0){
				var videoadd=contentarr[i].split('$');
				if(videoadd.length==1){
					contentarr[i]='第'+(i+1)+'集$'+contentarr[i];
				}else if(videoadd.length==2){
					contentarr[i]='第'+(i+1)+'集$'+videoadd[1];
				}
			}
			newadd+=contentarr[i]+"\r\n";
		}
		newadd=trim(newadd,"\r\n")
		$("#S_Vdata"+id).attr("value",newadd);
	}
	
function trim(str,filter){
	var len
	len=filter.length;
	if(str.substr(0,len)==filter){
		str=str.substr(len)
		}
	if(str.substr(str.length-len)==filter){
		str=str.substr(0,str.length-len)
		}
	return str
}	

	function topicsel(){
		var s_id=$(this).attr("s_id");
		var topicid=$(this).attr("topicid");
		var topicName=$(this);
		var offset=topicName.offset();
		var topicTop=offset.top;
		var topicLeft=offset.left;
		creatediv(99997,250,20);
		var ShowDiv=$("#confirm");
		ShowDiv.css('border','1px solid #55BBFF').css('background','#C1E7FF').css('padding',' 3px 0px 3px 4px').css('top',topicTop-4+'px').css('left',topicLeft-100+'px').html('正在加载内容......');
		ShowDiv.load("js/ajaxfun.php?s_id="+s_id+"&types=1&action=topic&tid="+topicid);
	}
	
	function remarksel(){
		var s_id=$(this).attr("r_id");
		var rid=$(this);
		var offset=rid.offset();
		var topicTop=offset.top;
		var topicLeft=offset.left;
		creatediv(99997,250,20);
		var ShowDiv=$("#confirm");
		ShowDiv.css('border','1px solid #55BBFF').css('background','#C1E7FF').css('padding',' 3px 0px 3px 4px').css('top',topicTop-4+'px').css('left',topicLeft-100+'px').html('&nbsp;&nbsp;连载至<input type="text" size="4" name="cremark" id="cremark">集&nbsp;<input type="button" value="确定" class="rb1" onclick="cremark('+s_id+')">&nbsp;<input type="button" value="关闭" class="rb1" id="remarkclose"   onclick="closet()">&nbsp;&nbsp;');
		//ShowDiv.load("js/ajaxfun.php?s_id="+s_id+"&action=remark");
	}
	

	function creatediv(z,w,h){
		$('<div id="adddiv"></div>')
        .css('top', '0')
		.css('width',document.documentElement.scrollWidth+"px")
		.css('height',document.documentElement.scrollHeight+"px")
		.css('z-index',z)
		.css('filter','Alpha(Opacity=0)')
		.css('position','absolute')
        .appendTo("body");
		
		$('<div id="confirm"></div>')
		.css('position','absolute')
		.css('z-index',z+1)
		//.css('width',w+"px")
		//.css('height',h+"px")
		.css('top','200px')
		.css('left','300px')
		.appendTo("body");
	}
	
})

function ShowStar(level,ids){
		   var htmlStr=""
			if(level==0){level=0}
			if (level>0){htmlStr+="<img src='images/starno.gif' border='0' style='cursor:pointer;margin-left:2px;' title='取消推荐' onclick='EditReco("+ids+",0)'/>"}
			for(i=1;i<=level;i++){
			htmlStr+= "<img src='images/star0.gif' border='0' style='cursor:pointer;margin-left:2px;'  title='推荐为"+i+"星级' id='star"+ids+"_"+i+"' onclick='EditReco("+ids+","+i+")' />"}
			for(j=level+1;j<=5;j++){
			htmlStr+= "<img src='images/star1.gif' border='0' style='cursor:pointer;margin-left:2px;'  title='推荐为"+j+"星级' id='star"+ids+"_"+j+"' onclick='EditReco("+ids+","+j+")' />"
	}
		   $("#S_Reco"+ids).html(htmlStr);
		   }
		   
function EditReco(ids,level){
  $.get(
	  "js/ajaxfun.php?id="+ids+"&types=0&level="+level+"&action=editreco", 
	  function(obj){
		  if(obj=='ok'){
			  ShowStar(level,ids);
		  }else{
			  $("#S_Reco"+ids).html("<font color='red'>Error!</font>");		
		  }
	  }
  );
} 

function ctopic(ids){
	var tid=$("#videotopicid").val();
	$.get( 
		  "js/ajaxfun.php?id="+ids+"&types=1&tid="+tid+"&action=topicsave", 
		  function(obj){
			  if(obj=='ok'){
				  $("#confirm").remove(); 
				  if(tid==0){
					  $("#ss"+ids).find("img").attr('src','images/topicno.gif');
				  }else{
				      $("#ss"+ids).find("img").attr('src','images/topicyes.gif');
				  }
			  }else{
				$("#ss"+ids).html("<font color='red'>Error!</font>");
				  }
			  	  
			  }
		  );
	}
	
function cremark(ids){
	var tid=$("#cremark").val();
	$.get( 
		  "js/ajaxfun.php?id="+ids+"&tid="+tid+"&action=remark", 
		  function(obj){
			  if(obj=='ok'){
				  $("#confirm").remove();
				  if(tid==""){
				  		$("#remark_"+ids).html("<font color='red'>[完结]</font>");
				  		}
				  else{
					  $("#remark_"+ids).html("<font color='red'>[连载至"+tid+"集]</font>");
					  }
			  }else{
					$("#remark_"+ids).html("<font color='red'>未知错误</font>");
				  }
			  	  
			  }
		  );
	}
	
function closet(){
	$("#confirm").remove();
	}
	
function appendplay(i,strs,type,server){
	var str=unescape(strs)
	var servers=unescape(server)
	var n=i-1
	var m=i+1
    source="<table border='0' cellpadding='0' cellspacing='0' width='100%'><tr ><td class='td_border' width='11%' height='35'>&nbsp;播放来源"+m+"：</td> <td width='89%' class='td_border'><select id='S_Source"+i+"' name='S_Source[]'><option value=''>暂无数据"+m+"</option>"+str+"</select>&nbsp;服务器组：<select id='S_Server"+i+"' name='S_Server[]'><option value='0'>服务器组</option>"+servers+"</select>&nbsp;</td></tr><tr><td width='11%' class='td_border'>&nbsp;数据地址"+m+"：<br/>&nbsp;&nbsp;<input type='button' value='校正' title='校正右侧文本框中的数据格式' class='rb1' name='reg' onclick='regulate2("+m+")' id='"+m+"'  /></td><td width='89%' align='left' class='td_border'><textarea id='S_Vdata"+m+"' name='S_Vdata[]' rows='8' cols='85'></textarea></td></tr><tr id='btn_addpaly"+i+"'><td colspan='2' class='td_border' height='30' >&nbsp;<img src='images/add.png'  id='btn_addplay' onclick=appendplay("+m+",\""+escape(str)+"\","+type+",\""+escape(server)+"\") value='添 加 地 址"+n+"' style='cursor:pointer' alt='添 加 地 址'>&nbsp;&nbsp;&nbsp;&nbsp;<img  src='images/cut.png' value='减 少 地 址' onclick='removeplay("+m+","+n+")' style='cursor:pointer' alt='减 少 地 址'></td></tr></table>"
	var playdiv=document.createElement("div");
		playdiv.id="ss"+m
		playdiv.innerHTML=source
	    $("#playarea").append(playdiv)
		$("#btn_addpaly"+n).css("display","none")

	}	
function removeplay(m,n){
	$('#ss'+m).remove();
	$("#btn_addpaly"+n).css("display","")
	}	
	
function regulate2(id){
		var content=$("#S_Vdata"+id).attr("value");
		if(content.length==0){jAlert('请填写影片地址!',"操作提示");return false;}
		if(navigator.userAgent.indexOf("Firefox")>0){var contentarr=content.split("\n");}else{contentarr=content.split("\r\n");}
		var newadd="";
		for(var i=0;i<contentarr.length;i++){
			if(contentarr[i].length>0){
				var videoadd=contentarr[i].split('$');
				if(videoadd.length==1){
					contentarr[i]='第'+(i+1)+'集$'+contentarr[i];
				}else if(videoadd.length==2){
					contentarr[i]='第'+(i+1)+'集$'+videoadd[1];
				}
			}
			newadd+=contentarr[i]+"\r\n";
		}
		$("#S_Vdata"+id).attr("value",newadd);
	}


function spango(id,url){
	$.post(''+url+'&id='+id,function (R){
		if(R=='ok'){
			$('#span'+id).remove();
			}				   
		 });
	}