$(document).ready(function(){

/*	if iPhone | iPad
	checkiPhone(), checkiPad() unten
**************************************************/

	jQuery(function() {
		iPhone=checkiPhone();
		iPad=checkiPad();
		
		iPadOS4=checkiPadOS4();
		iPhoneOS4=checkiPhoneOS4();
		iPhone3G=checkiPhone3G();
	});

	$('#content .bilderStartseite:first img').css('margin-top','0');
	$('#content .bilderStartseite:first img').wrap('<a href="http://typo3.p149188.mittwaldserver.info/referenzen/videos/"></a>');
	
	$('iframe:nth-child(3n)').css('margin-right','0');
	$('#sidebarRight #news .simpleNewsList li:last-child').addClass('lastchild');

	
	$('#mNavi ul li').hover(function(){
		$(this).height(320);
	},function(){
		$(this).height(120);
	});
	
/*	Listen halbieren
**************************************************/

	$('.halfList ul').each(function() {
	    var t = $(this),               // Store a copy of both $(this) and t.children(), 
	    children = t.children(),   // to avoid calling the jQuery function too much
		// Create a clone of the original list, then empty it and insert it after the original
		secondList = t.clone(false).empty().insertAfter(this), 
	    // Calculate the number of elements to move to the second list
	    // ie. half the number of elements in the list, divided by two and rounded up
	    length = Math.ceil(children.length / 5);
		
	   	// Move half the children of the original list to the new one
	    children.slice(children.length / 2).appendTo(secondList);
		
		// If the list is an ordered list, let the list start at the correct position 
	    if(this.nodeName === 'OL'){
	    	secondList.attr('start', length + 1);
		}
		$(this).parents('.halfList').find('ul:first').addClass('first');
		$(this).parents('.halfList').find('ul:last').addClass('second');
	});
	
	$('.halfList').find('ul').wrapAll('<div class="halfListWrap" />');

	
/*	Galerien
**************************************************/

	$('ul.gWrapper li.pic').each(function(){
		$(this).find('img.klein').wrap('<a href="'+$(this).find('img.gross').attr('src')+'" class="fancybox" rel="galleryRechts"></a>');
		$(this).find('img.gross').remove();
	});

	$('ul.galerie li.pic').each(function(){
		$(this).find('img.klein').wrap('<a href="'+$(this).find('img.gross').attr('src')+'" class="fancybox" rel="gallery"></a>');
		$(this).find('img.gross').remove();
	});
	
	$('.bilderStartseite span.imageWithCaption:nth-child(3)').css('margin-right','0');
	$('iframe').wrap('<div class="iframe"></div>');
	$('.iframe:nth-child(3)').css('margin-right','0').after('<div class="clearfix"></div>');

/*	Showreel
**************************************************/

	if($('#header img').length == 0){
		$('#header').titleSliderVideo();
	}

/*	Video Galerie
**************************************************/

	$('#content.videos').videoGallery();
//	$('#content.videos ul#videoGallery li.vid:first').addClass('big');

	$('#right').children('div:not(#sidebarRight)').wrapAll('<div id="misc"></div>');
	

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

	$('.fancybox').fancybox({
		'overlayColor'	: '#000000',
		'overlayOpacity': '0.4'
	});	
	
/*	Höhe anpassen: content / right
**************************************************/

/*
	var maxHeight = Math.max($('#content').outerHeight(true),$('#right').outerHeight(true));
	$('#content').height(maxHeight);
	$('#right').height(maxHeight);
*/
	
/*	Submenu
**************************************************/

	$('.subNavi').each(function(){
		$(this).appendTo($(this).prev('li'));
		$(this).hide();
	});
	
	$('#mNavi ul li').hover(function(){
		$(this).find('.subNavi').show();
	},function(){
		$(this).find('.subNavi').hide();
	});

/* 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()
		});
	});
	

	
/*	Rechte Spalte anpassen - iframe
**************************************************/

	$('#wrap.startseite #sidebarRight .iframe iframe').height(104);
	$('#sidebarRight .iframe').each(function(){
		if( $(this).parent().next().length == 0 ) {
			$(this).css('margin-bottom','0');
		}
	});
	

/* MailForm ***************************************/
	$('#mailForm').find('input, textarea, select').focus(function() {
		$(this).attr('style', '');
	});
	
	$('#resetButton').click(function() {
		$('#mailForm').find('input').attr('value', '');
		$('#antwort').val('');
		$(this).attr('value', 'Felder löschen');
		$('#submitButton').attr('value', 'Absenden');
	});
	
});


/*	if iPhone | iPad
**************************************************/

	function checkiPhone() {
	    var iPhone = ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)));
	    return iPhone;
	}
	
	function checkiPad() {
	    var iPad = navigator.userAgent.match(/iPad/i);
	    return iPad;
	}
	
	function checkiPadOS4() {
	    var iPadOS4 = navigator.userAgent.match(/CPU OS 4/i);
	    return iPadOS4;
	}
	
	function checkiPhoneOS4() {
	    var iPhone4 = navigator.userAgent.match(/CPU iPhone OS 4/i);
	    return iPhone4;
	}
	
	function checkiPhone3G() {
	    var iPhone3G = navigator.userAgent.match(/8C148/i);
	    return iPhone3G;
	}
