/*********************************************************
* Variablen
*********************************************************/

var animationSpeed = parseInt('500'); // Animationsgeschwindigkeit


/*********************************************************
* Animation und Nachladen von Seiteninhalten
*********************************************************/

function animateSite()
{
	$('#content-menu li[id]').each(function(){ // Hauptmenü-Links markieren
		$(this).children('a').addClass('mainmenu-link');
	});
	$('#content-menu li ul').addClass('submenu'); // Submenüs markieren
	$('ul.submenu li a').addClass('submenu-link'); // Submenü-Links markieren
	
	if( $('#side-menu').attr('id') != 'side-menu' ) // Startseiten side-menu
		$('.skip').after('<ul id="side-menu"></ul>');
	
	animateSiteEventRegMainMenu($('#side-menu li a, #content-menu li a.mainmenu-link')); // Events registrieren
	animateSiteEventRegSubmenu($('.submenu-link'));
}

function animateSiteEventRegMainMenu( el )
{
	$(el).click(function(){ // Hauptmenü-Links mit Event versehen
		var curEl = $(this); // Event-Element
		if( !($(curEl).parents('.parent').hasClass('parent') || $(curEl).parents('.parent').hasClass('chosen')) ) // nicht auf aktive Menüeinträge anwenden
		{
			// aktuellen Inhalt einfahren
			$('.current-content').slideUp(animationSpeed, function(){
				
				$.getJSON($(curEl).attr('href') + '?action=firstlevel&random' + '=' + Math.random() * 100000, { a_rqt: true }, function(data){ // JSON-Request

					// Variablen-Annahme
					var pageTitle = ( data.pageTitle == null || data.pageTitle == 'false' || data.pageTitle == '0' ) ? '' : data.pageTitle;
					var firstlevelName = ( data.firstlevelName == null || data.firstlevelName == 'false' || data.firstlevelName == '0' ) ? '' : data.firstlevelName;
					var firstlevelClass = ( data.firstlevelClass == null || data.firstlevelClass == 'false' || data.firstlevelClass == '0' ) ? '' : data.firstlevelClass;
					var sideMenu = ( data.sideMenu == null || data.sideMenu == 'false' || data.sideMenu == '0' ) ? '' : data.sideMenu;
					var categoryImage = ( data.categoryImage == null || data.categoryImage == 'false' || data.categoryImage == '0' ) ? '' : data.categoryImage;
					var submenu = ( data.submenu == null || data.submenu == 'false' || data.submenu == '0' ) ? '' : data.submenu;
					var content = ( data.content == null || data.content == 'false' || data.content == '0' ) ? '' : data.content;
					content = content.replace(/<!--.*-->/, '');
					
					document.title = pageTitle + ' | ' + $.trim(document.title.replace(/.*\|/, '')); // Seitentitel anpassen
					var curElId = $(curEl).parent('li').attr('id'); // ID des content-menu-Eintrags auslesen
					
					// Status-Klassen neu setzen
					$('#content-menu li[id], #content-menu li[id]').removeClass('parent chosen'); // alte Klasse entfernen
					if( $(curEl).parent('li').attr('id').search(/side-menu.+/) != -1 ) // Wenn Klick auf Randmenü ...
						var curElId = 'menu-' + $(curEl).parent('li').attr('id').replace(/side-menu-/, ''); // ID des content-menu-Eintrags generieren
					$('#' + curElId).addClass(firstlevelClass); // Klasse vergeben
					
					// neuen Inhaltscontainer setzen und befüllen + Klassenvergabe
					$('.current-content').remove();
					$('#' + curElId + ' a').after('<div class="next-current">' + '</div>');
					$('.next-current').hide();
					$('.next-current').append(categoryImage + submenu + '<div id="content">' + content + '</div>');
					mailDecoding();
					
					if( submenu != '' )
					{
						animateSiteEventRegSubmenu($('#content-menu li ul li a'));
					}
					
					if( content != '' )
					{
						$('.next-current').slideDown(animationSpeed, function(){
							var curElOffset = $('#' + curElId).offset().top;
							$(this).attr('class', 'current-content');

							// Randmenü erneuern
							
							/*
							$('#side-menu').animate({marginTop: (((curElOffset-$('#header').height())*0.0625)-0.75) + 'em', opacity: 0}, animationSpeed, 'swing', function(){
								$('#side-menu').html(sideMenu);
								animateSiteEventRegMainMenu($('#side-menu li a'));
								$('#side-menu').animate({opacity: 1}, 300, function(){
									$('html,body').animate({scrollTop: 0}, animationSpeed, 'swing', function(){
										*/																	 
										var startLastBg = $('#content-menu .last').css('backgroundImage');
										if( startLastBg.search(/footer-home-background/) != -1 && firstlevelName != 'start' )
										{
											$('#content-menu .last').animate({paddingBottom: 0}, 300, function(){
												newStartLastBg = $(this).css('backgroundImage').replace(/footer-home-background/, 'footer-background');
												$(this).css({backgroundImage: newStartLastBg, backgroundRepeat: 'repeat-x'});
												$(this).animate({paddingBottom: 9.375 + 'em'}, 300);
											});
										}
										/*
									});
								});
							});
							*/
						});
					}
					else
					{
						var curElOffset = $('#' + curElId).offset().top;
						$('.next-current').attr('class', 'current-content');

						// Randmenü erneuern
						
						/*
						$('#side-menu').animate({marginTop: (((curElOffset-$('#header').height())*0.0625)-0.75) + 'em', opacity: 0}, animationSpeed, 'swing', function(){
							$('#side-menu').html(sideMenu);
							animateSiteEventRegMainMenu($('#side-menu li a'));
							$('#side-menu').animate({opacity: 1}, 300, function(){
								$('html,body').animate({scrollTop: 0}, animationSpeed, 'swing', function(){
									*/
									var startLastBg = $('#content-menu .last').css('backgroundImage');
									if( startLastBg.search(/footer-home-background/) == -1 && firstlevelName == 'start' )
									{
										$('#content-menu .last').animate({paddingBottom: 0}, 300, function(){
											newStartLastBg = $(this).css('backgroundImage').replace(/footer-background/, 'footer-home-background');
											$(this).css({backgroundImage: newStartLastBg, backgroundRepeat: 'no-repeat'});
											$(this).animate({paddingBottom: 28.125 + 'em'}, 300);
										});
									}
									/*
								});
							});
						});
						*/
					}
				});	
			});
		}

		return false;
	});
}

