/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//reproduce una lista de reproduccion
function playSong(current)
{

    total_grid = gridObj.getGridParam('records');
    
    //reproduce las canciones
    if(total_grid > 0){

        //no hay ninguna lista en reproduccion
        if(lastSel < 0){
            playVideo(gridObj.getCell(1,"vid"));
            gridObj.setSelection("1");
            jQuery("#scroller").html(gridObj.getCell(1,"name"));
            $jScroller.start();
            lastSel      = 1;
            current_song = 1;
        }else{
            //pasa a la siguiente cancion
            curr = gridObj.getGridParam('selrow');
            next = (parseInt(curr) + 1);
            current_song = (current > 0 )?current: next;
            
            //alert(current_song + ' > ' + total_grid);
            if(parseInt(current_song) > parseInt(total_grid)){
                current_song = 1;
                lastSel      = 1;
            }
             //alert(current_song + ',' + lastSel);
             lastSel = gridObj.getGridParam('selrow');
             playVideo(gridObj.getCell(current_song,"vid"));
             gridObj.setSelection(current_song + "");
             jQuery("#scroller").html(gridObj.getCell(current_song,"name"));
             $jScroller.start();
             


        }
    }
    return false;
}


function playVideo(vid)
{
   jQuery('#youtube_vids').html('');
  
    if (ytplayer){
         //ytplayer.loadVideoById(vid,0);
         ytplayer.clearVideo();
         ytplayer.cueVideoById(vid,0);
         ytplayer.playVideo();
     }else{
        //jQuery('#youtube_vids').append(html);
        var params = { allowScriptAccess: "always",allowFullScreen:true, wmode: "transparent"};
        var atts = { id: "myytplayer" };
        if(vid != undefined){
            swfobject.embedSWF('http://www.youtube.com/v/'+ vid +'&autoplay=1&rel=0&fs=1&color1=0x72225E&color2=0xEF0292&border=0&enablejsapi=1&playerapiid=ytplayer&loop=0',
                       "youtube_vids", "345", "270", "8", null, null, params, atts);
        }
     }
}

//inicia la interaccion del reproductos
function onYouTubePlayerReady(playerId)
{
      //alert('akii ' + playerId);
      ytplayer = document.getElementById("myytplayer");
      ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
      ytplayer.addEventListener("onError", "onPlayerError");


}

//listener de estados
/*
 * no iniciado (-1), finalizado (0), en reproducción (1), pausado (2), almacenamiento en búfer (3), en cola de vídeos (5)
 */
function onytplayerStateChange(newState)
{
   //alert("Player's new state: " + newState);
   //pasa a la siguiente cancion
   if(newState == 0){
      playSong();
   }else if(newState == 2 && playererr!=''){
      //si viene de un error y el video ya fue cargado, lo inicia
      if (ytplayer) {
        playererr = '';
        ytplayer.playVideo();
      }
   }
}
//listener de errores
/*
 * Actualmente sólo existe un código de error, que es 100 para "No se ha encontrado el vídeo"
 */
function onPlayerError(errorCode)
{
    //alert("An error occured: " + errorCode);
      //si ocurre un error, para a la siguiente cancion
      playererr = errorCode;
      playSong();

}



function play()
{
  if (ytplayer) {
    ytplayer.playVideo();
  }
}

/**
* videos by playlist
*
*/
function getSongsbyList(uid,l_id, alias)
{
     url =  site_url + "c=opensocial&m=getSongsbyList&uid=" + uid + "&l_id=" + l_id + "&alias=" + alias + "&iframe=true&nocache=" + getcache();
     //alert(url);
     jQuery("#t_list").html('');
     resetvars();
     gridObj.setGridParam({url:url,datatype:"json"}).trigger("reloadGrid")

}

/**
* Return songs by playlist suggested
*
*/
function getSongsbySuggestedList(id)
{
     url =  site_url + "c=opensocial&m=getSuggestedSongs&pl_id=" + id + "&iframe=true&nocache=" + getcache();
     //alert(url);
     jQuery("#t_list").html('');
     resetvars();
     gridObj.setGridParam({url:url,datatype:"json"}).trigger("reloadGrid")

}
