
$(document).ready(function(){
	// Funktionen fuer die Veranstaltungen
	if ($("table.veranstaltungen").length > 0) {
		init_veranstaltungen();
	}
	
	// Bildwechsel Startseite
	if ($(".bildwechsel").length > 0) {
		setInterval( "daumenKino()", 4500 );
	}
	
	$("input.pflichtfeld,textarea.pflichtfeld").blur(function(){
		//if(this.name.search('_r')!=-1) {
			if($(this).val()!='') {
				 $('#'+this.name).removeClass('textfeld-error');
		 		 $('#'+this.name).addClass('textfeld');
			}
			else {
				$('#'+this.name).removeClass('textfeld');
				$('#'+this.name).addClass('textfeld-error');
			}
		//}
		 
	});
	
	// Behelfsloesung Buttons
	$("ul.materialbuttons li input[type=button]").click (
		function () {
			window.open($(this).parent("a").attr("href"));
			return false;
		}
	);
	
});


// ================
// Veranstaltungen
// ================

function init_veranstaltungen () {
	// Versteckt Details
	$("table.veranstaltungen p").hide();
	
	// Bei Klick auf eine Veranstaltung werden Details eingeblendet
	$("table.veranstaltungen tr").click (
		function () {
			$(this).find("td p").slideToggle(100);
		}
	);
	
	// Ein Pfeil zeigt an, welche Zeile gerade angewaehlt ist
	$("table.veranstaltungen tr td").hover (
		function () {
			$("img#pfeil-termin").css("top", $(this).offset().top + 8);
			if (!$(this).hasClass('keinedetails')) { $(this).css("cursor", "pointer"); }
		}
	);
	
	// Bewegt den Pfeil zur ersten Veranstaltung der aktuellen Liste, sofern existent
	if ($("table.veranstaltungen tr td:first").length == 0) {
		$("img#pfeil-termin").hide();
	}
	else {
		$("table.veranstaltungen tr td:first").css("top",
			function() {
				$("img#pfeil-termin").css("top", $(this).offset().top + 8);
			}
		);
	}
	
	// Absende-Button ohne JS
	$("input#submitbutton").hide();
	
	// Filtert Veranstaltungsliste
	$("select").change(function () {
		/*
		cookiename = ($(this).attr('name'));
		cookieval = $(this).val();
		$.cookie(cookiename,cookieval);
	    */
		this.form.submit();
    });
	
	// Checkbox "vergangene Veranstatlungen"
	$("table.veranstaltungen input[type=checkbox]").click(function() {
		/*
		$(":checked").each(function () {
               str = $(this).val();
        });
	
		if(str!=0) {
			$.cookie("showall", true);
		}
		else {
			$.cookie("showall", false);
		}
		*/
		this.form.submit();

	});
	
	// Behelfsloesung Buttons
	$("table.veranstaltungen input[type=button], ul.materialbuttons li input[type=button]").click (
		function () {
			window.location.href = $(this).parent("a").attr("href");
		}
	);
	
}


function EasyTicket(vnr,etid) {
	mylink2 = "https://www.easyticket.de/shop/index.php?affi=7&clear=true&noback=true&veranstnr=" + etid;
	sX2 =  screen.width/2-400;
	sY2 = screen.height/2-365;
	MeinFenster3 =
	window.open(mylink2,"EasyTicket","width=830,height=700,screenX=sX,screenY=sY,toolbar=0,menubar=0,location=0,resizable=0,scrollbars=yes"); 
	MeinFenster3.moveTo(sX2,sY2);
	MeinFenster3.focus();
}


// =============
// Bildergalerie
// =============

function setImage (obj) {
	if( $(obj).parent("li").attr("class") != "aktiv") {
		// Marktiert das aktive Thumbnail
		$(".galerie-thumbs li.aktiv").toggleClass("aktiv"); 
		
		// Generiert das neue Bild aus dem Link
		var newimg = document.createElement("img");
		newimg.src = $(obj).attr("href");
		
		// Laedt das neue Bild, ...
		$(newimg).load(function() {
			// ... ergaenzt es im Quelltext ...
			$("#bild-1").prepend(newimg);
			
			// ... und blendet das alte Bild aus
			$("#bild-1 img:last").fadeOut("500", function() {
				$(this).remove();
			});
			
			$(obj).parent("li").toggleClass("aktiv");
			
			/*$("#bild-1").fadeTo(300, 0.1, function() {
				$("#bild-1").html(newimg);
				$("#bild-1").fadeTo(300, 1.0);
				$(obj).parent("li").toggleClass("aktiv");
			});*/
		});
	}
}

function blaettern (direction) {	
	var bild_alt = $(".galerie-thumbs li.aktiv");
	
	// Blaettern: Zurueck
	if (direction < 0 ) {
		if ( $(bild_alt).prev().length ) { 
			var bild_neu = bild_alt.prev();
		}
		// Ist das erste Bild erreicht, wird das letzte angezeigt
		else {
			var bild_neu = $(".galerie-thumbs li:last");
		}
	}
	
	// Blaettern: Weiter
	else if (direction > 0 ) {
		if ( $(bild_alt).next().length ) { 
			var bild_neu = bild_alt.next();
		}
		// Ist das letzte Bild erreicht, wird das erste angezeigt
		else {
			var bild_neu = $(".galerie-thumbs li:first");
		}
	}
	
	setImage( $(bild_neu).children("a") );
}

// Auomatischer Bildwechsel
function daumenKino() {
	var $alt = $('.bildwechsel ul li.obersteebene');
	
	if($alt.next().length)
		var $neu = $alt.next();
	else
		var $neu = $('.bildwechsel ul li:first');
	
	$alt.addClass('mittlereebene');
	$alt.removeClass('obersteebene');
	$neu.css({opacity: 0.0});
	$neu.addClass('obersteebene');
	
	$neu.animate({opacity: 1.0}, 1500, function() {
		$alt.removeClass('mittlereebene');
	});
}


// Formularvalidierung
function checkform(obj) {
	var str='';
	valid = true;
	$.each(obj, function(key, value) { 
		if(value.name!='' && value.name && value.name.search('_r')!=-1 ) {
			if(value.value.length==0) {				
				$("input#"+value.name).attr('class', 'textfeld-error');
				$("textarea#"+value.name).attr('class', 'textfeld-error');
				valid=false;
			}
			else {
				$("input#"+value.name).attr('class', 'textfeld');
			}
		}		
	});
	return valid;
}
