(function($) {

/////////////////////////////////////////////////////////////////////

	var slideCount, slidePos, slideVisibleItems,
		  header, logo, nav, subNav, subNavFirst;

/////////////////////////////////////////////////////////////////////

	function init() {
		
		$('#royal-design').css('opacity', 0.25).hover(hoverIn, hoverOut);
		
		setupNav();
		setupAjax();
		
		$('a').mouseover(function(e){
			e.preventDefault();
		});
		
		$.address.change(function(e) {
			var hash = e.value;
			if ( hash != '/' ) {
				$('.helper').hide();
				makeRequest(hash);
			}
    });
	 
		$('a[rel]').click(function(e) {
			e.preventDefault();
		  $.address.value($(this).attr('rel'));
			$.address.title($(this).attr('xtitle')); 
		});

		$('.action-detail').live('click', function(e) {			
			e.preventDefault();
			
			
			    
			    
			$('body').append('<div class="detail-overlay"><a class="close" href="' + base + '">Schließen</a><iframe src="" frameborder="0" border="0"></iframe></div>');
			
			var url = $(e.target).attr('href');
			
			$('.detail-overlay iframe').attr('src', url);
			
			var overlay = $('.detail-overlay').css({
		    height: $(window).height(),
		    width: $(window).width()
	    });
			
			$(window).resize(function() {
			  overlay.css({
					height: $(window).height(),
					width: $(window).width()
				});
			});
			
			setTimeout(function() {
				overlay.fadeIn(800);
				overlay.find('.close')
				.css('opacity', 0.25)
				.hover(hoverIn, hoverOut)
				.click(function(e) {
					e.preventDefault();
					overlay.fadeOut(300, function() {
						$('.detail-overlay').remove();
					});			
				});		
			}, 40);
		});
		
		swfobject.embedSWF(base + "assets/audioplayer/audioplayer.swf", "audioplayer", "13", "13", "9", "expressInstall.swf", { xmlPath: base + "xml/audioplayer.xml" }, { wmode: "transparent" });
	}

/////////////////////////////////////////////////////////////////////	

	function makeRequest( hash ) {
		$('.helper').fadeOut();
		$('.content').append('<div class="loading"></div>');
		$('.loading').fadeIn(400, function() {
			$('.helper').load(base + hash + ' .content > .helper > .bd', function() {
				$('a').removeClass('active');
				$('.nav h2').removeClass('active');
				$('.loading').fadeOut(400, function() {
					$(this).remove();
				});
				$('.helper').fadeIn();
				setupAjax( hash.substring(1) );
				
				var active = $('.nav a[rel~="' + hash.substring(1) + '"]');
				active.addClass('active');
				active.parents('.nav li').find('h2').addClass('active');
				//console.log(active.parents('.nav li').find('h2'));

				if ( hash.substring(1) == 'impressum') {
					$('a[rel~="impressum"]').addClass('active');
				}
			});
			
		});
	}

/////////////////////////////////////////////////////////////////////	

	function setupAjax( hash ) {
	
		$('.detail-overlay').remove();
		
		/**/
		$('.slider').css('overflow', 'hidden');
	
		/**/
		$('.slider').scrollTo({ top: '0px', left: '0px' }, 0);
	
		/**/
		$('#next').css('opacity', 0.25).hover(hoverIn, hoverOut);
		$('#prev').css('opacity', 0.25).hover(hoverIn, hoverOut);
	
		/**/
		$('.action-detail').hide();
	
		/**/
		$('.hit-area').hover(function() {
			$(this).find('.action-detail').fadeIn();
		}, function() {
				$(this).find('.action-detail').fadeOut();
		});
	
		// Slider
	
		/**/
		slideCount = $('.image-slider .image').length || $('.slider .employee').length,
		slidePos = 1;
	
		/**/
		if ( slideCount > 2 ) {
			$('#next').show();
			slideVisibleItems = 2;
		}
		
		if ( $('.image-slider').length && slideCount > 3 ) {
			$('#next').show();
			slideVisibleItems = 3;
		}
	
		/**/
		$('body').unbind('click', nextSlide);
		$('body').unbind('click', prevSlide);
		
		$('body').bind('click', nextSlide);
		$('body').bind('click', prevSlide);
		
		
		if ( $('#gmap').length != 0 ) {
			
			var latitude = 50.73347,
				  longitude = 7.09816;
			
			if ( hash == 'anfahrt-koeln' ) {
				latitude = 50.93739;
				longitude = 6.9451;
			}

					var latlng = new google.maps.LatLng(latitude, longitude);
					var myOptions = {
						zoom: 16,
						center: latlng,
						mapTypeId: google.maps.MapTypeId.ROADMAP
					};

					var map = new google.maps.Map(document.getElementById("gmap"), myOptions);

					var marker = new google.maps.Marker({
						position: new google.maps.LatLng(latitude, longitude),
						clickable: false,
						map: map
					});

					var point = new google.maps.LatLng(latitude, longitude);
					
					marker.setPosition(point);
					map.setCenter(point);
		}
		
	}

/////////////////////////////////////////////////////////////////////

	function nextSlide(e) {
		
		if ( e.target.id == 'next' ) {
		
		e.preventDefault();
			/*
			console.log(
				"slideCount: " + slideCount + 
				" slidePos: " + slidePos + 
				" slideVisibleItems: " + slideVisibleItems +
				" posCalc: " + slideCount / slideVisibleItems
				);
			*/

			$('body').unbind(e);
		
			$('.slider').scrollTo({ top: '0px', left: slidePos * 1000 + 'px' }, 1400, { easing: 'easeInOutExpo', onAfter: function() {
				slidePos++;
				$('body').bind('click', nextSlide);
				if ( $(this).scrollLeft() != 0 ) {
					$('#prev').fadeIn();
					if ( slidePos == slideCount / slideVisibleItems ) {
						$('#next').hide();	
					}
				}
			} });
		
		}
	}

	function prevSlide(e) {

		if ( e.target.id == 'prev' ) {
			
				e.preventDefault();
		
			$('body').unbind(e);
		
			$('.slider').scrollTo({ top: '0px', left: ( $('.slider').scrollLeft() - 1000 ) + 'px' }, 1400, { easing: 'easeInOutExpo', onAfter: function() {
				slidePos--;
				$('body').bind('click', prevSlide);
				if ( $(this).scrollLeft() == 0 ) {
					$('#prev').hide();
				}
				if ( slidePos != slideCount / slideVisibleItems ) {
					$('#next').fadeIn();
				}
			} });
		}
	}

/////////////////////////////////////////////////////////////////////

	function setupNav() {
		
		header = $('.header').css('height', '80px');
		logo = header.find('h1').css('margin-top', '20px');
		subNav = $('.nav ul').hide();
		subNavFirst = $('.nav .first').css('width', '80px');
		nav = $('.nav').css('margin-left', '90px');
		
		nav.hoverIntent(navHoverIn, navHoverOut);
	}
	
	function navHoverIn() {
		header.animate({ height: 160 });
		logo = $('h1').animate({ marginTop: 55 });
		
		nav.animate({ marginLeft: 50 });
		
		subNav = $('.nav ul').fadeIn();
		subNavFirst = $('.nav .first').animate({ width: 240 }, {duration: 900, easing: 'easeInOutExpo'});
	}
	
	function navHoverOut() {
		
		header.animate({ height: 80 });
		logo.animate({ marginTop: 30 });
		
		nav.animate({ marginLeft: 90 });
		
		subNav.fadeOut();
		subNavFirst.animate({ width: 80 });
	}

/////////////////////////////////////////////////////////////////////

	function hoverIn() {
		$(this).stop().animate({ opacity: 1 });
	}
	
	function hoverOut() {
		$(this).stop().animate({ opacity: 0.25 });
	}

/////////////////////////////////////////////////////////////////////

	$(init);

/////////////////////////////////////////////////////////////////////

})(jQuery);
