// nowplaying.js
// 1/1/2012
// Matthew R. Demicco (armitage@armitunes.com)

// Global to track the current playing song
var newsong;
var initProgressBar;

// This main function is called by the jquery document.ready function.
// It reloads ices.cue every 5 seconds and determines if the song
// has changed. If so, it forces a reload of the album art and upcoming
// track data.

function reloadData()
{
  // Display the most recent data immediately
  getData();

  // Every 5 seconds, refresh the content
  setTimeout(reloadData, 10000);

  // Old way didn't work with IE7 very well
  //var auto_refresh = setInterval(
  //function()
  //{
  //  getData();
  //}, 5000);
}

// Does the acutal heavy lifting of loading the ices.cue file
// and making a determination if it needs to load new album art
// and track lists.

function getData()
{
  $.get('ices.cue?' + Math.random(),function(data) {


    // Set current data text
    var mySplitResult = data.split("\n");
    var myAlbumSong = mySplitResult[0].split('/');
    var mySong = myAlbumSong[myAlbumSong.length-1].replace(/\.[Mm][Pp]3/,"");
    var mySongLength = mySplitResult[3].split(':');
    var mySongPercent = Math.round(mySplitResult[4]);
    var mySongSeconds = String("0" + mySongLength[3]).slice(-2);

    // Old way to writing to the big div
    //dataDiv = document.getElementById('albumInfo');
    //dataDiv.innerHTML = 'Album: ' + myAlbumSong[myAlbumSong.length-2] + '<br>Track: ' + mySong + '<br>Length: ' + mySongLength[2] + 'min ' + mySongLength[3] + 'sec';

    // Create the progress bar
    //$('#trackLength').text("(" + mySongLength[2] + 'min ' + mySongLength[3] + 'sec )');
    $('#progressBar').progressbar({ value: mySongPercent });

    // If it's the first time, we'll initialize the popup
    if (initProgressBar==undefined) 
    {
      $('#progressBar').qtip({
        content: mySongPercent + "% of " + mySongLength[2] + ":" + mySongSeconds,
        show: 'mouseover',
        hide: 'mouseout',
        delay: 0,
        hide: { effect: {
          type: 'grow',
  	  length: 400
        } }
      });

      initProgressBar = 1;

    // Otherwise, just update it
    } else  {
      $('#progressBar').qtip('api').updateContent(mySongPercent + "% of " + mySongLength[2] + ":" + mySongSeconds);
    }

    // Load the initial album art and next tracks

    if (newsong==undefined)
    {
      // Load the Now Playing track name
      $('#trackName').html(myAlbumSong[myAlbumSong.length-2] + " : " + mySong + ' <a name="addfav"><img class="favSong" src="/images/heart.png" height=10 title="Add to Favorites"></a>');
      $('a[name|="addfav"]').click(function(event) {
        $(this).hide("slow");
        $.post('/staticpages/index.php?page=20060330221520628', { song: mySong, artist: myAlbumSong[myAlbumSong.length-2]} , function(data) {
          alert(data);
        });
      });

      // Load the album art

      $('#albumCover').load('nowplaying.php?action=albumart&' + Math.random()).hide().fadeIn(2000);

      // Load the next songs list
      reloadNext();

      // Set the tracking variable
      newsong=mySong;
    }

    // If the song has changed, load a new picture and next tracks
    else if (newsong!=mySong)
    {
      // Load the Now Playing track name
      $('#trackName').html(myAlbumSong[myAlbumSong.length-2] + " : " + mySong + ' <a name="addfav"><img class="favSong" src="/images/heart.png" height=10 title="Add to Favorites"></a>');
      $('a[name|="addfav"]').click(function(event) {
        $(this).hide("slow");
        $.post('/staticpages/index.php?page=20060330221520628', { song: mySong, artist: myAlbumSong[myAlbumSong.length-2]} , function(data) {
          alert(data);
        });
      });

      // Reload the album cover
      $('#albumCover').fadeOut(2000, function() { $(this).load('nowplaying.php?action=albumart&' + Math.random()).fadeTo(3000,1); });

      // Reload the next songs list
      reloadNext();

      // Set the tracking variable
      newsong=mySong;
    }

    // Otherwise, we won't change any data

  });

}

// This function is called to reload the track data divs

function reloadNext()
{
  $.get('nowplaying.php?action=nextsongs&' + Math.random(),function(data) {

   var myNextTracks = data.split("\n");
   $('#nextTrack1').html(myNextTracks[0]);
   $('#nextTrack2').html(myNextTracks[1]);
   $('#nextTrack3').html(myNextTracks[2]);
   $('#nextTrack4').html(myNextTracks[3]);
   $('#nextTrack5').html(myNextTracks[4]);

  });

}

