if m_simpleTV.Control.ChangeAdress ~= 'No' then return end
local inAdr =  m_simpleTV.Control.CurrentAdress
if inAdr == nil then return end
if not string.match(inAdr, 'http://www%.hdkinoteatr%.com/(.+)') then return end
require('json')
require 'moonwalkcore'
local session = m_simpleTV.WinInet.New("Opera/9.80 (Windows NT 6.1; U; ru) Presto/2.9.168 Version/11.52")
if session == nil then return end
--INTERNET_OPTION_CONNECT_TIMEOUT
m_simpleTV.WinInet.SetOptionInt(session, 2, 3000, 0)
--INTERNET_OPTION_RECEIVE_TIMEOUT
m_simpleTV.WinInet.SetOptionInt(session, 6, 5000, 0)
local rc, answer = m_simpleTV.WinInet.Get(session, inAdr)
if rc ~= 200 then
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.OSD.ShowMessage("Connection error " .. rc .. '\ninAdr=' .. inAdr, 255, 10)
	return
end
m_simpleTV.WinInet.Close(session)
local header = string.match(answer, '<title>(.-)</title>')
if header == nil then header = '' end
header = m_simpleTV.Common.string_fromUTF8(header)
m_simpleTV.Control.CurrentTitle = header
--debug_in_file(inAdr .. '\n\n' .. answer, 'C:\\_TEMP\\debug.txt', true)
local retAdr = ''
local content = string.match(answer, 'var vkArr=(.-);')
if content == nil then
	retAdr = string.match(answer, "	makePlayer%('(.-)'")
	--debug_in_file(retAdr, 'C:\\_TEMP\\debug.txt', true)
else
	local tab = json.decode(content)
	if tab == nil or tab[1] == nil then
		m_simpleTV.OSD.ShowMessage("Error hdkinoteatr.com - can't find address", 255, 10)
		return
	end
	--debug_in_file(content .. '\n\n', 'C:\\_TEMP\\debug.txt', true)
	------------------------------------------------------------------------------
	local t, i = {}, 1
	while true do
		if tab[i] == nil then break end
		t[i] = {}
		t[i].Id = i
		local name = m_simpleTV.Common.string_fromUTF8(tab[i].comment)
		local address = tab[i].file
		if address == nil then address = tab[i].playlist end 
		t[i].Name = name
		t[i].Adress = address
		--debug_in_file(t[i].Adress .. '\n', 'C:\\_TEMP\\debug.txt')
		i = i + 1
	end
	if i > 2 then
		local _, id = m_simpleTV.OSD.ShowSelect(header, 0, t, 0, 1)
		if id == nil then id = 1 end
		retAdr = t[id].Adress
	else
		if t[1] ~= nil then
			retAdr = t[1].Adress
		else
			m_simpleTV.OSD.ShowMessage("Ошибка - видео не найдено", 255, 10)
			return
		end
	end
end
if retAdr == nil then
	m_simpleTV.OSD.ShowMessage("Ошибка - видео не найдено", 255, 10)
	return
end
if type(retAdr) == 'table' then
	local tab = retAdr
	local t, i = {}, 1
	while true do
		if tab[i] == nil then break end
		t[i] = {}
		t[i].Id = i
		local name = m_simpleTV.Common.string_fromUTF8(tab[i].comment)
		t[i].Name = string.gsub(name, '%b<>', ' ')
		t[i].Adress = tab[i].file
		--debug_in_file(t[i].Adress .. '\n', 'C:\\_TEMP\\debug.txt')
		i = i + 1
	end
	if i > 2 then
		local _, id = m_simpleTV.OSD.ShowSelect(header, 0, t, 0, 1)
		if id == nil then id = 1 end
		retAdr = t[id].Adress
	else
		if t[1] ~= nil then
			retAdr = t[1].Adress
		else
			m_simpleTV.OSD.ShowMessage("Ошибка - видео не найдено", 255, 10)
			return
		end
	end
end
--debug_in_file(retAdr .. '\n', 'C:\\_TEMP\\debug.txt', true)
if string.match(retAdr, 'http://moonwalk%.cc/video/') then
	retAdr = moonwalkcore.MoonwalkGetAddress(retAdr)
	debug_in_file(retAdr .. '\n', 'C:\\_TEMP\\debug.txt', true)
	m_simpleTV.Control.PlayAddress(retAdr)
	return
end
if string.match(retAdr, 'www%.youtube%.com/embed/') then
	--debug_in_file('2) ' .. retAdr .. '\n', 'C:\\_TEMP\\debug.txt', true)
	m_simpleTV.Control.CurrentAdress = retAdr
	--m_simpleTV.Control.PlayAddress(retAdr)
	return
end
debug_in_file(retAdr .. '\n', 'C:\\_TEMP\\debug.txt', true)
if not string.match(retAdr, 'oid=(.+)') then return end
local s = string.match(answer, 'if%(code%.search%(/%^oid=/%) %!= %-1%) {(.-)vidWrapper%.innerHTML=')
if s ~= nil then
 --debug_in_file(s .. '\n\n', 'C:\\_TEMP\\debug.txt', true)
 local pattern_1, pattern_2 = string.match(s, '%.(.-)%)%.(.-)%);')
 pattern_1 = string.gsub(pattern_1, 'replace%(/', '')
 pattern_1 = string.gsub(pattern_1, '/g', '')
 pattern_1 = string.gsub(pattern_1, '%b[]', '%.')
 pattern_1 = string.gsub(pattern_1, '{1}', '')
 pattern_1 = string.gsub(pattern_1, '{2}', '%.')
 pattern_1 = string.gsub(pattern_1, '{3}', '%.%.')
 pattern_1 = string.gsub(pattern_1, '%$', '%%') 
 pattern_1, repl_1 = string.match(pattern_1, "^(.-),'(.-)'")
 --debug_in_file(pattern_1 .. '    ' .. repl_1 .. '\n', 'C:\\_TEMP\\debug.txt')
 --Это у всех одинаково
 local code_1 = string.gsub(retAdr, pattern_1, repl_1)
 --debug_in_file(code_1 .. '\n', 'C:\\_TEMP\\debug.txt')
 pattern_2 = string.gsub(pattern_2, 'replace%(/', '')
 pattern_2 = string.gsub(pattern_2, '/g', '')
 pattern_2 = string.gsub(pattern_2, '%b[]', '%.')
 pattern_2 = string.gsub(pattern_2, '{1}', '')
 pattern_2 = string.gsub(pattern_2, '{2}', '%.')
 pattern_2 = string.gsub(pattern_2, '{3}', '%.%.')
 pattern_2 = string.gsub(pattern_2, '%$', '%%')
 pattern_2, repl_2 = string.match(pattern_2, "^(.-),'(.-)'")
 --debug_in_file(pattern_2 .. '    ' .. repl_2 .. '\n', 'C:\\_TEMP\\debug.txt')
 retAdr = string.gsub(code_1, pattern_2, repl_2)
 --debug_in_file(retAdr .. '\n', 'C:\\_TEMP\\debug.txt')
 if not string.find(retAdr, 'http://vk%.com/video_ext%.php') then retAdr = "http://vk.com/video_ext.php?" .. retAdr end
 m_simpleTV.Control.CurrentAdress = retAdr
end