if m_simpleTV.Control.ChangeAdress ~= 'No' then return end

local inAdr = m_simpleTV.Control.CurrentAdress
--debug_in_file(inAdr .. '\n\n', 'C:\\test.txt', true)
if inAdr == nil then return end

if not string.match(inAdr, 'kinopod%.') then return end

m_simpleTV.Control.ChangeAdress = 'No'
m_simpleTV.Control.CurrentAdress = ''

---------------------------------------------------------------------------
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

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, 255, 10)
	return
end

answer = RuStr(answer, true)

local header = findpattern(answer, '<title>(.-)</title>', 1, 7, 8)
if header == nil then header = inAdr end

if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle = header end

function Megogo_Net(answer, retAdr)
	local content = string.match(answer, '<!%-%- seasons %-%->(.-)<!%-%- providers %-%->')
	if content == nil then return end
	local t, i = {}, 1
	for w in string.gmatch(content, '<div class="item">(.-)</div>') do
		local name, link = string.match(w, 'title="(.-)" .- href="(.-)"')
		link = string.gsub(link, '&amp;', "&")
		if name == nil then break end
		local season = string.match(link, '&season=(%d-)&')
		--debug_in_file(name .. '\n' .. link .. '\n' .. season .. '\n\n', 'C:\\test.txt')        
		t[i]         = {}
		t[i].Id      = i
		t[i].Name = RuStr(name, true) .. ' (Сезон ' .. season .. ')'
		t[i].Adress  = 'http://kinopod.ru' .. 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
	rc, answer = m_simpleTV.WinInet.Get(session, retAdr, nil, nil, 'Referer: ' .. inAdr)
	if rc ~= 200 then
		m_simpleTV.WinInet.Close(session)
		m_simpleTV.OSD.ShowMessage("Connection error - " .. rc, 255, 10)
		return
	end
	retAdr = string.match(answer, '<iframe .- src="(.-)"')
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.Control.ChangeAdress = 'Yes'
	--debug_in_file(retAdr .. '\n\n', 'C:\\test.txt', true)
	m_simpleTV.Control.PlayAddress(retAdr)
end

function Tvzavr_Ru(retAdr)
	retAdr = unescape(retAdr)
	--http://www.tvzavr.ru/js/v5/tvzavrplayer2.swf?autoplay=0&ref=http://www.tvzavr.ru/api/tzf/video-url/6990
	retAdr = string.match(retAdr, 'autoplay=0&ref=(.+)')
	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
	--debug_in_file(answer .. '\n\n', 'C:\\_TEMP\\debug.txt', true)
	retAdr = string.match(answer, 'video url="(.-)"', 1)
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.Control.ChangeAdress = 'Yes'
	m_simpleTV.Control.PlayAddress(retAdr)
end

if string.match(inAdr, 'kinopod%.ru') then
	--debug_in_file(answer .. '\n\n', 'C:\\test.txt')
	local tmp = findpattern(answer,'<div class="view%-online%-tabs.- id="online%-tabs">(.-)</div>', 1, 0, 0)
	if tmp == nil then return end
	tmp  = findpattern(tmp,'src=&quot;(.-)&amp;',1, 10, 1)
	rc, answer = m_simpleTV.WinInet.Get(session, tmp, nil, nil, 'Referer: ' .. inAdr)
	if rc ~= 200 then
			m_simpleTV.WinInet.Close(session)
			m_simpleTV.OSD.ShowMessage("Connection error - " .. rc ,255,10)
			return
	end
	retAdr = string.match(answer, '<embed src="(.-)"', 1)
	if retAdr == nil then retAdr = string.match(answer, '<iframe .- src="(.-)"', 1) end
	if retAdr and string.match(retAdr, 'megogo%.net') then Megogo_Net(answer, retAdr) return end
	if retAdr and string.match(retAdr, 'www%.tvzavr%.ru') then Tvzavr_Ru(retAdr) return end
	if retAdr and string.match(retAdr, 'www%.ivi%.ru') then
		local id = string.match(retAdr, 'videoId=(%d+)&')
		if id then 
			retAdr = "http://www.ivi.ru/watch/" .. id
			m_simpleTV.WinInet.Close(session)
			--debug_in_file(retAdr .. '\n\n', 'C:\\_TEMP\\debug.txt', true)
			m_simpleTV.Control.PlayAddress(retAdr)
		end
		return
	end
