// This will create the audio sample objects found throughout the NMC family of websites.
// The idea here is that a user clicks the Hear Audio button, the name of the audio sample
// is sent to the function, the function will check to see if another sample is all ready playing
// if a sample is all ready playing, it will check to see if it is the same sample that it was just passed
// If it is the same sample, we will do nothing, because the sample is currently playing and the user
// can control the flash player directly.
// If it is a different sample, we will first fade out the current audio sample, then remove it from the
// page entirely. After the initial sample is removed from the page, we will write the new embed code
// to the page using the audioSample variable as the source for the .mp3. These mp3s are stored in the
// audio folder (themes/NWR1/audio/stories). Their mp3 names must match the id of the toggle link.

// Available Variables and their default values
//	isPlaying = 0 This signals the program that an audio sample is playing, or not playing.
//  audioQueue = ""; This holds the value of the audio sample that should be played
//  audioCurrent =  "" ; This holds the value of what is currently being played

var audioCurrent;	// will hold current song playing

$(document).ready(function(){

	var isPlaying = 0;
	var t;				// will hold $(this) of current element
	var audioQueue;		// will hold next song

	$(".toggleLink").click(function(){ // on click of an element with class toggleLink

		t = $(this); 				// set t to current element
		audioQueue = t.attr('id'); 	// grab the audio sample name from the id of the toggle link

		if (isPlaying == 1) {
		
			// this means a sample is currently playing
			
			if (audioQueue != audioCurrent) {
			
				// the queued sample is different from the currently playing sample
			
				// remove the old player
				$("#" + audioCurrent + "_player").flash().remove();

				// add new player
				$("#" + audioQueue + "_player").flash({
					//swf: "audio/miniplayer/player_mp3_mini.swf",
					swf: "audio/miniplayer/player_mp3_mini.swf",
					width: 200,
					height: 20,
					//movie: "audio/miniplayer/player_mp3_mini.swf",
					movie: "audio/miniplayer/player_mp3_mini.swf",
					bgcolor: "4680A9",
					flashvars: {
						//mp3: "audio/stories/" + audioQueue + ".mp3",
						mp3: "audio/stories/" + audioQueue + ".mp3",
						buttoncolor: "ffffff",
						slidercolor: "265678",
						autoplay: "1"
					}
				});

				audioCurrent = audioQueue;
				audioQueue ="";
				
			} else {
			
				// the user clicked hear radio again on the current track
				// do nothing
				
			}

		} else {

			// no audio is playing, must be the first time through

			$("#" + audioQueue + "_player").flash({
				//swf: "audio/miniplayer/player_mp3_mini.swf",
				swf: "audio/miniplayer/player_mp3_mini.swf",
				width: 200,
				height: 20,
				//movie: "audio/miniplayer/player_mp3_mini.swf",
				movie: "audio/miniplayer/player_mp3_mini.swf",
				bgcolor: "4680A9",
				flashvars: {
					//mp3: "audio/stories/" + audioQueue + ".mp3",
					mp3: "audio/stories/" + audioQueue + ".mp3",
					buttoncolor: "ffffff",
					slidercolor: "265678",
					autoplay: "1"
				}
			});

			isPlaying = 1; 				// set isPlaying to 1
			audioCurrent = audioQueue; 	// set the current audio var to the audio sample that was clicked
			audioQueue = ""; 			// clear the queued audio

		}
	})

});
