jQuery(function(){
	var viewport_height = jQuery( window ).height() - ( jQuery( '#site_head' ).height() + jQuery( '#site_footer' ).height() ),
	content_container = jQuery( '#site_content_right', '#site_content' );
	
	var heads = content_container.find( 'h3.accordion-head' );
	
	heads
		.bind( 'click', function(){
			var index = heads.index( this ),
				tables = heads.next( 'table' );

			var container_offset = content_container.offset();
			
			content_container
				.css( 'top', container_offset.top + 'px' )
				.addClass( 'site_content_right_fixed' )
				.css( 'position', 'fixed' );
			
			content_container
				.find( 'table' )
				.filter( ':not(:eq(' + index + '))' )
				.slideUp()
				.end()
				.eq( index )
				.slideDown(function(){
					jQuery( window ).scrollTop( container_offset.top );
					content_container.attr( 'style', 'visibility: visible' );
					content_large = false;
					content_height = content_container.height();

					if ( viewport_height > content_height ) {
						content_container.addClass( 'site_content_right_fixed' );
					}
					else {
						content_container.removeClass( 'site_content_right_fixed' );
					} 
				});
			
			return false;
		})
		.eq( 0 )
		.next( 'table' )
		.show();

	var content_height = content_container.height(),
		content_large = false;
		
	if ( viewport_height > content_height ) {
		content_container.addClass( 'site_content_right_fixed' );
	}
	
	jQuery( '#site_main_content_toggler', content_container ).toggle(
		function( ignore, params ){
			var height = content_container.find( 'h1:first' ).height() + content_container.find( 'h2:first' ).height() + 40;
			
			if ( !content_container.hasClass( 'site_content_right_fixed' ) ) {
				content_large = true;
				
				content_container
					.addClass( 'site_content_right_fixed' )
					.css( 'height', viewport_height - 40 );
			}
			
			if ( typeof params == 'object' && typeof params.fn == 'function' ) {
				content_container.animate( { height: height }, 'slow', params.fn );
			}
			else {
				content_container.animate( { height: height } );
			}
			
			jQuery( this ).addClass( 'closed' );
		},
		function( ignore, params ){
			if ( params && 'animate' in params && params.animate === false ) {
				if ( content_large ) {
					content_container.removeClass( 'site_content_right_fixed' );
					content_container.height( viewport_height - 40 );
				}
				else {
					content_container.height( content_height );
				}
			}
			else {
				if ( content_large ) {
					content_container.animate( { height: viewport_height - 40 }, 'slow', function(){
						content_container.removeClass( 'site_content_right_fixed' );
					} );
				}
				else {
					content_container.animate( { height: content_height } );
				}
			}

			
			jQuery( this ).removeClass( 'closed' );
		}
	)
	.trigger( 'click', {fn:function(){
		content_container.css( 'visibility', 'visible' );
		
		if ( jQuery( 'body' ).is( '.submenu2' ) ) {
			jQuery( '#site_main_content_toggler', content_container ).trigger( 'click', { animate: false } );
		}
		else {
			/* Nach gewisser Zeit automatisch ausfahren */
			window.setTimeout(function(){
				jQuery( '#site_main_content_toggler', content_container ).trigger( 'click' );
			}, 5000 );
		}
	}});
})

/*$(document).ready(function(){

	$('div.article').find('h2.toggle').parent().eq(0).wrapInner('<div id="accordion" />')
	    $('#accordion').accordion({
	        header: 'h2.toggle',
	        autoHeight: false
	       }); 
}*/
