

$(document).ready(function() {

	jQuery.jQueryRandom = 0;
	jQuery.extend(jQuery.expr[":"],
	{
	    random: function(a, i, m, r) {
	        if (i == 0) {
	            jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
	        };
	        return i == jQuery.jQueryRandom;
	    }
	});


	// activate newsbutton in header
	$('#news-header').click(function() {
		$('#news-content').slideToggle('fast');
		return false;
	});
	
	$('.timeFrameContainer input').click(function() {
		var label = $(this).val();
		var val   = $(this).attr('id');
		var infos = val.split('-');
		var type = '';
		
			// it's a seminar
		if (infos[1].indexOf('s') === 0) {
			type = 'Seminar';
			$(this).parents('.sectionForm').find('.colDescription').hide();
			$(this).parents('.colCheckbox').nextAll('.colDescription').show();
		} else {
			type = 'Forum';
		}
		
		$('#' + infos[0] + '-kind').html(type);
		$('#' + infos[0] + '-title').html(label);
	});
	

	// find all form with class jqtransform and apply the plugin
	$(function() {
		$('form.jqtransform').jqTransform();
	});
	
	/*** anmeldungsslider ***/
	var slide = function(direction, slider) {
		var elementLength = 542;
		var numItems = slider.children().length;
		var currentPos = parseInt(slider.css('marginLeft'));
				
		// make sure that we're moving to the right position
		currentItem = Math.floor(currentPos/elementLength);
		currentItem = Math.abs(currentItem);

		if (direction == 'right') {
			// non-item-based, click often to see the bug:
			// currentPos -= elementLength;
			currentPos = -(elementLength * (currentItem+1));
			if (Math.abs(currentPos) >= elementLength * numItems) {
				currentPos = 0;
			}
		} else {
			if (currentPos >= 0) {
				currentPos = -(elementLength * (numItems-1));
			} else {
				// non-item-based, click often to see the bug: 
				//currentPos += elementLength;
				currentPos = -(elementLength * (currentItem-1));
			}
		}

		slider.animate({'marginLeft': currentPos}, 'fast', 'swing');
	}

	$('#tf1NavigateLeft, #tf2NavigateLeft').click(function() {
		slide('left', $(this).next('.timeFrameContainer').children());
		return false;
	});
	$('#tf1NavigateRight, #tf2NavigateRight').click(function() {
		slide('right', $(this).prev('.timeFrameContainer').children());
		return false;
	});
	
	
	/*** slideshow ***/
	var nextHeaderImage = function() {
		var img = $('#image img:visible');
		var nextImg = img.next('img');
		if (!nextImg.length) {
			nextImg = img.siblings('img:eq(0)');
		}
		img.fadeOut('slow');
		nextImg.fadeIn('slow')
		window.setTimeout(nextHeaderImage, 8000);
	}
	
	if ($('#image img').length > 1) {
		setTimeout(nextHeaderImage, 8000);
	}

	/*** slideshow ***/
	var nextTestimonial = function() {
		var itm = $('#sidebar-inner .testimonial:visible');
		var nextItm = itm.next('.testimonial');
		if (!nextItm.length) {
			nextItm = itm.siblings('.testimonial:eq(0)');
		}
		itm.slideUp('slow', function() {
			nextItm.slideDown('slow');
			window.setTimeout(nextTestimonial, 1800);
		});
	}
	
	if ($('#sidebar-inner .testimonial').length > 1) {
		$('#sidebar-inner .testimonial:first').slideDown('slow');
		setTimeout(nextTestimonial, 1800);
	}

    /*** slideshow in main column ***/
    var nextTestimonial2 = function() {
        var itm = $('#content-inner .testimonial:visible');
        var nextItm = itm.next('.testimonial');
        if (!nextItm.length) {
            nextItm = itm.siblings('.testimonial:eq(0)');
        }
        itm.slideUp('slow', function() {
            nextItm.slideDown('slow');
            window.setTimeout(nextTestimonial2, 18000);
        });
    }
    
    if ($('.testimonial', '#content-inner').length > 1) {
        $('.testimonial:first', '#content-inner').slideDown('slow');
		setTimeout(nextTestimonial2, 18000);
    }

	
	/** more content extension **/
	$('.t3-more-content').hide();
	$('.t3-more-link').click(function(evt) {
		evt.preventDefault();
		var contentEl = $(this).parent().parent().next('.t3-more-content');
		if (contentEl.css('display') == 'none') {
			contentEl.slideDown();
			$(this).html('weniger...');
		} else {
			contentEl.slideUp();
			$(this).html('mehr...');
		}
	});
	
});
