
var canLoad = true ; 
var act = '';
var NewsOverviewOnLoad = function () {
	$('#newsMenu ul li').click(function () {
		$('#newsMenu ul li').removeClass('selected');
		$(this).addClass('selected');
		
		loadActivities();
	});
	

	var loadingOffset = $('.loading').offset();
	$(window).scroll(function(){
		if(loadingOffset.top < $(window).scrollTop()+ $(window).height())
		{
			if (canLoad == true){
				LoadAnimations($('.animationContainer div.inlineblock').length);
			}
		}	
	});
	
	// HK Support-0001 Problem with #Events-menu element is not clickable
	// HK Support-0002 Problem with #Events-menu in Safari

	UTILS.compileDustTemplates();
	
	var hashValue = decodeURIComponent(window.location.hash);
	
	var arr = hashValue.substring(1).split('&');
	
	var hashIsLinkToMenu = false;

	if (hashValue) {
		hashValue = arr[0];
	} else {
		var query = decodeURIComponent(window.location.search);
		var i2 = query.indexOf('&');
		if (i2 > 1)
			arr = query.substring(1,  i2).split('=');
		else
			arr = query.substring(1).split('=');

		if (arr[0] == 'tab' && arr.length > 1) {
			hashValue = arr[1];
			arr = [hashValue];
		}else{
			arr = [];
		}
	}

	if (arr.length > 0) {
		$('#newsMenu ul li[data-name="'+hashValue+'"]').each(function (index, elem) {
			hashIsLinkToMenu = true;
			if (arr.length > 1){
				act = arr[1];
				if (act.indexOf('?') > -1){
					act = act.substring(0, act.indexOf('?'));
				}
			}
			$(elem).click();
		});
	}

	if (!hashIsLinkToMenu){
		$('#newsMenu ul li').first().click();
	}
}

var LoadAnimations = function (offset) {
	canLoad = false ;

	// Merge news + tech news in single tab
	var rtId = $.map($('#newsMenu ul li'), function(li) {
		return $(li).attr('data-id');
	});

	var activityId = $('#activitiesFilter .inlineblock.selected').attr('data-id');
	
	if ($('.loadingContainer').hasClass("finishLoading")) return;

	Web_AnimationOverview.loadNextSixAnims(
		rtId, activityId, offset, UTILS.universId, UTILS.language, UTILS.country,
		function(result, event) {
			if (!event.status) return;

			const noMoreResults = result.length < 6;
			$('.loadingContainer').toggle(noMoreResults).toggleClass('finishLoading', noMoreResults);

			result.forEach(val => {
				val.monthString = $(`.monthName div#${ val.month }`).text();
				val.activityName = $(`.radio[data-id="${ val.Id }"] span.radioText`).text();
				val.isVideo = 'Video' === val.obj.Animation__r.RecordType.Name;
			});

			UTILS.dustRender('tmpl_animations',result, $('.animationContainer'), function(){
				$('.video, .news').each(function (index) {
					if (index & 1) $(this).css('margin-left', '3%');
				});
			});

			canLoad= true;
		},
		{escape:false}
	);
};

var loadEvents = function(offset){
	canLoad = false ;
	var rtId = $('#newsMenu ul li.selected').attr('data-id');
	var activityId = $('#activitiesFilter .inlineblock.selected').attr('data-id');
	
	
	if($('.loadingContainer').hasClass("finishLoading") )
		return;

	Web_AnimationOverview.loadNextEightEvents(rtId, activityId, offset, UTILS.universId, UTILS.language,UTILS.country, function(result, event) {
		
		if(event.status){
		
			
		
			
			$('.loadingContainer').hide();
			$('.newsContainer').hide();
				
			$.map( result, function( val, i ) {
				val.monthString = $('.monthName div#' + val.month).text();
				//val.activityName = $('.radio[data-id="' + val.Activity + '"]  span.radioText').text();
				
			});
			
			
			UTILS.dustRender('tmpl_event',result, $('.eventsContainer'));
			//MY 22.07.2015 Req-003690
			$('.event.inlineblock:nth-child(4n)').after(function() { 
	             return "<div class=\'lineSeparator\'></div>"}
			);
			
			$('.eventsContainer').show();
			canLoad= true ;
		}
	}, {escape:false});
}
var loadActivities = function () {
	var rtId = $('#newsMenu ul li.selected').attr('data-id');
	$('.radioContainer').empty();
	
	Web_AnimationOverview.loadAllActivities(
		rtId , UTILS.universId, UTILS.language , UTILS.country,
		function (result, event) {
			if (!event.status) return;

			{
				const DQSA = document.querySelectorAll.bind(document), DQS = document.querySelector.bind(document);

				const $activitiesSelector = $('.radioContainer');
				UTILS.dustRender('tmpl_activities', result, $activitiesSelector, () => {
					const defaultActivity = document.getElementById('activitiesFilter').dataset.defaultActivity;
					DQSA('#activitiesFilter > div')
						.forEach(e => e.classList.toggle('selected', e.matches(`div[data-description-name="${ defaultActivity }"]`)));
					if (!DQS('#activitiesFilter > div.selected')) DQS('#activitiesFilter > div:first-child').classList.add('selected');
				});
			}

			var isFirstClick = true;

			$('body').mouseup(function (e) {
				if (e.which === 1) return;
				if (!$(e.target).is('#newsMenu ul li')) return;

				e.stopImmediatePropagation();
				$(e.target).click();
			});

			$('#activitiesFilter .inlineblock').click(function () {
				{
					const u = new URL(window.location);
					if ('All activities' === this.dataset.descriptionName) {
						u.searchParams.delete('Activity');
					} else {
						u.searchParams.set('Activity', this.dataset.descriptionName);
					}
					history.pushState({}, '', u);
				}


				$('.loadingContainer').removeClass('finishLoading');

				var tag = $('#newsMenu ul li.selected .description').text();

				$('#activitiesFilter .inlineblock').removeClass('selected');

				$(this).addClass('selected');
				$('.newsContainer, .eventsContainer, .videoContainer').empty();

				if ('Partner Event' === tag) {
					$('.newsContainer').hide();
					$('.eventsContainer').show();
					loadEvents(0);
				} else {
					$('.animationContainer').show();
					LoadAnimations(0);
				}

				if (!isFirstClick) {
					var activityName = $(this).data('name');
					var eventName = $(this).data('gaq-event');
					_gaq.push(['_trackEvent', 'filters', eventName, activityName]);
				}
			});


			var isActivityClick = false;

			if (act) {
				$('#activitiesFilter .radio  .radioText').each(function (index, elem) {
					var t = $(elem).text();
					if (t.replace(' ', '-') == act){
						isActivityClick = true ;
						$(elem).parent().click();
					}
				});
			}

			if (!isActivityClick) {
				$('#activitiesFilter .inlineblock.selected').click();
			}

			isFirstClick = false;
		},
		{escape:false}
	);
}