google.setOnLoadCallback(function() {

	$(document).ready(function(){

			/* PROGRAMM */
				$("#programmListe .programmTrennlinie:last").css("display", "none");
				
				

			/* IMPRESSIONEN */
				$("#impressionen").nivoSlider({
					effect: "fade",
					slices: 1,
					animSpeed: 600,
					pauseTime: 4500,
					directionNav: true,
					directionNavHide: false, 
					controlNav: false
				});

			
			
			/* COLORBOX */
				$("#bilderGalerie a, #bandDetail a.detailAnsicht, a.colorbox").colorbox({
					transition: "elastic",
					speed: 650,
					opacity: 0.96 
				});
				
				
		
			/* HOERPROBE */		
				$("a.linkHoerprobe").colorbox({
					transition: "elastic",
					speed: 650,
					opacity: 0.75,
					width: "400px",
					title: "H�rprobe",
					height: "70px",
					iframe: true,
					scrolling: false
				});			
				
				
				
			/* NAVIGATION */
				$("#navigation li").click(function() {
					location.href = $(this).children("a").attr("href");				
				});



			/* FORMULAR TICKETBESTELLUNG */
				var warenkorbText;
				
				$("table.ticketItem table select").change(function() {

					/* WARENKORB AKTUALISIEREN */

						var bandID 		= $(this).next("input[name=einzelPreis]").next("input[name=bandName]").next("input[name=bandID]").val()
						var itemName 	= $(this).attr("name");
						
						$("input." + itemName + "[name=anzTickets]").val($(this).val());
						
						warenkorbText = "";
						
						$("input[name=anzTickets]").each(function(){
							if (parseInt($(this).val()) > 0) {
								warenkorbText += $(this).val() + " x ";
								warenkorbText += $(this).parent().children("input[name=bandName]").val() + " "; /* Bandname */
								warenkorbText += "(Kategorie: " + $(this).parent().children("input[name=preisKat]").val() + ", "; /* Preiskategorie */
								warenkorbText += "Einzelpreis: CHF " + $(this).parent().children("input[name=einzelPreis]").val() + ".-)"; /* Einzelpreis */
								warenkorbText += "<br />";
							}
						});
						
						if (warenkorbText.length == 0) {
							$("#warenKorb").html("<b>Keine Tickets ausgewählt.</b>");
						}
						else {
							$("#warenKorb").html("<b>Ihre Bestellung:</b><br /><br />" + warenkorbText);
						}
						
				});

				$("#bestellungAbsenden").click(function() {

					var formValid = true;

					/* FORMULAR VALIDIEREN */

						function isValidEmailAddress(emailAddress) {
							var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
							return pattern.test(emailAddress);
						}
					
						if ($("table#bestellFormular input[name=bf_vorname]").val().length < 3) {
							$("table#bestellFormular input[name=bf_vorname]").css("border-color", "red");
							$("table#bestellFormular label[for=bf_vorname]").css("color", "red");
							formValid = false;
						}
						
						if ($("table#bestellFormular input[name=bf_nachname]").val().length < 3) {
							$("table#bestellFormular input[name=bf_nachname]").css("border-color", "red");
							$("table#bestellFormular label[for=bf_nachname]").css("color", "red");
							formValid = false;
						}
						
						if ($("table#bestellFormular input[name=bf_adresse]").val().length < 3) {
							$("table#bestellFormular input[name=bf_adresse]").css("border-color", "red");
							$("table#bestellFormular label[for=bf_adresse]").css("color", "red");
							formValid = false;
						}
						
						if ($("table#bestellFormular input[name=bf_plzort]").val().length < 4) {
							$("table#bestellFormular input[name=bf_plzort]").css("border-color", "red");
							$("table#bestellFormular label[for=bf_plzort]").css("color", "red");
							formValid = false;
						}
	
						if ($("table#bestellFormular input[name=bf_telefon]").val().length < 4) {
							$("table#bestellFormular input[name=bf_telefon]").css("border-color", "red");
							$("table#bestellFormular label[for=bf_telefon]").css("color", "red");
							formValid = false;
						}
	
						if (isValidEmailAddress($("table#bestellFormular input[name=bf_email]").val()) != true) {
							$("table#bestellFormular input[name=bf_email]").css("border-color", "red");
							$("table#bestellFormular label[for=bf_email]").css("color", "red");
							formValid = false;
						}
					
					if (formValid == true) {

						$.ajax({
						   type: 	"GET",
						   url: 	"cms/custom_lib/bestellung_senden.php",
						   data: 	{bf_vorname: $("table#bestellFormular input[name=bf_vorname]").val(),
									 bf_nachname: $("table#bestellFormular input[name=bf_nachname]").val(),
									 bf_bemerkungen: $("table#bestellFormular textarea[name=bf_bemerkungen]").val(),
									 bf_adresse: $("table#bestellFormular input[name=bf_adresse]").val(),
									 bf_plzort: $("table#bestellFormular input[name=bf_plzort]").val(),
									 bf_telefon: $("table#bestellFormular input[name=bf_telefon]").val(),
									 bf_telefax: $("table#bestellFormular input[name=bf_telefax]").val(),
									 bf_mobile: $("table#bestellFormular input[name=bf_mobile]").val(),
									 bf_bestellung: warenkorbText,
									 bf_email: $("table#bestellFormular input[name=bf_email]").val()},
									 
						   success: function(msg) {
								
								var infoText = "Die Tickets wurden erfolgreich bestellt. Besten Dank!<br /><br /><a href='stimmenfestivalettiswil.php'>Zur Startseite</a>";

								$("#bestellung_info_text").html(infoText);
								
								$.fn.colorbox({
									transition: "elastic",
									speed: 600,
									title: "Ticketbestellung", 
									inline: true, 
									href: "#bestellung_info_text", 
									opacity: 0.83 
								});		

							}
							
						 });

					}
					
				});

	});
	
});
