--hd on=1 off=0
local hd = 0
------------------------------------------
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, 'http://simple%-tv%.com' ) 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")
local rc,answer =  m_simpleTV.WinInet.Get (session,inAdr) 
 if rc~=200 then 
      m_simpleTV.WinInet.Close (session)
	  return 
 end
 
 --debug_in_file(answer .. '\n\n')

 local channel,phpsession,datestr
   
 phpsession=findpattern(answer,'session=(.-)&',1,8,1)
 channel=findpattern(answer,'channel=(.-)&',1,8,1)
 datestr=findpattern(answer,'date=(.-)"',1,5,1)
  
 if channel==nil or phpsession==nil or datestr==nil then 
	   m_simpleTV.WinInet.Close (session)
	  return 
 end
 
 local adr1 = 'http://clients.cdnet.tv/flashbroadcast.php?channel=' .. channel .. '&session=' .. phpsession .. '&date=' .. datestr
  
 rc,answer =  m_simpleTV.WinInet.Get (session,adr1) 
 if rc~=200 then 
      m_simpleTV.WinInet.Close (session)
	  return 
 end
 
 --debug_in_file(answer .. '\n\n')
 --debug_in_file(phpsession .. '\n\n')
 
 adr1 = 'http://clients.cdnet.tv/flashplayer/instruction.php?userid=0&videoid=' .. channel .. '&type=stream&sessid=' .. phpsession .. '&' .. math.random() 
 --local header = 'Referer: http://clients.cdnet.tv/flashplayer/player.swf?date=' .. datestr
 
 rc,answer =  m_simpleTV.WinInet.Get (session,adr1) --,nil,0,hed) 
  m_simpleTV.WinInet.Close (session)
 if rc~=200 then 
      m_simpleTV.WinInet.Close (session)
	  return 
 end
  
  --debug_in_file(answer)
  
  --stream_sd="rtmpe://94.242.221.195/4" stream_hd="rtmpe://94.242.221.103/4_hd" chanel_sd="channel_4.stream" chanel_hd="channel_4_hd.stream">
  local stream,playpath,host

 if hd ~= 1 then
  stream = findpattern(answer,'stream_sd=%b""',1,11,1)
  host = findpattern(stream,'rtmpe://(.-)/',1,8,1) 
  playpath =findpattern(answer,'chanel_sd=%b""',1,11,1)

if host == '' then
stream = findpattern(answer,'stream_hd=%b""',1,11,1)
playpath =findpattern(answer,'chanel_hd=%b""',1,11,1)
end

  else
  stream = findpattern(answer,'stream_hd=%b""',1,11,1)
  host = findpattern(stream,'rtmpe://(.-)/',1,8,1)
  playpath =findpattern(answer,'chanel_hd=%b""',1,11,1)

	if host == '' then
	  stream = findpattern(answer,'stream_sd=%b""',1,11,1)
	  playpath =findpattern(answer,'chanel_sd=%b""',1,11,1)
	end
end   

  if stream==nil or playpath==nil then return end
  local outAdr = 'rtmp://$OPT:rtmp-raw=' .. stream .. '/' .. playpath .. ' live=1 token=Rd#n@k72JDh pageurl=http://tvforsite.net'
  
 m_simpleTV.Control.CurrentAdress = outAdr 
 
 
 
 
 