﻿//global var
var player ; 
var playlist= null; 
var timerPlaylistId =null;
var timerPlaysCounterId=null;
var currentlyPlayedFrom=null;
var currentItemIndex = -1;
var currentSong =null;
var playlistShowInterval=6000;
var playlistButtonClick=false;
var playlistRowHeight=40;
var playlistRowWidth=40;
var playlistContentdivHeight=130;
//get player object event handler - loading the player 
function playerReady(obj)
 {
 if (player==null)
	player = document.getElementById(obj['id']);
      //player's events listening
    addListeners() ;
    //make unsoft load to the playlist 
    player.sendEvent("SOFT",false); 
   
};
 

//play song function 
function PlaySong(param_author,param_description,param_duration,param_link,param_image,param_title,param_type,param_file,param_songid)
{
        songInfo = { author:param_author,description:param_description,duration:param_duration,link:param_link,image:param_image,title:param_title,type:param_type,id:param_songid,file:param_file,type:'sound',playlistIndex:'0'};
        currentSong=songInfo;
      //  currentItemIndex=0; 
       currentlyPlayedFrom="PlayButton";
       player.sendEvent("SOFT",false); 
       player.sendEvent("LOAD",songInfo);
        
       SetPlaylistBackground(); 
       setArtistImage(songInfo);
};
 


//ajax - update User Session About play list 
function PlaySongRegister(songid)
   {
      parent.PageMethods.PlaySongRegister(songid,PlaySongRegisterSucceededHandler,PlaySongRegisterFailedHandler); 
   }
function PlaySongRegisterSucceededHandler(results,context,methodName)
   {
 	
    
   };
 function   PlaySongRegisterFailedHandler(results,context,methodName)
 {
 
 };

 
 
  function addListeners()
   {
       if(player)
       {
           //    Fired when the player switches to a new playlistitem. The new item will immediately start playing. The currently playing item is always available through the item flashvar.
               player.addControllerListener("ITEM", "PlaylistItemChanged");
               player.addControllerListener("STOP", "PlayerStop");
               player.addControllerListener("PLAY", "PlayerPlay");
               player.addModelListener("STATE", "stateListener");
        }
        else 
        {
          setTimeout("addListeners()",100);
        }
	 
   }   
 function stateListener(obj)
 {
    if (obj.newstate=="COMPLETED")
    {
       //hide panelinfo
       ShowDivCurrentlyPlaying(false);
      if (currentlyPlayedFrom=="PlayButton")
      {
         if(playlist)
            {
//             SetPlaylistBackground(); 
//             setArtistImage(playlist[0]);
//             currentlyPlayedFrom="Playlist";
//             BlurPlayListRow(0);
//             currentItemIndex=0;
//             currentSong=playlist[0];
        //     player.sendEvent("SOFT",false);
        //     player.sendEvent("LOAD",playlist);      
             
            }
      }
    
    }
   
 }
function setArtistImage(songinfo)
{
    document.getElementById("PanelSongInfo").style.display='block';
    var artistImage=parent.document.getElementById('imgArtistImage');
    var artistName =parent.document.getElementById('artistNameSpan');
    var songName =parent.document.getElementById('songNameSpan');
    var trCurrentlySongPlay = parent.document.getElementById('trCurrentlyPlayingSong');
    trCurrentlySongPlay.style.display='block';
    artistImage.src = songinfo.image ; 
    artistName.innerHTML= songinfo.description;
    songName.innerHTML= songinfo.author;
    //Count to 15 second and than update the database 
    SetSongCount(15000,songinfo.id);
   
}
      
 
//set the currently playing div to visible or not - depend on flag 
function  ShowDivCurrentlyPlaying(flag)
    {
            var panelsonginfo = parent.document.getElementById('PanelSongInfo');
            if (flag)
            panelsonginfo.style.display='block';
            else
            panelsonginfo.style.display='none';
   }
            //update the database as song played once
            function SetSongCount(interval,songid)
            { 
             if (timerPlaysCounterId!=null)
	              {
	                clearTimeout(timerPlaysCounterId)  
	              }
	      timerPlaysCounterId=setTimeout ("parent.window.frames[0].SetSongNumberOfPlays("+songid +")",interval);

    }


