/**
   * Switch to page at given index
   *
   * @param index
   */
  public void switchToPage(int index) {

    closeCurrentPages();
    IPage page;
    if (pageController2 != null) {
      if ((!showCover && index % 2 > 0) || (showCover && index % 2 == 0 && index > 0)) {
        index -= 1;
      }
    }
    if (index < contentModel.getPages().getTotalPageCount()) {
      page = contentModel.getPage(index);
    } else {
      page = contentModel.getPage(0);
    }

    if (showCover && index == 0) {
      playerView.showSinglePage();
      switchToPage(page, pageController1);
    } else {
      switchToPage(page, pageController1);
      if (pageController2 != null && index + 1 < contentModel.getPages().getTotalPageCount()) {
        playerView.showTwoPages();
        page = contentModel.getPage(index + 1);
        switchToPage(page, pageController2);
      }
    }
  }
 private void pageLoaded(Page page, PageController pageController) {
   pageController.setPage(page);
   if (headerController != null) {
     headerController.setPage(contentModel.getHeader());
   }
   if (footerController != null) {
     footerController.setPage(contentModel.getFooter());
   }
 }
  /**
   * get current loaded page index
   *
   * @return
   */
  public int getCurrentPageIndex() {

    int index = 0;
    for (int i = 0; i < contentModel.getPageCount(); i++) {
      if (contentModel.getPage(i) == pageController1.getPage()) {
        index = i;
        break;
      }
    }
    return index;
  }
 @Override
 public void showPopup(String pageName, String additionalClasses) {
   if (isPopupEnabled()) {
     return;
   }
   setPopupEnabled(true);
   Page page = contentModel.findPageByName(pageName);
   PageController popupPageControler = new PageController(this);
   popupPanel = new PagePopupPanel(getView(), popupPageControler, additionalClasses);
   popupPanel.showPage(page, contentModel.getBaseUrl());
 }
 public void initHeaders() {
   if (contentModel.getHeader() != null) {
     playerView.showHeader();
     headerController = new PageController(pageController1.getPlayerServices());
     headerController.setView(playerView.getHeaderView());
     //			headerController.setPage(contentModel.getHeader());
   }
   if (contentModel.getFooter() != null) {
     playerView.showFooter();
     footerController = new PageController(pageController1.getPlayerServices());
     footerController.setView(playerView.getFooterView());
     //			footerController.setPage(contentModel.getFooter());
   }
 }
  private void switchToPage(IPage page, final PageController pageController) {

    HashMap<String, String> params = new HashMap<String, String>();
    params.put("page", page.getId());
    sendAnalytics("switch to page", params);
    // Load new page
    String baseUrl = contentModel.getBaseUrl();
    XMLLoader reader = new XMLLoader((IXMLSerializable) page);
    String url = URLUtils.resolveURL(baseUrl, page.getHref());
    playerView.showWaitDialog();
    reader.load(
        url,
        new ILoadListener() {

          @Override
          public void onFinishedLoading(Object obj) {
            Page page = (Page) obj;
            pageLoaded(page, pageController);
            if (pageLoadListener != null) {
              pageLoadListener.onFinishedLoading(obj);
            }
            playerView.hideWaitDialog();
            if (timeStart == 0) {
              timeStart = System.currentTimeMillis();
            }
            scrollViewToBeggining();
          }

          @Override
          public void onError(String error) {
            playerView.hideWaitDialog();
            JavaScriptUtils.log("Can't load page: " + error);
          }
        });
  }
  public PlayerController(Content content, PlayerView view, boolean bookMode) {

    contentModel = content;
    playerView = view;
    playerView.setPlayerController(this);
    sessionId = UUID.uuid();
    scoreService = new ScoreService(contentModel.getScoreType() == ScoreType.last);
    stateService = new StateService();
    createPageControllers(bookMode);
  }
 public void switchToPrevPage() {
   PageList pages = contentModel.getPages();
   for (int i = 0; i < pages.getTotalPageCount(); i++) {
     if (pages.getAllPages().get(i) == pageController1.getPage()) {
       int index = i - 1;
       if (pageController2 != null && index > 0) {
         index -= 1;
       }
       if (index >= 0) {
         switchToPage(index);
       }
       break;
     }
   }
 }