'use strict';

/* Controllers */
angular.module('cargoApp.controllers')
  .controller('homeController', function($rootScope,$scope,cargosFactory, $filter, $routeParams) {
  	$scope.ready= false;
  	
    $scope.autoPersons = [];
  	


  	$scope.activePersons = [];
    $scope.estado = "";
  	$rootScope.observers =[];
    $rootScope.yearObserver =[];
    $scope.filterLinea ="cargo";



  	


  var onDataLoaded = function(){
    
     $scope.estado = "Motor de Visualizacion";
      for (var i = 0; i < $rootScope.observers.length; i++) {
        var observer = $rootScope.observers[i];
        observer();
      };
      $scope.estado = "Listo!";
      $scope.ready= true;

      //Load initial ids from the url
      if($routeParams.ids){
        angular.forEach($routeParams.ids.split('-'), function(id){
          $scope.add($scope.autoPersons[id], id);
        });
      }

  };

  $scope.redrawPoderometro = function(){
    
    if ($scope.activePersons.length > 0 ){
      for (var i = 0; i < $rootScope.yearObserver.length; i++) {
        var observer = $rootScope.yearObserver[i];
        var poderometro = cargosFactory.getPoderometro($scope.poderometroYear, $scope.activePersons);
        observer(poderometro);
      };
    };
  }

  cargosFactory.load($scope,onDataLoaded);





    $scope.add = function(autoPersona, id){

      $scope.autocomplete = " ";
      autoPersona.agregada = true;
    	
      var person = cargosFactory.getFullPerson(id);
      person.autoPersona = autoPersona;
      $scope.activePersons.push(person);


    	var idPersonas = cargoTimeline.options.filtro.idPersonas;
    	idPersonas.push(person.id);
			var timelineParams = {
			   filtro: { idPersonas: idPersonas },
			   mostrarPor: $scope.filterLinea,
			};
    	window.cargoTimeline.update(timelineParams);


    };

    $scope.orderLine =function(order){
      $scope.filterLinea = order;
      var idPersonas = cargoTimeline.options.filtro.idPersonas;
      var timelineParams = {
         filtro: { idPersonas: idPersonas },
         mostrarPor: $scope.filterLinea,
      };
      window.cargoTimeline.update(timelineParams);

    }

    $scope.remove = function(person){
    	var indexOf = $scope.activePersons.indexOf(person);
    	if (indexOf > -1){
    		 $scope.activePersons.splice(indexOf, 1);
    	}
      person.autoPersona.agregada = false;
    	indexOf = cargoTimeline.options.filtro.idPersonas.indexOf(person.id);
    	if (indexOf > -1){
    		 cargoTimeline.options.filtro.idPersonas.splice(indexOf, 1);
    	}
    	var idPersonas = cargoTimeline.options.filtro.idPersonas;
			var timelineParams = {
			   filtro: { idPersonas: idPersonas },
			   mostrarPor: $scope.filterLinea,
			};
    	window.cargoTimeline.update(timelineParams);

    };

    $scope.clearAll = function(){
      for (var i = 0; i < $scope.activePersons.length; i++) {
        $scope.activePersons[i].autoPersona.agregada = false;
      };
      $scope.activePersons = [];
      var idPersonas = [];
      var timelineParams = {
         filtro: { idPersonas: idPersonas },
         mostrarPor: $scope.filterLinea,
      };
      window.cargoTimeline.update(timelineParams);

    }




  });


@@ -160,20 +160,31 @@
 teParams
+, $location
 ) %7B%0A
-
   %09$scop
@@ -1293,17 +1293,180 @@
 ded);%0A%0A%0A
+  function updateTheUrl()%7B%0A      //Update the URL%0A      // $location.path(%22/%22 + $scope.activePersons.map(function(p)%7B return p.autoPersona.index %7D).join('-'));%0A  %7D
 %0A
-
 %0A%0A    $s
@@ -1971,17 +1971,43 @@
 arams);%0A
+    %0A      updateTheUrl();
 %0A
-
 %0A    %7D;%0A
@@ -2939,24 +2939,47 @@
 neParams);%0A%0A
+      updateTheUrl();%0A%0A
     %7D;%0A%0A    
@@ -3317,32 +3317,32 @@
 Linea,%0A      %7D;%0A
-
       window.car
@@ -3373,24 +3373,47 @@
 neParams);%0A%0A
+      updateTheUrl();%0A%0A
     %7D%0A%0A%0A%0A%0A  

Update the url take 1