$(function(){
	"use strict";
// -----------------------------------------------------------------------------------------------------------------
// Startbild
	if($('.start').length){
		$.supersized({
			slides:[
				{image:'/wp-content/themes/einstein/img/intro/1.jpg'},
				{image:'/wp-content/themes/einstein/img/intro/2.jpg'},
				{image:'/wp-content/themes/einstein/img/intro/3.jpg'},
				{image:'/wp-content/themes/einstein/img/intro/4.jpg'},
				{image:'/wp-content/themes/einstein/img/intro/5.jpg'},
				{image:'/wp-content/themes/einstein/img/intro/6.jpg'},
				{image:'/wp-content/themes/einstein/img/intro/7.jpg'},
				{image:'/wp-content/themes/einstein/img/intro/8.jpg'}
			],
			random:1,
			autoplay:0,
			slideshow:0,
			vertical_center:1
		});
		$('header').animate({'top':0},750,'easeOutExpo');
		$('footer').animate({'bottom':0},750,'easeOutExpo');
		$('#supersized').fadeTo(1000,1).delay(2000).fadeTo(300,0,function(){
			$(this).hide();
			$('.start').removeClass('start');
			$('#content').fadeTo(300,1);
		});
	} else {
		$('#content').show();
	}

// -----------------------------------------------------------------------------------------------------------------
// Gesammelte Funktionen (Ablauf)
	nav();
	function functions(){
		subpages();
		form();
		content();
		congress();
		footerHeight();
		if($('#supersized:not(:empty)').length || $('.start').length){
			$('#content').hide();
			impressionen();
		}
	}
	functions();
// -----------------------------------------------------------------------------------------------------------------
// Funktionen, die bei Fenstergrössen-Änderung ausgeführt werden

	$(window).resize(function(){ footerHeight(); });

// -----------------------------------------------------------------------------------------------------------------
// Footer-Höhe anpassen

	function footerHeight(){
		if($('#supersized:hidden').length){
			$('footer').stop(true).delay(300).animate({
				height:($(window).height()-$('#header').outerHeight()-($('#content').outerHeight()))
			},700,'easeOutExpo').css('bottom',0);
		} else {
			$('footer').stop(true).delay(300).animate({
				height:35
			},700,'easeOutExpo').css('bottom',0);
		}
	}

// -----------------------------------------------------------------------------------------------------------------
// Teaser-Box mit Pagination

	function subpages() {
		var $left = 0;
		var $active = 0;
		var $split_by = 3;
		var splitcounter = 0;
		var results =[];
		var elements = $('#slider').children('.subpage');
		$.map(elements,function(i,n){
			if( n%$split_by === 0 ){
				results.push(n);
			}
		});
		if(elements.length > 3){
			$.each(results,function(i,v){
				splitcounter++;
				$('#slider').attr('data-pages',splitcounter);
			});
			$('.arrow.right').fadeIn(250);
			$('#slider').attr('data-active',$active);
			$('#arrows .arrow').live('click',function(){
				if($(this).hasClass('right')){
					if(splitcounter-($active+2) < 1){
						$('.arrow.left').fadeIn(250);
						$('.arrow.right').fadeOut(250);
					}
					if(splitcounter-($active+1) >= 1){
						$active = $active+1;
						console.log($active);
						$left = $active * 866;
						$('#slider').stop(true).animate({left:-$left},1200,'easeOutExpo');
						$('#slider').attr('data-active',$active);
					}
				} else {
					if($active-2===-1){
						$('.arrow.right').fadeIn(250);
						$('.arrow.left').fadeOut(250);
					}
					if($active-1 >= 0){
						$active = $active-1;
						$left = $active * 866;
						$('#slider').stop(true).animate({left:-$left},1200,'easeOutExpo');
						$('#slider').attr('data-active',$active);
					}
				}
			});
		}
	}
// -----------------------------------------------------------------------------------------------------------------
// Kongress-Räume

	function congress(){
		$('.beschreibung-raeume').hide();
		$('.kongress-h1').click(function(){
			$(this).next('.beschreibung-raeume').toggle('blind');
			if($(this).find('.plus').length) {
				$(this).find('.plus').removeClass('plus').addClass('minus').text('-');
				return false;
			}
			if($(this).find('.minus').length) {
				$(this).find('.minus').removeClass('minus').addClass('plus').text('+');
				return false;
			}
		});
		var $split_by = 1;
		var splitcounter = 0;
		var results =[];
		var elements = $('#slider').children('.sichtfenster');
		$.map(elements,function(i,n){
			if( n%$split_by === 0 ){
				results.push(n);
			}
		});
		$('#pagination a:not(.back):last').addClass('active');
		$('#pagination a:not(.back)').live('click',function(){
			$('#pagination a').removeClass('active');
			$(this).addClass('active');
			var $left = 866 * ($(this).data('pagination')-1);
			$('#slider').stop().animate({left:-$left},1000,'easeOutExpo');
		});
	}
// -----------------------------------------------------------------------------------------------------------------
// Formular

	function form() {
		$('.wpcf7-validates-as-required').live('blur',function(){
			if($(this).attr('value') === ''){
				$(this).next().show();
			} else {
				$(this).next().hide();
			}
		});
		$('.wpcf7-not-valid-tip-no-ajax').live('click',function(){
			$(this).hide().prev().focus();
		});
		$('.wpcf7-response-output').live('click',function(){
			$(this).fadeOut(300);
		});
		if($('#nav.de').length){
			var $dateFormat = 'dd.mm.yy';
			var $dayNamesMin = ['So','Mo','Di','Mi','Do','Fr','Sa'];
			var $monthNames = ['Januar','Februar','März','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember'];
		} else if($('#nav.en').length){
			$dateFormat = 'dd.mm.yy';
		}
		$('.date').datepicker({
			dateFormat:$dateFormat,
			dayNamesMin:$dayNamesMin,
			monthNames:$monthNames,
			firstDay: 1,
			prevText: '<',
			nextText: '>'
		});
	}
// -----------------------------------------------------------------------------------------------------------------
// Navigation (verzögerter Hover, Logo ausblenden)

	function nav() {
		var config = {    
			over: function(){
				$(this).addClass('active');
				$(this).find('.children').fadeIn(50);
			},
			timeout: 200,
			out: function(){
				$(this).find('.children').fadeOut(200);
				$(this).removeClass('active');
			}
		};
		$('#nav a').removeAttr('title');
		$('.current_page_item').removeClass('current_page_item');
		$('#nav>li').hoverIntent(config);
		$('header').hover(function(){
			$('#logo').stop(true).fadeTo(500,.1,function(){
				$(this).addClass('active');
			});
		},function(){
			$('#logo').stop(true).animate({'height':150},1000,function(){
				$(this).stop(true).fadeTo(500,1).removeClass('active');
			});
		});
		// Newsletter DE/EN
		if($('#nav.en').length && $('#newsletter_de').length){
			$('#newsletter_de').hide();
			$('#newsletter_en').show();
		}
	}
// -----------------------------------------------------------------------------------------------------------------
// Inhaltsanpassungen

	function content(){
		$('#personal li:nth-child(4n-2)').addClass('marginL marginR');
		$('#personal li:nth-child(4n-1)').addClass('marginR');
		$('table').find('th:first').addClass('first');
		$('.anfragen').attr('href','/hotel/reservation/');
		$('#personal li a').hover(function(){
			$(this).find('.normal').stop(true).fadeTo(300,0);
		},function(){
			$(this).find('.normal').stop(true).fadeTo(300,1);
		});
		if($('iframe:not(.fancybox-iframe)').length){
			$('.spalte-artikelbild-bild').remove();
			$('iframe').css('visibility','visible');
		}
		$('iframe[src^="http://www.youtube.com"]').attr('id','youtube');
		$('.book').attr('target','_blank').attr('href','https://booking.einstein.ch/WBE3/');
		if(document.referrer===''){
			$('.back').hide();
		}
		$('.back').live('click',function(){
			history.back();
			return false
			
		});
		$('#header_bg').fadeTo(300,.8);
		$('a[href$=pdf]').addClass('pdf').attr('target','_blank');
		$('.pdf:last-child').after('<div class="clear" />');
		
		if($('#content p:first').html() === '&nbsp;'){
			$('#content p:first').remove();
		}

	}
// -----------------------------------------------------------------------------------------------------------------
// Impressionen

	function impressionen(){
		$('#header_bg').fadeTo(500,.25);
		$('#imprNext').live('click',function(){
			api.nextSlide();
		});
		$('#imprPrev').live('click',function(){
			api.prevSlide();
		});
		$('.imprNav').delay(2000).fadeTo(500,0).hover(function(){
			$(this).stop(true).fadeTo(300,1);
		},function(){
			$('.imprNav').delay(700).fadeTo(300,0);
		});
	}
});
