(function(){

  angular
  .module("izApp")
  .controller("VeranstaltungController", VeranstaltungController);

VeranstaltungController.$inject = ['$location','$scope', '$anchorScroll', '$state', '$timeout', 'Logger', 'VeranstaltungenService', 'veranstaltung', 'AgendaService'];

function VeranstaltungController($location,$scope,$anchorScroll, $state, $timeout, Logger, VeranstaltungenService, veranstaltung, AgendaService){
  var vm = this;
	var elementToWatch = angular.element(document.getElementById("module-der-veranstaltung"));

  vm.veranstaltung;
  vm.moduleData = null;
  vm.sameDay;

  // Methoden
  vm.init = init;
  vm.scrollToId = scrollToId;
  vm.url = $location.url();
  vm.subEvent = false;

  // Controller initialisieren
  vm.init();
	if(window.location.hash.replace("#","")){
	  watcherFn = $scope.$watch( function(){
		var e = document.getElementById(decodeURIComponent(window.location.hash.replace("#","")));
		if(!e){
		  return elementToWatch;
		} else {
		  return e;
		}

	  }, function(nV,oV){
		if(nV && document.getElementById(decodeURIComponent(window.location.hash.replace("#","")))){
		  initScroll();
		}
	  });
	}
  function init(){

	// spin my head right round ... Just start that for president ^^
	/*
	$timeout(function () {
	  $('.carousel').carousel({
		interval: 4000
	  });
	},600);
	*/

	var titleElement = document.getElementById("title");
	if(titleElement){
	  titleElement.innerHTML = veranstaltung.title;
	}

	// Vorerst Veranstaltungen abfangen die nicht geladen wurden
	if(veranstaltung.status){
	  if(veranstaltung.status != "200"){
		$state.go("veranstaltungen");
	  }
	}

	vm.veranstaltung = veranstaltung;
	$scope.veranstaltung = veranstaltung;
	
	if (veranstaltung.modules) {
		angular.forEach(veranstaltung.modules, function(module,k){
			if (module.name && module.name === 'SUBEVENT' && module.visible === true) {
				vm.subEvent = true;
			}
		});
	}

	vm.sameDay = false;
	var start = new Date(vm.veranstaltung.startDate);
	var end = new Date(vm.veranstaltung.endDate);
	if(start.getTime() == end.getTime()) {
	  vm.sameDay = true;
	}
	
	if(decodeURIComponent(window.location.hash.replace("#","")) == ""){
	  vm.scrollToId('header');
	}
  }
	function initScroll(){

	  if(watcherFn){
		console.log("deregistration");
		watcherFn();
	  }
	  var domId = decodeURIComponent(window.location.hash.replace("#",""));
	  
	// (i) Nice to know: Wenn $timeout keinen delay übergeben bekommt... dann wird die Funktion darin erst ausgeführt wenn der DOM rerendert wurde - AW 07.07.2016
	  if(!domId) {
		$timeout(function () {
		  console.log("Directing to "+ domId);
		  vm.scrollToId('headline-title');
		});
	  } else {
		$timeout(function () {
		  console.log("Directing to "+ domId);
		  vm.scrollToId(domId);
		},400); //3000
	  }
	  
	}
	


  function scrollToId(id) {
	var someElement = angular.element(document.getElementById(id));
	var offset = 51;
	var mobile = false;
	if($(document).width() <= 767){
	  offset = 20;
	  mobile = true;
	}
	$(document).duScrollToElementAnimated(someElement, offset, 400).then(function(){
	  $(document).duScrollToElementAnimated(someElement, offset,250);
	  /*
	  barwicki@iz.de, 20.06.2016, auskommentiert da esdie Menu auf mobil beim Start öffnet 
	  if(mobile){
		document.getElementById("module-navigation").click();
	  }
	  */
	});
  };
}
  
  
}());
