/*
	Player logic
	
	This code controls how the theme operates in conjunction with iTunes.
	The functions "artworkUpdate", "playerUpdate", and "trackUpdate" are automatically
	called by Bowtie (the names of these functions can be specified in your Info.plist file).
*/

oldPlayState = 0;
function $(el) { return document.getElementById(el); }

// This function is called when the artwork needs to change with the URL to the new image file
// In reality, this is the base64-encoded image data, but it functions just as any normal
// HTTP URL would.
function artworkUpdate(artURL)
{
	if (artURL == "")
	// use "no art" image
		$('artHolder').src = "img/noartwork.png";
	else
	// use artURL
		$('artHolder').src = artURL;
}

// This function is called every second or so to indicate that the controller should
// update its state information. It essentially achieves the same function as a JavaScript
// interval, but is less resource intensive because Bowtie can use separate threads as
// needed.
function playerUpdate()
{
	// The playerUpdate function receives no parameters-- you get information
	// yourself via the iTunes object, or occasionally, the Bowtie object.
	
	var pState = iTunes.playState();
	// this branch updates the play/pause graphic
	if (pState != oldPlayState)
	{
		if (pState == 0 || pState == 2)
			$('playLink').innerHTML = '<img src="img/play.png">';
		else
			$('playLink').innerHTML = '<img src="img/pause.png">';
		
		oldPlayState = pState;
	}
}

// This function is called automatically when the track changes, and receives
// a TAWrapper object as its parameter containing information on the new track.
// The full list of track properties can be found in the documentation.
function trackUpdate(track)
{
	var name = track.property('title');
	var artist = track.property('artist');
	var album = track.property('album');
	
	$('songname').innerHTML = name;
	$('artist').innerHTML = artist;
	$('album').innerHTML = album;
	
	// This is a custom function that updates the star interface
	// for ratings based on the rating of the current track
	updateRatingUI(iTunes.rating());
}

// We're using this function rather than calling iTunes.playPause() directly
// because this allows us to also ensure a player update is called immediately
// (in this case, so that the play/pause button changes its image immediately)
function playPause()
{
	iTunes.playPause();
	playerUpdate();
}

// We use this function rather than calling iTunes.setRating() directly
// because this allows us to update our own interface before sending in
// the new data, eliminating round-trip delays/UI lag
function setRating(rating)
{
	updateRatingUI(rating);
	iTunes.setRating(rating);
}


// This function is completely custom, and not necessary for your player;
// it takes a numerical representation of the track's rating (0 - 100) and
// displays the correct number of stars in the player.
function updateRatingUI(rating)
{
	if(rating > 0){
		$('star1').innerHTML = "★";
	} else {
		// $('star1').innerHTML = "☆";
		$('star1').innerHTML = "☆";
	}

	if(rating > 20){
		$('star2').innerHTML = "★";
	} else {
		$('star2').innerHTML = "☆";
	}

	if(rating > 40){
		$('star3').innerHTML = "★";
	} else {
		$('star3').innerHTML = "☆";
	}

	if(rating > 60){
		$('star4').innerHTML = "★";
	} else {
		$('star4').innerHTML = "☆";
	}

	if(rating > 80){
		$('star5').innerHTML = "★";
	} else {
		$('star5').innerHTML = "☆";
	}
}