/*
	Roku.MySB.Radio
*/

if(typeof(Roku) == "undefined")
    Roku = { classes : [] };

if(typeof(Roku.MySB) == "undefined")
    Roku.MySB = { };

Roku.MySB.Radio =
{
	cur_sb_ip				: "",
	
	entry					: null,
	commands				: new Array(),
	
	error_handler			: null
};

Roku.classes.push(Roku.MySB.Radio);

Roku.MySB.Radio.do_sb_command_done = function()
{
	if (Roku.MySB.Radio.check_exception())
		return;
	
	if(typeof(this.entry.result_handler) == "function")
	{
		var result = document.RokuRadio.getResponse() + "";
		
		this.entry.result_handler(result, this.entry.custom_data);
	}
		
	this.entry = null;
	Roku.MySB.Radio.next_sb_command();		// get next
}

function RCPDone()
{
	Roku.MySB.Radio.do_sb_command_done();
}

Roku.MySB.Radio.do_sb_command_opened = function()
{
	var request = document.RokuRadio;
	if(!request)
		return;
		
	if (Roku.MySB.Radio.check_exception())
		return;
		
	try
	{
		var command_data = "";
		
		if (this.entry.cmd.length > 0)
		{
			request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			command_data = "command=" + request.encode(this.entry.cmd, "UTF-8");
		}
		request.setRequestHeader("Connection", "keep-alive");
		request.send(command_data, "RCPDone");
	}
	catch(e) {} //{ Roku.Util.Trace(e); }
}

function RCPOpen()
{
	Roku.MySB.Radio.do_sb_command_opened();
}

Roku.MySB.Radio.next_sb_command = function()
{
	if (!this.entry)
	{
		this.entry = this.commands.shift();
		
		try
		{
			document.RokuRadio.open(this.entry.url, "RCPOpen");
		}
		catch(e) {} //{ Roku.Util.Trace(e); }
	}
}

Roku.MySB.Radio.add_sb_command = function(data)
{
	if (data.priority)
		this.commands.unshift(data);	// Do not check high priority commands
	else
	{
		// Check command not already in the queue
		for (entry in this.commands)
		{
			if (entry.cmd == data.cmd)
				return false;
		}
		this.commands.push(data);
	}
		
	if (!this.entry)
		Roku.MySB.Radio.next_sb_command();
		
	return true;
}

Roku.MySB.Radio.do_sb_command = function(cmd, result_handler, custom_data, priority)
{
	if(!this.cur_sb_ip)
		return;

	if(!document.RokuRadio)
		return;
		
	var url = "http://" + this.cur_sb_ip + "/RCP";
	//Roku.Util.Trace("debug_out", url + " -> Do Command: " + cmd, true);
	
	Roku.MySB.Mutex.execute(function(data) { Roku.MySB.Radio.add_sb_command(
		{ url : url, cmd : cmd,  result_handler : result_handler, custom_data : custom_data, priority: priority } ) });
}


Roku.MySB.Radio.play_station = function(station)
{
	var title = Roku.Station.getTitle(station);
	var url = Roku.Station.getUrl(station);

	var id = Roku.Station.getId(station);
	
	var live = Roku.Station.getLive(station);

	//alert("type0");

	if (Roku.MySB.Radio.IsNumeric(id) == false)
	{
		//alert("type0");
		
		/*if (id.substring(0, 3) == "Url")
		{
			//Roku.Util.Trace("debug_out", "type1 " + id, true);
			alert("type1");
			
			Roku.MySB.Radio.do_sb_command("PlayStation " + url + " " + title, null, null, true);
		}
		else
		{*/
			var pos = id.indexOf('=');
			var pid = id.substring(pos+1);
			
			//Roku.Util.Trace("debug_out", "type2", true);
			//alert("type2");

			Roku.MySB.Radio.do_sb_command("PlayPreset " + pid, null, null, true);
		//}
	}
	else
	{
		var byId = false;
		
		if (live)
		{
			//alert(live);
			
			if (live == 'true')
				byId = true;
		}
		//else
		//	alert('unknown');

		if (url)
		{
			//Roku.Util.Trace("debug_out", "type3", true);
			//alert("type3 " + id);
			
			if (byId != false)
				Roku.MySB.Radio.do_sb_command("PlayStationWithID " + url + " " + id + " " + title, null, null, true);
			else
				Roku.MySB.Radio.do_sb_command("PlayStation " + url + " " + title, null, null, true);
		}
		else
		{
			if(id && Roku.Query)
			{
				//Roku.Util.Trace("debug_out", "type4", true);
				//alert("type4");

				if (byId != false)
				{
					Roku.Query.invoke("station_url=" + id,
						function (response, title) { Roku.MySB.Radio.do_sb_command("PlayStationWithID " + Roku.Util.Trim(response) + " " + id + " " + title, null, null, true); },
						title);
				}
				else
				{
					Roku.Query.invoke("station_url=" + id,
						function (response, title) { Roku.MySB.Radio.do_sb_command("PlayStation " + Roku.Util.Trim(response) + " " + title, null, null, true); },
						title);
				}
			}
		}
	}
}
	
Roku.MySB.Radio.check_exception = function()
{
	try
	{
		var exception = document.RokuRadio.getException() + "";
		
		if (exception != "")
		{
			//Roku.Util.Trace("debug_out", "Check Error: " + exception, true);
			
			Roku.MySB.Radio.clear();
			this.entry = null
			
			if(this.error_handler != null && typeof(this.error_handler) == "function")
				this.error_handler(exception);
			
			return true;		
		}
	}
	catch(e) {} //{ Roku.Util.Trace(e); }
	
	return false;
}

Roku.MySB.Radio.can_play = function()
{
	return this.cur_sb_ip;
}

Roku.MySB.Radio.clear = function()
{
	this.commands = new Array();
}

Roku.MySB.Radio.get_ip = function()
{
	return this.cur_sb_ip;
}

Roku.MySB.Radio.set_ip = function(new_ip)
{
	if(this.cur_sb_ip != new_ip)
	{
		this.cur_sb_ip = new_ip;
		Roku.MySB.Mutex.execute(function(data) { Roku.MySB.Radio.clear(); });
	}
}

Roku.MySB.Radio.set_error_handler = function(new_error_handler)
{
	this.error_handler = new_error_handler;
}

Roku.MySB.Radio.IsNumeric = function(sText)
{
	var ValidChars = "0123456789";
	var IsNumber = true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++)
	{ 
		Char = sText.charAt(i); 
	
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}

	return IsNumber;
}
