var ajax = new AJAX();ajax.setcharset("GBK");

function openUpdateWin(width, height,str){
	openWindow2(101,width,height,50)
	var msgDiv=$("msg")
	var bgDiv=$("bg")
	var iWidth = document.documentElement.scrollWidth
	var str="<div style='width:400px;'><div class='divboxtitle'><span onclick=\"closeWin();set($('update'),'<font color=red >版本暂未升级...</font>');\" ><img src='../images/btn_close.gif'/></span>MaxCms2.0在线升级</div><div  class='divboxbody'>"+str+"<font color='red'><br>检测到更新文件(如有修改，请务必做好备份)</font><br><input type='button' value='升   级' id='openwin' class='btn'/>&nbsp;<input id='closewin' type='button' value='取   消' name='button' class='btn'  /></div><div class='divboxbottom'>Power By Maxcms2.0</div></div>"
	msgDiv.style.cssText += "FONT-SIZE: 12px;top:100px;left:"+(iWidth-width)/2+"px;text-align:center;";
	set(msgDiv,str)
	$("closewin").onclick = function(){
		closeWin()
		set($("update"),"<font color='red'>版本暂未升级...</font>");
	}
	$("openwin").onclick = function(){
		closeWin()
		submitUpdate(1);
	}
}

function updateCallBack(obj){
	var str = "\n<font color='red'>当前已是最新版本!</font>"
	set($("update"),obj.responseText + str);
}

function submitUpdate(type){
	set($("update"),"<font color='red'>请不要刷新页面，正在升级中...</font>");
	var getUrl = "admin_update.asp?action=all&type="+type;
	ajax.get(
		getUrl,
		updateCallBack
	);	
}

function checkNewVersionCallBack(obj){
	if (obj.responseText == "False"){
		set($("update"),"<font color='red'>当前已是最新版本!</font>");
	}else{
		openUpdateWin(400, "auto",obj.responseText)	
	}
}

function checkNewVersion(){
	set($("update"),"<font color='red'>请稍等，正在检测新版本...</font>");
	var getUrl = "admin_update.asp?action=isNew";
	ajax.get(
		getUrl,
		checkNewVersionCallBack
	);	
}

function checkRepeat(){
	ajax.get(
		"admin_ajax.asp?action=checkrepeat&m_name="+$('m_name').value,
		function(obj){
			if (obj.responseText == "ok"){set($("m_name_ok"),"<img src='images/yes.gif' border=0></img>");}else{set($("m_name_ok"),"<img src='images/no.gif' border=0></img>");}
		}
	);	
}

function setVideoTopic(videoId,topicId){
	openWindow2(101,230,20,0)
	var msgDiv=$("msg")
	var topicTDObj = $("topic"+videoId);
	var topicTDTop = topicTDObj.offsetTop;
    var topicTDLeft = topicTDObj.offsetLeft; 
    while (topicTDObj = topicTDObj.offsetParent){topicTDTop+=topicTDObj.offsetTop; topicTDLeft+=topicTDObj.offsetLeft;}
    msgDiv.style.cssText+="border:1px solid #55BBFF;background: #C1E7FF;padding:3px 0px 3px 4px;"
	msgDiv.style.top = (topicTDTop-1)+"px";
    msgDiv.style.left = (topicTDLeft-1)+"px"; 
	msgDiv.innerHTML="正在加载内容....";	
	ajax.get(
		"admin_ajax.asp?id="+videoId+"&action=select&topicid="+topicId, 
		function(obj) {
			msgDiv.innerHTML=obj.responseText;
		}
	);
}

function setVideoState(videoId){
	openWindow2(101,250,20,0)
	var msgDiv=$("msg")
	var topicTDObj = $("state"+videoId);
	var topicTDTop = topicTDObj.offsetTop;
    var topicTDLeft = topicTDObj.offsetLeft; 
    while (topicTDObj = topicTDObj.offsetParent){topicTDTop+=topicTDObj.offsetTop; topicTDLeft+=topicTDObj.offsetLeft;}
    msgDiv.style.cssText+="border:1px solid #55BBFF;background: #C1E7FF;padding:3px 0px 3px 4px;"
	msgDiv.style.top = (topicTDTop-1)+"px";
    msgDiv.style.left = (topicTDLeft-1)+"px"; 
	msgDiv.innerHTML="状态：连载到第<input type='text' size='5' id='series' name='series'>集<input type='button' value='确定' onclick='submitVideoState("+videoId+")' class='btn' /><input type='button' value='取消' onclick='closeWin()' class='btn' />";	
}

