var initialFade = 1000;
var exitFade = 500;
var sectionFade = 1000;
var sectionFadeOut = 500;
var waitBeforeNextContentClick = 300;
var contentFade = 1000;
var contentFadeOut = 500;
var waitForContentInterval = 300;
var siteInitialized = false;
var videoVolume = 0.5;
/*
$('.something').addClass('masonryAll');
$('.something').masonry({
	fitWidth: true,
	itemSelector: '.imgElement',
	percentPosition: false,
	transitionDuration: '0.75s',
});
*/
var updateVideoVolumes = function() {
	$.each($('video'), function() {
		this.volume = videoVolume;
	});
};
$(document).ready(function() {
	var currBody = $('body');
	$('.mainContainer3D').removeClass('initRotation');
	$('.mainContainer').animate({
		opacity: 1,
	}, initialFade, function() {
		siteInitialized = true;
		$('.arrowBox').animate({
			opacity: 1
		}, initialFade);
	});
	/*setTimeout(function() {
		$('.arrowBox').animate({
			opacity: 1
		}, initialFade);
	}, initialFade);*/
	if (typeof initialKey!=='undefined' && typeof initialTitle!=='undefined') {
		loadContent(initialKey, initialTitle);
	}else{
		loadContent($('.menu > .menuEntry:first-child').data('key'), $('.menu > .menuEntry:first-child').data('title'));
	}
	currBody.on('click', '.menuEntry', function() {
		loadContent($(this).data('key'), $(this).data('title'));
	}).on('click', '.insideConMenu > *', function() {
		loadContent($(this).data('key'));
	// }).on('volumechange play pause', 'video', function() {
		// videoVolume = this.volume;
	// }).on('contextmenu', 'video', function(e) {
		// e.preventDefault();
	}).on('click', '.feedBackBox > *', function() {
		$(this).animate({
			opacity: 0,
		});
		var thisElement = this;
		setTimeout(function() {
			$(thisElement).remove();
		}, 1000);
	});
	var retrievingFeedback = false;
	function showFeedBack() {
		if (retrievingFeedback===true) return;
		retrievingFeedback = true;
		$.get('index.php?action=showfeedback', function(data) {
			$('.feedBackBox').append(data);
			$('.feedBackBox > *').not('.active').animate({
				opacity: 1
			}, 500);
			$('.feedBackBox > *').not('.active').addClass('active');
			retrievingFeedback = false;
		});
	}
	showFeedBack();
	$('.logoutButton').on('click', function() {
		$.get('index.php?action=logout', function(data) {
			if (data!=='true') {
				showFeedBack();
			}else{
				$('.mainContainer3D').addClass('exitRotation');
				$('.mainContainer, .feedBackBox, .arrowBox').animate({
					opacity: 0
				}, exitFade, function() {
					window.location.href = window.location.origin + window.location.pathname;
				});
			}
		});
	});
	function checkLogin() {
		var postData = {
			's': sessionID,
		};
		$.post('index.php?action=checklogin', postData, function(data) {
			if (data==='false') {
				checkIfLoginIsStillActivePlease = false;
console.log('LOGOUT!');
				//window.location.href = window.location.origin + window.location.pathname;
			}
		});
	}
	setInterval(function() {
		if (checkIfLoginIsStillActivePlease===true) checkLogin();
	}, 5000);
	$('.arrowBox > .arrowLeft').on('click', function() {
		var curr = $('.menuEntry.active');
		var key = $(curr).data('count');
		if (key==1) {
			key = lastMenuEntryCount;
		}else{
			key--;
		}
		loadContent($('.menuCount'+key).data('key'), $('.menuCount'+key).data('title'));
	});
	$('.arrowBox > .arrowRight').on('click', function() {
		var curr = $('.menuEntry.active');
		var key = $(curr).data('count');
		if (key==lastMenuEntryCount) {
			key = 1;
		}else{
			key++;
		}
		loadContent($('.menuCount'+key).data('key'), $('.menuCount'+key).data('title'));
	});
	$('.eoLink').on('click', function() {
		loadContent($(this).data('key'), $(this).data('title'));
	});
	currBody.on('submit', '.loginForm', function(e) {
		e.preventDefault();
		var pin = $('.loginForm .loginPin').val();
		$.post('index.php?action=login', {pin: pin}, function(data) {
			if (data!=='true') {
				showFeedBack();
			}else{
				$('.mainContainer3D').addClass('exitRotation');
				$('.mainContainer, .feedBackBox').animate({
					opacity: 0
				}, exitFade, function() {
					window.location.href = window.location.origin + window.location.pathname;
				});
			}
		});
	});
	currBody.on('click', '.clickImage', function() {
		var imgPath = $(this).attr('src');
		$('.fullImageBox').css('background-image', 'url('+imgPath+')');
		$('.fullImageBoxOuter').css('display', 'block');
		$('.fullImageBoxOuter').animate({
			opacity: 1
		}, 500);
	});
	/*var nextHoverID = 0;
	currBody.on('mouseover', '.clickImage', function() {
		if (typeof $(this).data('hoverid')!=='undefined') return;
		var myX = $(this).position().left;
		var myY = $(this).position().top;
		var myWidth = $(this).width();
		var myHeight = $(this).height();
		var hoverID = ++nextHoverID;
		var myFontSize = Math.round((myWidth>myHeight ? myHeight : myWidth)/1.5);
		$(this).data('hoverid', hoverID);
		$(this).addClass('clickImageHover-' + hoverID);
		$(this).parent().append('
');
		$('div.clickImageHover-' + hoverID).animate({
			opacity: 1
		}, 100);
	});
	currBody.on('mouseout', '.clickImage', function() {
		if (typeof $(this).data('hoverid')==='undefined') return;
		var curr = this;
		var hoverID = $(this).data('hoverid');
		$('div.clickImageHover-' + hoverID).animate({
			opacity: 0
		}, 100, function() {
			$('.clickImageHover').filter('[data-hoverid="' + $(curr).data('hoverid') + '"]').remove();
			$.removeData(curr, 'hoverid');
			$(curr).removeClass('clickImageHover-' + hoverID);
		});
	});
	setInterval(function() {
		$('.clickImageHover').each(function(index) {
			var hoverID = $(this).data('hoverid');
			//var curr = $('img').filter('[data-hoverid="' + hoverID + '"]');
			var curr = $('img.clickImageHover-' + hoverID)
			console.log(curr);
			var myX = $(curr).position().left;
			var myY = $(curr).position().top;
			var myWidth = $(curr).width();
			var myHeight = $(curr).height();
			$(this).css('top', myY+'px');
			$(this).css('left', myX+'px');
			$(this).css('width', myWidth+'px');
			$(this).css('height', myHeight+'px');
			var forTheAnimation = $(this).find('.material-icons');
			if ($(forTheAnimation).data('bestanimationintheworld')=='1') {
				$(forTheAnimation).data('bestanimationintheworld', '2');
				$(forTheAnimation).html('');
			}else{
				$(forTheAnimation).data('bestanimationintheworld', '1');
				$(forTheAnimation).html('');
			}
		});
	}, 800);*/
	function closeFullImageOverlay() {
		$('.fullImageBoxOuter').animate({
			opacity: 0
		}, 500, function() {
			$('.fullImageBoxOuter').css('display', 'none');
			$('.fullImageBox').css('background-image', '');
		});
	}
	currBody.on('click', '.fullImageBoxOuter', function(e) {
		if (e.target==this) {
			closeFullImageOverlay();
		}
	});
	currBody.on('click', '.fullImageBoxOuter .close', function() {
		closeFullImageOverlay();
	});
	currBody.on('click', '.eoLink', function() {
		loadContent($(this).data('key'), $(this).data('title'));
	});
	$(window).on('resize', function() {
		if ($('.masonryAll').length==0) return;
		$('.masonryAll').masonry('layout');
	});
	currBody.on('click', '.backToWebsiteButton', function() {
		location.href = "/de/";
	});
});
var retrievingNewContent = false;
var nextContentQueued = false;
var nextContentQueuedTitle = false;
function loadContent(key, title) {
	if (retrievingNewContent===true) {
		/*if (nextContentQueued===false) {
			nextContentQueued = key;
			nextContentQueuedTitle = title;
		}
		setTimeout(function() { loadContent(nextContentQueued, nextContentQueuedTitle) }, 200);*/
		return;
	}
	retrievingNewContent = true;
	var nextContent = 'WAIT';
	$.get('index.php?action=load&load='+key, function(data) {
		nextContent = data;
	}).fail(function() {
		nextContent = 'ERROR';
	});
	$('.menuEntry.active').removeClass('active');
	$('#menuEntry_' + key + ', .activeIf-' + key).addClass('active');
	if (typeof title!=='undefined' && $('.sectionName').html()!==title) {
		$('.sectionName').animate({
			opacity: 0
		}, sectionFadeOut-1);
		setTimeout(function() {
			$('.sectionName').html(title);
			$('.sectionName').animate({
				opacity: 1
			}, sectionFade);
		}, sectionFadeOut);
	}
	/*}else{
		$('.sectionName').css('opacity', '0');
		$('.sectionName').html(title);
		$('.sectionName').animate({
			opacity: 1
		}, sectionFade);
	}*/
	function renderContent() {
		if (nextContent=='WAIT' || siteInitialized!==true) {
			setTimeout(function() {
				renderContent();
			}, waitForContentInterval);
		}else{
			$('.masonryAll').masonry('destroy');
			$('.contentBox').remove();
			if (nextContent=='ERROR') {
				$('.mainContainer').append('Keine Verbindung zum Server möglich. Überprüfen Sie Ihre Internetanbindung.
 ');
			}else{
				$('.mainContainer').append(nextContent);
			}
			$('.useMasonry:not(.masonryAll)').masonry({
				fitWidth: false,
				itemSelector: 'img',
				percentPosition: false,
				transitionDuration: '0.5s',
			});
			$('.useMasonry:not(.masonryAll)').addClass('masonryAll');
			$('.masonryAll').masonry('layout');
			for(var i = 300; i <= 1500; i+=300) {
				setTimeout(function() { $('.masonryAll').masonry('layout'); }, i);
			}
			setTimeout(function() {
				retrievingNewContent = false;
			}, waitBeforeNextContentClick);
			$('.contentBox').animate({
				opacity: 1
			}, contentFade, function() {
				/*setTimeout(function() {
					nextContentQueued = false;
					nextContentQueuedTitle = false;
				}, 500);*/
			});
			$(document).scrollTop(0);
			/*setTimeout(function() {
				var main = $('.mainContainer');
				if (main.find('video').length==1) {
					main.find('video').first()[0].play();
				}
			}, 500);*/
			$.each($('video:not(.vid-init)'), function() {
				var curr = $(this);
				curr.addClass('vid-init');
				curr.on('volumechange play pause', function() {
					videoVolume = this.volume;
				}).on('contextmenu', function(e) {
					e.preventDefault();
				});
			});
			updateVideoVolumes();
		}
	}
	/*function checkDividers() {
		$('.divider').each(function(index) {
			var onlyif = $(this).data('onlyif');
			if ($(onlyif).length>0) {
				var checkX = $(onlyif).position.left;
				var checkY = $(onlyif).position.top;
				var allGood = true;
				$(onlyif).each(function(index) {
					if ($(this).position.left!==checkX || $(this).position.top!==checkY) {
						allGood = false;
					}
				});
				console.log(allGood ? 'Good' : 'Bad');
			}
		});
	}
	$(window).on('resize', function() {
		checkDividers();
	});*/
/*
$('.something').addClass('masonryAll');
$('.something').masonry({
	fitWidth: true,
	itemSelector: '.imgElement',
	percentPosition: false,
	transitionDuration: '0.75s',
});
*/
	if ($('.contentBox').length>0) {
		$('.contentBox').animate({
			opacity: 0
		}, contentFadeOut-1);
		setTimeout(function() {
			$('.contentBox').remove();
			renderContent();
		}, contentFadeOut);
	}else{
		renderContent();
	}
}