/*
  $Id: wp.js,v 1.3 2009/05/11 06:24:55 florawang Exp $
  KKPlayer.html 的 js
*/
var is_safari = (navigator.userAgent && navigator.userAgent.indexOf("Safari") != -1);
var is_firefox = (navigator.userAgent && navigator.userAgent.indexOf("Firefox") != -1);
var is_ie = (navigator.userAgent && navigator.userAgent.indexOf("MSIE") != -1);

var wp_path = "/";
var wp_domain = ".kkbox.com.tw";
var wp_pno = "0_119";
var wp_pcomment = "wp_paybn";

function wp_addSong(act,content_id){
  var WP_SUSPEND = 0;
  if (WP_SUSPEND) {
    alert("親愛的用戶您好，KKBOX WebPlayer 目前暫停服務，請大家使用KKBOX軟體收聽好音樂。");
    return false;
  }

  var is_login = getCookieValue("USER_LOGIN");
  if(is_login != 1){
    web_login(location.pathname+location.hash,'');
  }else{

    var tmp = getCookieValue("WP_SONG_DATA");
    var pno = getCookieValue("pno");

    /*
    alert(tmp);
    if(tmp=="" || tmp ==null){
      tmp = tmp.replace(/%2C/ig,",");
      tmp = tmp.replace(/%26/ig,"&");
      var songValue = tmp + act +","+content_id+"&";//,"+album_more+","+artist_more+","+song_more+"&";
      alert(1);
    }
    else{
      alert(2);
      var songValue = act+","+content_id+"&";//,"+album_more+","+artist_more+","+song_more+"&";
    }
    */

    var songValue = act+","+content_id+"&";
 //   alert(songValue);

    userSetCookie("WP_SONG_DATA",songValue, 1000 * 60 * 10 ,wp_path, wp_domain, "");

    if(pno=='' || pno==null){
      userSetCookie("pno",wp_pno, 1000 * 60 * 60 * 24, wp_path, wp_domain, "");
      userSetCookie("pcomment",wp_pcomment, 1000 * 60 * 60 * 24,wp_path, wp_domain, "");
    }
    wp_open();
  }

}
//點了就開 WP
function wp_open2()
{
  window.open("/wp/KKPlayer.html",'KKPlayer','modal=yes,width=629,height=399,resizable=no,scrollbars=no');
  /*
  if(is_ie){
    window.showModalDialog("/wp/KKPlayer.html","KKPlayer","width=629,height=399,resizable=no,scrollbars=no");
  }else{
    window.open("/wp/KKPlayer.html",'KKPlayer','modal=yes,width=629,height=399,resizable=no,scrollbars=no');
  }*/
}
function wp_open()
{
  var is_addSong = getCookieValue("WP_SONG_DATA");
  var is_wp_open = getCookieValue("WP_STATUS");
  var add_tmpArr = new Array();
  var tmp = 0;
  var is_crash = 0;

/*
  if(is_safari){
    if(is_addSong ==null){is_addSong = ''; }
    if(is_wp_open ==null){is_wp_open = ''; }
  }else if(is_ie){
    if(is_addSong ==null){is_addSong = '';}
    if(is_wp_open ==null){is_wp_open = '';}
  }else if(is_firefox){
    if(is_addSong ==null){is_addSong = '';}
    if(is_wp_open ==null){is_wp_open = '';}
  }
  */

  if(is_addSong !='' || is_addSong !=null){
    add_tmpArr = is_addSong.split("%26");
    if(add_tmpArr.length>2){
      is_crash = 1;
    }
    if(is_crash==1 || is_wp_open =='' || is_wp_open==0 || is_wp_open ==null){
      wp_open2();
    }
  }
/*
  if(is_addSong !='' && (is_crash==1 || is_wp_open =='' || is_wp_open==0)) {
    // window.open("/wp/KKPlayer.html",'KKPlayer','width=629,height=399,resizable=no,scrollbars=no');
    wp_open2();
    //window.setTimeout('is_wp_filtered()',1000 * 5);
  }*/
}
function is_wp_filtered()
{
  var is_wp_open = getCookieValue("WP_STATUS");
  if(is_wp_open ==null || is_wp_open ==''){
    alert("Web Player若無法開啟,請設定允許開啟 KKBOX 網站的視窗");
  }
  return false;
  /*
  if(typeof(is_wp_open) == "undefined"){
    alert("Web Player若無法開啟,請設定允許開啟 KKBOX 網站的視窗");
  }*/
}


function userSetCookie( name, value, expires, path, domain,  secure ) {
  //例外處理swf中的WPID
  if(name=='WPID'){
    path = wp_path ;
    domain = wp_domain;
  }
  // set time, it's in milliseconds
  var today = new Date();
  today.setTime( today.getTime() );
  if ( expires )
  {
    expires = expires * 1000 * 60 * 60 * 24;
  }
  var expires_date = new Date( today.getTime() + (expires) );
  //alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

  document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}
