text: (function () {
    'use strict';
  
    angular.module('photoWorks')
      .directive('photoWork', photoWork)
      .directive('viewPhotoWork', viewPhotoWork)
      .directive('workImage', workImage);
  
    photoWork.$inject = ['$rootScope', '$http', '$mdDialog', '$mdMedia', '$mdToast', 'PhotoWorks', 'VideoWorks', 'EditImages', 'Upload'];
    viewPhotoWork.$inject = ['$rootScope', '$state', '$timeout', '$mdDialog', '$mdMedia', 'SelectedImages', 'PhotoWorks'];
    workImage.$inject = ['$rootScope', '$state', '$timeout', '$mdDialog', '$mdMedia', 'SelectedImages', 'PhotoWorks'];
  
  
    function workImage ($rootScope, $state, $timeout, $mdDialog, $mdMedia, SelectedImages, PhotoWorks) {
      var directive = {
        restrict: 'E',
        scope: {
          imageUrl: '='
        },
        link: function (scope) {
  
        },
        templateUrl: 'modules/photo-works/client/views/work-image.html'
      };
  
      return directive;
    }
  
    function viewPhotoWork ($rootScope, $state, $timeout, $mdDialog, $mdMedia, SelectedImages, PhotoWorks) {
      var directive = {
        restrict: 'E',
        scope: {
          photoWork: '='
        },
        link: function (scope) {
  
        },
        templateUrl: 'modules/photo-works/client/views/view-photo-work.html'
      };
  
      return directive;
    }
  
    function photoWork ($rootScope, $http, $mdDialog, $mdMedia, $mdToast, PhotoWorks, VideoWorks, EditImages, Upload) {
      var directive = {
        restrict: 'E',
        scope: {
          coverImageUrl: '=',
          work: '='
        },
        link: function (scope, elem, attr) {
  
  
  
          scope.coverImage = PhotoWorks.coverImage;
          scope.customFullscreen = $mdMedia('xs') || $mdMedia('sm');
          scope.editPhotoWork = function (work) {
            EditImages.images = [];
            EditImages.newImages = [];
            EditImages.imagesToDelete = [];
            EditImages.newImageFiles = [];
            EditImages.serverImages = [];
            EditImages.chosenCoverImage = '';
  
  
            // on edit click, add images already apart
            // of the photo work to the EditImages service
            for (var i = 0; i < work.images.length; ++i) {
  
              // if the image we are currently look at
              // is the cover image
              if (work.images[i].coverImage === true) {
  
                // set the chosenCoverImage field of the
                // EditImages service for the current
                // photo work to edit.
                EditImages.chosenCoverImage = work.images[i].imageUrl;
              }
                                  // url of image                 // isCoverImage Boolean
              EditImages.addImage(work.images[i].imageUrl, true, work.images[i].coverImage);
            }                                     // is serverImage Boolean
  
            scope.modelsModel = [];
  
            // iterate through the currently set models for the photo work
            for (var i = 0; i < work.models.length; ++i) {
  
              // push each model onto the data model for              // this for loop might be unnecessary
              // models input on photo work
              scope.modelsModel.push({name: 'model_' + i});
              scope.modelNumber = i;
            }
  
            // show edit dialog
            $mdDialog.show({
  
              // inject scope variables to dialog controller
              locals: {
  
                // current photo work object
                work: scope.work,
  
                // photo work's models
                models: scope.work.models,
  
                // models input data model
                modelsModel: scope.modelsModel,
                modelNumber: scope.modelNumber
              },
              controller: function ($scope, $rootScope, $mdDialog, $mdToast, work, modelsModel, modelNumber, models) {
  
                var last = {
                  bottom: false,
                  top: true,
                  left: false,
                  right: true
                };
                $scope.toastPosition = angular.extend({},last);
                $scope.bwUploadingEdit = {
                  visibility: 'hidden'
                };
                $scope.getToastPosition = function() {
                  sanitizePosition();
                  return Object.keys($scope.toastPosition)
                    .filter(function(pos) { return $scope.toastPosition[pos]; })
                    .join(' ');
                };
                function sanitizePosition() {
                  var current = $scope.toastPosition;
                  if ( current.bottom && last.top ) current.top = false;
                  if ( current.top && last.bottom ) current.bottom = false;
                  if ( current.right && last.left ) current.left = false;
                  if ( current.left && last.right ) current.right = false;
                  last = angular.extend({},current);
                }
                $scope.showSimpleToast = function(error) {
                  var pinTo = $scope.getToastPosition();
                  if (error === 'images') {
                    $mdToast.show(
                      $mdToast.simple()
                        .textContent('Please add at least one image.')
                        .position(pinTo )
                        .hideDelay(3000)
                        .parent ($mdDialog)
                    );
                  } else {
                    if (error === 'title') {
                      $mdToast.show (
                        $mdToast.simple()
                          .textContent ('Another work with that title already exists.')
                          .position (pinTo)
                          .hideDelay (3000)
                      );
                    } else {
                      if (error === 'video-image') {
                        $mdToast.show(
                          $mdToast.simple()
                            .textContent('Please add a cover image.')
                            .position(pinTo )
                            .hideDelay(3000)
                        );
                      } else {
                        if (error === 'missing-title') {
                          $mdToast.show(
                            $mdToast.simple()
                              .textContent('Please add a title.')
                              .position(pinTo )
                              .hideDelay(3000)
                          );
                        } else {
                          if (error === 'video-url') {
                            $mdToast.show(
                              $mdToast.simple()
                                .textContent('Please add a video URL.')
                                .position(pinTo )
                                .hideDelay(3000)
                            );
                          }
                        }
                      }
                    }
                  }
                };
  
                // set up scope variables for controller/dialog scope.
                $scope.work = work;
                $scope.models = models;
                $scope.modelsModel = modelsModel;
  
  
                // set up variables to hold data before
                // any edits to restore in case of a
                // cancelled edit.
                var oldModels = $scope.work.models;
                var model = modelsModel;
                var workTitle = $scope.work.title;
                var postText = $scope.work.postText;
                var copyright = $scope.work.copyright;
  
                $scope.addModel = function (event) {
                  $scope.modelsModel.push( {name: 'model_' + ++modelNumber} )
                };
  
                $scope.cancelEdit = function (event) {
                  work.title = workTitle;
                  work.copyright = copyright;
                  work.models = oldModels;
                  work.postText = postText;
                  $scope.modelsModel = model;
                  for (var model in work.models) {
                    if (work.models[model] === '') {
                      work.models.splice (model, 1);
                    }
                  }
                  for (model in modelsModel) {
                    if (modelsModel[model] === '') modelsModel.splice (i, 1);
                  }
                  $mdDialog.hide();
                };
  
                // submit edits for updating photo work on back end.
                $scope.submitEditedWork = function (event) {
                  if (EditImages.images.length === 0) {
                    $scope.showSimpleToast('images');
                    return;
                  } else {
  
                    if (work.title === '') {
                      $scope.showSimpleToast ('missing-title');
                      return;
                    }
  
                    // check if the entered work title already exists
                    // (another work with the same name already exists).
                    for (var i = 0; i < PhotoWorks.photoWorks.length; ++i) {
                      if (PhotoWorks.photoWorks[i].title === $scope.photoWorkTitle) {
                        $scope.showSimpleToast ('title');
                        return;
                      }
                    }
                    for (var i = 0; i < VideoWorks.videoWorks.length; ++i) {
                      if (VideoWorks.videoWorks[i].title === $scope.photoWorkTitle) {
                        $scope.showSimpleToast ('title');
                        return;
                      }
                    }
  
                    for (var i = 0; i < work.models.length; ++i) {
                      if (work.models[i] === '' || work.models[i].match(/^\s*$/)) {
                        work.models.splice(i, 1);
                        i = 0;
                      }
                    }
                  }
                  var editObject = {
                    newImages: EditImages.newImages,
                    chosenCoverImage: EditImages.chosenCoverImage,
  
                    // DB entry identifier for easy lookup
                    identifier: work._id,
  
                    // photo work images that were already on server
                    serverImages: EditImages.serverImages,
  
                    // any server images that need to be deleted after
                    // the edit
                    imagesToDelete: EditImages.imagesToDelete,
                    workTitle: work.title,
                    postText: work.postText,
                    copyright: work.copyright,
                    models: work.models
                  };
  
                  if (EditImages.newImageFiles.length > 0) {
                    $scope.bwUploadingEdit = {
                      visibility: 'visible'
                    };
                  }
  
                  Upload.upload({
                    url: '/api/photo_works/edit_photo_work',
  
                    // arrayKey ensures that any submitted array fields
                    // remain arrays when they are parsed on back end.
                    arrayKey: '',
                    data: {
  
                      // file key needs to be set to any new image files
                      // as new image files require an upload
                      file: EditImages.newImageFiles,
                      editObject: JSON.stringify (editObject)
                    }
                  }).then (function (resp) {
                    var newWork = resp.data;
                    for (var i = 0; i < PhotoWorks.photoWorks.length; ++i) {
                      if (PhotoWorks.photoWorks[i]._id === newWork._id) {
                        PhotoWorks.photoWorks[i] = newWork;
                      }
                    }
                    $mdDialog.cancel();
                    EditImages.reset();
                  }, function (resp) {
                  }, function (evt) {
                    $scope.uploadProgress = (evt.loaded / evt.total) * 100;
                  });
  
                };
  
                $scope.editSelected = function (files, badFiles) {
  
                };
  
                $scope.hide = function () {
                  $mdDialog.hide();
                }
              },
              controllerAs: 'editCtrl',
              onRemoving: function () {
                EditImages.reset();
              },
              clickOutsideToClose: true,
              templateUrl: 'modules/bw-interface/client/views/edit-photo-work.html'
            });
          };
  
          scope.deletePhotoWork = function (ev) {
            PhotoWorks.deletePhotoWork(scope.work);
          };
  
          scope.showPhotoWork = function (ev) {
            var useFullScreen = ($mdMedia('sm') || $mdMedia('xs')) && scope.customFullscreen;
            $rootScope.currentWork = scope.work;
            $mdDialog.show({
              controller: function (scope, $mdDialog, work) {
                scope.work = work;
  
                scope.viewImage = function(event, imageUrl) {
                  $mdDialog.show({
                    controllerAs: 'ViewImageController',
                    controller: function ($mdDialog, scope, work) {
                      scope.imageUrl = imageUrl;
  
                      scope.closeDialog = function () {
                        $mdDialog.cancel();
                      }
                    },
                    preserveScope: true,
                    autoWrap: true,
                    skipHide: true,
                    clickOutsideToClose: true,
                    locals: {
                      work: scope.work
                    },
                    template: '<md-dialog class="view-image-dialog" aria-label="viewImageLabel">' +
                                  '<span flex>' +
                                  '<md-button ng-click="closeDialog()" layout-align="end center" class="md-warn md-hue-2 md-fab md-mini view-image-close">' +
                                    '<md-icon>close</md-icon>' +
                                  '</md-button>' +
                                  '</span>' +
                                  '<img class="view-image-img" ng-src="{{imageUrl}}"/>' +
                              '</md-dialog>'
                  });
                };
  
                $rootScope.hide = function () {
                  $mdDialog.hide();
                };
                scope.cancel = function () {
                  $mdDialog.cancel();
                };
                $rootScope.answer = function (answer) {
                  $mdDialog.hide(answer);
                };
              },
              locals: {
                work: scope.work
              },
              templateUrl: 'modules/photo-works/client/views/view-photo-work.html',
              parent: angular.element(document.body),
              targetEvent: ev,
              autoWrap: false,
              clickOutsideToClose: true,
  
              fullscreen: useFullScreen
            });
  
  
          };
        },
        templateUrl: 'modules/photo-works/client/views/photo-work.html'
      };
  
      return directive;
    }
  }());
  