

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

    'use strict';
    $mixpanel.track('Index View');
    var bg_loaded = false;
    var menu_opened = false;
    $scope.lang = 0;

    $rootScope.$on('background:loaded', function() {
      if(Data.isLangSet()){
        openMenu();
      }
      bg_loaded = true;
    });

    $rootScope.$on('data:lang_set', function() {
      if(bg_loaded){
        openMenu();
      }
      $scope.lang = Data.getLang();
    });

    var openMenu = function() {
      if(!$scope.$$phase) {
        $scope.$apply(function(){
          $scope.navigation = Data.getNavigation();
          if(menu_opened == false){
            menu_opened = true;
            $scope.menu1pos = getMenuPos($scope.navigation.length);
          }
        });
      } else {
        $scope.navigation = Data.getNavigation();
        if(menu_opened == false){
          menu_opened = true;
          $scope.menu1pos = getMenuPos($scope.navigation.length);
        }
      }
    }

    $scope.subNavigation = [];
    $scope.sub2Navigation = [];
    $scope.last_click = "";
    $scope.last_click2 = "";

    $scope.btnNavclick = function(index){
      if($scope.navigation[index].link == 'submenu') {
        if($scope.last_click != $scope.navigation[index].title){
          $scope.menu2pos = getMenuPos($scope.navigation[index].submenu.length);
          console.log("Opening submenu: " + $scope.navigation[index].title);
          $scope.sub2Navigation = [];
          $scope.subNavigation = $scope.navigation[index].submenu;
        } else if ($scope.last_click === $scope.navigation[index].title && $scope.subNavigation.length === 0) {
          $scope.menu2pos = getMenuPos($scope.navigation[index].submenu.length);
          $scope.subNavigation = $scope.navigation[index].submenu;
          console.log("Opening submenu: " + $scope.navigation[index].title);
        } else {
          $scope.subNavigation = [];
          $scope.sub2Navigation = [];
          console.log("Closing submenu: " + $scope.navigation[index].title);
        }
        $scope.last_click = $scope.navigation[index].title;
      } else {
        console.log("> Redirecting to page " + $scope.navigation[index].link);
        $location.path($scope.navigation[index].link);
      }
    };

    $scope.btnSubNavclick = function(index){
      if($scope.subNavigation[index].link == "submenu") {
        if($scope.last_click2 != $scope.subNavigation[index].title){
          $scope.menu3pos = getMenuPos($scope.subNavigation[index].submenu.length);
          $scope.sub2Navigation = [];
          $scope.sub2Navigation = $scope.subNavigation[index].submenu;
        } else if ($scope.last_click2 === $scope.subNavigation[index].title && $scope.sub2Navigation.length === 0){
          $scope.menu3pos = getMenuPos($scope.subNavigation[index].submenu.length);
          $scope.sub2Navigation = $scope.subNavigation[index].submenu;
          console.log("Opening submenu: " + $scope.subNavigation[index].title);
        } else {
          $scope.sub2Navigation = [];
          console.log("Closing submenu: " + $scope.subNavigation[index].title);
        }
        $scope.last_click2 = $scope.subNavigation[index].title;
      } else {
        console.log("> Redirecting to page " + $scope.subNavigation[index].link);
        $location.path($scope.subNavigation[index].link);
      }
    };

    $scope.btnSub2Navclick = function(index) {
      if($scope.sub2Navigation[index].link != "submenu") {
        console.log("> Redirecting to page " + $scope.sub2Navigation[index].link);
        $location.path($scope.sub2Navigation[index].link);
      } else {
        console.log("* Error: Only 2 levels of submenu allowed.");
      }
    };

    var getMenuPos = function(len) {
      var menu_size = len * 20;
      var max = window.innerHeight - menu_size - 25;
      var pos = Math.floor(Math.random()*(max-15+1)+15)
      return {'margin-top': pos };
    }

  }]);