function submitVideoTopic(videoId){
	var topic = $("topicselect").value
	if (topic.length==0) {
		alert('请选择专题')
		return false
	}
	ajax.get(
		"admin_ajax.asp?id="+videoId+"&topic="+topic+"&action=submittopic", 
		function(obj) {
			if(obj.responseText == "submitok"){
				set($("topic"+videoId),"<font color='red'>"+$("topicselect").options[$("topicselect").selectedIndex].text+"</font>");
				closeWin();
			}else{
				set($("topic"+videoId),"<font color='red'>发生错误</font>");		
			}
		}
	);
}

function submitVideoState(videoId){
	var state = $("series").value
	if (isNaN(state)) {
		alert('集数为数字')
		return false
	}else if(state == 'undefined' || state == ''){
		alert('集数不能为空')
		return false
	}
	ajax.get(
		"admin_ajax.asp?id="+videoId+"&state="+state+"&action=submitstate", 
		function(obj) {
			if(obj.responseText == "submitok"){
				set($("state"+videoId),"<font color='red'>(连载到"+state+"集)</font>");
				closeWin();
			}else{
				set($("state"+videoId),"<font color='red'>发生错误</font>");		
			}
		}
	);
}

function selectPicLink(selectObj,str){
	var selectValue=selectObj.options[selectObj.selectedIndex].value
	if 	(selectValue==str)
		$("tr_m_pic").style.display=""
	else 
		$("tr_m_pic").style.display="none"
}

function openAdWin(divid,path,url){
	$(divid).style.display="block";
	selfLabelWindefault(divid);	
	$("adpath").value='<script type=\"text/javascript\" language=\"javascript" src=\"'+url+path.replace("../","")+'\"></script>';	
}

function openHtmlToJsWin(divid){
	$(divid).style.display="block";
	selfLabelWindefault(divid);	
}

function insertHtmlToJsWin(divid,divid2,divid3){
	hide(divid);
	$(divid2).value=$(divid3).value
}

function openSelfLabelWin(divid,id){
	$(divid).style.display="block";
	selfLabelWindefault(divid);	
	set($("labelcontent"),"代码加载中...");	
	ajax.get(
		"admin_ajax.asp?id="+id+"&action=getselflabel", 
		function(obj) {
			if(obj.responseText == "err"){
				set($("labelcontent"),"发生错误");	
			}else{
				set($("labelcontent"),obj.responseText);
			}
		}
	);
}

function selfLabelWindefault(divid){	
	$(divid).style.left=(document.documentElement.clientWidth-568)/2+"px"
	$(divid).style.top=(getScroll()+60)+"px"
}

function viewCurrentAdTr(id){
	var adtrObj=getElementsByName("tr","adtr")
	var n=adtrObj.length
	for (var i=0;i<=n-1;i++){
		adtrObj[i].className="";
	}
	$("adtr"+id).className="editlast";
}

function isExistUsername(id){
	var username=$("m_username").value
	if (username.length == 0){
		set($("checkmanagername"),"管理员名称不能为空");
		return false; 
	}
	ajax.get(
		"admin_ajax.asp?username="+username+"&action=checkuser&id="+id, 
		function(obj) {
			var value = obj.responseText
			if(value == "no"){
				set($("checkmanagername"),"发生错误");	
			}else{
				if (value == "1")
					set($("checkmanagername"),"已经存在此管理员，请更换名称");	
				else if (value == "0")
					set($("checkmanagername"),"恭喜，改名称可用");	
			}
		}
	);
}

function selectDataBase(value){
	if (value==0){view("acc");for (var i=1;i<=4;i++){hide("mssql"+i);}}
	if (value==1){hide("acc");for (var i=1;i<=4;i++){view("mssql"+i);}}
}

function selectCache(value){
	if (value==1){view("cacheset");}
	if (value==0){hide("cacheset");}
}

function selectAlertWin(value){
	if (value==1){view("alertwinset");}
	if (value==0){hide("alertwinset");}
}

function selectWaterMark(value){
	if (value==1){view("watermarkfont");}
	if (value==0){hide("watermarkfont");}
}

function selecRunMode(value){
	if (value=='dynamic'){hide("static");view("dynamic");hide('ismakeplaytr');}
	if (value=='static'){view("static");hide("dynamic");view('ismakeplaytr');}
}

