--get address
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, 'cxz%.to/' ) then return end
if  string.match( inAdr, 'o/get/' ) then return end
if  string.match( inAdr, '^http://cxz%.to/flist/' ) then 
        dofile(m_simpleTV.MainScriptDir .. "user\\video\\cxztoAlt.lua") 
    return
end

m_simpleTV.Control.ChangeAdress='Yes'
m_simpleTV.Control.CurrentAdress = 'error'

if not string.match(inAdr,'/view/') then
 local d,f =   inAdr:match'(.*/)(.*)' 
 if d == nil or f ==nil then return end
 f= findpattern (f,'(.-)%-',1,0,1)
 if f ==nil then return end
 inAdr = d .. 'view/' .. f 
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)
 m_simpleTV.WinInet.Close(session)
 if rc~=200 then
	   m_simpleTV.OSD.ShowMessage("Net error " .. rc,255,5)
	   return
 end

 answer = m_simpleTV.Common.string_fromUTF8(answer) 
  
 local title = findpattern(answer,'<title>(.-)</title>',1,7,8)
 if title~=nil then 
    m_simpleTV.Control.CurrentTitle = title 
  else title = ''	
 end
 
 local pl = findpattern( answer,"playlist:(%s*)%b[]",1,0,0)
 if pl == nil then return end
 pl = pl:gsub('[\n\r\t]','')
 pl = pl:gsub('download_url','download_url1')
  
 local t={}
 local i=1
 
  for w in string.gmatch(pl,'({url(.-)}})') do
	local url = findpattern( w,"url:(.-),",1,4,1)
	if url ~=nil then
			url = url:gsub('[ \'",]','')	
            local name =  findpattern( w,"file_name:(.-),",1,10,1) or 'Серия ' .. i
			name = name:gsub('[\'",]','')	
			t[i] = {}
			t[i].Id   =  i
			t[i].Name = name
			t[i].Adress  = 'http://cxz.to' .. url
			
			name = findpattern( w,"file_series:(.-),",1,12,1) or '1'
			name = name:gsub('[ \'",]','')
			t[i].Comp = tonumber(name,10) or 1
						
			i=i+1
	end
  end
  
 if t[1] == nil then return end 
 if i>2 then 
    table.sort (t, function(a,b) return a.Comp<b.Comp end)
	m_simpleTV.OSD.ShowSelect(title,0,t,5000)
	
 end 
 
 m_simpleTV.Control.CurrentAdress = t[1].Adress




