/*
	mp3streamer.js

	A JavaScript library for streaming mp3 files using Flash, whereby the files that need to be streamed,
	are files that are supplied by calling a JavaScript function.

	NOTES:

	This library uses the name "mp3streamer" for pointing to the Flash movie. Make sure that this name is
	used so everything works properly.

	For the image buttons goes:
	- always use the names play.gif and mute.gif for the image files
	- make sure the IDs of the image are unique and set. Without them, the JS routines will not work properly

	FUNCTIONS:

	++ mp3streamer_DoFSCommand(command, args)
	This function is triggered from Flash when a mp3 file has reached its end

	++ get_flash_movie_object(movieName)
	Returns a Flash movie object

	++ mute_sound()
	Mutes the sound of the mp3 streamer

	++ play_mp3_url
*/

var currently_playing = false; // is the player currently working ?
var tid;

function end_of_file()
{
	document.getElementById(tid).src = template_path + "images/icon_speaker.gif";
	currently_playing = false;
}

function get_flash_movie_object(movieName)
{
  if (window.document[movieName]) 
  {
    return window.document[movieName];
  } 
  else if (navigator.appName.indexOf("Microsoft Internet") == -1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName];
    else
      alert('Unable to get movie object!');
  }
  else // if (navigator.appName.indexOf("Microsoft Internet") != -1)
  {
    var elem = document.getElementById(movieName);
    
    if (! elem)
    {
      alert('Movie Name is NULL!');
    }
    return document.getElementById(movieName);
  }
}

function send_data_to_flash_movie(sData)
{
  var preview = get_flash_movie_object("mp3streamer");
  preview.Rewind();
  preview.SetVariable("/:testvar", sData);
  preview.GotoFrame(14);
}

function mute_sound()
{
	var flashMovie = get_flash_movie_object("mp3streamer");
	flashMovie.GotoFrame(19);
}

function play_mp3_url(url, tobj)
{
	//var tid = tobj.id;
	tid = tobj.id;
	if (currently_playing != false) // the mp3streamer has been started before
	{
		// see if the button that was pressed was the one from the song that was already playing
		var nobj = document.getElementById(currently_playing);
		if (tid == nobj.id)
		{
			// yes, the button clicked was the one that was playing, turn it off
			mute_sound();
			tobj.src = template_path + "images/icon_speaker.gif";
			currently_playing = false;
		}
		else
		{
			/// no, the button clicked was not from the mp3 that was already streaming, turn the previous button off
			nobj.src = template_path + "images/icon_speaker.gif";
			tobj.src = template_path + "images/icon_speaker_off.gif";
			currently_playing = tid;
			send_data_to_flash_movie(url);
		}
	}
	else
	{
		// first time the mp3streamer plays
		tobj.src = template_path + "images/icon_speaker_off.gif";
		currently_playing = tid;
		send_data_to_flash_movie(url);
	}
}

