$(document).ready(function(){
/*	Fancybox
**************************************************/
	$('.fancybox').fancybox();
	
/*	Startseite - News
**************************************************/
	// Text wird abgeschnitten
	$('#content.startseite ul.newsList .newsListText .borderLeft .theText p').textCutter({ minChars:330 });

/*	Portrait - News
**************************************************/
	// Text wird abgeschnitten
	$('#content.portrait ul.newsList .newsListContent p').textCutter({ minChars:370 });
	$('#content.portrait ul.newsList .newsListContent').each(function(){
		$(this).children('p').append('&nbsp;&nbsp;');
		$(this).children('a').appendTo($(this).children('p'));
	});

/*	Startseite - Events
**************************************************/
	// Text wird abgeschnitten
	$('#mycarousel li .newsListContent .newsListText .theText p').textCutter({ minChars:200 });	
	var highestLi = 0;
	$('div.eventsStartseite .newsList li').each(function(){
		$(this).height($(this).find('.newsListText').outerHeight(true) + $(this).find('.newsListImage').outerHeight(true));
		if($(this).height() > highestLi){
			highestLi = $(this).height();
		}
		$('div.eventsStartseite .newsList li').height(highestLi);
		$('div#eventsLeft').height(highestLi);
		$('div#eventsRight').height(highestLi);
	});
	$('#mycarousel').jcarousel({
		'auto'				: 5,
		'scroll'            : 1,
		'animation'         : 'slow',
		'wrap'              : 'circular',
		'buttonNextHTML'    : '<div id="eventsRight"><span></span></div>',
		'buttonPrevHTML'    : '<div id="eventsLeft"><span></span></div>'
    });
    $('#eventsLeft').prependTo('.eventsStartseite');
    $('#eventsRight').prependTo('.eventsStartseite');
	$('#eventsLeft').hide();
	$('#eventsRight').hide();
	$('.eventsStartseite').hover(function(){
		$('#eventsLeft').show();
		$('#eventsRight').show();
	},function(){
		$('#eventsLeft').hide();
		$('#eventsRight').hide();
	});

/*	pop-eye Galerie
**************************************************/
	$('.gLi').each(function(){
		$(this).wrapInner('<a href="' + $(this).find('img:first-child').attr('src') + '"></a>');
		$(this).find('img:first-child').remove();
		$(this).find('img').attr('alt',$(this).find('.caption').text());
		$(this).find('a').attr('title',$(this).find('.caption').text());
	});
	$('ul.gUl').each(function(){
		if($(this).attr('title').length > 0) {
			var title = $(this).attr('title');
			$(this).find('.gLi').each(function(){
				$(this).find('.ppy-extcaption').text(title);
				$(this).find('img').attr('alt', title);
				$(this).find('a').attr('title', title);
			});
		}
	});
	$('.ppy-div').popeye({
		caption:    'permanent',
		direction:  'right'
	});
	$('.ppy-placeholder').each(function(){
		if($(this).find('ul.gUl').attr('title').length > 0) {
			var title = $(this).find('ul.gUl').attr('title');
			$(this).find('.title td').html('<span class="h2">' +title + '</span>');
		}
		$(this).find('.title').hide();
		if( $(this).find('.title td').text().length > 0 ) {
			$(this).hover(function(){
				$(this).find('.title').show();
			},function(){
				$(this).find('.title').hide();
			});
		}
	});
	$('.ppy-stage').each(function(){
		$(this).find('.ppy-nav .ppy-switch-compact').text('X').css({
			'background'    : 'none',
			'font-size'     : '25px',
			'margin'        : '-3px 0 0',
			'padding'       : '0 0 0 8px',
			'width'         : '26px'
		});
	});

/* Downloads *************************************/
	downloadBox($('.csc-default .csc-downloadBox'));
	function downloadBox(element){
		element.find('br').remove();
		element.find('span:not(.fileDesc)').each(function(){
			var spanA = $(this);
			var spanB = $(this).next();
			spanA.wrapInner('<span class="linkText"></span>');
			spanA.find('span.linkText a').text(spanB.text());
			spanB.remove();
			spanA.css({
				display		: 'block',
				clear		: 'both',
				marginBottom: '6px'
			});
			spanA.children().css({
				display		: 'block',
				float		: 'left'
			});
			spanA.append('<span class="clearfix"></span>');
			spanA.find('.linkText a').each(function(){
				if ( $(this).text().search('###') >= 0 ) {
					var split = $(this).text().split('###');
					$(this).text(split[0]);
					$(this).after(' ' + (split[1]));
				}
			});
			spanA.wrap('<li></li>');
		});
		element.wrapInner('<ul></ul>');
	}
	
/*	Bildergrößen korrigieren
**************************************************/
	$('#content.eineSpalte ul.newsList .newsListImage img').each(function(){
		var width	= $(this).width();
		var height	= $(this).height();
		$(this).width(140);
		$(this).height(height/(width/140));
	});

	$('#rechts img').each(function(){
		var width	= $(this).width();
		var height	= $(this).height();
		$(this).width(165);
		$(this).height(height/(width/165));
	});

	$('#eineSpalte .newsContent .news-single-img img').each(function(){
		var width	= $(this).width();
		var height	= $(this).height();
		$(this).width(334);
		$(this).height(height/(width/334));
	});
	
/*	Main Navigation
**************************************************/
	$('#mainNavi li a').wrapInner('<span></span>');
	$('#mainNavi li a').each(function(){
		if($(this).find('span').height() < '20'){
			$(this).css('padding-top','15px');
		} 
	});

/*	Sub-Navigation
**************************************************/
	$('#subNavi li a').wrapInner('<span></span>');
	$('#subNavi li:first-child').find('a').css('border-top','none');
	$('#subNavi li:last-child').find('a').css('border-bottom','none');
	
/*	Online-Bewerbung
**************************************************/
/*
	$('#online_bewerbung').each(function(){
		var navi = $('#mainnavi');
		$(this).appendTo(navi);
		var button = navi.find('#online_bewerbung');
		navi.css({
			'position'	: 'relative',
			'box-shadow': '0 0 0 0'
		});
		button.css({
			'position'	: 'absolute',
			'top'		: '55px',
			'left'		: '190px',
			'margin'	: '0 0 0 0'
		});
		button.find('li').css('position','relative').append('<span class="arrow"></span>');
		$('#content').css('paddingTop','60px');
	});
*/

/*	Header Bilder
**************************************************/
	$('#infoBox').titleSlider({
		speed:	2000,
		delay:	2000
	});

	$('#headerLeft').click(function(){
		$('#infoBox').titleSlider('next');
    });

    $('#headerRight').click(function(){
		$('#infoBox').titleSlider('prev');
    });

	$('#headerInfo').hide();
	$('#headerLeft').hide();
	$('#headerRight').hide();
	$('#hoverSpace').hover(function(){
		$('#headerInfo').show();
		$('#headerLeft').show();
		$('#headerRight').show();
	},function(){
		$('#headerInfo').hide();
		$('#headerLeft').hide();
		$('#headerRight').hide();
	});
	$('#headerInfo').hover(function(){
		$('#headerInfo').show();
		$('#headerLeft').show();
		$('#headerRight').show();
	},function(){});
	$('#headerRight').hover(function(){
		$('#headerInfo').show();
		$('#headerLeft').show();
		$('#headerRight').show();
	},function(){});
	$('#headerLeft').hover(function(){
		$('#headerInfo').show();
		$('#headerLeft').show();
		$('#headerRight').show();
	},function(){});

/* Accordion *************************************/
	$('.accordion h3').each(function(){
		$(this).siblings().wrapAll('<div></div>');
	});
	$('.accordion:first').before('<div class="theAccordion"></div>');
	$('.accordion').appendTo('.theAccordion');
	$('.theAccordion h3').unwrap();
	$(".theAccordion").accordion({header: 'h3', autoHeight: false, active: 'h3:first', collapsible: true});

/* Navi ******************************************/
	$('#mainNavi').find('li').find('.dropdown').hide();
	$('#mainNavi').find('li').hover(function(){
		$(this).find('.dropdown').css('opacity','0').show().animate({
			opacity: 1
		},200);
	},function() {
		$(this).find('.dropdown').animate({
			opacity: 0
		},0, function(){
			$(this).hide();
		});
	});
	
/* MailForm ***************************************/
	$('#mailForm').find('input, textarea, select').focus(function() {
		$(this).attr('style', '');
	});
	
	var mailExpr   = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;


	$('#mailForm .btn').click(function(){
		var error = 0;
		if(mailExpr.test($('#email').val()) === false) {
			$('#email').addClass('formFail');
			error = 1;
			return false;
		} else {
			$('#email').removeClass('formFail');
			$('#mailForm').submit();
		}
	});
	
/*	Karten-Selektor Anfahrt
**************************************************/
	$('#content.portrait').each(function(){
		var berlin    = $(this).find('iframe.berlin');
		var hannover  = $(this).find('iframe.hannover');
		var stuttgart = $(this).find('iframe.stuttgart');
		var active    = 0;
		berlin.parent('.csc-default').height(0).css('overflow','hidden');
		stuttgart.parent('.csc-default').height(0).css('overflow','hidden');
		
		$(this).find('#rechts .csc-default h4').each(function(){
			if($(this).text() === "hannover" || $(this).text() === "Hannover"){
				$(this).parent('.csc-default').addClass('mapSelect hannover');
				active = 1;
			}
			if($(this).text() === "berlin" || $(this).text() === "Berlin"){
				$(this).parent('.csc-default').addClass('mapSelect berlin');
				active = 1;
			}
			if($(this).text() === "stuttgart" || $(this).text() === "Stuttgart"){
				$(this).parent('.csc-default').addClass('mapSelect stuttgart');
				active = 1;
			}
		});
		if(active === 1){
			$(this).find('#rechts .csc-default.mapSelect').each(function(){
				var town = $(this).find('h4').text();
				$(this).click(function(){
					$('#content.portrait #links iframe').parent('.csc-default').height(0).css('overflow','hidden');
					var iframeHeight = $('#content.portrait #links iframe.'+town).height();
					$('#content.portrait #links iframe.'+town).parent('.csc-default').height(iframeHeight).css('overflow','hidden');
				});
			});
		}
	});

/*	IE7 Bugfix
**************************************************/
	var pageHeight = 0;
	$('#content').each(function(){
		if($(this).find('#sNavi').outerHeight(true) > pageHeight){
			pageHeight = $(this).find('#sNavi').outerHeight(true);
		}
		if($(this).find('#links').outerHeight(true) > pageHeight){
			pageHeight = $(this).find('#links').outerHeight(true);
		}
		if($(this).find('#rechts').outerHeight(true) > pageHeight){
			pageHeight = $(this).find('#rechts').outerHeight(true);
		}
		$('#sNavi').height(pageHeight);
		$('#rechts').height(pageHeight);
		$('#links').height(pageHeight);
	});

/*	Socials -> target = _blank
**************************************************/
	$('#footer .socials a').attr('target','_blank');
	$('body#en #content.startseite div.eventsStartseite ul.newsList .newsListText').children('span.more').hide();
	$('body.en ul.newsList .newsListText').children('span.more').hide();
});
