text: angular
      .module('app', ['pascalprecht.translate','ngSanitize'], function ($translateProvider) {
          $translateProvider.useSanitizeValueStrategy(null);
          $translateProvider.useStaticFilesLoader({
              prefix: 'data/lang.',
              suffix: '.json'
          });
      })
      .directive('expirience',function(){
          return {
              restrict: 'A',
              scope: {
                  expirience: '='
              },
              templateUrl: "templates/expirience.html",
              link: function(scope, element, attrs) {
                  console.log(scope.expirience)
              }
          }
      })
      .controller('ctrl',function($scope,$translate,$http,$timeout){
  
        /**
         *
         * @param object
         * @param criteria - выполнять функцию со всеми объектами
         */
          $scope.recoursive = function(object, criteria) {
            var readed = [];
            if (!object) return;
            return (function sub_recoursive(object) {
              if (readed.indexOf(object) != -1) {
                return;
              }
              readed.push(object);
  
  
              if (object instanceof Array) {
                for (var prop = object.length; prop--;) {
                  if (object[prop] && (object[prop].constructor == Object || object[prop].constructor == Array)) {
                    sub_recoursive(object[prop]);
                  } else {
                    var break_ = criteria(prop, object[prop], object);
                  }
                }
              } else {
                for (var prop in object) {
                  if (object[prop] && (object[prop].constructor == Object || object[prop].constructor == Array)) {
                    sub_recoursive(object[prop]);
                  } else {
                    var break_ = criteria(prop, object[prop], object);
                  }
                }
              }
            })(object);
          };
  
  
          $scope.lang = '';
          $scope.view = 'document';
          $scope.style = 'modern';
          $scope.closePreview = function() {
              delete $scope.preview;
          };
          $scope.show = function(info,whatToShow) {
              if(info.hide){
                  return false;
              }
              if(whatToShow.constructor == String){
                  whatToShow = [whatToShow]
              }
              if(info.category){
                  if(info.category.constructor == String){
                      info.category = [info.category]
                  }
                  for(var i in info.category){
                      if(whatToShow.indexOf(info.category[i]) != -1){
                          return true;
                      }
                  }
                  return false;
              }
              return true;
          };
          $scope.previewImage = function(img) {
              $scope.preview = img.url ? img : {url: img};
          };
          $scope.changeStyle = function(newStyle) {
            if($scope.style == newStyle){
              return;
            }else{
              $scope.style = newStyle;
            }
          };
          $scope.changeView = function() {
              $scope.view = ($scope.view == 'document' )? 'web' : 'document';
          };
          $scope.print = function() {
              window.print();
          };
          $scope.setLang = function(langKey) {
  
              $http.get('data/' + langKey + '.json').then(function(data){
  
                  $scope.recoursive(data,
                    function (property, value, parent) {
                      if(parent.when){
                        parent.begin = parent.end = new Date(parent.when);
                      }
                      if(parent.end){
                        parent.end = new Date(parent.end);
                      }
                      if(parent.begin){
                        parent.begin = new Date(parent.begin);
                      }
                    }
                  );
  
                  $scope.data = $.extend(true,$scope.data,data.data);
  
                  // You can change the language during runtime
                  $translate.use(langKey);
                  // A data generated by the script have to be regenerated
                  //$scope.jsTrSimple = $translate.instant('SERVICE');
                  //$scope.jsTrParams = $translate.instant('SERVICE_PARAMS', $scope.tlData);
                  $scope.lang = langKey;
                  $scope.$broadcast("update");
              })
  
          };
          $scope.sendToFront = function(){
              $('html').animate({ scrollTop: $(document).height() }, 'slow');
              $('body').animate({ scrollTop: $(document).height() }, 'slow');
              $('#second').removeClass('back');
          };
          $scope.setProject = function(work){
              $scope.project = work;
              $timeout(function(){
                  $scope.showProject = true;
              })
          };
          $scope.closeProject = function(){
              $scope.showProject = false;
              $timeout(function(){
                  $scope.project = null;
              },1000)
          };
          $scope.data = {};
  
          $scope.right = $(window).width();
  
          function resize(){
  
              var art = $('article,#card');
              var _ah = 1123;
              var _aw = 794;
              var a = ($(window).height() - 25) / _ah ;//art.height() ;
              art.css('transform','scale(' + a +')');
              var _w =  _aw * a;
              var _h = _ah * a;
              var _containers = $('.container');
              if(!_containers.length)return;
              _containers.width(_w).height(_h);
              var _c = $(_containers[0]);
              var _r = _c.width() + _c.position().left + 20;
              twopages = _r <  $('body').width() / 2;
              doBack();
  
              $("#project").width(_h).height(_w);
  
              if(_containers[1]){
                  $scope.right = $(_containers[1]).position().left + $(_containers[1]).width() * 1.1 + 20  ;
              }
              $scope.left = $(_containers[0]).position().left * 0.7;
  
              $scope.$apply();
          }
  
  
          var twopages= false;
  
  
          $timeout(function() {
              resize();
              doBack();
          })
  
  
          $(window).resize(resize);
  
  
  
          $(document).scroll(doBack)
  
          function doBack(){
  
              if(twopages ||  $('html').scrollTop() || $('body').scrollTop()  > $('html')[0].scrollHeight * 0.1){
                  $('#second').removeClass('back');
              }else{
                  $('#second').addClass('back');
              }
          }
  
          $scope.setLang('en_US');
  
      })
      .directive('page', [ '$http','$templateCache','$compile','$timeout','$rootScope', function($http,$templateCache,$compile,$timeout,$rootScope) {
          return {
              restrict: 'A',
              link: function(scope, element, attrs) {
                  var articles = [];
  
                  var _ah = 1123, _aw = 794,
                      a = ($(window).height() - 25) / 1123 ,
                      _w =  _aw * a, _h = _ah * a;
  
                  function createPage(){
                      var $article = $("<article>");
  
  
  
                      if(articles.length){
                          var $container = $("<div>").addClass("container").append($article);
                          var $last = $(articles[articles.length - 1]).parent();
                          $container.attr('class', $last.attr('class'));
                          $last.after($container);
                      }else{
                          var $container = element.addClass("container").append($article);
                      }
                      if(attrs.horizontal !== undefined){
                          $container.width(_h).height(_w);
                      }else{
                          $container.width(_w).height(_h);
                      }
                      $article.css('transform','scale(' + a +')');
                      articles.push($article[0]);
                      return $article;
                  }
  
                  function rearrangeAll(){
                      for(var i = 1; i < articles.length; i++){
                          var $article = $(articles[i]);
                          $(articles[0]).append($article.children());
                          $($article.parent()).remove();
                      }
                      articles.length = 1;
                      rearrange(articles[0]);
                  }
  
                  function rearrange(article){
                      var $article = $(article);
  
                      var _h = $article.innerHeight();// * a;
                      var _children = $article.children();
                      var _new_page;
                      _children.each(function(){
                          if(_new_page){
                              _new_page.append($(this));
                              return;
                          }
                          var _cel = $(this);
                          var _top = _cel.position().top;
                          var _elh = _cel.height();
                          if(_top + _elh > _h){
                              _new_page = createPage();
                              _new_page.append($(this));
                          }
                      });
                      if(_new_page){
                          rearrange(_new_page);
                      }
                  }
  
                  createPage();
  
                  $http.get(attrs.page, {
                      cache: $templateCache
                  }).then(function(result) {
                      var $compiled = $compile(result.data)(scope);
                      $(articles[0]).append($compiled);
                  });
  
                  if(attrs.rearrange !== undefined){
                      $rootScope.$on('image:loaded',function(){
                          rearrangeAll();
                      });
  
                      scope.$on('update',function(){
                          $timeout(rearrangeAll);
                      })
                  }
              }
          }
      }])
      .directive('imgPreload', ['$rootScope', function($rootScope) {
          return {
              restrict: 'A',
              scope: {
                  ngSrc: '@',
                  onLoad: '&'
              },
              link: function(scope, element, attrs) {
                  scope.spinner = $('<i class="fa fa-spinner fa-pulse">');
                  element.addClass('fade');
  
                  element.on('load', function() {
                      // console.log('> add fade in class to...', element);
                      element.addClass('in');
                      scope.spinner.remove();
                      if(attrs.onLoad){
                          var $data = {
                              image: this
                          }
                          scope.onLoad({ $data: $data });
                      }
                      $rootScope.$emit('image:loaded');
                      scope.$apply();
                  }).on('error', function() {
                      scope.spinner.remove();
                      scope.$apply();
                  });
  
                  scope.$watch('ngSrc', function(newVal) {
                      if(newVal == undefined || newVal == ''){
                          element.attr('src', '');
                          if(attrs.onLoad){
                              var $data = {
                                  image: element[0]
                              }
                              scope.onLoad({ $data: $data });
                          }
                      }
                      if(element[0].complete == false){
                          element.removeClass('in');
                          scope.spinner.insertAfter(element);
                      }
                      // console.log('> remove fade in class to...', newVal, element);
                  });
              }
          };
      }]);
  
  