var playlist = document.getElementById('playlist')
var playlist1 = document.getElementById('playlist1')
var SongList = document.getElementById('SongList')
var curUrl;
var cookiestr;
var TotalSongs;
var gobalTotalSongs;
var mysonglenth=0;
var found=0;
var playerstop=0;
var cookiestr=document.cookie;
var fx= cookiestr.split("***");
var curBg=0;
if(fx.length>1)
 cookiestr=fx[1];
else
  cookiestr=""; 
function $(a){return document.getElementById(a)}
function setcursong()
  {
    if(cookiestr!='')
	   {
	   
		var mysongarray = cookiestr.split("###");
		
        mysonglenth=mysongarray.length-2;
		
		
		}
		else
		 mysonglenth=0;
   }

function Savelist()
	{
	// alert(cookiestr);
	// document.cookie= "***"+cookiestr+"***; expires=Wednesday, 27-April-10 00:00:00 GMT;"
	// alert('歌曲列表保存成功！');
	 }

//插入歌曲列表入口函数

function Insertsong(songstr){
    
	 var tstr;
	 tstr = songstr.split("|");
	 if(tstr.length >= 5){
		 if (cookiestr == null)
			 cookiestr = songstr;
		 else
			{
			  var index1=cookiestr.indexOf(songstr);
			 
			  if(index1==-1) { cookiestr = cookiestr + "###" + songstr;} 
       			  if(found==0) {
			  var mysongarray = cookiestr.split("###");
			 
			 for(i = 0;i < mysongarray.length;i++)
				   if(mysongarray[i]==songstr) { mysonglenth=i-1;found=1;break;}}

				  
			 }
	 }
}
//
function clearmybox()
  {
    cookiestr='';
	InitList();
	DispList();
  }
//容错代码
//function killerror() {return true;}
//window.onerror = killerror;

//初始化歌曲列表过程
function InitList(){
	 var songarray,htmlstr,i,j,tstr,ttstr,selectedindex;
	 htmlstr = "<select  size=\"5\" name=\"SongList\" style=\"width:100%; height:100px;border: 1px solid #333333;display:none \" OnDblClick=\"plays();\">";
	 songarray = cookiestr.split("###");
	 TotalSongs = 0;
	 for(i = 0;i < songarray.length;i++){
		tstr = songarray[i].split("|");
		if(tstr.length >= 5){
			 TotalSongs += 1;
			 htmlstr += "<option value=\"" + songarray[i] + "\">";
			 ttstr = TotalSongs + "." + tstr[1] + "**" + tstr[3];
			 if(ttstr.length < 20){
				 ttstr = ttstr.replace("**"," - ");
			 }else{
				 ttstr = ttstr.replace("**"," - ");
				 ttstr = ttstr.substr(0,20) + "...";
			 }
			 htmlstr += ttstr + "</option>;";
		}
	 }
	 htmlstr += "</select>";
	// alert(htmlstr)
	 playlist.innerHTML = htmlstr;
}
function DispList(){
	 var songarray,htmlstr,i,j,tstr,ttstr,selectedindex;
	 htmlstr = '<table width="100%" border="0" cellpadding="0" cellspacing="0" class="bfdet" id="cpy">';
	 songarray = cookiestr.split("###");
	 TotalSongs = 0;
	 curBg=0;
	 for(i = 0;i < songarray.length;i++){
		tstr = songarray[i].split("|");
		if(tstr.length >= 5){
			bg=''
			/*
			if( $('ptitle').innerHTML=='正在播放：'+tstr[1] )
			{
				bg ='bgColor="#ffffcc"'
				curBg=TotalSongs;
			}
			*/
			TotalSongs += 1;
			gobalTotalSongs=(pageno-1)*vps+TotalSongs;
			htmlstr += '<tr id="songtb'+TotalSongs+'" '+bg+'><td width="30">'+gobalTotalSongs+'</td><td width="110" align="left"><a id="'+tstr[0]+'" href="javascript:playran2(\'songtb'+TotalSongs+'\');"  class="cBlue3" title="'+tstr[1]+'">'+subs(tstr[1],17)+'</a></td><td width="110"><a href="javascript:;" onclick="singer(\''+tstr[3]+'\');return false;" class="cBlue3">'+subs(tstr[3],10)+'</a></td><td width="30"><img src="img/sc.gif" width="12" height="12" onclick="favm('+tstr[4]+')"/></td><td width="30"><img src="img/del_ico.gif" width="12" height="12" onclick="delc('+tstr[4]+')" /></td></tr>'
		}
	 }
	 htmlstr +="</table>";
	o('playlist1').innerHTML = htmlstr;
	 //alert(htmlstr);
}