function  starView(level,vid){
	var i,j,htmlStr;
	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='commendVideo("+vid+",0)'/>"}
	for (i=1;i<=level;i++){
		htmlStr+= "<img src='images/star0.gif' border='0' style='cursor:pointer;margin-left:2px;' onclick='commendVideo("+vid+","+i+")' title='推荐为"+i+"星级' id='star"+vid+"_"+i+"'  />"
	}
	for(j=level+1;j<=5;j++){
		htmlStr+= "<img src='images/star1.gif' border='0' style='cursor:pointer;margin-left:2px;' onclick='commendVideo("+vid+","+j+")' title='推荐为"+j+"星级' id='star"+vid+"_"+j+"' />"
	}
	set($('star'+vid),htmlStr)
}

function selecMakeMode(value){
	if (value=='dir1'){hide("dir2");view("dir1");}
	if (value=='dir2'){view("dir2");hide("dir1");}
}

function commendVideo(vid,commendid){
	ajax.get(
		"admin_ajax.asp?id="+vid+"&commendid="+commendid+"&action=commend", 
		function(obj){
			if(obj.responseText == "submitok"){
				starView(commendid,vid);
			}else{
				set($("star"+vid),"<font color='red'>发生错误</font>");		
			}
		}
	);
}

function viewCurrentTopicTr(id){
	var topictrObj=getElementsByName("tr","topictr")
	var n=topictrObj.length
	for (var i=0;i<=n-1;i++){
		topictrObj[i].style.background="#ffffff";	
	}
	$("topictr"+id).style.background="#E7E7E7";
}

function openTopicDesWin(divid,id){
	selfLabelWindefault(divid);	
	view(divid)
	$("f_m_id").value=id
	set($("f_m_des"),"加载中...");	
	ajax.get(
		"admin_ajax.asp?id="+id+"&action=gettopicdes", 
		function(obj) {
			if(obj.responseText == "err"){
				set($("f_m_des"),"发生错误");	
			}else{
				set($("f_m_des"),obj.responseText);
			}
		}
	);
}

function submitTopicDes(divid){
	ajax.postf(
		$("f_des"),
		function(obj){if(obj.responseText=="ok"){hide(divid);alert('修改成功');}else{alert('发生错误');}}
	);
}

function gather(){
	var url=$("gatherurl").value
	if(url.length == 0) 
		return false
	else{
		view("loading")
		ajax.get(
			"admin_webgather.asp?url="+url+"&action=gather", 
			function(obj) {
				if(obj.responseText == "err"){
					$("gathercontent").value = "发生错误";	
				}else{
					$("gathercontent").value = obj.responseText;
				}
				hide("loading")
			}
		);
	}
}

function insertResult(i){
	var id=$("areaid").value
	$("m_playurl"+id).value = $("gathercontent").value
	$("gatherurl").value=''
	$("gathercontent").value=''
	$("areaid").value=i;
	hide('gathervideo')
}

function getReferedId(str){
	if(str.indexOf('土豆高清')>-1) return "hd_tudou"
	if(str.indexOf('新浪高清')>-1) return "hd_iask"
	if(str.indexOf('搜狐高清')>-1) return "hd_sohu"
	if(str.indexOf('天线高清')>-1) return "hd_openv"
	if(str.indexOf('56高清')>-1) return "hd_56"
	if(str.indexOf('56')>-1) return "56"
	if(str.indexOf('优酷')>-1) return "youku"
	if(str.indexOf('土豆')>-1) return "tudou"
	if(str.indexOf('搜狐')>-1) return "sohu"
	if(str.indexOf('新浪')>-1) return "iask"
	if(str.indexOf('六间房')>-1) return "6rooms"
	if(str.indexOf('qq')>-1) return "qq"
	if(str.indexOf('youtube')>-1) return "youtube"
	if(str.indexOf('17173')>-1) return "17173"
	if(str.indexOf('ku6视频')>-1) return "ku6"
	if(str.indexOf('FLV')>-1) return "flv"
	if(str.indexOf('SWF')>-1) return "swf"
	if(str.indexOf('real')>-1) return "real"
	if(str.indexOf('media')>-1) return "media"
	if(str.indexOf('qvod')>-1) return "qvod"
	if(str.indexOf('ppstream')>-1) return "pps"
	if(str.indexOf('迅播高清')>-1) return "gvod"
	if(str.indexOf('远古高清')>-1) return "wp2008"
	return ""
}

