// JavaScript Document

/* set up the player UI and Init the Player & Playlist */
$(document).ready(function(){
	
	jQuery.log = function(message) {
	  if(window.console) {
		console.debug(message);
	  } else {
		//alert(message);
	  }
	};
	
     // add the CSS file that's only used for this page into the header
	$('head').append('<link href="jplayer.blue.monday.css" rel="stylesheet" type="text/css" />');
	
	// insert Dom Elements
	$('#radioAds').append(myHTML);

	var playItem = 0;
	
	//  Dynamically build the playlist from the UL in the page	
	var myPlayList = [];
	$('#radioAds ul#tracklisting li a').each(function(){
		myPlayList.push({name: $(this).text() ,mp3:$(this).attr("href")});
	});
	
	// Local copy of jQuery selectors, for performance.
	var jpPlayTime = $("#jplayer_play_time");
	var jpTotalTime = $("#jplayer_total_time");
	var jpStatus = $("#demo_status"); // For displaying information about jPlayer's status in the demo page

	$("#jquery_jplayer").jPlayer({
		ready: function() {			
			displayPlayList();
			playListInit(); // Parameter is a boolean: true for autoplay.
			
			// hide the UL with the original tracklist
			$('#radioAds ul#tracklisting').css('display', 'none');
		},
		swfPath : ''
	})
	.jPlayer("onProgressChange", function(loadPercent, playedPercentRelative, playedPercentAbsolute, playedTime, totalTime) {
		jpPlayTime.text($.jPlayer.convertTime(playedTime));
		jpTotalTime.text($.jPlayer.convertTime(totalTime));
	})
	.jPlayer("onSoundComplete", function() {
		playListNext();
	});

	$("#jplayer_previous").click( function() {
		playListPrev();
		return false;
	});

	$("#jplayer_next").click( function() {
		playListNext();
		return false;
	});

	function displayPlayList() {
		for (i=0; i < myPlayList.length; i++) {
			$("#jplayer_playlist ul").append("<li id='jplayer_playlist_item_"+i+"'>"+ myPlayList[i].name +"</li>");
			$("#jplayer_playlist_item_"+i).data( "index", i ).click( function() {
				var index = $(this).data("index");
				if (playItem != index) {
					playListChange( index );
				} else {
					$("#jquery_jplayer").jPlayer("play");
				}
			});
		}
	}

	function playListInit(autoplay) {
		if(autoplay) {
			playListChange( playItem );
		} else {
			playListConfig( playItem );
		}
	}

	function playListConfig( index ) {
		$("#jplayer_playlist_item_"+playItem).removeClass("jplayer_playlist_current");
		$("#jplayer_playlist_item_"+index).addClass("jplayer_playlist_current");
		playItem = index;
		$("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3);
	}

	function playListChange( index ) {
		playListConfig( index );
		$("#jquery_jplayer").jPlayer("play");
	}

	function playListNext() {
		var index = (playItem+1 < myPlayList.length) ? playItem+1 : 0;
		playListChange( index );
	}

	function playListPrev() {
		var index = (playItem-1 >= 0) ? playItem-1 : myPlayList.length-1;
		playListChange( index );
	}
});

var myHTML = '<div id="jquery_jplayer"></div><div class="jp-playlist-player"><div class="jp-interface"><ul class="jp-controls"><li id="jplayer_play" class="jp-play">play</li><li id="jplayer_pause" class="jp-pause">pause</li><li id="jplayer_stop" class="jp-stop">stop</li><li id="jplayer_volume_min" class="jp-volume-min">min volume</li><li id="jplayer_volume_max" class="jp-volume-max">max volume</li><li id="jplayer_previous" class="jp-previous">previous</li><li id="jplayer_next" class="jp-next">next</li></ul><div class="jp-progress"><div id="jplayer_load_bar" class="jp-load-bar"><div id="jplayer_play_bar" class="jp-play-bar"></div></div></div><div id="jplayer_volume_bar" class="jp-volume-bar"><div id="jplayer_volume_bar_value" class="jp-volume-bar-value"></div></div><div id="jplayer_play_time" class="jp-play-time"></div><div id="jplayer_total_time" class="jp-total-time"></div></div><div id="jplayer_playlist" class="jp-playlist"><ul><!-- The function displayPlayList() uses this unordered list --></ul></div></div><div id="demo_status"></div><div id="demo_info"></div>';

