

angular.module('goApp.data', [])
.service('Data', [ '$rootScope', '$http', function($rootScope, $http) {

  "use strict";

  var data = [];
  var data_loaded = false;
  var lang = 0;
  var lang_set = false;

  $http({
    method: 'GET',
    url: 'data.json'
  }).success(function(json_data) {
    data = json_data;
    data_loaded = true;
    console.log('> Loaded data.json');
    $rootScope.$broadcast('data:loaded', data);
  });

  this.setLang = function(n){
    lang = n;
    lang_set = true;
    $rootScope.$broadcast('data:lang_set', data);
  }

  this.getLang = function(){
    return lang;
  }

  this.isLangSet = function(){
    return lang_set;
  }

  this.get = function() {
    return data;
  };

  this.getPageData = function(link){
    var page_data = "";
    if (data_loaded) {
      for (var i = 0; i < data.navigation.length; i++) {
        if(data.navigation[i].link == "submenu"){
          for (var a = 0; a < data.navigation[i].submenu.length; a++) {
            if(data.navigation[i].submenu[a].link == "submenu"){
              for (var b = 0; b < data.navigation[i].submenu[a].submenu.length; b++) {
                  if(data.navigation[i].submenu[a].submenu[b].link == link){
                    page_data = data.navigation[i].submenu[a].submenu[b];
                  }
              }
            } else if(data.navigation[i].submenu[a].link == link){
              page_data = data.navigation[i].submenu[a];
            }
          }
        } else if(data.navigation[i].link == link){
          page_data = data.navigation[i];
        }
      }

      if(page_data){
        console.log(page_data);
      }
    }

    return page_data;
  };

  this.getNavigation = function() {
    return data.navigation;
  };

  this.getBackgrounds = function() {
    return data.backgrounds;
  };

}]);
