$.noConflict();

jQuery(document).ready(function($) {
	
	jQuery('#overlay').css('opacity', 0.65);

	/* Aufklapp-Inhalte
	-------------------------------------------------------------*/
	jQuery(".accordion .accordion-navigation").click(function() {
		jQuery(this).toggleClass('accordion-navigation-active');
		jQuery(this).next().toggleClass('accordion-content-active');
	});

	
	/* NAVIGATOR, BROWSER, OS
	-------------------------------------------------------------*/
	var agt=navigator.userAgent.toLowerCase();
	
	var NAVIGATOR;
	if( navigator.userAgent.indexOf('iPod') != -1 ) { 
		NAVIGATOR = "iPod";
	} else if( navigator.userAgent.indexOf('iPhone') != -1 ) {
		NAVIGATOR = "iPhone";
	} else if( navigator.userAgent.indexOf('iPad') != -1 ) {
		NAVIGATOR = "iPad";
	} else {
		NAVIGATOR = "any";
	}
	
	/* MENU-BG with SVG or PNG
	-------------------------------------------------------------*/
	if( document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") != true ) {
		// All browser, which do not support SVG at all
		jQuery('#nav').css("backgroundImage", "url(fileadmin/media/_statics/menu-bg.png)");
	} else {
		
		// IE with SVG-plugin will not display SVGs correctly
		if ( agt.indexOf("msie") != -1 ) {
			jQuery('#nav').css("backgroundImage", "url(fileadmin/media/_statics/menu-bg.png)");
			
		// Safari (does not support transparency for embeded SVGs, but for SVGs as background)
		} else if( agt.indexOf("safari") != -1 ) {
			jQuery('#nav').css("backgroundImage", "url(fileadmin/media/_statics/menu-bg.svg)");
		
		// Modern browsers with SVG Support for embeded SVGs
		// (btw: Firefox does not support transparency for SVGs as background)
		} else {
			jQuery('#nav').prepend("<div id='nav-firefox-bugfix-bg'><embed src='fileadmin/media/_statics/menu-bg.svg' width='600' height='1050' type='image/svg+xml' /></div>");
			jQuery('#nav').addClass("nav-firefox-bugfix");
		}
	}


	/* BEREICHSFOTOS
	-------------------------------------------------------------*/
	function adjustImages() {
		
		var 	_img = jQuery('#bereichsfotos-innerwrap > div > img:first'),
		
			 	imageTotalWidth = jQuery(_img).width(), 
				imageTotalHeight= jQuery(_img).height();
				imageRatio 		= imageTotalWidth / imageTotalHeight,
	
				contentWidth 	= jQuery(window).width() - 60,
				contentHeight	= jQuery(window).height() - 60, 
		  		contentRatio 	= contentWidth / contentHeight; 
		
		//=========== Bildgröße berechnen ===========
		if(imageRatio > contentRatio) 
		{
			var imageHeight = contentHeight, 
			 	imageWidth	= Math.round( imageHeight * imageRatio );
		} else {
			var imageWidth 	= contentWidth,
			 	imageHeight = Math.round( imageWidth / imageRatio );
		}
		
		//=========== Bildmitte berechnen ===========
		if( imageHeight >  contentHeight ) {
			var offsetY = Math.round(  (-0.5) * (imageHeight - contentHeight)  );
		} else {
			var offsetY = 0;
		}
		if( imageWidth >  contentWidth ) {
			var offsetX = Math.round(  (-0.5) * (imageWidth - contentWidth)  );
		} else {
			var offsetX = 0;
		}
		
		//============= Größen anwenden ==============
		jQuery("#bereichsfotos-innerwrap > div > img").width( imageWidth ).height( imageHeight ).css({ marginTop: offsetY, marginLeft: offsetX });

		
		//====== Höhe des sichtbaren Containers anpassen ======
		jQuery("#bereichsfotos-innerwrap").height( contentHeight );
	}


	/* SHOW & HIDE MENU
	-------------------------------------------------------------*/
	function showMenu() {
		
		if( jQuery('#home').length <= 0 ) {
			jQuery("#overlay").css({
				display: 'block',
				backgroundColor: '#ffe6e1',
				opacity: 0.65
			});	
			if( NAVIGATOR == "iPad" ) {
				jQuery("#nav-hide-button").css("display", "block");
			}
		} 
		jQuery("#menu-sub").css("zIndex", 1);
		jQuery("#nav-show-button").css("display", "none");
		jQuery('#bereichsfotos').css("display", "block");
		if( NAVIGATOR == "iPad" ) {
			jQuery("#nav").stop().animate({
				marginLeft: 0
			}, 700, 'swing');
		} else {
			jQuery("#nav").stop().css("marginLeft", 0);
		}
	}
	
	function hideMenu( _case ) {
		
		jQuery("#overlay").css("display", "none");
		jQuery("#menu-sub").css("zIndex", 500);
		jQuery("#nav-show-button").css("display", "block");
		jQuery('#bereichsfotos').css("display", "none");
		
		if( !_case ) {
			jQuery("#nav").stop().css("marginLeft", "-500px");
		} else {
			jQuery("#nav").stop().animate({
				marginLeft: '-500px'
			}, 1000, 'swing');
		}
	}
	
	
	
	/* INIT
	-------------------------------------------------------------*/
	var menuIsAnimating = false;
	
	function initSite() {
		/* Show menu one site "home" */
		if( jQuery('#home').length > 0 ) {
			showMenu();
		} 
	}
	initSite();
	
	
	/* HOME-TEASER
	-------------------------------------------------------------*/
	// No hover/opacity for mobile devices and the IE
	if( NAVIGATOR == "any" && agt.indexOf("msie") == -1 ) {
		jQuery(".teaser-item").mouseenter(function(){
			jQuery(this).css({
				'backgroundColor' : '#fff2f0',
				'borderTop' : '1px solid #f5cfc8',
				'borderBottom' : '1px solid #f5cfc8' });
		}).mouseleave(function(){
			jQuery(this).css({
				'backgroundImage' : 'none', 
				'backgroundColor' : 'transparent',
				'borderTop' : '1px solid transparent',
				'borderBottom' : '1px solid transparent' });
		});
	}
	
	/* MENU MAIN
	-------------------------------------------------------------*/
	
	jQuery("#nav-show-button").click(function(e){
		if( menuIsAnimating == false ) {
			if( jQuery("#nav").css("marginLeft") == '-500px' ) { 
				showMenu();
			}
		}
	});
	
	
	if( NAVIGATOR == "any" ) {
		// Hover for the overlay (desktop only)
		jQuery("#overlay").mouseenter(function(){
			jQuery("#overlay").css({
				backgroundColor: 'transparent',
				opacity: 1
			});
			jQuery("#nav").stop().animate({
				marginLeft: '-100px'
			}, 1000, 'swing');
			jQuery("#overlay #nav-hide-button").css("left", "700px").stop().animate({
				left: '-=50'
			}, 250, 'swing', function() {
				jQuery("#overlay #nav-hide-button").animate({
					left: '+=50'
				}, 250, 'swing', function() {
					jQuery("#overlay #nav-hide-button").animate({
						left: '-=50'
					}, 250, 'swing', function() {
						jQuery("#overlay #nav-hide-button").animate({
							left: '+=50'
						}, 250, 'swing');
					});
					
				});
			});
		});
		jQuery("#overlay").mouseleave(function(){
			//alert("mouseleave");
			if( jQuery(this).css('display') != 'none' ) {
				jQuery("#overlay").css({
					backgroundColor: '#ffe6e1',
					opacity: 0.65
				});
				jQuery("#nav").stop().animate({
					marginLeft: 0
				}, 500, 'swing');
				jQuery("#overlay #nav-hide-button").css("left", "700px").stop();
			}
		});
	}
	
	jQuery("#overlay").click(function(e){
		if( NAVIGATOR == "iPad" ) {
			hideMenu("fade");
		} else {
			hideMenu();
		}
	});
	
	/* First level 
	------------------------------*/
	var menuHover = false;
	
	jQuery("#nav .main-item").click(function(){
		if( jQuery('#home').length <= 0 ) {
			hideMenu();
		}
	});
	
	jQuery("#nav .main-item").mouseenter(function() {
		
		
		if(menuIsAnimating == false) {
			
			// BEREICHSFOTO zeigen
			jQuery('#ajax-content').css("display", "none");
			jQuery('#bereichsfotos').css("display", "block");
			jQuery('#bereichsfotos-innerwrap > div').css("display", "none");
			
			var act = "#" + jQuery(this).attr("data-bereichsfoto");
			jQuery(act).css("display", "block");
			
			// LOGOS
			var index = jQuery("#nav .main-item").index( jQuery(this) ),
				topPos = (index+1)*70 + 180 + 70;
			jQuery("#logos ul li img").stop().css("opacity", 0.35).eq(index).css("opacity", 1);
			if( menuHover == false ) {
				jQuery("#logos").css("display", "block").css("opacity", 0.2).stop().animate({ marginTop: topPos, opacity: 1 }, 1000, 'swing');
			} else {
				jQuery("#logos").css("display", "block").css("opacity", 1).stop().animate({ marginTop: topPos }, 500, 'swing');
			}
			menuHover = true;
		}
	});
	jQuery("#nav .main-item").mouseleave(function() {
			// BEREICHSFOTO ausblenden
			jQuery('#bereichsfotos-innerwrap > div').css("display", "none");
			jQuery('#bereichsfotos').css("display", "none");
			jQuery('#ajax-content').css("display", "block");
			jQuery("#logos").stop().animate({ opacity: 0 }, 500, 'swing', function() {																  
				menuHover = false;
				jQuery("#logos").css("display", "none");
			});
	});
	
	/* BEREICHSFOTOS
	-------------------------------------------------------------*/
	jQuery(window).resize( function(e){
		adjustImages();
	});
	adjustImages();
	
	
	
	
});