function repairUrl(i){
	var urlStr,urlArray,newStr,j,flagCount,fromText
	fromText=$("m_playfrom"+i).options[$("m_playfrom"+i).selectedIndex].value
	if (fromText.length==0){alert('请选择播放器类型');return false;}
	urlStr=$('m_playurl'+i).value
	if (urlStr.length==0){alert('请填写地址');return false;}
	if(navigator.userAgent.indexOf("Firefox")>0){urlArray=urlStr.split("\n");}else{urlArray=urlStr.split("\r\n");}
	newStr="";
	for(j=0;j<urlArray.length;j++){
		if(urlArray[j].length>0){
			flagCount=urlArray[j].split('$').length-1
			switch(flagCount){
				case 0:
					urlArray[j]='第'+(j+1)+'集$'+urlArray[j]+'$'+getReferedId(fromText)
					break;
				case 1:
					urlArray[j]=urlArray[j]+'$'+getReferedId(fromText)
					break;
				case 2:
					break;
			}
			newStr+=urlArray[j]+"\r\n";
		}
	}
	$('m_playurl'+i).value=trimOuterStr(newStr,"\r\n");
}

function repairUrl2(i){
	var urlStr,urlArray,newStr,j,flagCount,fromText
	fromText=$("m_downfrom"+i).options[$("m_downfrom"+i).selectedIndex].value
	if (fromText.length==0){alert('请选择下载类型');return false;}
	urlStr=$('m_downurl'+i).value
	if (urlStr.length==0){alert('请填写地址');return false;}
	if(navigator.userAgent.indexOf("Firefox")>0){urlArray=urlStr.split("\n");}else{urlArray=urlStr.split("\r\n");}
	newStr="";
	for(j=0;j<urlArray.length;j++){
		if(urlArray[j].length>0){
			flagCount=urlArray[j].split('$').length-1
			switch(flagCount){
				case 0:
					urlArray[j]='第'+(j+1)+'集$'+urlArray[j]+'$down'
					break;
				case 1:
					urlArray[j]=urlArray[j]+'$down'
					break;
				case 2:
					break;
			}
			newStr+=urlArray[j]+"\r\n";
		}
	}
	$('m_downurl'+i).value=trimOuterStr(newStr,"\r\n");
}

function expendPlayArea(i,optionStr,type){
	optionStr=unescape(optionStr)
	var n=i-1,m=i+1
	var sparkStr=(type==1)?"&nbsp;&nbsp;<font color='red'>＊</font>":""
	var sparkStr2=(type==1)?"<font color='blue'>数据地址单集格式： <font color='red'>标题$ID$来源</font>(如果多集就用行隔开)</font>":""
	var area="<table width='100%'><tr class='noborder-tb'><td align='right' height='22' width='70'>播放来源"+i+"：</td><td><select id='m_playfrom"+i+"' name='m_playfrom'><option value=''>暂无数据"+i+"</option>"+optionStr+"</select><input type='button'   class='btn' value='WEB采集' onclick='viewGatherWin("+i+");selectTogg();'/>"+sparkStr+sparkStr2+"</td></tr><tr><td align='right' height='22'>数据地址"+i+"：<br/><input type='button' value='校正' title='校正右侧文本框中的数据格式' class='btn'  onclick='repairUrl("+i+")'/></td><td><textarea id='m_playurl"+i+"' name='m_playurl' rows='8' cols='100'></textarea>"+sparkStr+"</td></tr><tr id='playexpend"+i+"'><td colspan='2'><img onclick=expendPlayArea("+m+",\""+escape(optionStr)+"\","+type+") src='images/btn_add.gif' class='pointer' ></img></td></tr></table>"
	var _nextdiv=document.createElement("div");
	_nextdiv.innerHTML=area
	$('m_playarea').appendChild(_nextdiv)
	if (i>type) 
		hide("playexpend"+n)
}

