
MN.BitRateSelector = MN.Class();
var _brsp = MN.BitRateSelector.prototype;
_brsp.initialize = function(player, container, numOfStreams)
{
	this.qmp = player;
	this.container = $(container);
	this.streams = null;
	this.numOfStreams = numOfStreams || 4 // We will make 4 the default
	this.selectedDiv = null;
	this._bitrateId = 'profile_bitrate_%s';
	this._RenderHTML();
	this.width = 308;
	this.qmp.meter = new MN.BitRateMeter(this.qmp, "meter_mask", {"width":this.width});
	if(this.qmp.CurrentPlayState() > 2 && !this.qmp.InGap()) // We have qmx info
	{
		this.streams = this._DetermineStreamsToUse(this.numOfStreams);
		this._UpdateBitRates(this.streams);
	}
	
	var func = MN.MakeBound(this, function () 
	{
		this._CloneRateMeter($('meter_mask_rate'));
	});
	
	MN.Event.Observe(this.qmp, 'TimelineLoaded', this._OnTimelineLoaded);
	MN.Event.Observe(this.qmp, 'BitRateChanged', func);
}

_brsp._Destroy = function()
{
	MN.Event.StopObserving(this.qmp, 'TimelineLoaded', this._OnTimelineLoaded);
	MN.Event.StopObserving(this.qmp, 'PlayStateChanged', this._OnPlayStateChanged);
	while(this.container.childNodes.length > 0)
		this.container.removeChild(this.container.childNodes[0]);
	this.qmp.meter._Destroy();
	this.qmp = null;
	return true;
}

_brsp._OnTimelineLoaded = function(qvt)
{
	MN.Event.Observe(this.qmp, 'PlayStateChanged', this._OnPlayStateChanged);	
}

_brsp._UpdateBitRates = function(streams)
{
	for(var i=0; i < streams.length; i++)
	{
		MN.SetInnerText($(this._bitrateId.format(i)), this._RoundBitRate(streams[i].totalKbps) + 'k');	
	}
}

_brsp._OnPlayStateChanged = function(oldS, newS)
{
	if(newS == MN.QMP.PS.Playing)
	{
		MN.Event.StopObserving(this.qmp, 'PlayStateChanged', this._OnPlayStateChanged);
		this.streams = this._DetermineStreamsToUse(this.numOfStreams);
		this._UpdateBitRates(this.streams);
	}
}

_brsp._DetermineStreamsToUse = function(numOfStreams)
{
	var streams = [];
	var streamCount = this.qmp.GetStreamCount();
	if(numOfStreams > streamCount) numOfStreams = streamCount;
	streams.push(this.qmp.GetStream(0));
	for(var i=1; i < numOfStreams-1; i++)
	{
		var index = Math.round(streamCount/numOfStreams) * i;
		streams.push(this.qmp.GetStream(index));
	}
	streams.push(this.qmp.GetStream(streamCount-1));
	return streams;
}

_brsp._RoundBitRate = function(rate)
{
	var factor = rate >= 1000 ? 100 : 50;
	var newRate = rate / factor;
	var func = rate < 100 ? Math.round : Math.ceil;
	newRate = func(newRate);
	return newRate *= factor
}

_brsp._ChangeStream = function(index)
{
	var index = this.streams[index].index; // get the actual stream index
	this.qmp.SelectStream(index, false); // false tells it to play up to that stream
	
	$('meter_selection').style.left = (Math.ceil((this.width * ((index+1) / this.qmp.GetStreamCount()))) + 1) + 'px';
}

_brsp._RenderHTML = function()
{
	var profile_container = document.createElement('div');
	profile_container.id = 'profile_container';
	this.container.appendChild(profile_container);
	
	var meter_container = document.createElement('div');
	meter_container.id = 'meter_container';
	
	var meter_image = document.createElement('div');
	meter_image.id = 'meter_image';
	
	var meter_mask = document.createElement('div');
	meter_mask.id = 'meter_mask';
	
	var meter_mask_rate = document.createElement('div');
	meter_mask_rate.id = 'meter_mask_rate';
	
	var meter_selection = document.createElement('div');
	meter_selection.id = 'meter_selection';
	
	meter_container.appendChild(meter_image);
	meter_container.appendChild(meter_mask);
	meter_container.appendChild(meter_mask_rate);
	meter_container.appendChild(meter_selection);
	this.container.appendChild(meter_container);
	
	var meter_desc = document.createElement('div');
	meter_desc.id = 'meter_desc';
	this.container.appendChild(meter_desc);

	this._RenderButtons(profile_container, this.numOfStreams);
}

_brsp._CloneRateMeter = function(meter_mask_rate)
{
	if (!meter_mask_rate) return;

	if (!MN.nonIE && meter_mask_rate.childNodes[0])
	{
		meter_mask_rate.removeChild(meter_mask_rate.childNodes[0]);
	}
	else if (meter_mask_rate.childNodes[0])
	{
		return;
	}

	var clone = $('plyr1_bitrate').cloneNode(true);
	clone.style.cssFloat = "none";
	clone.style.styleFloat = 'none';
	clone.style.left = '0px';
	clone.style.display = 'block';
	clone.style.visibility = 'visible';
	clone.style.opacity = '1';
	clone.style.width = '';
	clone.style.color = '#FFFF66';
	clone.style.fontSize = '9px';
	meter_mask_rate.appendChild(clone);
}

_brsp._RenderButtons = function(container, num)
{
	if(!num) num = 4;
	var thisObject = this;
	for(var i=0; i < num; i++)
	{
		var button_container = document.createElement('div');
		button_container.className = 'button_container button_container_%s'.format(i);
		var view = document.createElement('div');
		view.className = 'button_view';
		var canvas = document.createElement('div');
		canvas.className = 'button_canvas png';
		canvas.style.top = '-22px';
		canvas.index = i;
		canvas.id = 'button_container_%s'.format(i);
		canvas.imagestate = '-22px';
		
		// Tie the mouse events to the div
		canvas.onmouseover = function() 
		{
			if(this != thisObject.selectedDiv) this.style.top = '0px';
		}
		canvas.onmouseout = function()
		{
			if(this != thisObject.selectedDiv) this.style.top = this.imagestate;
		}
		canvas.onclick = function()
		{
			if(this == thisObject.selectedDiv) return;
			this.style.top = '-44px';
			this.imagestate = '-44px';
			if(thisObject.selectedDiv) 
			{
				thisObject.selectedDiv.style.top = '-22px';
				thisObject.selectedDiv.imagestate = '-22px';
			}
			thisObject.selectedDiv = this;
			thisObject._ChangeStream(this.index);
			for (var j = this.index + 1; j < num; j++) 
			{
				$('button_container_%s'.format(j)).style.top = '-66px';
				$('button_container_%s'.format(j)).imagestate = '-66px';
			}
		}
		
		var bitrate = document.createElement('div');
		bitrate.className = 'profile_bitrate';
		bitrate.id = 'profile_bitrate_'+i;
		view.appendChild(canvas);
		button_container.appendChild(view);
		button_container.appendChild(bitrate);
		container.appendChild(button_container)		
	}
}

delete _brsp;
