-------------------------------------------------------------------------------------------------------
if m_LinkToPlay.Control.ChangeAddress ~= 'No' then return end

--http://www.youtube.com/watch?v=YaK6ESL_SG0
local inAdr = m_LinkToPlay.Control.CurrentAddress

if inAdr == nil then return end
inAdr = string.gsub(inAdr, 'https', 'http')

if string.match(inAdr, '^http://www%.youtube(.+)&list=WL') then
	inAdr = findpattern(inAdr, '^(.-)&', 1, 0, 1)
end

if string.match( inAdr, '^http://youtu%.be' ) then
	inAdr = findpattern(inAdr, 'youtu%.be(.+)$', 1, 8, 0)
	inAdr = 'http://www.youtube.com/watch' .. inAdr
end

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

if string.match(inAdr, '^http://www%.youtube%.com/embed') then
	local id = findpattern(inAdr, 'embed/(.+)', 1, 6, 0)
	if id == nil then return end
	inAdr = 'http://www.youtube.com/watch?feature=player_embedded&v=' .. id
end

if not string.match(inAdr, '^http://www%.youtube%.com/watch') and not string.match(inAdr, '^http://www%.youtube%.com/attribution_link') then return end

if string.match(inAdr, 'list=') then
	local pls = findpattern(inAdr, 'list=(.-)$', 1, 5, 0)
	inAdr = 'http://www.youtube.com/playlist?list=' .. pls
	m_LinkToPlay.Control.PlayAddress(inAdr)
	return
end

require('youtubecore')

m_LinkToPlay.Control.ChangeAddress = 'Yes'
m_LinkToPlay.Control.CurrentAddress = ''

local session = m_LinkToPlay.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_LinkToPlay.WinInet.SetOptionInt(session, 2, 3000, 0)
--INTERNET_OPTION_RECEIVE_TIMEOUT
m_LinkToPlay.WinInet.SetOptionInt(session, 6, 5000, 0)

local function unescape_html(str)
  str = string.gsub( str, '&lt;', '<' )
  str = string.gsub( str, '&gt;', '>' )
  str = string.gsub( str, '&quot;', '"' )
  str = string.gsub( str, '&apos;', "'" )
  str = string.gsub( str, '&#(%d+);', function(n) return string.char(n) end )
  str = string.gsub( str, '&#x(%d+);', function(n) return string.char(tonumber(n,16)) end )
  str = string.gsub( str, '&amp;', '&' ) -- Be sure to do this after all others
  return str
end

local rc, answer = m_LinkToPlay.WinInet.Get(session, inAdr)
if rc ~= 200 then
	m_LinkToPlay.WinInet.Close(session)
	--m_simpleTV.OSD.ShowMessage("Connection error - " .. rc ,255,10)
	return
end
answer = m_LinkToPlay.Common.string_fromUTF8(answer)

local header = findpattern(answer, '<title>(.-)</title>', 1, 7, 8)
--if header == nil then local header =  findpattern(answer,'<meta property="og:title" content="(.-)">', 1, 35, 2) end
if header == nil then header = inAdr end
header = string.gsub(header, '&quot;', '"')
header = string.gsub(header, '&raquo;', '')
header = string.gsub(header, '&#39;', "'")
if m_LinkToPlay.Control.CurrentTitle ~= nil then m_LinkToPlay.Control.CurrentTitle = unescape_html(header) end

--livestream
local hlsvp = findpattern(answer, '"hlsvp":(.-)",', 1, 10, 2)
if hlsvp ~= nil then
	hlsvp = unescape(hlsvp)
	hlsvp = string.gsub(hlsvp, 'https', 'http')
	hlsvp = string.gsub(hlsvp, "\\", '')
	rc, answer = m_LinkToPlay.WinInet.Get(session, hlsvp)
	m_LinkToPlay.WinInet.Close(session)
	if rc ~= 200 then return end
	--debug_in_file(answer .. '\n')
	local i, t = 1, {}
	local name, adr
	for w in string.gmatch(answer, 'EXT%-X%-STREAM%-INF(.-)m3u8') do
		adr = findpattern(w, "http(.-)index", 1, 0, 0)
		if adr == nil then break end
		name = findpattern(w, 'RESOLUTION=(.-)\n', 1, 0, 0)
		if name == nil then break end
		t[i]         = {}
		t[i].Id      =  i
		t[i].Name    = name
		t[i].Address = adr .. 'm3u8'
		--debug_in_file(t[i].Name .. '  ' .. t[i].Address .. '\n\n')
		i = i + 1
	end
	local ret = m_LinkToPlay.Taskbar.AddList(header, 0, t, 7000, 0)
else
	m_LinkToPlay.WinInet.Close(session)
	if m_LinkToPlay.Control.CurrentTitle ~= nil then m_LinkToPlay.Control.CurrentTitle = unescape_html(header) end
	local retAdr, header = youtubecore.GetWatchAddress(inAdr, true)
	if retAdr == nil then return end
	if type(retAdr) == 'table' then
		local ret = m_LinkToPlay.Taskbar.AddList(header, 0, retAdr, 7000, -1)
	else
		m_LinkToPlay.Control.CurrentAddress = retAdr
	end
	--debug_in_file(retAdr .. '\n', nil, true)
	--debug_in_file(retAdr .. '\n\n\n', 'C:\\play.txt', true)
end

