// JavaScript Document

$(document).ready(pageInit);

function pageInit ()
{
	
	$("a[href$=mp3]").each(function ()
	 {
		var img = new Image();
		$(img).attr('src', '/img/control_play.png');
		$(img).addClass('player');
		$(img).addClass('icon');
		$(img).css('cursor', 'pointer');
		$(this).before(img);
	 });
	$("img.player").click(function ()
	  {
		var url = $(this).next().attr('href');
		myListener.current = this;
		if ($(this).data('playing') == 'true')
		{
			myListener.pause();
			$(this).data('playing', 'false');
			$(this).attr('src', '/img/control_play_blue.png');
		}
		else if ($(this).data('playing') == 'false')
		{
			myListener.play();
			$(this).attr('src', '/img/control_pause_blue.png');
			$(this).data('playing', 'true');
		}
		else
		{
			$(this).parent().append('<span></span>');
			$("img.player").attr("src", "/img/control_play.png");
			$("img.player").data("playing", '');
			$(this).attr('src', '/img/control_pause_blue.png');
			$(this).data('playing', 'true');
			myListener.current = this;
			myListener.setFile(url);
			myListener.play();

		}

		return false;
	 });
}

var myListener = new Object();

/**
 * Initialisation
 */
myListener.onInit = function()
{
	this.position = 0;
};
/**
 * Update
 */
myListener.onUpdate = function()
{
	var isPlaying = (this.isPlaying == "true");
	
	if (!isPlaying && myListener.current && myListener.position == 0)
	{
		$(myListener.current).attr('src', '/img/control_play.png');
		$(myListener.current).data('playing', null);
		myListener.current = null;
	}
};

function getFlashObject()
{
	return document.getElementById("myFlash");
}

myListener.setFile = function (url)
{
	getFlashObject().SetVariable("method:setUrl", url);	
}

myListener.play = function ()
{
	getFlashObject().SetVariable("method:play", "");
	getFlashObject().SetVariable("enabled", "true");
}
myListener.pause = function ()
{
	getFlashObject().SetVariable("method:pause", "");
}
myListener.stop = function ()
{
	getFlashObject().SetVariable("method:stop", "");
}
