// -----------------------------------------
// version 3.0 2007-03-13 for testing
// 使用 ajax 機制加歌 與 userdata 技術將清單儲存.
// 播放或加入時...先call write_status_x , 寫入 cookie 值 cc =no_check, 如果player還在會把 cc 值改成 check (表示player還在)
// 
// -----------------------------------------
// 切字串                  
function substr(str, len) {
    if(!str || !len){
        return ''; 
    }
    var a = 0;
    var i = 0;
    var temp = '';
    for (i=0;i<str.length;i++){
        if (str.charCodeAt(i)>255){
            a+=2;
        }else{
            a++;
        }
        if(a>len){ 
            temp = temp + "...";
            return temp; 
        }
        temp += str.charAt(i);
    }
    return str;
}
function cut_cdn(x){
    ttmp = x.split("-");
    stream_path = "mms://music.media.hinet.net/UC_ezpeer/"+
                  ttmp[0] + "/" + ttmp[2] + "/" + ttmp[1] + "/" + "stm_" + x + ".wma";
    return stream_path;
}
//************************** prototype fix ***********************************************************************************
Array.prototype.remove=function(dx){
    if(isNaN(dx)||dx>this.length){
        return false;
    }
    for(var i=0,n=0;i<this.length;i++){
        if(this[i]!=this[dx]){
            this[n++]=this[i];
        }
    }
    this.length-=1
}
Array.prototype.baoremove = function(dx){
    total = dx.split(",");
    for ( x=0 ; x<= total.length ; x++){    
        this.splice(x,1);
    }
}
Array.prototype.indexOf = function(v){
    for(var i = this.length; i-- && this[i] !== v;);
    return i;
}
//******************************************************************************************************************************
var x2='';
var ezPlayer = null;
document.onkeydown=keyFunction;
function keyFunction(){
    if (event.keyCode==46){
        delete_single();
    }
    /*
    if (event.keyCode==81){
        alert(document.cookie);
    }
    */
}
function abcup_check_cycle(){
    abc_value = getCookie("webplayer");
    if (ezPlayer.closed == null){
        setCookie("webplayer","no");
    }
    setTimeout("abcup_check_cycle()", 3000);
}
function open_abc(){  //開啟撥放器  
    var expdate = new Date();
    expdate.setTime(expdate.getTime()+365*24*60*60*1000);
    ezPlayer = window.open('/web_player.htm','ezplayer','width=550,height=320');
    ezPlayer.defaultStatus="歡迎使用ezPeer+";
    ezPlayer.moveTo((screen.width/2)-275,(screen.height/2)-160);
    setCookie("playeropen","yes",expdate);
    //setCookie("playeropen","yes");
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    setCookie("webplayer","yes",now);
    now.setTime(now.getTime() + 1 * 60 * 60 * 1000);
    if(getCookie("extlink")){  //判斷是否為外部連結
        setCookie("extezlist","yes",now);
        deleteCookie("extlink");
    }    
}
function open_abc2(){
    var playeropen = getCookie("playeropen");      
    if(playeropen!="yes"){
        open_abc();
    }else{
        var now = new Date();
        fixDate(now);
        now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
        setCookie("add_focus","1",now);
    }
} 
function dcsLoadHref(evt){
    if ((typeof(gHref)!="undefined")&&(gHref.length>0)){
        window.location=gHref;
        gHref="";
    }
}
function aps(x,z){
    var gImages=new Array;
    if(z =='1'){
        var P="http://203.69.41.168/check/dcs.gif?action=" + x + "&web=js_click";
    }else{
        var P="http://203.69.41.168/check/dcs1.gif?action=webplayer&web=player_x";
    }
    switch(z){
        case "1":
            g_value = getCookie("check_g");
            if(g_value!='yes'){ 
                var gIndex=0;
                if (document.images){
                    gImages[gIndex]=new Image;
                    if((typeof(gHref)!="undefined")&&(gHref.length>0)){
                        gImages[gIndex].onload=gImages[gIndex].onerror=dcsLoadHref;
                    }
                    gImages[gIndex].src=P;
                    gIndex++;
                }else{
                    document.write('<IMG ALT="" BORDER="0" NAME="DCSIMG" WIDTH="1" HEIGHT="1" SRC="' + P + '">');
                }            
                var now = new Date();
                fixDate(now);
                now.setTime(now.getTime() + 1 * 24 * 60 * 60 * 1000);		 
                setCookie("check_g","yes",now);      
            }
            break;
        case "2":
            t_value = getCookie("check_t");
            if (t_value!='yes'){ 
                var gIndex=0;
                if (document.images){
                    gImages[gIndex]=new Image;
                    if((typeof(gHref)!="undefined")&&(gHref.length>0)){
                        gImages[gIndex].onload=gImages[gIndex].onerror=dcsLoadHref;
                    }
                    gImages[gIndex].src=P;
                    gIndex++;
                }else{
                    document.write('<IMG ALT="" BORDER="0" NAME="DCSIMG" WIDTH="1" HEIGHT="1" SRC="' + P + '">');
                }            
                var now = new Date();
                fixDate(now);
                now.setTime(now.getTime() + 1 * 24 * 60 * 60 * 1000);
                setCookie("check_t","yes",now);      
            }
            break;
    }
}
function wait(msecs){
    var start = new Date().getTime();
    var cur = start;
    while(cur-start<msecs){
        cur = new Date().getTime();	
    } 
} 
function check_abc(x,y){
  if(!document.all)
  {
    alert('使用網站聽歌，建議你使用 IE 瀏覽器！');
    return;
  }
  var playeropen = getCookie("playeropen");          
  if(playeropen!="yes"){
      open_abc();
  }
  abc_value = getCookie("webplayer");
  switch(x){
    case "group_add":
 	    group_add(y,"add");
        break;
    case "group_play":
 	    group_add(y,"playit");
	    break;
    case "add": // 頁面加歌!
	    single_add(y,"add");
	    break;
    case "add_play": // 頁面加歌並播歌!
        single_add(y,"playit");
    	break;
    case "all_add": // 頁面全部加歌
     	run_all(y,"add");
    	break;
    case "all_add_play": 
     	run_all(y,"playit");
    	break;
    case "board_all_add": // 排行榜加歌
	   run_all_board_play(y,"add");
	   break;
    case "board_all_add_play":
	   run_all_board_play(y,"playit");
	   break;
  }
}
function run_all_board_play(x1,x2){
    if (x2 == 'playit'){
        var now = new Date();
        fixDate(now);
        now.setTime(now.getTime() + 300 * 24 * 60 * 60 * 1000);
        setCookie("play_now","playit");
        x2 = "";
    }
    if (x2 == 'add'){
        setCookie("add_focus","1");
        x2 = "";
    }
    var all_id="";
    var ajax_data="";    
    var zipUrl="/api/getboard_sid.php";
    var zipPar="zip="+x1;
    var prototype; 
    var prototype = new Ajax.Request (zipUrl,
                    {method: 'post', parameters: zipPar, onComplete: function(response){
                                    songidinfo=response.responseText;                                    
                                    var now = new Date();
                                    fixDate(now);
                                    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
                                    setCookie("playlist1",songidinfo,now);    
                                    var now = new Date();
                                    fixDate(now);
                                    now.setTime(now.getTime() + 1 * 60 * 60 * 1000);
                                    setCookie("add_focus","1",now);
                                    //setCookie("play_first","0,"+t_value,now);
                                    setCookie("total_list","1",now);
                                    if(getCookie("extlink")){  //判斷是否為外部連結
                                        setCookie("extezlist","yes",now);
                                        deleteCookie("extlink");
                                    }                                    
                                    }});                         
}
function run_all(x1,x2,x3){
    if(x3 == undefined){
        x3=0;
    }
    if(x2 == 'playit'){
        var now = new Date();
        fixDate(now);
        now.setTime(now.getTime() + 300 * 24 * 60 * 60 * 1000);
        setCookie("play_now","playit"); 
        x2 = "";
    }
    if(x2 == 'add'){
        setCookie("add_focus","1");
        x2 = "";
    }
    var key_value = x3;
    var all_id="";
    var ajax_data="";
    var zipUrl="/api/playall.php";
    var zipPar="zip="+x1+"&zip2="+key_value;
    var prototype = new Ajax.Request (zipUrl,
    {method:'get',parameters:zipPar,onComplete:function(res){
        if(x2 == 'playit'){
            var now = new Date();
            fixDate(now);
            now.setTime(now.getTime() + 1 * 60 * 60 * 1000); 
            setCookie("play_now","playit",now);
            x2 = "";
        } 
        if(x2 == 'add'){
            setCookie("add_focus","1");
            x2 = "";
        }  
        all_id=res.responseText.split("!?!");
        t_value = all_id.length - 1; 
        old_count=0;
        now_count = all_id.length - 1;
        total_count = old_count -1 ; 
        if(now_count >= 20){ 
            countx = 20;
        }else{
            countx = now_count;
        }
        var now = new Date();
        fixDate(now);
        now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);        
        var now = new Date();
        fixDate(now);
        now.setTime(now.getTime() + 1 * 60 * 60 * 1000);
        setCookie("add_focus","1",now);
        //setCookie("play_first","0,"+t_value,now);
        setCookie("total_list","1",now);   
        setCookie("all_play",x1,now);  //記錄專輯id
        if(getCookie("extlink")){  //判斷是否為外部連結
            setCookie("extezlist","yes",now);
            deleteCookie("extlink");
        }
        }
        }
    );
}
function run_all2(x1,x2){
    if(x2 == 'playit'){
        var now = new Date();
        fixDate(now);
        now.setTime(now.getTime() + 300 * 24 * 60 * 60 * 1000);
        setCookie("play_now","playit");
        x2 = "";
    }
    if (x2 == 'add'){
        setCookie("add_focus","1");
        x2 = "";
    }
    var all_id="";
    var ajax_data="";
    var zipUrl="/api/playall.php";
    var zipPar="zip="+x1;
    var prototype; 
    var prototype = new Ajax.Request (zipUrl,
    {method: 'get', parameters: zipPar, onComplete: group_add_list_single}); 
}
function group_add(x1,x2){
    if (x2 == 'playit'){ 
        var now = new Date();
        fixDate(now);
        now.setTime(now.getTime() + 1 * 60 * 60 * 1000);
        setCookie("play_now","playit",now);
        x2 = "";
    }
    if (x2 == 'add'){
        setCookie("add_focus","1");
        x2 = "";
    }
    var songidinfo=x1.split("=");
    
    var all_id="";
    var ajax_data="";
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    setCookie("playlist1",songidinfo,now);        
    var now = new Date();
    fixDate(now);    
    now.setTime(now.getTime() + 1 * 60 * 60 * 1000);    
    setCookie("add_focus","1",now);
    //setCookie("play_first","0,"+t_value,now);
    setCookie("total_list","1",now);
    if(getCookie("extlink")){  //判斷是否為外部連結        
        setCookie("extezlist","yes",now);
        deleteCookie("extlink");
    }      
}
function group_add_list_single(res){    
    all_id=res.responseText.split("!?!");
    t_value = all_id.length - 1;
    wmalist = ""; 
    old_count=0;
    now_count = all_id.length - 1;
    total_count = old_count -1 ;
    if ( now_count >= 20 ){ 
        countx = 20;
    }else{
        countx = now_count;
    }
    for (i=0; i < countx; i++){
        all_id_name=all_id[i].split(";:;");
        play_name = "mms://music.media.hinet.net/UC_ezpeer/drm_" + all_id_name[1] +".wma*lrc=" + all_id_name[1] ;
        name = all_id_name[0];
        filename_temp = play_name;
        picx = all_id_name[2];
        mobilechk=all_id_name[3];
        file_id=filename_temp.split("wma*lrc=");
        wmalist =  wmalist + name + ";:;" + file_id[1] + ";:;" + picx + ";:;" + mobilechk + "!?!";
        total_count = total_count + 1;
    }
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    setCookie("playlist1",wmalist,now);    
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 1 * 60 * 60 * 1000);
    setCookie("add_focus","1",now);
    //setCookie("play_first","0,"+t_value,now);
    setCookie("total_list","1",now);
    if(getCookie("extlink")){  //判斷是否為外部連結
        setCookie("extezlist","yes",now);
        deleteCookie("extlink");
    }
}