function getCookieValue(check_name) {  //若沒有值,部份瀏覽器會回傳 undefined而不是 ' '
  var a_all_cookies = document.cookie.split( ';' );
  var a_temp_cookie = '';
  var cookie_name = '';
  var cookie_value = '';
  var b_cookie_found = false; // set boolean t/f default f

  for ( i = 0; i < a_all_cookies.length; i++ )
  {
    // now we'll split apart each name=value pair
    a_temp_cookie = a_all_cookies[i].split( '=' );

    // and trim left/right whitespace while we're at it
    cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
    // if the extracted name matches passed check_name
    if ( cookie_name == check_name )
    {
      b_cookie_found = true;
      // we need to handle case where cookie has no value but exists (no = sign, that is):
      if ( a_temp_cookie.length > 1 )
      {
	cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
      }
      // note that in cases where cookie is initialized but no value, null is returned
      return cookie_value;
      break;
    }
    a_temp_cookie = null;
    cookie_name = '';
  }
  if ( !b_cookie_found ){
    return cookie_value;
  }
}
function delCookieValue( name, path, domain ) {
  if ( getCookieValue( name ) ) document.cookie = name + "=" +
    ( ( path ) ? ";path=" + path : "") +
      ( ( domain ) ? ";domain=" + domain : "" ) +
      ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
function WP_open(){
  userSetCookie("WP_STATUS",1, 1000 * 60 * 60 * 24, wp_path, wp_domain,"");
}

function WP_close(){
  delCookieValue( "WP_SONG_DATA", wp_path, wp_domain );
  delCookieValue( "WP_STATUS", wp_path, wp_domain );
  return 'KKBOX網頁播放器也會關閉並停止播放歌曲。';
}

function set_focus()
{
  window.focus();
}

function set_status(txt)
{
  window.status=txt;
}

function set_title(str)
{
  document.title = str;
}

function sortNumber(a,b)
{
  return a - b;
}
function del_backup(del_idxStr)
{
  var backup_data = getCookieValue("WP_SONG_DATA_BK");
  var backup_Arr = new Array();
  var del_idxArr = new Array();
  var len;
  var new_backup;
  var delIdx;
/*
  if(is_safari){
    if(backup_data ==''){backup_data = ''; }
  }else if(is_ie){
    if(backup_data == null){  backup_data = ''; }
  }else if(is_firefox){
    if(backup_data ==''){backup_data = ''; }
  }
  */

  if( backup_data=='' || backup_data==null){//  if(typeof(backup_data)!="undefined")
    backup_data = backup_data.replace(/%2C/ig,",");
    backup_data = backup_data.replace(/%26/ig,"&");
    backup_Arr = backup_data.split("&");
    del_idxArr = del_idxStr.split(",");
    len = del_idxArr.length;
    del_idxArr = del_idxArr.sort(sortNumber);

    for(i=0;i<len;i++)
    {
      delIdx = del_idxArr[i]-i;
      backup_Arr.splice(delIdx,1);
    }

    new_backup = backup_Arr.join("&");

    //userSetCookie("WP_SONG_DATA_BK",new_backup,1000 * 60 * 60 * 24,".kkbox.com.tw","/");
    userSetCookie("WP_SONG_DATA_BK", new_backup, 1000 * 60 * 60 * 24, wp_path, wp_domain, "");

    return 1;
  }
}

function del_add_data()
{
  delCookieValue( "WP_SONG_DATA", wp_path, wp_domain );
}
function open_relay()
{
  window.open('http://www.kkbox.com.tw/relay.php', 'KKBOX', 'width=700,height=500,scrollbars,resizable',false);
}

function add2KKBOX(song_id)
{
  kkgo('kkbox://add_song_'+song_id+'/');
}
function state_info(type)
{
  /*keep this function for contingent use
  var pay_status,try_status,expire_status;
  switch(type)
  {
    case 'pay_user':
          pay_status = "block";
          try_status = "none";
          expire_status = "none";
          break;
        case 'try_user':
          pay_status = "none";
          try_status = "block";
          expire_status = "none";
          break;
        case 'expire_user':
          pay_status = "none";
          try_status = "none";
          expire_status = "block";
          break;
  }

  document.getElementById("pay_user").style.display = pay_status;
  document.getElementById("try_user").style.display = try_status;
  document.getElementById("expire_user").style.display = expire_status;
  document.getElementById("preview").style.display = "none";
  */
}

//for click不轉址,只做計數用
function set_click(ClickUrl){

  if(ClickUrl!=''){
    document.getElementById('func_frame').src = ClickUrl;
  }
}



