var activeItem = null;
var playingItem = null;
var producerID = null;
var trackname = null;
var trackID = null;

var trackTimes = new Object();

var movingPlayerID = 1;
var playingPlayerID = 0;

function onMouseOverCell(obj)
{
	if (activeItem == obj)
	{
		$(activeItem).addClassName('active');
		showMovingPlayerSwf();
	}
}

function onMouseOutCell(obj)
{
	if (activeItem != playingItem)
	{
		$(activeItem).removeClassName('active');
		hideMovingPlayerSwf();
	}
}

function onMouseOverIcon(obj, p, t, trackID)
{
	if ((activeItem == obj) || (playingItem == obj))
	{
		return;
	}

	activeItem = obj;
	setSwfInfo(p, t, trackID);
}

function setSwfInfo(p, t, trackID)
{
	producerID = p;
	trackname = t;
	trackID      = trackID;

	var playerSwf = getMovingPlayerSwf();

	var trackTime = 0;
	if (trackTimes[producerID])
	{
		trackTime = trackTimes[producerID];
	}
	playerSwf.playerTarget = activeItem;
	playerSwf.setTrackInfo(producerID, trackname, trackTime, trackID);
}

function onMouseOverPlayer(playerID)
{
	if (playerID != playingPlayerID)
	{
		$(activeItem).addClassName('active');
		setSwfInfo(producerID, trackname, trackID);
		showMovingPlayerSwf();
	}
}

function onMouseOutPlayer(playerID)
{
	if (playerID != playingPlayerID)
	{
		$(activeItem).removeClassName('active');
		hideMovingPlayerSwf();
	}
}

function onPlayMP3(playerID)
{
	if (playerID != playingPlayerID)
	{
		if (playingItem)
		{
			var playerSwf = getPlayingPlayerSwf();
			if (playerSwf)
			{
				playerSwf.stopMP3();
			}

			$(playingItem).removeClassName('active');
		}

		playingItem = activeItem;

		// Toggle moving/playing player
		playingPlayerID = playerID;
		movingPlayerID = 3 - playingPlayerID;
	}

	hideMovingPlayerSwf();

	var playerSwf = getMovingPlayerSwf();
	playerSwf.stopMP3();
	playerSwf.playerTarget = null;
}

function onSetMP3Volume(playerID, volume)
{
	var otherPlayerID = 3 - playerID;
	var playerSwf = getPlayerSwf(otherPlayerID);
	playerSwf.setMP3Volume(volume);
}

function getPlayingPlayerSwf()
{
	var player = null;

	if (playingPlayerID)
	{
  		player = (Prototype.Browser.IE) ? window['mini-player-swf-' + playingPlayerID] : document['mini-player-swf-' + playingPlayerID];
  	}

  	return player;
}

function getMovingPlayerSwf()
{
  	return getPlayerSwf(movingPlayerID);
}

function getPlayerSwf(id)
{
  	return (Prototype.Browser.IE) ? window['mini-player-swf-' + id] : document['mini-player-swf-' + id];
}

function hideMovingPlayerSwf()
{
	$('mini-player-' + movingPlayerID).style.left = "-9999px";
}

function showMovingPlayerSwf()
{
	if (playingItem == activeItem)
	{
		return;
	}

	var playerSwf = getMovingPlayerSwf();

	var trackTime = 0;
	if (trackTimes[producerID])
	{
		trackTime = trackTimes[producerID];
	}
	playerSwf.playerTarget = activeItem;
	playerSwf.setTrackInfo(producerID, trackname, trackTime, trackID);

	$('mini-player-' + movingPlayerID).clonePosition($(activeItem).down().next(), {setWidth:false, setHeight:false});
}

function setInstanceTrackTime(instanceID, duration)
{
	if (instanceID == producerID)
	{
		trackTimes[instanceID] = duration;
	}
}
