content: 'Initializing Home screen
  sub init()
      initializeUIComponents()
  end sub
  
  'Initializing homescreen UI components
  sub initializeUIComponents()
      m.homeTrays = m.top.findNode("homeTrays")
      m.slideAnimation = m.top.findNode("slideAnimation")
      m.slideInterp = m.top.findNode("slideInterp")
      m.loadingIndicator = m.top.findNode("loadingIndicator")
      m.subscriptionStatus = false
      initializeIndexes()
  end sub
  
  'Initialize the indexes for the list
  sub initializeIndexes()
      m.viewportYPosArr = []
      m.yindex = 50
      m.xindex = 50
      m.xtranslate = 0
      m.rowItemSize = []
      m.rowHeights = []
      m.rowIndex = 0
      m.offsetIndex = 0
      m.pagenationNeeded = true
      m.shelfNode=createObject("roSGNode","ContentNode")
  end sub
  
  'Do the foscus handling
  sub setFocus()
      m.focusKey = 0
      updateFocus()
  end sub
  
  'Invoke landing page api
  sub loadDataForUI()
      loadingStart()
      getURLCall(getBaseURL()+"/comics?limit=20&offset="+m.offsetIndex.toStr(),"contentDataCall")
  end sub
  
  
  SUb loadContentData(json as Object)
      jsonData=parseJSON(json)
      if jsonData.data <> invalid and jsonData.data.results <> invalid and jsonData.data.results.Count() > 0
          contentData=jsonData.data.results
          parentNode=createObject("roSGNode","ContentNode")
          index = 0
          for each data in contentData
              itemNode=createObject("roSGNode","CustomContentNode")
              if data.thumbnail<>invalid and data.thumbnail.path<> invalid and data.thumbnail.extension <> invalid
                  itemNode.thumbnailImage=data.thumbnail.path+"."+data.thumbnail.extension
              end if
              if data.id<>invalid
                  itemNode.id=data.id
              end if
              if data.characters <> invalid and data.characters.collectionURI <> invalid
                  itemNode.collectionURI = data.characters.collectionURI
              end if
              itemNode.contentType = "contentcollection"
              itemNode.contentTitle=data.title
              m.shelfNode.appendChild(itemNode)
              index+=1
              if index MOD 4 = 0
                  parentNode.appendChild(m.shelfNode)
                  rowCreator(parentNode) 
                  parentNode=createObject("roSGNode","ContentNode")
                  m.shelfNode=createObject("roSGNode","ContentNode")
                  index = 0
              end if
          end for 
  
          if index < 4  and index > 0
              parentNode.appendChild(m.shelfNode)
              rowCreator(parentNode) 
          endif
            
          if m.homeTrays.getChildren(3000,0).Count()>0
              m.homeTrays.visible=true
              lastData=m.viewportYPosArr[m.viewportYPosArr.Count()-1]
              m.viewportYPosArr.Pop()
              m.viewportYPosArr.Push(lastData+258)
          end if
      end if
      m.focusKey = 0
      updateFocus()
  ENd SUb
  
  
  Sub rowCreator(node as dynamic)
      rowListhomeRow=m.homeTrays.createChild("Rowlist")
      rowListhomeRow.ObserveField("rowItemSelected","itemSelectionController")
      rowListhomeRow.translation=[50,m.xindex]
      rowListhomeRow.content=node
      rowListhomeRow.focusXOffset=[(0)]
      rowListhomeRow.itemComponentName="contentRowDesign"
      rowListhomeRow.rowFocusAnimationStyle="floatingFocus"
      rowListhomeRow.fadeFocusFeedbackWhenAutoScrolling=true
      rowListhomeRow.showRowLabel=false
      rowListhomeRow.numRows="1"
      rowListhomeRow.showRowCounter="false"
      m.viewportYPosArr.Push(m.yindex)
      rowListhomeRow.rowItemSize=[[400,600]]
      m.yindex=(m.yindex-(600+100))
      m.xindex=(m.xindex+600+100)
      rowListhomeRow.rowHeights=[[750]]
      rowListhomeRow.rowSpacings=[0]
      rowListhomeRow.itemSize=[1900,750]
      rowListhomeRow.itemSpacing=[0,0]
      rowListhomeRow.rowItemSpacing=[[75,0]]
      rowListhomeRow.rowLabelOffset=[[(0),(12)]]
      rowListhomeRow.rowLabelFont=fonts().getFontBold(32)
      rowListhomeRow.visible=true
  end sub
  
  
  'Animation handler for handling scrolling of list views
  sub scrollPane(index as integer)
      targetIndex = index
      currPos = m.homeTrays.translation
  
  
      targetPos = [0, m.viewportYPosArr[targetIndex]]
  
      if currPos[1] <> targetPos[1]:
          m.slideInterp.keyValue = [currPos, targetPos]
          m.slideAnimation.control = "start"
      end if
  
      m.homeTrays.getChildren(3000, 0)[m.rowIndex].setFocus(true)
  
  end sub
  
  'Function updates the focus of the currently selected item
  sub updateFocus()
      if m.global.meData<>invalid and m.global.meData.data.me.subscriptionAccount<>invalid and m.global.meData.data.me.subscriptionAccount.isActive<>invalid
          m.subscribed = m.global.meData.data.me.subscriptionAccount.isActive
      else if m.global.meData<>invalid and m.global.meData.data.me.activeUserFreePass<>invalid 
          m.subscribed  = true
      end if
      if m.focusKey = 0
          if m.homeTrays.getChildren(3000, 0)[m.rowIndex] <> invalid
              if m.homeTrays.getChildren(3000, 0)[m.rowIndex].id = "heroBanner"
                  m.homeTrays.getChildren(3000, 0)[m.rowIndex].setFocus = true
              else
                  if m.homeTrays.getChildren(3000, 0)[m.rowIndex-1]<>invalid and m.homeTrays.getChildren(3000, 0)[m.rowIndex-1].id = "heroBanner"
                      if m.subscribed = false
                          m.homeTrays.getChildren(3000, 0)[m.rowIndex-1].watchRecturi="pkg:/images/buttonimages/findoutmoreCta.png"
                      else
                          m.homeTrays.getChildren(3000, 0)[m.rowIndex-1].watchRecturi="pkg:/images/buttonimages/watchNowUnFocused.png"
                      end if
                  end if
                  m.homeTrays.getChildren(3000, 0)[m.rowIndex].setFocus(true)
              end if
          end if
      end if
  end sub
  
  'Item selection handling for homepage view
  sub itemSelectionController()
      rowList = m.homeTrays.getChildren(3000, 0)[m.rowIndex]
      contentData = rowList.content.getChild(rowList.rowItemSelected[0]).getChild(rowList.rowItemSelected[1])
      m.top.contentSelect = contentData
  end sub
  
  'Function that handles the Remotekeyevents
  '@param {string} key- The right/left etc remote button
  '@param {boolean} press- true if the button is pressed and false if button is not pressed
  '@return {boolean} The click is true/false
  function onKeyEvent(key as string, press as boolean) as boolean
      result = false
      if press then
          if key = "back"
              if m.rowIndex <> 0
                  m.rowIndex = 0
                  if m.homeTrays.getChildren(3000, 0)[m.rowIndex] <> invalid
                      scrollPane(m.rowIndex)
                  end if
                  result = true
              end if
          else if key = "options"
  
          else if key = "play"
  
          else if key = "down"
              m.rowIndex += 1
              if m.homeTrays.getChildren(3000, 0)[m.rowIndex] <> invalid
                  scrollPane(m.rowIndex)
              else
                  m.rowIndex -= 1
              end if
              if (m.rowIndex = m.homeTrays.getChildCount()-1) and m.pagenationNeeded
                  m.offsetIndex+=20
                  loadDataForUI()
              end if        
              result = true
          else if key = "up"
              if m.homeTrays.getChildren(3000,0)[m.rowIndex]<>invalid and m.rowIndex>0
                  m.rowIndex-=1
                  scrollPane(m.rowIndex)
                  result=true
              end if
          else if key = "left"
  
          end if
      end if
      return result
  end function