//add song to play list 
 function AddSongToPlayList(param_author,param_description,param_duration,param_link,param_image,param_title,param_type,param_file,param_songid)
{
     songInfo = { author:param_author,description:param_description,duration:param_duration,link:param_link,image:param_image,title:param_title,type:param_type,id:param_songid,file:param_file,playlistIndex:'0'};
     var play_list_table = parent.document.getElementById('playlistcontent');
   //  play_list_table.style.height=playlistContentdivHeight+20;
     play_list_table.style.width="92%";
     //check if playlist object exist 
     if (playlist==null)
          playlist=new Array();
          //insert the new song to the playlist object 
      playlist.push(songInfo); 
      //load the playlist array into the player 
       player.sendEvent("SOFT",true);
       player.sendEvent("LOAD",playlist);
       player.sendEvent("SOFT",false);
      //attached the new song to the html playlist table 
      CreatePlayListRow(play_list_table,songInfo);
      songInfo.playlistIndex=playlist.length-1;
     ShowPlayListWithInterval(playlistShowInterval);
};


// get html table and songindo object and attached it to the play list table
function CreatePlayListRow(table,songinfo)
{
    var lastRowNumber = table.rows.length;
    var row = table.insertRow(lastRowNumber);
    row.id=lastRowNumber;
    row.style.width="100%";
     // left cell
      var cellLeft = row.insertCell(0);
      var artistImage = document.createElement('img');
     artistImage.onclick=function(){ PlaylistSongClick(songinfo);}
     artistImage.style.cursor = "pointer";
      artistImage.src=songinfo.image ;
          artistImage.width=playlistRowWidth;
          artistImage.height=playlistRowHeight; 
          cellLeft.appendChild(artistImage);

      
      // middle cell
      var cellMiddle = row.insertCell(1);
      var songDetails = document.createTextNode(  songInfo.author +"-" + songInfo.description);
      cellMiddle.onclick=function(){ PlaylistSongClick(songinfo);}
     cellMiddle.style.cursor = "pointer";
      cellMiddle.appendChild(songDetails);
    //right  cell
    var cellRight = row.insertCell(2); 
    var el = document.createElement('img');
      el.src ="http://www.jumbey.com/ImagesApp/Icons/delete.png";
      el.alt = 'remove song';
      el.width="12"; 
      el.height="12"; 
     
     row.width="90%";
        //set index to the songs on the playlist 
      el.onclick=function(){RemoveSongFromPlayList(songinfo.playlistIndex)};
      cellRight.appendChild(el);
     
};
//get the song index on the play list and remove it from the html play list table and recreate the playlist
function RemoveSongFromPlayList(playlistindex)
      {   
          var play_list_table = parent.document.getElementById('playlistcontent');
           //delete the song fron the html playlist table 
            play_list_table.deleteRow(playlistindex);
            //remove song from the playlist array 
            playlist.splice(playlistindex,1);
            //rebuild playlist  songs indexes
            ResetPlayListIndex();
           // show the playlist 
            ShowPlayListWithInterval(playlistShowInterval);
           //check if we currently playing from playlist or from playbutton 
           if (currentlyPlayedFrom=="Playlist") 
           {
             if (playlist) 
             {  
                player.sendEvent("SOFT",true);
                player.sendEvent("LOAD",playlist); 
                player.sendEvent('SOFT',false);
             }
           }
           else 
           {
              
           }
            
           //in case removing the currently played song 
//           if (playlistindex==currentSong.playlistIndex)
//           {
//              alert('trying to delete the played song-not implemented yet') ; 
//           }
      };

 //playlist song click eventhandler 
 function PlaylistSongClick(songinfo)
 {
       currentItemIndex=songinfo.playlistIndex;
       currentSong=playlist[songinfo.playlistIndex];
       currentlyPlayedFrom="Playlist"; 
       player.sendEvent('SOFT',false);
       
       player.sendEvent('LOAD', playlist);
       player.sendEvent('ITEM', songinfo.playlistIndex);
       BlurPlayListRow(songinfo.playlistIndex);
       //show song info "currently play" div 
       setArtistImage(songinfo);
       ShowDivCurrentlyPlaying(true);
   }
  
   //rebuild playlist  songs indexes
  function ResetPlayListIndex()
  {
    for (i=0 ; i<playlist.length;i++)
                 {
                 playlist[i].playlistIndex=i;
                 } 
  };
  function PlaylistItemChanged(obj)
  {
      currentItemIndex= obj.index;
       
            if (currentlyPlayedFrom=="Playlist") 
            {
                  BlurPlayListRow(currentItemIndex);
                  setArtistImage(playlist[currentItemIndex]);
                  currentSong=playlist[currentItemIndex];
                  ShowPlayListWithInterval(playlistShowInterval);
                  //set focus on the current played item 
                  var playlistcontent= $get('divPlaylistContent');
                  var  page =parseInt(currentItemIndex/4);
                  
                 playlistcontent.scrollTop=page*(playlistRowHeight*4)+16*page;
            }
            else 
            {
            }
    ShowDivCurrentlyPlaying(true);
  
  };

      
  
  function BlurPlayListRow(index)
  {
   //get the play list table object
    var play_list_table = parent.document.getElementById('playlistcontent');
    SetPlaylistBackground();
    if (document.all)
    play_list_table.rows[index].style.backgroundColor="FFCC80";
    else 
    play_list_table.rows[index].style.backgroundColor="#ffcc80";
  }
  function PlayerStop()
  {
     ShowDivCurrentlyPlaying(false);
    
  }
  function PlayerPlay(obj)
  {
     ShowDivCurrentlyPlaying(true);
 //    player.sendEvent('LOAD', playlist);  
  }
  function SetPlaylistBackground()
  {
      //get the play list table object
   var play_list_table = parent.document.getElementById('playlistcontent');
   //clear all the previuos colors
   for (i=0;i<play_list_table.rows.length;i++)
   {
     if (document.all)
     play_list_table.rows[i].style.backgroundColor="FFFFFF";
     else 
     play_list_table.rows[i].style.backgroundColor='#ffffff';
     
   }
  }
 
  
 
function ShowPlayListWithInterval (interval) 
{
      var playlisttable = parent.document.getElementById("divPlayList"); 
         if (playlisttable.style.display=='none')
         {
             playlisttable.style.display="block"; 
              if (timerPlaylistId!=null)
              clearTimeout(timerPlaylistId);
             timerPlaylistId= setTimeout("parent.document.getElementById('divPlayList').style.display='none';" ,interval);
         }
         else 
             {
                   if (playlistButtonClick==false)
                   {
                       if (timerPlaylistId!=null)
                           {
                              clearTimeout(timerPlaylistId);
                              timerPlaylistId= setTimeout("parent.document.getElementById('divPlayList').style.display='none';" ,interval);
                           }
                   }
             }
      
} 
// event hendler for playlist mouse over 
function PlaylistMouseOver()
{
 if(playlistButtonClick==false)
    {
         if (timerPlaylistId!=null) 
         {
            clearTimeout(timerPlaylistId) ; 
            parent.document.getElementById('divPlayList').onmouseout=function(){timerPlaylistId=setTimeout("parent.document.getElementById('divPlayList').style.display='none';" ,playlistShowInterval)};
         
         }
     }
     else 
     {
         parent.document.getElementById('divPlayList').onmouseout=function(){}
     }

  
}
