$(document).ready( function($){
	
	// SETUP
	$('#content').children().hide();
	$('#home').show();
	$('#content').css({opacity: 0, left: '500px'}).animate({opacity: 1, left: '-=270px'}, {easing:'easeOutQuint',duration:700});
	
	$('#menu img[alt=home]').attr("active", 1).attr('src', 'gfx/home_hover.png');
	
	// MENU 
	$('#menu').find('img').live('mouseover mouseout click', function(event) {
		if (event.type == 'mouseover' && !$(this).attr('active')) {
			$(this).attr('src', 'gfx/' + $(this).attr('alt') + '_hover.png');
		}
		if (event.type == 'mouseout' && !$(this).attr('active')) {
			$(this).attr('src', 'gfx/' + $(this).attr('alt') + '.png');
		}
		if (event.type == 'click' && !$(this).attr('active')) {
			
			// content anpassen
			$('#content').children().hide(); // content-kinder ausblenden
			$('#' + $(this).attr('alt')).show(); // seite einblenden
			$('#content').stop(true, true).css({opacity: 0, left: '500px'}).animate({opacity: 1, left: '-=270px'}, {easing:'easeOutQuint',duration:700});
	
			// menu anpassen
			$('#menu').find('img').removeAttr('active');
			$('#menu').find('img').each(function() { $(this).attr('src', 'gfx/' + $(this).attr('alt') + '.png'); });
			$(this).attr("active", 1); 
			$(this).attr('src', 'gfx/' + $(this).attr('alt') + '_hover.png');
		}
	}); 
	
	$('a:not([href])').click(function() {
		// content anpassen
		$('#content').children().hide(); // content-kinder ausblenden
		$('#' + $(this).attr('name')).show();  // seite einblenden
		$('#content').stop(true, true).css({opacity: 0, left: '500px'}).animate({opacity: 1, left: '-=270px'}, {easing:'easeOutQuint',duration:700});
	
		// menu anpassen
		$('#menu').find('img').removeAttr('active');
		$('#menu').find('img').each(function() { $(this).attr('src', 'gfx/' + $(this).attr('alt') + '.png'); });
		
	});
	
	$('#submitbutton').click(function() {
		if (checkData()) {
			sendRequest();
		}
		
	});
	
	function getRandom(min, max) {		
		var randomNum = Math.random() * (max-min);
		return(Math.round(randomNum) + min); 
	}
	
	function doanimation(id, max_right, max_top) {
		var duration_min = 3000, duration_max = 5000;
	
		$(id).animate({opacity: 0.8, right: getRandom(0, max_right), top: getRandom(0, max_top)}, 0);
		$(id).fadeIn(getRandom(duration_min, duration_max), function() { $(id).hide(); doanimation(id, max_right, max_top); });
		
		//$(id).animate({opacity: 0.8, right: max_right + getRandom(0,1), top: max_top + getRandom(0,0)}, getRandom(duration_min, duration_max), function() {});
	}
	
	function checkData() {
		result = true;
		$('input, textarea').each(function() {
			if ($(this).val() == "") {
				$(this).css('border', '1px solid #029bf2');
				result = false;
			}
			else {
				$(this).css('border', '1px solid #abadb3');
			}
		});
		result ? $('#fehler').html('') : $('#fehler').fadeIn().html("Bitte f&uuml;llen Sie alle Felder aus");
		return result; 
	}
	
	
	
	// HOME-SLIDESHOW
	
	function slideSwitch(id, fadetime) {
	    var $active = $('#' + id + ' IMG.active');
	    if ( $active.length == 0 ) $active = $('#' + id + ' IMG:last');
	    var $next =  $active.next().length ? $active.next()
	        : $('#' + id + ' IMG:first');
	    $active.addClass('last-active');
		$next.css({opacity: 0.0}).addClass('active')
	        .animate({opacity: 1}, fadetime, function() {
	            $active.removeClass('active last-active');
	        });
	}
	
	function slideshow(id, fadetime, interval) {
		setInterval(function() {slideSwitch(id, fadetime);},interval);
	}
	
	slideshow('slideshow', 1200, 6000);
});