function animateSiteEventRegSubmenu( el )
{
	$(el).click(function(){
		var curEl = $(this); // Event-Element
		$.getJSON($(this).attr('href') + '?action=sublevel&random' + '=' + Math.random() * 100000, { a_rqt: true }, function(data){ // JSON-Request
			
			var pageTitle = ( data.pageTitle == null || data.pageTitle == 'false' || data.pageTitle == '0' ) ? '' : data.pageTitle;
			var categoryImage = ( data.categoryImage == null || data.categoryImage == 'false' || data.categoryImage == '0' ) ? '' : data.categoryImage;
			var submenu = ( data.submenu == null || data.submenu == 'false' || data.submenu == '0' ) ? '' : data.submenu;
			var content = ( data.content == null || data.content == 'false' || data.content == '0' ) ? '' : data.content;
			content = content.replace(/<!--.*-->/, '');
			
			document.title = pageTitle + ' | ' + $.trim(document.title.replace(/.*\|/, '')); // Seitentitel anpassen
			
			$('.current-content').slideUp(animationSpeed, function(){
				$(this).html(categoryImage + submenu + '<div id="content">' + content + '</div>');
				animateSiteEventRegSubmenu($('.current-content ul li a').not('#content'));
				mailDecoding();
				$(this).slideDown(animationSpeed);
			});	
		});
		
		return false;
	});
}


/*********************************************************
* Scrolllinks
*********************************************************/

function scrollInterna()
{
	$('a[href*=#]').click(function() {
		if( location.pathname.replace( /^\//,'' ) == this.pathname.replace( /^\//,'' ) && location.hostname == this.hostname && $(this).attr('href').replace( /#/,'' ) != '' )
		{
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length)
			{
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 800, 'swing');
				return false;
			}
		}
	});
};


/*********************************************************
* eMail-Verschlüsselung
*********************************************************/

function mailDecoding()
{
	$('.email').each(function(){
		var mailArr = $(this).html();
		mailArr = mailArr.split(' ');
		var mailTxt = mailArr[0] + "@" + mailArr[2] + "." + mailArr[4];
		var mailOut = "<a href=\"mailto:" + mailTxt + "\" title=\"" + mailTxt + "\">" + mailTxt + "</a>";
		$(this).html(mailOut);
	});
}


/*********************************************************
* Document-Ready
*********************************************************/

$(document).ready(function() { // Wenn DOM geladen, dann ...
	scrollInterna(); // Scrollinks
	mailDecoding(); // eMail-Verschlüsselung
	animateSite(); // Seitenanimation
});
