$(document).ready(function(){


/*
	Probleme / Fehler ??
	Vielleicht findest Du hier ne Antwort:
	=================================================
	- mit Bildervergrößern in der englischen Version?
		> suche nach "picEnlargeFixEn"		
*/




/* Layout fixes etc. *****************************/

if($('#contentAssets').html() == '') $('#contentAssets').hide();
if($('#subNavi').html() != '' && $('#subNavi').html()) $('#contentMain').css('padding-top', '15px');

noWrap($('.csc-drittel'));

function noWrap(element){
	i = 1
	element.each(function(){
		if(i == 3){
			$(this).addClass('noWrap');
		}
		i++;
	});
}
	
/* 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});
	
	
/* Header *****************************************/

$('#toiSlider').toiSlider();


/* MailForm ***************************************/
$('#mailForm').find('input, textarea, select').focus(function() {
	$(this).attr('style', '');
});


/* Fotogallerie ***********************************
 
	Die "kleinen" Bilder liegen in pub/images/ 
	und werden über TYPO3 eingebunden, wobei die
	Klick-Vergrößerung angeklickt wird.
	
		kleine Bilder Standard: 190 * 116 px
		lange Bilder: 			628 *  74 px
	
		
	Die "großen" Bilder liegen in uploads/pics/ ,
	haben den gleichen Namen, aber mit "_gross.jpg"
	am Ende.
	
		große Bilder Standard: 	700 *  X px
*/


/* Aufräumen */

$('div#contentMain div.gallery').find('.csc-textpic-imagewrap').each(function(){
	$(this).before($(this).find('span.imageWithCaption'));
	$(this).remove();
});

thirdChild($('div#contentMain div.gallery div.csc-textpic-single-image span.imageWithCaption'));
thirdChild($('div#contentMain div.gallery div.textboxAboveCenter span.imageWithCaption'));

function thirdChild(element){
	var i = 1;
	element.each(function(){
		if (i >= 4) {
			$(this).addClass('notFirstRow');
		}
		i++;
	});
}

/* Bild ohne Rahmen beim Hover */

picHover($('div#contentMain div.picLong span.imageWithCaption'));

function picHover(element){
	element.hover(function(){
		$(this).prepend('<div class="hover"></div>');
		$(this).find('.hover').css('z-index','100');
		$(this).find('a img').css({'z-index':'10'});
	}, function(){
		$(this).find('.hover').remove();
	});
}

/* Bild mit Rahmen beim Hover */

picHoverBorder($('div#contentMain div.gallery div.csc-textpic-single-image span.imageWithCaption'));
picHoverBorder($('div#contentMain div.gallery div.textboxAboveCenter span.imageWithCaption'));

function picHoverBorder(elements){
	elements.each(function(){
		$(this).prepend('<div class="hover"><a></a></div>');
		$(this).find('.hover').hide();
		$(this).find('.hover').css('z-index','100');
		$(this).find('a img').css({'z-index':'10'});
		$(this).css('border','2px solid #999');
		$(this).find('a').attr('href',$(this).find('a.fancybox').attr('href')).addClass('fancybox');
		$(this).hover(function(){
			$(this).find('.hover').show();
		}, function(){
			$(this).find('.hover').hide();
		});
	});
}


/* Fancybox **************************************/

$('.fancybox').each(function(){
	href = $(this).attr('href').substring(0,$(this).attr('href').length-4);
	$(this).attr('href',href + '_gross.jpg');
});

$('.fancybox').fancybox({
	'overlayColor' 	 : '#000',
	'overlayOpacity' : 0.8,
	'showNavArrows'  : true,
	'showCloseButton': true
});


/* 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').before('<img class="imgPDF" src="pub/css/img/imgPDF.png" />');
		spanA.find('span.linkText a').text(spanB.text());
		spanB.remove();
	});
}


/* Iframes ***************************************/

$('.csc-default iframe').parents('.csc-default').removeClass('csc-default');


/* Sprites (Hover) *******************************/

picSpriteHover($('#contentAssets .csc-drittel .imageWithCaption img'));

function picSpriteHover(element){
	element.each(function(){
		picLink = $(this).attr('src').toString();
		if (picLink.search('sprite') > 0) {
			height = $(this).attr('height');
			height = height*0.5;
			$(this).hover(function(){
				$(this).css('top','-'+height+'px');
			},function(){
				$(this).css('top','0');
			});
		}
	});
}



/*	Language - Header anpassen
**************************************************/

	/* Deutsch */

	if($('#languageSelectorDe').find('a').length == 0){
		$('#header').addClass('de');
		$('div#contentMain div.gallery div.textboxAboveCenter span.imageWithCaption div.hover').addClass('de');
		$('div#contentMain div.picLong div.textboxBelowCenter span.imageWithCaption div.hover').addClass('de');
	}
	
	/* Englisch */

	if($('#languageSelectorEn').find('a').length == 0){
		$('#header').addClass('en');
		$('div#contentMain div.gallery div.textboxAboveCenter span.imageWithCaption div.hover').addClass('en');
		$('div#contentMain div.picLong div.textboxBelowCenter span.imageWithCaption div.hover').addClass('en');
		
		/*	picEnlargeFixEn:
			Durch das Kopieren der Inhalte, werden auch die Bilder
			auf dem Server kopiert (img-file => xxx_01.jpg).
			"_01" wird darum in der englischen Version gelöscht */
		
		$('.fancybox').each(function(){
			href = $(this).attr('href').substring(0,$(this).attr('href').length-13);
			$(this).attr('href',href + '_gross.jpg');
		});
	}


});