end

function Now_Ru(retAdr)
	local 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
	------------------------------------------------------------------------------------------------------------------
	retAdr = string.match(answer, 'src="(.-)"', 1)
	--INTERNET_FLAG_NO_AUTO_REDIRECT 0x00200000
	m_simpleTV.WinInet.SetOpenRequestFlags(session, 0x00200000)
	rc = m_simpleTV.WinInet.Get(session, retAdr) 
	local head = m_simpleTV.WinInet.GetRawHeader(session)
	if head == nil then return nil end 
	if rc == 302 then 
		local head = m_simpleTV.WinInet.GetRawHeader(session)
		if head == nil then return nil end
		head = findpattern(head, 'Location: (.-)\n', 1, 10, 1)
		retAdr = findpattern(head, 'smil=(.-)full_screenshot=', 1, 5, 17)
		retAdr = unescape(retAdr)
		--debug_in_file(retAdr .. '\n\n', 'C:\\_TEMP\\debug.txt', true)
	else
		local _, code = string.match(answer, 'src="(.+)/(.-)"', 1)
		retAdr = 'http://www.now.ru/ajax.php?method=getsmil&content_id=' .. name .. '&efui=' .. code .. '&embed=1'
	end
	--Получаем http://www.now.ru/ajax.php?method=getsmil&content_id=1030130&efui=45f6a641ed6ab5ebc208773be6df9434&embed=1
	-------------------------------------------------------------------------------------------------------------------
	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
	_, retAdr = string.match(answer, '<httpds (.-) src="(.-)">', 1)
	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
	_, retAdr = string.match(retAdr, 'http://(.-)/vod/(.+)', 1)
	retAdr = string.match(answer, '<to>(.-)</to>', 1) .. retAdr
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.Control.PlayAddress(retAdr)
end

function Videomore_Ru(retAdr)
	local 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
	local id = string.match(answer, "'video_id' : '(.-)'", 1)
	if id then
		retAdr = 'http://videomore.ru/video/tracks/' .. id .. '.xml'
		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
		retAdr = string.match(answer, '<video_url>(.-)</video_url>', 1)
		--debug_in_file(retAdr .. '\n\n', 'C:\\_TEMP\\debug.txt', true)
	end
	--debug_in_file(retAdr .. '\n\nvideomore', 'C:\\_TEMP\\debug.txt', true)
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.Control.ChangeAdress = 'Yes'
	m_simpleTV.Control.PlayAddress(retAdr)
end

if string.match( inAdr, 'kinopod%.tv' ) then 
	local tmp  = findpattern(answer,'<table class="se%-list">(.+)', 1, 0, 0)
	if tmp == nil then return end
	--debug_in_file(tmp .. '\n\nivi', 'C:\\_TEMP\\debug.txt', true)
	local retAdr = ''
	local i, t = 1, {}
	for _, link, title in string.gmatch(tmp, '<td>(.-) href="(.-)">(.-)</a></td>') do
		t[i] = {}
		t[i].Id = i
		t[i].Name = title
		t[i].Adress = 'http://kinopod.tv' .. link
		if retAdr == '' then retAdr = t[i].Adress end
		i = i + 1
	end
	local id
	if i > 2 then ret, id = m_simpleTV.OSD.ShowSelect(header, 0, t, 0, 1) end
	--debug_in_file(id .. '\n\nivi', 'C:\\_TEMP\\debug.txt', true)
	if id == nil then id = 1 end
	rc, answer = m_simpleTV.WinInet.Get(session, t[id].Adress)
	if rc ~= 200 then
			m_simpleTV.WinInet.Close(session)
			m_simpleTV.OSD.ShowMessage("Connection error - " .. rc ,255,10)
			return
	end
	--debug_in_file(answer .. '\n\nivi', 'C:\\_TEMP\\debug.txt', true)
	retAdr = string.match(answer, '</iframe>(.-)</div>', 1)
	if retAdr then retAdr = string.match(retAdr, 'href="(.-)"', 1) end
	if not retAdr then
		_, retAdr = string.match(answer, '<iframe name="now(%d+)" src="(.-)"', 1)
		if retAdr then Now_Ru(retAdr) return end
	end
	
	if not retAdr then
		_, retAdr = string.match(answer, '<iframe(.-)src="(.-)"', 1)
	end
	if retAdr and string.match(retAdr, 'dreampartners%.ru') then Videomore_Ru(retAdr) return end