function check_size(t){
    var x_array = new Array();    
    tmp = t.length;
    if(tmp > 1750){
        wmalist_m = t;
        tmp_x = tmp;
        while(tmp_x > 1750){ 
            x_array = wmalist_m.split("!?!");
            x_array.shift(); 
            wmalist_m = x_array.join("!?!");
            tmp_x = wmalist_m.length;
        } 
        return wmalist_m;
    }    
    return t;
}
function group_add_list(res){
    var wmalist = new Array();
    var q=0;
    var i=0;
    qx = getCookie("total_list");
    if(qx == null){
    q = 1;
    }else{ 
        q= parseInt(qx) + 1;
        for( u =1 ; u<=qx;u++){
            tt = "playlist" + u ;
            wmalist[u] = getCookie(tt);
        }
    }
    all_id=res.responseText.split("!?!");
    alert("==>"+all_id);
    t_num = all_id.length - 1;
    for(i=0; i<t_num; i++){
        all_id_name=all_id[i].split(";:;");
        play_name = "mms://music.media.hinet.net/UC_ezpeer/drm_" + all_id_name[1] +".wma*lrc=" + all_id_name[1] ;  
        name = all_id_name[0];
        filename_temp = play_name;
        picx = all_id_name[2];
        file_id=filename_temp.split("wma*lrc=");
        if( i != 0 ){  
            if (i % 30 == 0){
                q = parseInt(q) + 1;
            } 
        }
        if (wmalist[q] == undefined ){
            wmalist[q] = name + ";:;" + file_id[1] + ";:;" + picx + "!?!";
        }else{
            wmalist[q] = wmalist[q] + name + ";:;" + file_id[1] + ";:;" + picx + "!?!";
        }
    }    
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 300 * 24 * 60 * 60 * 1000);
    pp = "playlist1";
    wmalist = check_size(wmalist);
    setCookie(pp,wmalist[p],now);
    setCookie("total_list",q,now);
}
function single_add(s,x2){
    var all_id = new Array;
    if (x2 == 'playit'){ 
        var now = new Date();
        fixDate(now); 
        now.setTime(now.getTime() + 300 * 24 * 60 * 60 * 1000);
        setCookie("play_now","playit");
        x2 = "";
    }
    if (x2 == 'add'){
        setCookie("add_focus","1");
        x2 = "";
    }
    old_list = getCookie("playlist1"); 
    if (old_list == null ){
        wmalist = ""; 
        old_count = 0;
    }else{ 
        wmalist= old_list;
        orr=wmalist.split("!?!"); old_count = orr.length;
    }  
    now_count = all_id.length - 1;
    total_count = old_count -1 ;    
    all_id=s.split("!?!");
    t_value = all_id.length - 1;
    for(i=0; i < all_id.length-1; i++){
        all_id_name=all_id[i].split(";:;");
        play_name = "mms://music.media.hinet.net/UC_ezpeer/drm_" + all_id_name[1] +".wma*lrc=" + all_id_name[1] ;
        name = all_id_name[0];
        filename_temp = play_name;
        picx = all_id_name[2];
        file_id=filename_temp.split("wma*lrc=");
        wmalist =  wmalist + name + ";:;" + file_id[1] + ";:;" + picx + "!?!";
        total_count = total_count + 1;
    }
    if( total_count > 25 ){
        pp = "over";
        setCookie(pp,"yes");
        setCookie("play_now","");
    }else{
        var now = new Date();
        fixDate(now);
        now.setTime(now.getTime() + 300 * 24 * 60 * 60 * 1000);
        pp = "playlist1";
        setCookie(pp,wmalist,now);
    }
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 1 * 60 * 60 * 1000);	
    setCookie("add_focus","1",now);
    setCookie("play_first","0,"+t_value);
}
/*
function yf_wmp_list_delete(){
    var tag = "x";
    var del_tag = new Array;
    var ch = new Array;
    var p =0;
    now_pos = yf_wmp_current_index  ; // 目前播放位置
    now_pos_name = yf_wmp_file_list_name[now_pos];    
    orig_file_total = yf_wmp_file_list.length; // 陣列長度
    selindex = document.form1.yf_wmp_name_list.selectedIndex ;  // 游標停留處
    for(v=0;v<=document.form1.yf_wmp_name_list.options.length-1;v++){
        if (document.form1.yf_wmp_name_list.options[v].selected==true){ 
            if(yf_wmp_current_index == v ){
                tag=v;
            }
        }else{
            del_tag[p]=v; p=p+1;
        }
    }
    if( document.form1.yf_wmp_name_list.options.length == 1){
        if(tag == 0 ){
            delete_all();
        }
    }
    var o =0;     var q=0;      var new_list = ""; var ttag = new Array; var tttag=""; var n=0;
    origin = del_tag.length;
    check_play = del_tag.indexOf(yf_wmp_current_index) < 0;
    for(x=0;x<origin;x++){
        p = del_tag[x];
        spStr = yf_wmp_file_list[p].split("*");
        sid = spStr[1].split("=");
        new_list = new_list + yf_wmp_file_list_name[p]+";:;"+sid[1]+";:;"+yf_wmp_file_list_pic[p]+"!?!";
    }
    ch = new_list.split("!?!");
    for( i=0 ; i<= ch.length-1 ; i++){
        fu = ch[i].split(";:;");
        if(fu[0] == now_pos_name){
            ttag[n] = i;
            n++;
        }
    }
    if(ttag.length > 1 ){
        if(now_pos < ttag[1]){
            tttag = ttag[0];
        }else{
            tttag = ttag[1];
        }
    }else{
        tttag =ttag[0];
    }
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 300 * 24 * 60 * 60 * 1000);
    pp = "playlist1";
    wmalist ="";
    setCookie(pp,wmalist,now);
    setCookie("play_now","",now);
    yf_wmp_file_list.clear();
    yf_wmp_file_list_name.clear();
    yf_wmp_file_list_pic.clear();
    document.form1.yf_wmp_name_list.options.length = 0;
    var oPersist=document.form1.oPersistInput;
    oPersist.load("oXMLBranch");
    oPersist.removeAttribute("sPersist");
    restore_list(new_list,tttag);
    if ( tag !="x" ) { yf_wmp_next('del');}
}
*/
function save_list2() {
    var wmalist = new Array();
    var file_id = new Array();
    wmalist[1]="";
    var q=1;
    llist = yf_wmp_file_list_name.length-1;
    for(i=0;i<=llist;i++){
        name = yf_wmp_file_list_name[i];
        filename_temp = yf_wmp_file_list[i];
        picx = yf_wmp_file_list_pic[i];
        file_id=filename_temp.split("wma*lrc=");
        wmalist[1] =  wmalist[1] + name + ";:;" + file_id[1] + ";:;" + picx + "!?!";
    }
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 300 * 24 * 60 * 60 * 1000);
    for(p=1;p<=q;p++){
        pp = "playlist" + p ;
        setCookie(pp,wmalist[p],now);
    }
    setCookie("total",q,now);
}
