/*
Funktion zum initialisieren der Slideshow
Parameter: control (bool) - entscheidet, ob die Kontrollleiste angezeigt wird
*/
function init_fadeshow() {
	/* erstes Bild anzeigen */
	$('#fadeshow img:first').addClass('topLayer').show();
	setInterval("fade_img()", 9500);
}

/*
Funktion zum Ein- und Ausblenden der Bilder
Parameter: nextImg (string / optional) - ID des Bildes, das als nächstes angezeigt werden soll
*/
function fade_img() {
	var $img1 = $('#fadeshow img.topLayer');
	var $img2 = ($img1.next('img').length) ? $img1.next('img') : $('#fadeshow img:first');
	
	$img1.addClass('bottomLayer');
	$img1.removeClass('topLayer');
	$img2.hide();
	$img2.addClass('topLayer');
	
	$img2.fadeIn( 1000, function() { $img1.removeClass('bottomLayer') });
}


function calcWebsiteHeight() {
	var windowHeight = $(window).height();
	var contentHeight = $('#head').height() + $('#body').height()+50;
	if( windowHeight > contentHeight ) {
		$('#website').css({'height' : windowHeight});
	} else {
		$('#website').css({'height' : contentHeight});
	}
}

function slide(direction) {
	
	if($('.slider_reel:animated').length) return false;
	
	$('.slider_reel').each(function(i,container) {
	
		var pos = 0;
		var dur = 1000;
		switch(direction) {
			case 'next':
				if( $(container).position().left-740 + $(container).width() != 0 ) { pos = -740; };
				break;
			case 'prev':
				if( $(container).position().left != 0 ) { pos = 740; };
				break;
			default:
				$(this).children('li').each(function(i, slide) {
					if($(slide).attr("rel") == direction ) {
						pos = i * 740 * -1;
						dur = i * 750;
					}
				});
		}
		
		$(container).animate({
			left: '+='+pos+'px'
		}, {
			duration: dur,
			easing: 'swing',
			queue: false
		});
	});
	
}

/* Funktion zum anzeigen einer E-Mail-Adresse */
var showmail = function (e, name) {
	$.get('../js/ajax/backend.php', {
		id: name
	}, function(data) {
		$(e).attr('href', 'mailto:'+data).text(data);
		$(e).removeAttr('onclick');
	});
}


/*
	Document-Ready-Funktion
	wird geladen, wenn Seite aufgebaut ist
*/
$(document).ready(function() {
	
	/* entfernen des Rahmens um aktiven Link */					   
	$('a').click(function() { this.blur(); });
	
	calcWebsiteHeight();
	$(window).resize(function() { calcWebsiteHeight(); });
	
	
	/* Initialisierung Slider */
	$('.slider').each(function(i, slider) {
	
		// Berechnung reelWidth
		$(slider).children('.slider_reel').width( function(i, width) {
			var reelWidth = 0;
			$(this).children('li').each(function(j, element) {
				reelWidth += $(element).width();
			});
			return reelWidth;
		});
			
		// Berechnung reelHeight
	$(slider).children('.slider_reel').height( function(i, height) {
		var reelHeight = 0;
			$(this).children('li').each(function(j, element) {
				if( reelHeight < $(element).height() ) reelHeight = $(element).height();
			});
			return reelHeight;
		});
		
		// Einfügen Controls
		if( $(slider).hasClass('control') ) {
			$(slider).append('<a id="prevSlide" onClick="slide(\'prev\'); return false;" href="#"></a><a id="nextSlide" onClick="slide(\'next\'); return false;" href="#"></a>');
		}
		
	});
	
	
	/* Slide-Effekt Navigation */
	$('#nav a').click(function() {
		if( $(this).next('ul.submenu').length != 0 ) {
			if( $(this).next('ul.submenu:hidden').length != 0 ) {
				$('ul.submenu:visible').slideUp();
				$(this).next('ul.submenu').slideDown();
			} else {
				$(this).next('ul.submenu').slideUp(500);
			}
			return false;
		}
	});
	
	
	/* Slide-Effekt Buttons-Top */
	$('#buttons-top a').mouseenter(function() {
		$(this).children('img').stop().animate({ 'bottom' : '0px' }, 250);
	}).mouseleave( function() {
		$(this).children('img').stop().animate({ 'bottom' : '70px' }, 250);
	});
	
	
	/* wird ausgeführt, wenn das Kontaktforumar abgeschickt wird */
	$('#kontaktformular').submit(function() {
		$.post('../js/ajax/mail.php', $(this).serialize(),
		function(data) {
			if(data != 'sent') {
				$('#'+data).addClass('error');
				alert('Bitte füllen Sie alle Pflichtfelder aus!');
			} else {
				_gaq.push(['_trackPageview', '/kontakt/anfrage-gesendet.html']);
				alert('Ihre Nachricht wurde erfolgreich versendet!');
			}
		});
		return false;
	});
	$('#kontaktformular input').focusin(function() { $(this).removeClass('error'); });
	
	
	/* Routenplaner - wird vor dem Abschicken ausgeführt */
	$('#routenplaner').submit(function() {
		$str = $('#r_strasse').attr('value');
		$ort = $('#r_ort').attr('value');
		if( $str != "" && $ort != "") {
			$saddr = $str+", "+$ort;
			$('#saddr').attr('value', $saddr);
		} else {
			$('#r_strasse, #r_ort').addClass('error');
			alert("Bitte geben Sie Ihre Adresse ein!");
			return false;
		}
	});
	$('#routenplaner input').focusin(function() { $(this).removeClass('error'); });
	
	/* wird ausgeführt, wenn das Formular der Umfrage abgeschickt wird */
	$('#formUmfrage').submit(function() {
		$('.error').removeClass('error');
		$.post('../js/ajax/umfrage.php', $(this).serialize(),
		function(data) {
			if(data != 'sent') {
				var error = data.split(",");
				$.each(error, function(index, value){
					$('#'+value).addClass('error');
				});
				alert('Bitte füllen Sie alle blau hinterlegten Felder aus!');
			} else {
				_gaq.push(['_trackPageview', '/umfrage/umfrage-gesendet.html']);
				alert('Vielen Dank, dass Sie an unserer Umfrage teilgenommen haben.\nWir wünschen Ihnen viel Glück bei unserer Verlosung.');
			}
		});
		return false;
	});
	
});
