

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, 'vtraheporn%.com') then return end

--debug_in_file(inAdr, 'C:\\_TEMP\\tmp.txt', true)

m_simpleTV.Control.ChangeAdress = 'Yes'
m_simpleTV.Control.CurrentAdress = ''
---------------------------------------------------------------------------
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

require 'json'

function RuStr(Str, isFrom)
  if Str == nil then return end
  if isFrom == true then
   return m_simpleTV.Common.string_fromUTF8(Str, 1251)
  else
    return m_simpleTV.Common.string_toUTF8(Str, 1251)
  end
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, 255, 10)
	return
end

local header = findpattern(answer, '<title>(.-)</title>', 1, 7, 8)
if header == nil then header = inAdr end
header = string.gsub(header, '&raquo;', "»")
if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle = header end

local retAdr
--debug_in_file('', 'C:\\_TEMP\\tmp.txt', true)
--debug_in_file(answer .. '\n\n\n', 'C:\\_TEMP\\tmp.txt', true)
--/play/index.php?oid=289919888&id=171299726&hash=f7620838624f82af&hd=2
for w in string.gmatch(answer, '<iframe.-src="(.-)".-</iframe>') do
	--debug_in_file(w .. '\n', 'C:\\_TEMP\\tmp.txt')
	--http://video.vtraheporn.com/vp_iframe_embed.php?video_id=9161
	
	if string.match(w, 'play/index%.php%?oid=') then
		retAdr = string.gsub(w, '/play/index%.php', '/method/video%.getEmbed')
		retAdr = string.gsub(retAdr, '&id=', '&video_id=')
		retAdr = string.gsub(retAdr, '&hash=', '&embed_hash=')
		retAdr = 'https://api.vk.com' .. retAdr
		break 
	end
	if string.match(w, 'vp_iframe_embed%.php%?video_id=') then
		retAdr = w
		break 
	end
end

if retAdr == nil then return end
--debug_in_file(retAdr .. '\n\n\n', 'C:\\_TEMP\\tmp.txt', true)

--m_simpleTV.WinInet.SetOpenRequestFlags(session, 0x00800000 + 0x00002000 + 0x00001000)
rc, answer = m_simpleTV.WinInet.Get(session, retAdr)
if rc ~= 200 then
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.OSD.ShowMessage("Connection error - " .. rc, 255, 10)
	return
end
m_simpleTV.WinInet.Close(session)

answer = RuStr(answer, true)

--debug_in_file(answer .. '\n\n\n', 'C:\\_TEMP\\tmp.txt', true)

--<a href="#" onclick="changeFormatTo('http://video.vtraheporn.com/get_file/17/a403c7277ce01eafae0ed09bc427806b/9000/9161/9161_240p.mp4/')" class="qualitybutton first">Смотреть в низком<br>качестве</a>

local const = 'onclick="changeFormatTo%(\'(.-)\'%)" class="qualitybutton.-">(.-)</a>'

local i, t = 1, {}

for link, title in string.gmatch(answer, const) do
	local name = string.gsub(title, '%b\<\>', ' ')
	--debug_in_file(link .. '\n' .. name .. '\n\n', 'C:\\_TEMP\\tmp.txt')
	t[i]        = {}
	t[i].Id     = i
	t[i].Name   = name
	t[i].Adress = link
	--debug_in_file(t[i].Adress .. '\n', 'C:\\_temp\\tmp.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
--debug_in_file(retAdr .. '\n', 'C:\\_temp\\tmp.txt', true)
m_simpleTV.Control.CurrentAdress = retAdr