// Simple class to create a bitrate meter
// Basically this will manipulate a mask that covers another graphic
MN.BitRateMeter = MN.Class();
var _brmp = MN.BitRateMeter.prototype;
_brmp.initialize = function(player, mask, options)
{
	this.player = player;
	this.mask = $(mask);
	this.rate = $(this.mask.id + '_rate');
	this.width = options.width || this.mask.offsetWidth;
	this.stepWidth = options.stepWidth || 1;
	this.stepUnit = options.stepUnit || 'px';
	this.tweenDuration = options.tweenDuration || .5;
	this.tween = null;
	this.streamindex = 0;
	if(this.player.CurrentPlayState() >= MN.QMP.PS.Playing && this.player.CurrentBitRate())
	{
		this._OnBitRateChanged(this.player.CurrentBitRate(), this.player.GetStream().index);	
	}
	MN.Event.Observe(this.player, 'BitRateChanged', this._OnBitRateChanged);
}
_brmp._Destroy = function() // Work on this.... There may be some more things that we need to do.
{
	MN.Event.StopObserving(this.qmp, 'BitRateChanged', this._OnBitRateChanged);
	this.qmp = null;
	return true;
}
_brmp._OnBitRateChanged = function(rate, streamIndex)
{
	this.streamIndex = streamIndex;
	if(this.tween) this.tween.stop();
	setTimeout(this._DelayMotion, 100); // Sometimes for the first clip the data is not ready	
}
_brmp._DelayMotion = function()
{
	var newWidth = this.width - (this.width * ((this.streamIndex+1) / this.player.GetStreamCount()));
	if(this.tween)
		this.tween.continueTo(newWidth, this.tweenDuration);
	else
	{
		this.tween = new Tween(new Object(), 'width', Tween.regularEaseOut, this.mask.offsetWidth, newWidth, this.tweenDuration, 'px');
		this.tween.onMotionChanged = this._OnMotionChanged;
		this.tween.start();
	}
}
_brmp._OnMotionChanged = function(event)
{
	var pos = parseInt(event.target._pos, 10);
	pos = pos - (pos % this.stepWidth); // Only move the width at multiples of the stwpWidth
	this.mask.style.width = pos + 'px';
	this.rate.style.width = pos + 'px';
}