-- выбор источника --
-------------------------------------------------------------------
if m_simpleTV.Control.CurrentTitle ~= nil and (m_simpleTV.Control.CurrentAdress:find('TVSources') or m_simpleTV.Control.CurrentAdress:find('TVSSTART')) then

	local inAdr = string.gsub(m_simpleTV.Control.CurrentAdress,'\'','"') or 'empty'
	local retAdr = TVSources_var.CurrentAdress or 'empty'
	
	local Id
	if string.match( inAdr, 'TVSources' ) then 
		Id = findpattern( inAdr,'id=%b""',0,4,1) or tvs_core.tvs_clearname(m_simpleTV.Control.CurrentTitle_UTF8)
	else
		Id = tvs_core.tvs_clearname(m_simpleTV.Control.CurrentTitle_UTF8)
	end
	
	if Id == nil then return end
	
	local script_error   = tvs_core.tvs_fcheck(m_simpleTV.MainScriptDir .. 'user/TVSources/lists/' .. Id .. '.lst')
	
	if script_error ~= 'ok' then return end
	
	local i,b,err
	local source
	local t={}
	local SourceNumber
	
	
	local ch_list = dofile (m_simpleTV.MainScriptDir .. 'user/TVSources/lists/' .. Id .. '.lst')
	
	source = tvs_core.tvs_GetSourceParam()
	
	if source~= nil then
		for i,b in pairs(ch_list) do
			SourceNumber = findpattern(ch_list[i][1],'#(.-)$',1,0,0) or ''
			if SourceNumber ~= '' then 
				ch_list[i][1] = string.gsub(ch_list[i][1],'#(.-)$','')
				SourceNumber = ' ' .. SourceNumber
			end
			if source[ch_list[i][1]] then
				ch_list[i][3] = ch_list[i][1]
				ch_list[i][1] = source[ch_list[i][1]].name or tvs_core.tvs_GetLangStr('src_group')
				ch_list[i][1] = ch_list[i][1] .. SourceNumber
			else
				ch_list[i][1] = tvs_core.tvs_GetLangStr('src_group')
				ch_list[i][1] = ch_list[i][1] .. SourceNumber		
			end
		end
		table.sort(ch_list,function(a,b) return a[1] < b[1] end)
	end

	

	local users , str_users
	i=1
	b=1	
	repeat
	    str_users =''
	    t[i] = {}
	    t[i].Id  =  i
	    if ch_list[i][2]:find(':') then
	    	users = tvs_func.active_users(ch_list[i][2])
	    	if users>=0 then str_users = ' (' .. users .. ' польз.)' end
	    end	
		t[i].InfoPanelDesk = ch_list[i][1] 
		t[i].Name =  ch_list[i][1] .. str_users
		t[i].sortname =''
		if source[ch_list[i][3]] then
		 t[i].sortname = (source[ch_list[i][3]].sortname or '').. ch_list[i][1] 
		end
	    t[i].Adress = '$TVSSTART="' .. ch_list[i][1] .. '" ' .. ch_list[i][2]
		t[i].Adr1 = ch_list[i][2]
		if retAdr == ch_list[i][2] then b=i end	
		i = i+1
	until i > #ch_list

	tvs_func.quicksort(t,'sortname')
	
	b=1
	
	for i=1,#t  do   -- после сортировки индекс тоже надо переприсвоить
	    --if retAdr == t[i].Adr1 then b=i end
	    if TVSources_var.CurrentSource == t[i].Name:gsub(' %b()','') then b=i end
	    t[i].Id = i
	    t[i].Name =  i ..'. ' ..t[i].Name 
	end	
	
	local vers, vers_about = m_simpleTV.Common.GetVersion() 
	local flags = ( (vers <= 580 ) and 8 or 8+64 )  
	
	if i>0 then                            -- начиная с 581 версии используем флаг NOT_CHANGE_MAIN_TITLE=64
	   
	   m_simpleTV.Control.SetTitle(m_simpleTV.Control.CurrentTitle_UTF8:gsub("%b[]%s+",""))
	   
	   err, i = m_simpleTV.OSD.ShowSelect_UTF8(tvs_core.tvs_GetLangStr('source_sel'),b-1,t,10000, 8+64)	   
       m_simpleTV.Common.SetForeground()
	   
	   
	   if err == 1 then 
	     TVSources_var.CurrentSource    = t[i].InfoPanelDesk 
	     TVSources_var.CurrentSourceSel = t[i].Name
	     --m_simpleTV.Control.SetTitle(m_simpleTV.Control.CurrentTitle_UTF8:gsub("%b[]%s+",""))

	     --m_simpleTV.Control.CurrentAdress = t[i].Adress
	     --m_simpleTV.Control.PlayAddress_UTF8( t[i].Adress, m_simpleTV.Control.CurrentTitle_UTF8 ) 
	     --m_simpleTV.OSD.ShowMessage_UTF8( 't[i].Name='..(t[i].Name or '')..'\nerr='..(err or 'nil')..'\n i='..(i or 'nil') ..'\n t[i].Adress' .. (t[i].Adress or 'nil')..'\n' ..(t[i].Adr1 or 'nil'),0x00ff0f,15) 	   
	   end
	end
	
else

	--local s = m_simpleTV.Control.CurrentAdress or 'nil'
	--m_simpleTV.OSD.ShowMessage_UTF8(s ,0xffffff,5)

end