 var plug_embedsong_played_song_id = 0;
var plug_embedsong_stopped_song_id = 0;
var plug_embedsong_is_preview = false;

function plug_embedsong_fetchSong(song_id, is_preview) {
	php.data.defaultCallBack = function (key, value) {
		if (	key == "not_valid_nopaymentsetting" ||
			key == "not_valid_nopayment" ||
			key == "not_valid_nologin" ||
			key == "not_valid_nopreview" ||
			key == "not_valid_exceedplaylimit")
		{
			jQuery.facebox(""+value);
		}
		else if (key == "preview_success")
		{
			// preview success
			if (plug_embedsong_played_song_id != 0)
			{
				var img_html = '<img style="cursor:pointer;" src="images/btn_preview.png" alt="preview" align="middle" onClick="plug_embedsong_fetchSong('+plug_embedsong_played_song_id+', true);"/>';
				jQuery("span#plug_embedsong_song_prev_btn-"+plug_embedsong_played_song_id).html(img_html);
			}
			plug_embedsong_playSong(value, true);
		}
		else if (key == "play_success")
		{
			// play success
			if (plug_embedsong_played_song_id != 0)
			{
				var img_html = '<img style="cursor:pointer;" src="images/btn_play.png" alt="play" align="middle" onClick="plug_embedsong_fetchSong('+plug_embedsong_played_song_id+', false);"/>';
				jQuery("span#plug_embedsong_song_btn-"+plug_embedsong_played_song_id).html(img_html);				
			}
			plug_embedsong_playSong(value, false);
		}
		return;
	};

        // send the synchronous request for song
	jQuery.php("<?= JURI::base() ?>index.php?option=com_music&task=fetch&format=raw",{
                   'id': song_id,
		   'is_preview': is_preview ? '1' : '0'
                  }, false);
}

function plug_embedsong_selectPlaylist(song_id)
{
	jQuery.facebox({ ajax: '<?= JURI::base() ?>index.php?option=com_playlist&task=fetch&format=raw&sid='+song_id});
}

function plug_embedsong_resetPlay()
{
	if (plug_embedsong_is_preview == true)
	{
		var img_html = '<img style="cursor:pointer;" src="images/btn_preview.png" alt="preview" align="middle" onClick="plug_embedsong_fetchSong('+plug_embedsong_played_song_id+', true);"/>';
		jQuery("span#plug_embedsong_song_prev_btn-"+plug_embedsong_played_song_id).html(img_html);
		jQuery("div#plug_embedsong_musicPlayer").html(' ');
	}
	else
	{
		var img_html = '<img style="cursor:pointer;" src="images/btn_play.png" alt="play" align="middle" onClick="plug_embedsong_fetchSong('+plug_embedsong_played_song_id+', false);"/>';
		jQuery("span#plug_embedsong_song_btn-"+plug_embedsong_played_song_id).html(img_html);
		jQuery("div#plug_embedsong_musicPlayer").html(' ');
	}			
	
}

function plug_embedsong_stopSong(song_id, is_preview)
{
	php.data.defaultCallBack = function (key, value) {
		if (key == 'stop')
		{
			if (plug_embedsong_is_preview == true)
			{
				var img_html = '<img style="cursor:pointer;" src="images/btn_preview.png" alt="preview" align="middle" onClick="plug_embedsong_fetchSong('+plug_embedsong_stopped_song_id+', true);"/>';
				jQuery("span#plug_embedsong_song_prev_btn-"+plug_embedsong_stopped_song_id).html(img_html);
				jQuery("div#plug_embedsong_musicPlayer").html(value);
			}
			else
			{
				var img_html = '<img style="cursor:pointer;" src="images/btn_play.png" alt="play" align="middle" onClick="plug_embedsong_fetchSong('+plug_embedsong_stopped_song_id+', false);"/>';
				jQuery("span#plug_embedsong_song_btn-"+plug_embedsong_stopped_song_id).html(img_html);
				jQuery("div#plug_embedsong_musicPlayer").html(value);
			}			
		}
		else
		{
			
		}
		return;
	}
	
	plug_embedsong_is_preview = is_preview;
	plug_embedsong_stopped_song_id = song_id;
	jQuery.php("<?= JURI::base() ?>index.php?option=com_music&controller=player&format=raw",{
		   'songid': song_id,
		   'act': "stop"
		  });
	
}