end

answer = m_simpleTV.Common.string_fromUTF8(answer)
--debug_in_file(answer .. '\n\n', 'C:\\_TEMP\\debug.txt', true)

local name, retAdr = string.match(answer, 'iframe name="now(.-)" src="(.-)"', 1)

if retAdr then Now_Ru(retAdr) return end

retAdr  = findpattern(answer, "player.addVariable%('video', '(.-)'%);", 1, 29, 3)

function Zoomby_Ru()
	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
	retAdr = ''
	local i, t = 1, {}
	for host, file, rate, sign, label in string.gmatch(answer, 'video streamer="(.-)" file="(.-)" system%-bitrate="(%d+)" sign="(.-)" label="(.-)"(.-) />') do
		t[i] = {}
		t[i].Id = i
		t[i].Name = m_simpleTV.Common.string_fromUTF8(label .. '(' .. sign .. ')')
		t[i].Adress = host .. file
		if retAdr == '' then retAdr = t[i].Adress end
		--debug_in_file(t[i].Adress .. '\n\n')
		i = i + 1
	end
	if i > 2 then m_simpleTV.OSD.ShowSelect(header, 0, t, 5000) end
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.Control.ChangeAdress = 'Yes'
	m_simpleTV.Control.CurrentAdress = retAdr
end

if retAdr then Zoomby_Ru() return end

--<embed src="http://www.ivi.ru/video/player?siteId=s161&videoId=100809&_isB2C=1"
retAdr = findpattern(answer, '<embed src="(.-)"', 1, 12, 1)
if retAdr then
	if string.match(retAdr, 'www%.tvzavr%.ru') then Tvzavr_Ru(retAdr) return end
	--debug_in_file(retAdr .. '\n\n0', 'C:\\_TEMP\\debug.txt', true)
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.Control.ChangeAdress = 'Yes'
	m_simpleTV.Control.CurrentAdress = retAdr
	return
end

function Tvigle_Ru()
	retAdr = findpattern(retAdr, 'prt=(.-)&w=', 1, 0, 3)
	retAdr = 'http://pub.tvigle.ru/xml/index.php?' .. retAdr .. '&mode=1&geo=RU'
	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
	retAdr = string.match(answer, 'videoLink="(.-)"', 1)
	--debug_in_file(retAdr .. '\n\n', 'C:\\_TEMP\\debug.txt', true)
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.Control.ChangeAdress = 'Yes'
	m_simpleTV.Control.CurrentAdress = retAdr
end

retAdr = findpattern(answer, '<embed id=(.-)</embed>', 1, 7, 8)
if retAdr then 
	if string.find(retAdr, 'pub.tvigle.ru', 1, true) then Tvigle_Ru() return end
end

--player.addVariable('file', bdec('aHR0cDovL2JzLmJvb21zdHJlYW0uY29tL2FkYXB0aXZlL29Wd1FwRzlkL3BsYXlsaXN0Lm0zdTg='));
retAdr = string.match(answer, "player.addVariable%('file', bdec%('(.-)'%)%);", 1)
if retAdr then
	retAdr = decode64(retAdr)
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.Control.ChangeAdress = 'Yes'
	m_simpleTV.Control.CurrentAdress = retAdr
	return
end

--<iframe width="640" height="360"  src="http://megogo.net/e/2654" frameborder="0" allowfullscreen></iframe>
_, retAdr = string.match(answer, '<iframe(.+) src="(.-)"', 1)
if retAdr and string.match( retAdr, 'megogo%.net' ) then
	retAdr = string.gsub(retAdr, 'megogo%.net/e/', 'megogo%.net/view/')
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.Control.ChangeAdress = 'Yes'
	m_simpleTV.Control.PlayAddress(retAdr)
	return
end

--<a href="http://kinopod.tv/serials/view/matros-zheleznyak.html" class="link-online" target="_blank">
retAdr = string.match(answer, '<a href="(.-)" class="link%-online" target="_blank">', 1)
if retAdr then
	--debug_in_file(retAdr .. '\n\n2', 'C:\\_TEMP\\debug.txt', true)
	m_simpleTV.WinInet.Close(session)
	m_simpleTV.Control.ChangeAdress = 'Yes'
	m_simpleTV.Control.PlayAddress(retAdr)
end