//播放歌曲
function plays(){
	try{
	if($('SongList').selectedIndex < 0)
	{
		return 0;
		 alert('请选择你要播放的曲目!');
	}
	 else{
		var varvar,person,collect,song,url;
		varvar = $('SongList').options[document.getElementById('SongList').selectedIndex].value.split("|");
		url = varvar[0];song = varvar[1];collect = varvar[2];person = varvar[3];SongID = varvar[4];		
		$('Exobud').FileName=url
		Exobud.play();
		v_play_num = (document.getElementById('SongList').selectedIndex+1)
		var obj=eval("songtb"+v_play_num);
		obj.bgColor='#ffffcc';
		$('ptitle').innerHTML='正在播放：'+song
		v_play_id=varvar[4];
		//$('Exobud').url = serverurl+url//+'?'+'<%=AuthCode%>';
		playerstop=1;
		showTLab();
		return;
	 }
	}catch(e){;}
}

//控制按钮 上一首
function Last_OneOK(){
	 if(($('SongList').selectedIndex > 0) && ($('SongList').selectedIndex < TotalSongs)){
		$('SongList').options[$('SongList').selectedIndex - 1].selected = true;
		plays();
	 }
}

//控制按钮 下一首
function Next_OneOK(){
	 if(($('SongList').selectedIndex >= 0) && ($('SongList').selectedIndex < TotalSongs - 1)){
		scur = v_play_num?v_play_num-1:$('SongList').selectedIndex
		try{$('SongList').options[scur + 1].selected = true;}
		catch(e){
		 	goprepage(pageno+1);
			$('SongList').options[0].selected = true;
			plays();
		}
	 plays();
	 }
	 else
	  {
	    
		goprepage(pageno+1);
		try{$('SongList').options[0].selected = true;}catch(e){;}
	 	plays();
	  }
}
var nexti=0;
function showTLab()
{
	if(document.Exobud.playState == 0 && playerstop==1)
	{
		if(nexti>30) return
		var b = $('cpy');
		var as = b.getElementsByTagName('TR');
		nexti++;
		for(i=0;i<as.length;i++)if( as[i].bgColor!='' ) as[i].bgColor=''
	  // vpl = ($('SongList').selectedIndex+1)
		//var obj=eval("songtb"+vpl)
		//obj.bgColor=''
		Next_OneOK()
	}
	setTimeout("showTLab()",1000)
}

function playran(ob)
{
 
 if(ob.checked==true)
  {ob.checked=false;
   //alert($('SongList').selectedIndex);
   var obj=eval("songtb"+($('SongList').selectedIndex+1));
   obj.bgColor=''
   $('SongList').options[ob.value-1].selected = true;
  // alert($('SongList').options[ob.value-1].text)
   plays();
  }
}

function playran2(ob)
{
   var idstr=ob;
   idstr = idstr.substr(6,2);
   idstr = idstr.replace('/','');
   vpl = ($('SongList').selectedIndex+1)
   var obj=eval("songtb"+vpl);
   obj.bgColor=''
  // alert(v_play_num)
   if( curBg )
	{
	   $('songtb'+(curBg+1)).style.background=''
	}
//	alert(idstr-1)
   $('SongList').options[idstr-1].selected = true;
   plays();
}


document.writeln("<script language=vbscript>");
document.writeln("Function bytes2BSTR(vIn)");
document.writeln("dim i");
document.writeln("strReturn = \"\"");
document.writeln("For i = 1 To LenB(vIn)");
document.writeln("ThisCharCode = AscB(MidB(vIn,i,1))");
document.writeln("If ThisCharCode < &H80 Then");
document.writeln("strReturn = strReturn & Chr(ThisCharCode)");
document.writeln("Else");
document.writeln("NextCharCode = AscB(MidB(vIn,i+1,1))");
document.writeln("strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))");
document.writeln("i = i + 1");
document.writeln("End If");
document.writeln("Next");
document.writeln("bytes2BSTR = strReturn");
document.writeln("End Function");
document.writeln("<\/script>");



 function songfavadd(ss)
 {
    ajaxsongadd(ss);
 
 }
 
 function ajaxsongadd(tm) {
		var url = '/sAction/m_box_add.asp?time='+ Math.random()+ '&songid='+tm;
		/*
		0： 表示没有登陆。
        1： 表示你的音乐盒已超出系统分配的最大数！
        2： 表示此歌曲已存在！
		3:  参数不正确！
		*/
	   var oHttpReq1 = myxmlhttp();
	  
       oHttpReq1.open("get", url, false);
       oHttpReq1.send("");
       var str=bytes2BSTR(oHttpReq1.responseBody);
	   
      if( str == "0" ) {
        alert("请先登陆后再收藏歌曲");
		playlist1.style.display='none'; 
        document.getElementById("loginpage").style.display="block";
		document.getElementById("loginpage").src='/sAction/login.asp';
        }
	  else if( str == "1" ) {
		
         alert("你的音乐盒已超出系统分配的最大数！");}
	
	  else if( str == "2" ) {
		
        alert("你已经收藏此歌曲！");}
	
	  else if(str=="3") {alert("参数不正确！"); }
	   
        
	  else {  alert("收藏成功！");}
	   
       
}

String.prototype.len=function() 
{return this.replace(/[^\x00-\xff]/g,"rr").length; } 