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

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

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

--INTERNET_OPTION_CONNECT_TIMEOUT
m_simpleTV.WinInet.SetOptionInt(session, 2, 3000, 0)
--INTERNET_OPTION_RECEIVE_TIMEOUT
m_simpleTV.WinInet.SetOptionInt(session, 6, 8000, 0)

------------------------------------------------------------------------------------------------------------------
local function GetAddress(inAdr)
	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 id = findpattern(answer, 'data%-video_id="(.-)"', 1, 15, 1)
	if id == nil then  m_simpleTV.OSD.ShowMessage("Script error", 255, 10) return end
	local host = 'http://megogo.net/b/info/'
	local body = 'm=%2D1&h=http%3A%2F%2Fmegogo%2Enet&s=0&rnd=4%2E870214266702533&i=' .. id .. '&t=0&e=0&p=0&l=' 
	local header = 
		'Host: megogo.net\n' ..
		'Accept-Language: ru-RU,ru;q=0.9,en;q=0.8\n' ..
		'Referer: http://megogo.net/s/js/v2/swf/megogo_201305211226.swf\n' ..
		'Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1\n' ..
		'Content-Type: application/x-www-form-urlencoded'
	rc, answer = m_simpleTV.WinInet.Post(session, host, nil, header, body)
	if rc ~= 200 then
		m_simpleTV.WinInet.Close(session)
		m_simpleTV.OSD.ShowMessage("Connection error - " .. rc, 255, 10)
		return
	end
	local str = findpattern(answer, '<src>(.-)</src>', 1, 5, 6)
	str = string.gsub(str, 'manifest.f4m', 'playlist.m3u8')
	rc, answer = m_simpleTV.WinInet.Get(session, str)
	if rc ~= 200 then
		m_simpleTV.WinInet.Close(session)
		m_simpleTV.OSD.ShowMessage("Connection error - " .. rc, 255, 10)
		return
	end
	local retAdr = findpattern(answer, 'http(.-)\n', 1, 0, 0)
	if retAdr == nil then
		m_simpleTV.OSD.ShowMessage_UTF8(RuStr("Ошибка - видео не найдено"), 255, 10)
		return
	end
	return retAdr
end
------------------------------------------------------------------------------------------------------------------
local header

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
answer = RuStr(answer, true) --WIN-1251
--debug_in_file(answer, 'C:\\test.txt', true)
--flashvars="configuration=/b/info/?i=497591%26p=0%26t=%26m=-1%26l=ru"

local retAdr
local content = findpattern(answer, '<div id="playerContent">(.-)</div>', 1, 0, 0)
if content ~= nil then
	--debug_in_file(content .. '\n\n', 'C:\\test.txt', true)
	retAdr = string.match(content, 'flashvars="configuration=(.-)"', 1)
	if retAdr ~= nil then
		retAdr = unescape(retAdr)
		retAdr = 'http://megogo.net' .. retAdr
		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
		answer = RuStr(answer, true) --WIN-1251
		retAdr = string.match(answer, '<src>(.-)</src>', 1)
		header = string.match(answer, '<title><!%[CDATA%[(.-)%]%]></title>', 1)
		if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle = header end
		if retAdr == nil then return end
		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)
		--debug_in_file(answer .. '\n\n', 'C:\\test.txt', true)
		local t, i = {}, 1
		for name, link in string.gmatch(answer, '#EXT%-X%-STREAM%-INF:.-,NAME="(.-)"\n(.-)\n') do
			--debug_in_file(name .. '\n' .. link .. '\n', 'C:\\test.txt')        
			t[i]         = {}
			t[i].Id      = i
			t[i].Name = name
			t[i].Adress  = link
			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 end
		end
		--debug_in_file(header .. '\n\n' .. retAdr, 'C:\\test.txt')		
		m_simpleTV.Control.CurrentAdress = retAdr
		return
	end
end

header = findpattern(answer, '<title>(.-)</title>', 1, 7, 8)
if header == nil then 
	header = inAdr
else
	header = string.gsub(header, '&quot;', '"')
	header = string.match(header, '(.-)%- смотреть', 1)
end

local ser = findpattern(answer, '<div id="videosEpisodesContainer">(.-)<p class="seriesToggle">', 1, 0, 0)
local nm
if ser ~= nil then
	local i, t = 1, {}
	local name, adr
	for w in string.gmatch(ser, '<p class="voi__title">(.-)</p>') do
		name = findpattern(w, '%b><', 1, 1, 1)
		adr = findpattern(w, 'href="(.-)"', 1, 6, 1)
		t[i]      =   {}
		t[i].Id   =   i
		t[i].Name =   RuStr(name)
		t[i].Adress = adr
		--debug_in_file(t[i].Adress .. '\n')
		i = i + 1
	end
	if i > 2 then
		local _, id = m_simpleTV.OSD.ShowSelect_UTF8(RuStr(header), 0, t, 0, 1)
		if id == nil then id = 1 end
		retAdr = t[id].Adress
		nm = t[id].Name
	else
		retAdr = t[1].Adress
		nm = t[1].Name
	end
	if retAdr == nil then
		m_simpleTV.OSD.ShowMessage_UTF8(RuStr("Ошибка - видео не найдено"), 255, 10)
		return
	end
else
	retAdr = inAdr
end
if retAdr == nil then return end
if (nm ~= nil) and (header ~= nil) then header = header .. ' - ' .. RuStr(nm, true) end
if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle = header end

retAdr = GetAddress(retAdr)
--debug_in_file(retAdr.."\n\n")
m_simpleTV.WinInet.Close(session)
m_simpleTV.Control.CurrentAdress = retAdr