'use strict';

/**
 * @ngdoc function
 * @name barteguidenMarkedsWebApp.controller:EditCtrl
 * @description
 * # EditCtrl
 * Controller of the barteguidenMarkedsWebApp
 */
angular.module('barteguidenMarkedsWebApp.controllers')
  .controller('EditCtrl', function ($scope, $routeParams, Event) {

    $scope.event = {};

    var event = Event.get({id: $routeParams.id}, function() {
      console.log(event);
      $scope.event = event;
    });


    $scope.format = 'dd. MMMM yyyy';

    $scope.open = function($event, elementOpened) {
      $event.preventDefault();
      $event.stopPropagation();

      $scope.datepicker[elementOpened] = !$scope.datepicker[elementOpened];

    };

    $scope.dateOptions = {
      formatYear: 'yy',
      startingDay: 1
    };

    $scope.toggleMinMax = function() {
      $scope.minDate = new Date();

      $scope.maxDate = new Date();
      $scope.maxDate.setFullYear($scope.minDate.getFullYear() + 1);
      // sets max available date one year ahead

    };
    $scope.toggleMinMax();


    $scope.insertTimeIntoDate = function(time, index, date) {
      if(time !== undefined && $scope.event.shows[index][date] !== undefined)  {
        $scope.event.shows[index][date].setHours(parseInt(time.slice(0,2),10));
        $scope.event.shows[index][date].setMinutes(parseInt(time.slice(3,5),10));
      }

    };

    // categories
    $scope.categoryOptions = [
    {name: 'Debatter', id: 'DEBATE'},
    {name: 'Utstillinger', id: 'EXHIBITIONS'},
    {name: 'Musikk', id: 'MUSIC'},
    {name: 'Uteliv', id: 'NIGHTLIFE'},
    {name: 'Forestillinger', id: 'PERFORMANCES'},
    {name: 'Presentasjoner', id: 'PRESENTATIONS'},
    {name: 'Sport', id: 'SPORT'},
    {name: 'Andre', id: 'OTHER'}
    ];
  });

@@ -287,17 +287,38 @@
 vent) %7B%0A
+    $scope.time = %5B%5D;
 %0A
-
     $sco
@@ -331,16 +331,37 @@
 nt = %7B%7D;
+%0A    $scope.cat = %7B%7D;
 %0A%0A    va
@@ -420,34 +420,8 @@
 ) %7B%0A
-      console.log(event);%0A
     
@@ -448,14 +448,864 @@
 nt;%0A
+      for(var i = 0; i %3C $scope.event.shows.length; i++) %7B%0A        var start = new Date($scope.event.shows%5Bi%5D.startDate);%0A        var end = $scope.event.shows%5Bi%5D.endDate === null ? null : new Date($scope.event.shows%5Bi%5D.endDate);%0A%0A        $scope.time.push(%7B%0A          startTime: $scope.formatTime(start.getHours()) + ':' + $scope.formatTime(start.getMinutes()),%0A          endTime: end === null ? null : $scope.formatTime(end.getHours()) + ':' + $scope.formatTime(end.getMinutes())%0A        %7D);%0A      %7D%0A      $scope.cat.id = $scope.event.tags.pop();%0A      $scope._id = event._id;%0A%0A    %7D);%0A%0A%0A    $scope.formatTime = function(time) %7B%0A      if (time %3C 10) %7B%0A        return '0'+ time;%0A      %7D%0A      return time;%0A    %7D;%0A%0A    $scope.update = function() %7B%0A      $scope.event.$update(%7Bid:$routeParams.id %7D, function() %7B%0A        console.log('Success');%0A      %7D);%0A
     %7D
-)
 ;%0A%0A%0A

Update the event on submit. Add a hack to reinsert the event start and end time