// Sample data returned from player.Get("Stream.index")
// normal=T thumbnail=F emergency=F secure=F hasV=T hasA=T, totalKbps=318,vH=404,vW=720,vBPP=16,vKbps=253,vCodec=825708630,vFR=23.974609, aCh=2,aKbps=65,aSR=44100,aCodec=26447
MN.QMP.GetStreamInfo = function(streamStr, streamIndex)
{
	var obj = {};
	var chunks = streamStr.split(",");
	
	var typeInfo = chunks[0]; // normal=T thumnail=F... hasA=T
	var typeChunks = typeInfo.split(" ");
	for(var i=0; i < typeChunks.length; i++)
	{
		var kv = typeChunks[i].split("=");
		if(kv[0] == "hasV" || kv[0] == "hasA")
			obj[kv[0]] = kv[1] == "T" ? true : false;
		else
			if(kv[1] == "T")
				obj.streamType = kv[0]
	}
	
	for(var i=1; i < chunks.length; i++)
	{
		var kv = chunks[i].split("=");
		obj[kv[0].strip()] = kv[1].strip();
	}
	obj.index = streamIndex;
	return obj;	
}
MN.QVT.PlayerWrapper.prototype._PlayerBitRateChanged = function(newRateKbps)
{   // Add the stream number (0 based) in the return data
	this.PostEvent('BitRateChanged', newRateKbps,this.GetStreamIndexByBitRate(newRateKbps));	
}
MN.QVT.PlayerWrapper.prototype.GetStream = function(index)
{
	if(arguments.length == 0 || index == null)
		return this.GetCurrentStream(); // We should get the current stream if we could	
	else
		return MN.QMP.GetStreamInfo(this.Get("Stream."+index), index);
		
}
MN.QVT.PlayerWrapper.prototype.GetCurrentStream = function()
{   // this will change to ask the player with the 7.8 release of the client
	return this.GetStream(this.GetStreamIndexByBitRate(this.CurrentBitRate()));
}
MN.QVT.PlayerWrapper.prototype.GetStreamCount = function()
{
	return parseInt(this.Get("StreamCount"),10);
}
MN.QVT.PlayerWrapper.prototype.GetStreamIndexByBitRate = function(rate)
{   // return the stream number that is closest to the given rate
	// I know this is very hacky but we need it until release 7.8 of the client :)
	var streamCount = this.GetStreamCount();
	if(streamCount <=0) return -1;
	var minDiff = 10000, streamIndex = 0;
	for(var i=0; i < streamCount; i++)
	{
		var stream = this.GetStream(i);
		var diff = Math.abs(rate - parseInt(stream.totalKbps, 10));
		if(diff < minDiff)
		{
			minDiff = diff;
			streamIndex = i
		}
	}
	return streamIndex;
}
MN.QVT.PlayerWrapper.prototype.SelectStream = function(index, veto)
{
	// if veto dont allow anything but the stream at index
	// else allow up to the stream at given index e.g. 0,0,0,-1,-1,-1,-1 for index=4
	var streamCount = this.GetStreamCount();
	if(streamCount <= 0) streamCount = 15; // This could be bad later but for now we should be okay
	var arr = [];
	for(var i=0; i < streamCount; i++)
	{
		if(i < index)
		{
			if(veto == true)
				arr[i] = -1;
			else
				arr[i] = 0;
		}
		else if(i == index)
			arr[i] = 0;
		else
			arr[i] = -1;
	}
	this.Set("SelectStreams", arr.join(",")); // e.g. -1,0,-1,-1,-1,-1,-1,-1
	this.Set("Commit", "");
	return true;
}
