
/* ==================== Chapters Controller ==================== */

var chaptersController = new TKPageSliderController({
  id: 'chapters',
  actions: [
    { selector: '.play', action: bookletController.playFeature },
    { selector: '.resume', action: bookletController.resumeFeature }
  ],
  outlets: [
    { name: 'label', selector: '.label' },
    { name: 'resumeButton', selector: '.resume'}
  ],
  previousPageButton : '.left-arrow',
  nextPageButton : '.right-arrow',
  backButton: '.home'
});

/* ==================== View Management ==================== */

chaptersController._elementWasUnhighlighted  = TKPageSliderController.prototype.elementWasUnhighlighted;
chaptersController._elementWasHighlighted = TKPageSliderController.prototype.elementWasHighlighted;

chaptersController.elementWasUnhighlighted = function(currentTarget, relatedTarget) {
    if (IS_APPLE_TV && currentTarget != relatedTarget) {
        elements = currentTarget.getElementsByTagName("img");
        if (elements != null && elements.length > 1
                && elements[1].src.length > 15
                && elements[1].src.substr(-15, 15) == 'buttonPlayCircle.png') {
            currentTarget.getElementsByTagName("img")[1].style.display = 'none';
        }
    }

    chaptersController._elementWasUnhighlighted(currentTarget, relatedTarget);
}

chaptersController.elementWasHighlighted = function(currentTarget, relatedTarget) {
    if (IS_APPLE_TV && currentTarget != relatedTarget) {
        elements = currentTarget.getElementsByTagName("img");
        if (elements != null && elements.length > 1
                && elements[1].src.length > 15
                && elements[1].src.substr(-15, 15) == 'buttonPlayCircle.png') {
            currentTarget.getElementsByTagName("img")[1].style.display = 'block';
        }
    }

    chaptersController._elementWasHighlighted(currentTarget, relatedTarget);
}

chaptersController.viewDidLoad = function () {
  // customize the sliding view
  this.slidingViewData = {
    orientation: TKSlidingViewOrientationHorizontal,
    activeElementIndex: 0,
    sideElementsVisible: 2,
    distanceBetweenElements: 600,  // distance between the center points of elements
    sideOffsetBefore: 0, // any extra gap you want between center and "before" element
    sideOffsetAfter: 0, // any extra gap you want between center and "after" element
    elements: this.createThumbnails(),
    incrementalLoading : true
  };
  // customize the page control
  this.pageControlData = {
    numPages: appData.numberOfChapters,
    distanceBetweenPageIndicators: 50,
    showPageElements: false,
    indicatorElement: { type: "emptyDiv" },
    pageElement: { type: "emptyDiv" },
    incrementalJumpsOnly: false,
    allowsDragging: true
  };
  bookletController.registerForDisplayUpdates(this);
};

chaptersController.viewDidAppear = function (){
  this.updateDisplay(); 
};

chaptersController.updateDisplay = function () {
  this.resumeButton[bookletController.playbackHasStarted ? 'removeClassName' : 'addClassName'](TKSpatialNavigationManagerInactiveCSSClass);
  if (this.highlightedPageIndex != (bookletController.getChapter() - 1)) {
    this.highlightedPageIndex = bookletController.getChapter() - 1;
  }
};

/* ==================== Creating Pages ==================== */

chaptersController.createThumbnails = function () {
  var elements = [];
  for (var i = 1; i <= appData.numberOfChapters; i++) {
    var padded_index = (i < 10) ? '0' + i : i;
    var url = 'images/chapters/thumbs/thumb' + padded_index + '.jpg';
    elements.push({
      type: 'container',
      children: [
        { type: 'container', children : [
          { type: 'image', src: url },
          { type: 'image', src: 'images/interface/buttonPlayCircle.png' }
        ]}
      ]
    });
  }
  return elements;
};

/* ==================== Pages Navigation ==================== */

// called when the user focuses another page
chaptersController.pageWasHighlighted = function (index) {
  var idx = index + 1;
  var padded_index = (idx < 10) ? '0' + idx : idx;
  this.label.src = 'images/chapters/labels/label' + padded_index + '.png';
};

// called when the user activates the focused pages
chaptersController.pageWasSelected = function (index) {
  bookletController.playChapter(index + 1);
};