function plug_embedsong_playSong(song_id, play_preview)
{
	act = "start";
	plug_embedsong_is_preview = play_preview;
	if (play_preview == true)
	{
		act = "preview";
	}
	php.data.defaultCallBack = function (key, value) {
		if (key == 'start')
		{
			var img_html = '<img style="cursor:pointer;" src="images/btn_stop.png" alt="stop" align="middle" onClick="plug_embedsong_stopSong('+plug_embedsong_played_song_id+', false);"/> ';
			jQuery("span#plug_embedsong_song_btn-"+plug_embedsong_played_song_id).html(img_html);
			jQuery("div#plug_embedsong_musicPlayer").html(value);
		}
		else if (key == 'preview')
		{
			var img_html = '<img style="cursor:pointer;" src="images/btn_start_preview.png" alt="preview" align="middle" onClick="plug_embedsong_stopSong('+plug_embedsong_played_song_id+', true);"/>';
			jQuery("span#plug_embedsong_song_prev_btn-"+plug_embedsong_played_song_id).html(img_html);
			jQuery("div#plug_embedsong_musicPlayer").html(value);
		}
		return;
	}
	
	plug_embedsong_played_song_id = song_id;
        // send the synchronous request for play the song
	jQuery.php("<?= JURI::base() ?>index.php?option=com_music&controller=player&format=raw",{
                   'songid': song_id,
		   'act': act
	  });

}

function plug_embedsong_stopSong2(songid, is_preview)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
		  alert ("Your browser does not support XMLHTTP!");
		  return;
	  }

	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
		{
			var response_array=xmlhttp.responseText.split("\n");
			if (response_array[0] == ' ')
			{
				if (plug_embedsong_is_preview == true)
				{
				
				}
				else
				{
					var img_html = '<img style="cursor:pointer;" src="images/btn_play.png" alt="play" align="middle" onClick="plug_embedsong_fetchSong('+plug_embedsong_stopped_song_id+', false);plug_embedsong_playSong2('+plug_embedsong_stopped_song_id+', false);"/>';
					jQuery("span#plug_embedsong_song_btn-"+plug_embedsong_stopped_song_id).html(img_html);
					document.getElementById('plug_embedsong_musicPlayer').innerHTML = xmlhttp.responseText;
				}
			}
			else
			{
				// NOTE: later clean up this
                                //jQuery.facebox(xmlhttp.responseText);
			}
		}
		return;
	}

	plug_embedsong_is_preview = is_preview;
	plug_embedsong_stopped_song_id = songid;
	xmlhttp.open("GET","index.php?option=com_music&controller=player&format=raw&songid="+songid+"&act=stop",true);
	xmlhttp.send(null);
}

function plug_embedsong_playSong2(songid, play_preview)
{
	act = "start";
	if (play_preview == true)
	{
		act = "preview";
	}
	if (plug_embedsong_played_song_id == songid)
	{
		plug_embedsong_stopSong2(songid, play_preview);
		plug_embedsong_played_song_id = 0;
		return;
	}
	else
	{
		if (plug_embedsong_played_song_id != 0)
		{
			var img_html = '<img style="cursor:pointer;" src="images/btn_play.png" alt="stop" align="middle" onClick="plug_embedsong_fetchSong('+plug_embedsong_played_song_id+', false);plug_embedsong_playSong2('+plug_embedsong_played_song_id+', false);"/> ';
			jQuery("span#plug_embedsong_song_btn-"+plug_embedsong_played_song_id).html(img_html);			
		}
	}
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  {
		  alert ("Your browser does not support XMLHTTP!");
		  return;
	  }

	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
		{
			var response_array=xmlhttp.responseText.split("\n");
			if (response_array[0] == ' ')
			{
				var img_html = '<img style="cursor:pointer;" src="images/btn_stop.png" alt="stop" align="middle" onClick="plug_embedsong_fetchSong('+plug_embedsong_played_song_id+', false);plug_embedsong_stopSong2('+plug_embedsong_played_song_id+', false);"/> ';
				jQuery("span#plug_embedsong_song_btn-"+plug_embedsong_played_song_id).html(img_html);
				document.getElementById('plug_embedsong_musicPlayer').innerHTML = xmlhttp.responseText;
			}
			else
			{
				// NOTE : later clean this up
                                //jQuery.facebox(xmlhttp.responseText);
				//alert(xmlhttp.responseText);
			}
		}
		return;
	}

	plug_embedsong_played_song_id = songid;
	xmlhttp.open("GET","index.php?option=com_music&controller=player&format=raw&songid="+songid+"&act="+act,true);
	xmlhttp.send(null);
}
