--script for now.ru (17/10/2014)
--open link http://www.now.ru/item/animation/foreign/Las_aventuras_de_Tadeo_Jones_261232

require('nowrucore')

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

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

if string.match( inAdr, '/embed/' )  or string.match( inAdr, '#%d+' )  then 
  m_simpleTV.Control.CurrentAdress = nowrucore.nowru_getaddress(inAdr)  
   return
end

---------------------------------------------------------------------------
function trim1(s)
  return (s:gsub("^%s*(.-)%s*$", "%1"))
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 = m_simpleTV.Common.string_fromUTF8(answer)
 
local header =  findpattern( answer,'<span>(.-)</span>',1,6,7)
if header==nil then header=inAdr end
header = trim1(header)
if m_simpleTV.Control.CurrentTitle ~= nil then m_simpleTV.Control.CurrentTitle=header end

local ser = findpattern (answer,'<div class="balloontabs">(.-)<div id="stabs">',1,0,0)
if ser ~= nil then

local i=1
local tt={}
local retAdr,sname,adr

  for w in string.gmatch(ser,'<div class="c">(.-)</div>') do
	adr = findpattern (w,'%b""',1,1,1)
	sname = findpattern (w,'%b><',1,1,1)
	if adr == nil or sname == nil then break end

     tt[i] = {}
     tt[i].Id   =  i
     tt[i].Name =  sname 
     tt[i].Adress  = 'http://www.now.ru' .. adr

--debug_in_file(tt[i].Name .. '  ' .. tt[i].Adress .. '\n\n')

    i=i+1
  end 
 
if i>1 then
    local ret,id = m_simpleTV.OSD.ShowSelect('Выбор сезона',0,tt,10000,1+4+8)
  if ret==1 then

    rc,answer=m_simpleTV.WinInet.Get(session,tt[id].Adress)

 	 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("Connection error - " .. rc ,255,10)
	   return
	 end

 answer = m_simpleTV.Common.string_fromUTF8(answer)
 --end	
--debug_in_file(answer .. '\n')
local s = findpattern (answer,'size: 80%%">(.-)<',1,11,1)
if s == nil then s='' end
s = trim1(s)

local tmp = findpattern (answer,'<div class="short%-scroll%-content"(.-)<aside id="right_banner_container">',1,0,0)

 if tmp == nil then return end
 tmp = tmp:gsub('[\n\r\t]','')

local a=1
local t={}

  for w in string.gmatch(tmp,'href=(.-)/div></div>') do
	adr = findpattern (w,'%b""',1,1,1)
	local name = findpattern (w,'margin%-top:2px;">(.-)<',1,17,1)
	if adr == nil or name == nil then break end

     t[a] = {}
     t[a].Id   =  a
     t[a].Name =  s .. ' ' .. name
     t[a].Adress  = 'http://www.now.ru' .. adr ..'#' .. a

--debug_in_file(t[a].Name .. '  ' .. t[a].Adress .. '\n\n')

    a=a+1
  end 
 

if a >2 then
    m_simpleTV.OSD.ShowSelect(header,0,t,5000)
 end

 if a >1 then
	retAdr = nowrucore.nowru_getaddress(t[1].Adress)
	if retAdr == nil then retAdr ='' end
  end

m_simpleTV.WinInet.Close(session)
m_simpleTV.Control.CurrentAdress = retAdr

   end 
  end
end

if ser == nil then
local tmp = findpattern (answer,'<div class="short%-scroll%-content"(.-)<aside id="right_banner_container">',1,0,0)

  if tmp == nil then 
    m_simpleTV.Control.CurrentAdress = nowrucore.nowru_getaddress(inAdr)
    return 
  end
 tmp = tmp:gsub('[\n\r\t]','')

local a=1
local t={}

  for w in string.gmatch(tmp,'href=(.-)/div></div>') do
	adr = findpattern (w,'%b""',1,1,1)
	local name = findpattern (w,'margin%-top:2px;">(.-)<',1,17,1)
	if adr == nil or name == nil then break end

     t[a] = {}
     t[a].Id   =  a
     t[a].Name =  name
     t[a].Adress  = 'http://www.now.ru' .. adr ..'#' .. a

--debug_in_file(t[a].Name .. '  ' .. t[a].Adress .. '\n\n')

    a=a+1
  end 
 

if a >2 then
    m_simpleTV.OSD.ShowSelect(header,0,t,5000)
 end

 if a >1 then
	retAdr = nowrucore.nowru_getaddress(t[1].Adress)
	if retAdr == nil then retAdr ='' end
  end

m_simpleTV.WinInet.Close(session)
m_simpleTV.Control.CurrentAdress = retAdr
end

