$(function() {
	$(document).ready(function () {

		var CurObject = null;
		var CurLang = 'de';
		var delayTime = 5000;
		var dissolveTime = 60;

		// Startseite laden
		loadSite("willkommen.html")

		resizer();
		
		$(window).resize(function() {
			resizer();
		});			
		
		// Startseite im Mainmenu highlighten
		$("#MainMenuWrapper a").each(function() {
			if(CurObject == null && $(this).attr('href') == 'willkommen.html') {
				$(this).attr({'class' : "ActMenuItem"});
				CurObject = this;
			}
		});

		$('body a').each(function() {
			$(this).focus(function() {
				if ( $(this).blur() ) { $(this).blur(); }
			})
		});
		
		$("#MainMenuWrapper li a").each(function() {
			$(this).click(function(){

				if(this != CurObject) {
				// Seiteninhalt laden
					
					loadSite($(this).attr('href'));
				
					$(this).attr( {'class' : "ActMenuItem"} );
					$(CurObject).attr( {'class' : ""} );
					CurObject = this;
				}
				return false;
			
			});
		});

		$('#BottomMenuWrapper li a').each(function() {
			
			if( $(this).attr('href') != 'index.html') {
				$(this).click(function(){

					// Seiteninhalt laden
					if(this != CurObject) { 
						$(CurObject).attr( {'class' : ""} );
						$(this).attr( {'class' : "ActMenuItem"} );
						loadSite($(this).attr('href'));
						CurObject = this;
					}
					return false;
		
				});
			}
		});

		function loadSite(SiteURL) {

			// 1. Init
				$('#content').fadeOut('fast');
				$('#slideshowcontainer').fadeOut('fast',loadTopImages);

				SiteURL = SiteURL.replace(/content\/de\//g, "");

                // 2. Slideshow laden
                function loadTopImages() {
                    var slideLoad = SiteURL + " #TopImages";
                    $('#TopImages').fadeOut('fast',loadTopImagesContent);
                    
                    function loadTopImagesContent() {
                        $('#TopImages').load('content/'+CurLang+'/'+slideLoad,'',function(){ showTopImagesContent(); })
                    }
                    function showTopImagesContent() {
                        $('#TopImages').fadeIn('normal',loadSlideShow);
                    }
                }
	
				// 2. Slideshow laden
				function loadSlideShow() {
					var slideLoad = SiteURL + " #SlideShow";
					$('#slideshowcontainer').fadeOut('fast',loadSlideContent);
					
					function loadSlideContent() {
						$('#slideshowcontainer').load('content/'+CurLang+'/'+slideLoad,'',function(){ showNewSlideContent(); })
					}
					function showNewSlideContent() {
						$('#slideshowcontainer').fadeIn('normal',loadText);
						$("#SlideShow").cjSimpleSlideShow({delay: delayTime,dissolve: dissolveTime});
					}
				}

				// 3. Textcontent laden
				function loadText() {
					var textLoad = SiteURL + " #SiteContent";
					$('#content').fadeOut('fast',loadTextContent);

					function loadTextContent() {
						$('#content').load('content/'+CurLang+'/'+textLoad,'',showNewTextContent())
					}
					
					function showNewTextContent() {
						$('#content').fadeIn('normal', function() {});
					}
				}

		}
		
		function resizer() {
			
			var winHeight = $(window).height();
			var winWidth  = $(window).width();
			
			if(winWidth > 800) {
				var leftWidth  = ( (winWidth - 800) / 2 ) + 197;
				var rightWidth = ( (winWidth - 800) / 2 ) + 162;
			} else {
				var leftWidth  = 197;
				var rightWidth = 162;
			}

			$('#LeftBackground').css( 'width', leftWidth + 'px' );
			$('#RightBackground').css( 'width', rightWidth + 'px' );

		}			
		
	});
});
