/**
 * @ngdoc function
 * @name domainManagerApp.controller:AboutCtrl
 * @description
 * # AboutCtrl
 * Controller of the domainManagerApp
 */
angular.module('goApp.slideshow', ['ngFx', 'ngAnimate', 'goApp.data'])
  .controller('SlideshowController',['$scope', '$rootScope', '$famous', '$timeline', '$location', '$route', '$timeout', 'Data', '$mixpanel', function ($scope,  $rootScope, $famous, $timeline, $location, $route, $timeout, Data, $mixpanel) {

    'use strict';

    console.log('> Loading Slideshow');
    $scope.lang = Data.getLang();

    // Get Page Data
    var link = $location.url();
    $scope.pageData = Data.getPageData(link);
    $rootScope.$on('data:loaded', function(data) {
      if(!$scope.$$phase) {
        $scope.$apply(function(){
          $scope.pageData = Data.getPageData(link);
          $scope.showSlider();
          $mixpanel.track('Page View', { "Page": $scope.pageData.link });
        });
      }
    });

    // FadeIn
    $timeout(function(){
      $scope.showView = true;
    },100);
    // FadeOut
    $scope.fadeOut = function() {
      $scope.showView = false;
      $timeout(function(){
        $scope.showView = false;
        $location.path("/");
      },800);
    };

    $scope.index = 0;
    $scope.images = [];

    $scope.showSlider = function() {
      var show = false;
      if($scope.pageData.slides !== undefined) {
        if($scope.pageData.slides.length > 0) {
          show = true;
          for (var i = 0; i < $scope.pageData.slides.length; i++) {
            $scope.images.push($scope.pageData.slides[i].url);
          }
        } else {
          show = false;
        }
        if(!$scope.$$phase) {
          $scope.$apply(function(){
            $scope.slider = show;
          });
        } else {
          $scope.slider = show;
        }
        $timeout(function(){
          //$scope.resizeImages();
        },250);

      }
    };


    // callbacks for change in slides
    $scope.updateTsPrevious = function() {
      $scope.tsPrevious = +new Date();
      //$scope.resizeImages();
    };
    $scope.updateTsNext = function() {
      $scope.tsNext = +new Date();
      //$scope.resizeImages();
    };


    $scope.resizeImages = function() {
      var allImages = [];
      var clientHeight = document.getElementById('slider').clientHeight;
      $('img').each(function(idx, img) {
        allImages.push(img);
      });
      console.log('> Starting image resize process (resizing to ' + clientHeight + 'px)');
      for(var i = 0, max = allImages.length; i < max; i++){
        console.log('> resizing image');
        allImages[i].style.height = (clientHeight-25) + 'px';
        allImages[i].style.maxHeight = 'none';
        allImages[i].style.width = 'auto';
      }
    };

    $scope.showSlider();




  }]);