function expendDownArea(i,optionStr,type){
	optionStr=unescape(optionStr)
	var n=i-1,m=i+1
	var sparkStr=(type==1)?"&nbsp;&nbsp;<font color='blue'>下载地址单集格式：<font color='red'>下载名称$下载地址$down</font>(如果多集就用行隔开)</font>":""
	var area="<table width='100%'><tr><td align='right' height='22' width='70'>下载来源"+i+"：</td><td><select id='m_downfrom"+i+"' name='m_downfrom'><option value=''>暂无数据"+i+"</option>"+optionStr+"</select>"+sparkStr+"</td></tr><tr><td align='right' height='22'>下载地址"+i+"：<br/><input type='button' value='校正' title='校正右侧文本框中的下载格式' class='btn'  onclick='repairUrl2("+i+")'/></td><td><textarea id='m_downurl"+i+"' name='m_downurl' rows='8' cols='100'></textarea></td></tr><tr id='downexpend"+i+"'><td colspan='2'><img onclick=expendDownArea("+m+",\""+escape(optionStr)+"\","+type+") src='images/btn_add.gif' class='pointer' ></img></td></tr></table>";
	var _nextdiv=document.createElement("div");
	_nextdiv.innerHTML=area
	$('m_downarea').appendChild(_nextdiv)
	if (i>type) 
		hide("downexpend"+n)
}

function expendPlayerSkin(i){
	var str="皮肤"+(i+1)+" :背景颜色 #<input type='text' name='playerbgcolor'  alt='clrDlg0"+i+"' /> 文字颜色 #<input type='text' name='playerfontcolor'  alt='clrDlg1"+i+"' /><br><div id='addplayerskin"+i+"'><img  src='images/btn_add.gif' onclick='expendPlayerSkin("+(i+1)+")'  style='cursor:pointer'/></div>";
	var _nextdiv=document.createElement("div")
	set(_nextdiv,str)
	$('playerskindiv').appendChild(_nextdiv)
	if(i>0){hide("addplayerskin"+(i-1))}
}

function viewGatherWin(i){
	$('gathervideo').style.display='block';$("areaid").value=i;selfLabelWindefault('gathervideo');
}

function viewUnionArea(){
	selfLabelWindefault("unionarea");	
	view("unionarea")
	set($("unionids"),"正在加载中......")
	ajax.get(
			"admin_ajax.asp?action=getunionid", 
			function(obj) {
				if(obj.responseText == "err"){
					set($("unionids"),"发生错误......")
				}else{
					set($("unionids"),obj.responseText)
				}
			}
		);
}

function alertUpdatePic(){
	ajax.get(
		"admin_ajax.asp?action=updatepic", 
		function(obj) {
			if(obj.responseText > 0){
				view('updatepic');set($("updatepicnum"),obj.responseText)
			}
		}
	);
	floatBtttom("updatepic",500);
	}

function floatBtttom(id,retime) {
	$(id).style.top = (document.documentElement.scrollTop-0+document.documentElement.clientHeight-$(id).clientHeight)+"px";
	timer = setTimeout("floatBtttom('"+id+"',"+retime+");",retime);
}

function isViewState(){
	if ($("m_statebox").checked){$("m_statespan").style.display="inline";}	else{hide("m_statespan");$("m_state").value='';}
}

function clearCache(){
	set($("upcacheresult"),'加载中...')
	ajax.get(
		"admin_ajax.asp?action=restartcache", 
		function(obj) {
			if(obj.responseText == 'ok'){
				set($("upcacheresult"),'缓存更新成功')
			}else{
				set($("upcacheresult"),'缓存更新失败')
			}
		}
	);	
}

function htmlToJs(htmlinput,jsinput){
	$(jsinput).value="document.writeln(\""+$(htmlinput).value.replace(/\\/g,"\\\\").replace(/\//g,"\\/").replace(/\'/g,"\\\'").replace(/\"/g,"\\\"").split('\r\n').join("\");\ndocument.writeln(\"")+"\")"
} 

function trimOuterStr(str,outerstr){
	var len1
	len1=outerstr.length;
	if(str.substr(0,len1)==outerstr){str=str.substr(len1)}
	if(str.substr(str.length-len1)==outerstr){str=str.substr(0,str.length-len1)}
	return str
}

function reverseOrder(){
	if($('gathercontent').value==""){alert("没有地址内容");return;}
	if(navigator.userAgent.indexOf("Firefox")>0){var listArray=$('gathercontent').value.split("\n");}else{var listArray=$('gathercontent').value.split("\r\n");}
	var newStr="";
	for(var i=listArray.length-1;i>=0;i--){
			newStr+=listArray[i]+"\r\n";
	}
	$('gathercontent').value=trimOuterStr(newStr,"\r\n");
}

function replaceStr(){
	var contentObj=$('gathercontent'),str="gi";
	if(contentObj.value==""){alert("没有地址内容");return;}
	var replace1=$("replace1").value,replace2=$("replace2").value;
	var content=contentObj.value;
	var reg=new RegExp(replace1,str);
	contentObj.value=content.replace(reg,replace2);	
}