// Now Playing for The Current
// Updated 6/2/2010 to use jQuery and allow for Facebook Like
// Replaces now_playing_detail.js for The Current

var currentDisplayedSongs = new Array(); // Tracks the five currently displayed songs by timestamp
// DEVEL: var dataSource = 'playlist_data.xml';
var dataSource = '/radio/services/the_current/songs_played/playlist_data.php';
var updateInterval = 30000;

function formatted_song_html(track) {
	// Returns the HTML for a single song, given a track XML element
	var listItem = $(document.createElement('li'));
	var songTitle = $(track).find("title").text();
	var songArtist = $(track).find("creator").text();
	var songPlayTime = $(track).find("play-time").text();
	var songPlayDateTime = $(track).find("play-datetime").text();
	var songId = '';
	var songLink = '';
	var linkedSongTitle = '';
	var songFacebookLike = '';
	
	// Create an Amazon link when artist and title are available
	if (songArtist != null && songTitle != null) {
		var commerceLink = '<a href="http://www.amazon.com/gp/search?ie=UTF8&amp;keywords=' +
			encodeURIComponent(songArtist) + 
			'&amp;tag=current-20&amp;index=music-artist&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325;' +
			'">Buy Now</a>';
	} else {
		var commerceLink = "";
	}
	
	// Create a linked song title (without links when none are available)
	if ($(track).find('song-id').text() != '') {
		// Get Song ID and create link
		songId = $(track).find('song-id').text();
		songLink = '/radio/services/the_current/playlist/song_detail.php?song_id=' + songId;
		linkedSongTitle = '<a href="' + songLink + '">' + songTitle + '</a>';
		songFacebookLike = '<fb:like layout="button_count" href="http://minnesota.publicradio.org/radio/services/the_current/playlist/song_detail.php?song_id=' + songId + '" show_faces="false" width="180" height="" font="arial" colorscheme="light"></fb:like>';
	} else {
		linkedSongTitle = songTitle;
		songFacebookLike = '';
	}
	
	listItem = $(listItem).append('<span class="selection">' + linkedSongTitle +
		'<span class="selection-timestamp">' + songPlayTime + '</span></span>' +
		songArtist + '<br />' + commerceLink + '<br />' + songFacebookLike);
		
	return listItem;
}

function setup_now_playing() {
	// Sets up the playlist and begins the updating process
	// Clear the Loading... message and set up the UL
	$("span#now-playing-text-the-current").empty().append('<ul id="latest-song-list"></ul>');
	update_now_playing();
}

function update_now_playing() {
	// This function updates the playlist every n seconds
	
	var thisUpdateSongList = new Array();
	var needsDisplayUpdate = false;
	
	$.ajax({
		url: dataSource,
		dataType: 'xml',
		cache: false,
		timeout: 3000,
		success: function(data){
			// If the play-time for any track in the XML file isn't in the currentDisplayedSongs array,
			// the list needs to be rebuilt. This should happen every time a new song is played.
			$(data).find("track play-time").each(function() {
				if ($.inArray($(this).text(), currentDisplayedSongs) > -1) {
					//console.log("no update needed" + $(this).text());
				} else {
					//console.log("update needed");
					//console.log(currentDisplayedSongs);
					needsDisplayUpdate = true;
				}
			});
			if (needsDisplayUpdate == true) {
				//console.log("updating display");
				$("ul#latest-song-list").empty();
				currentDisplayedSongs.length = 0;
				//console.log("cleared array?");
				//console.log(currentDisplayedSongs);
				$(data).find("track").each(function() {
					currentDisplayedSongs.push( $(this).find("play-time").text() );
					$("ul#latest-song-list").append(formatted_song_html(this));
				});

				// Re-parse Facebook XFBML within the latest played list, to add the Like buttons
				// Check to ensure that FB is defined first (to see if the API is loaded yet)
				if (typeof(FB) != "undefined") {
					FB.XFBML.parse(document.getElementById('latest-song-list'));
				}
				needsDisplayUpdate = false;
			}
		}
	});
	setTimeout("update_now_playing()", updateInterval);
}
			
$(document).ready(function() {
	setup_now_playing();
});

