

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

    'use strict';

    $scope.showError = false;
    $scope.showSuccess = false;
    $scope.showSubmit = true;

    console.log('> Loading Contact Page');
    $mixpanel.track('Page View', { "Page": "Contact" });

    // 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();
        });
      }
    });

    $scope.sendMessage = function(msgData){
      console.log("> Sending message from " + msgData.name);
      Mandrill.sendMail(msgData);
      $mixpanel.track('Messsage Sent', { "Name": msgData.name, "email": msgData.email });
      if(!$scope.$$phase) {
        $scope.$apply(function(){
          $scope.showSubmit = false;
        });
      } else {
        $scope.showSubmit = false;
      }
    };

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

    $rootScope.$on('email_sent:error', function(data) {
      if(!$scope.$$phase) {
        $scope.$apply(function(){
          $scope.showError = true;
          $scope.showSubmit = false;
          $timeout(function(){
            if(!$scope.$$phase) {
              $scope.$apply(function(){
                $scope.showError = false;
                $scope.showSubmit = true;
              });
            }
          },5000);
        });
      }
    });

    $rootScope.$on('email_sent:success', function(data) {
      if(!$scope.$$phase) {
        $scope.$apply(function(){
          $scope.showSuccess = true;
          $scope.msgData.name = "";
          $scope.msgData.email = "";
          $scope.msgData.message = "";
          $timeout(function(){
            if(!$scope.$$phase) {
              $scope.$apply(function(){
                $scope.showSuccess = false;
                $scope.showSubmit = true;
              });
            } else {
              $scope.showSuccess = false;
              $scope.showSubmit = true;
            }
          },5000);
        });
      } else {
        $scope.showSuccess = true;
        $scope.msgData.name = "";
        $scope.msgData.email = "";
        $scope.msgData.message = "";
        $timeout(function(){
          if(!$scope.$$phase) {
            $scope.$apply(function(){
              $scope.showSuccess = false;
              $scope.showSubmit = true;
            });
          } else {
            $scope.showSuccess = false;
            $scope.showSubmit = true;
          }
        },5000);
      }
    });


  }]);
