$(document).ready(function() {

	$('.testimonial_opener_closer').click(function() {
		$(this).toggleClass('testimonial_opener_closer_open');

		$('#all_pictures').toggle();

		// Verstecke die Textbloecke
		$('#text_blocks').hide();

	});

	$('#testimonials #pictures .picture').click(function() {
	
		// Schliesse alle Bubbles
		$('#testimonials * .bubble').hide();
	
		// Schliesse alle vorher geoeffneten Textbloecke
		$('#testimonials * .testimonial_eintry').hide();

		// Schliesse die erweiterten bilder
		$('#all_pictures').hide();

		$('.testimonial_opener_closer').removeClass('testimonial_opener_closer_open');

		// oeffne den angefragten block
		$('#block'+ $(this).attr('blockid')).show();

		// Zeige den Textblock
		$('#text_blocks').show();
		
		$(this).addClass('picture_seen');

		// tracke click auf element
		dcsMultiTrack(
		  'DCSext.axd_cbox',
		  $(this).parents('.teaser2').find('.teaser2head').text()
		);
	});
	
	/**
	 * Diese Funktion zeigt bei einem Mouseover �ber ein Testimonial-Bild eine Sprechblase mit Namen und Funktion an.
	 */
	$('#testimonials #pictures .picture').mouseover(function() {
		if ( $(this).hasClass('picture_seen') ) {
			$(this).removeClass('picture_seen');
			$(this).addClass('picture_was_seen');
		}
	
		// Verstecke alle Sprechblasen
		$('#testimonials * .bubble').hide();
		
		$('#testimonials #pictures .picture').addClass('picture_inactive');
		$(this).removeClass('picture_inactive');

		// Erzeuge ein Delay und zeige die Sprechblase zu diesem Bild an
		$(this).children('.bubble').show();
		
	});
	
	$('#testimonials #pictures .picture').mouseout(function() {
		if ( $(this).hasClass('picture_was_seen') ) {
			$(this).addClass('picture_seen');
			$(this).removeClass('picture_was_seen');
		}
	
		$('#testimonials #pictures .picture').removeClass('picture_inactive');

		// Zeige die Sprechblase zu diesem Bild an
		$(this).children('.bubble').hide();
	});
	
	
	/**
	 * 
	 */
	$('#testimonials .page_hopper .page_hopper_next').click(function() {
		var openTestimonial = $(this).parents('#text_blocks').children('div.testimonial_eintry:visible');
		
		var nextTestimonial = $(openTestimonial).next('.testimonial_eintry');

		// get the first one if there is no next testimonial
		if ( $(nextTestimonial).attr('id') == undefined )
			nextTestimonial = $(this).parents('#text_blocks').children('div.testimonial_eintry:first');
		
		// open next testimonial ...
		$(nextTestimonial).show();
		
		// ... and close current one
		$(openTestimonial).hide();
		
		$("#testimonials #pictures .picture[blockid='"+ $(nextTestimonial).attr('id').substr(5) +"']").addClass('picture_seen');
	});
	

	/**
	 * 
	 */
	$('#testimonials .page_hopper .page_hopper_back').click(function() {
		var openTestimonial = $(this).parents('#text_blocks').children('div.testimonial_eintry:visible');
		var prevTestimonial = $(openTestimonial).prev('.testimonial_eintry');
		
		// get the last one if there is no previous testimonial
		if ( $(prevTestimonial).attr('id') == undefined )
			prevTestimonial = $(this).parents('#text_blocks').children('div.testimonial_eintry:last');
		
		// close current testimonial
		$(openTestimonial).hide();

		// ... and open previous one ...
		$(prevTestimonial).show();
		
		$("#testimonials #pictures .picture[blockid='"+ $(prevTestimonial).attr('id').substr(5) +"']").addClass('picture_seen');
	});
	

	$('.content_testimonials .testimonials_content .left_column .small_pictures .small_picture').click(function() {
	
		// Verstecke alle Textbl�cke in diesen Testimonials
		$(this).parents('.testimonials_content').children('.right_column').children('.text_blocks').children('.testimonial_eintry').hide();
		
		// Verstecke den gro�e Bild
		$(this).parents('.testimonials_content').children('.left_column').children('.big_pictures').children('.big_picture').hide();
		
		// Zeige alle kleinen Bilder wieder an
		$(this).siblings('.small_picture').show();
		
		// Zeige das gro�e Bild dieses Testimonials an
		$('#big_image_'+ $(this).attr('id') +'.big_picture').show();
		
		// Zeige den Text dieses Testimonials
		$('#block_'+ $(this).attr('id') +'.testimonial_eintry').show();
		
		$(this).addClass('picture_seen');
		
		// Verstecke dieses kleine Bild
		$(this).hide();
		
	});
	
	
	/**
	 * Diese Funktion zeigt bei einem Mouseover �ber ein Testimonial-Bild eine Sprechblase mit Namen und Funktion an.
	 */
	$('.content_testimonials .testimonials_content .left_column .small_pictures .small_picture').mouseover(function() {
		if ( $(this).hasClass('picture_seen') ) {
			$(this).removeClass('picture_seen');
			$(this).addClass('picture_was_seen');
		}
	
		$('.content_testimonials .testimonials_content .left_column .small_pictures .small_picture').addClass('picture_inactive');
		$(this).removeClass('picture_inactive');

	});
	
	$('.content_testimonials .testimonials_content .left_column .small_pictures .small_picture').mouseout(function() {
		if ( $(this).hasClass('picture_was_seen') ) {
			$(this).addClass('picture_seen');
			$(this).removeClass('picture_was_seen');
		}
	
		$('.content_testimonials .testimonials_content .left_column .small_pictures .small_picture').removeClass('picture_inactive');
	});

	
	
	/**
	 * Funktion fuer die neuen Testimonials 2011
	 */

	/*
	 * Gehe durch die Sidebarteaser und find wo die Klasse 'sidebar-person' vorhanden ist,
	 * zeige in jedem Sidebarteaser jeweils das erste Item an.
	 */
	$('.sidebar-neu div').each(function(){
		if($(this).hasClass('sidebar-person')){
			$(this).parent().find('.sidebar-person:first').removeClass('hide');
			$(this).parent().find('.sidebar-person:first').addClass('show');
			// Blende den Pager aus, wenn es nur ein Element im Sidebarteaser gibt.
			if($(this).siblings().size() <=2){
				$(this).parent().find('.pager').addClass('hide');
			}
		}
	});
		
	/*
	 * Bei Klick auf den vorherigen Button finde, ein vorheriges Element und zeige es an, fals es keins gibt
	 * zeige das letzte Element an.
	 */
	$('.pager .prev').click(function(){
		var curr = $(this).parent().parent().find('.sidebar-person.show');
		var lastSidebarPerson = $(this).parent().parent().find('.sidebar-person:last');
		if(curr.prev().hasClass('sidebar-person')){
			curr.removeClass('show');
			curr.addClass('hide');
			curr.prev().removeClass('hide');
			curr.prev().addClass('show');
		}else{
			curr.removeClass('show');
			curr.addClass('hide');
			lastSidebarPerson.removeClass('hide');
			lastSidebarPerson.addClass('show');
		}
	});
	/*
	 * Bei Klick auf den nächsten Button finde, ein nächstes Element und zeige es an, fals es keins gibt
	 * zeige das erste Element an.
	 */
	$('.pager .next').click(function(){
		var curr = $(this).parent().parent().find('.sidebar-person.show');
		var firstSidebarPerson = $(this).parent().parent().find('.sidebar-person:first');
		if(curr.next().hasClass('sidebar-person')){
			curr.removeClass('show');
			curr.addClass('hide');
			curr.next().removeClass('hide');
			curr.next().addClass('show');
		}else{
			curr.removeClass('show');
			curr.addClass('hide');
			firstSidebarPerson.removeClass('hide');
			firstSidebarPerson.addClass('show');
		}
	});
});

