$(document).ready(function() {

		var options, link;
		
		var qs = new Querystring();
	
		var dd = qs.get("dd");
		var mm = qs.get("mm");
		var yy = qs.get("yy");

		if ( dd != null && mm != null && yy != null ) {
			$.getJSON("varausJSON.php", window.location.href, function(data) {
					options = data;
					
					$("#henkilotiedot").hide();
					$("#henkilotiedot").hide();
					$("#kiitos").hide();
					
					$("#henk_tiedot").show(); 	//n�ytet��n nappi
					updateForm( null  );
			});

		} else {
			$("#tiedot_incomplete").show();
			$("#tiedot").hide();
		}
		
		$('#lomake').submit( function(){
				var params = $("#lomake input, #lomake textarea").serialize() + /* $("input.radio:checked").serialize() + $("input.checkbox:checked").serialize() + */ "&action=submit" ;
	
				$.ajax ({
					url:	this.action,
					type:	"POST",
					data:	params,
					dataType: "json",
					success: function(data) {
						if ( data.errors != null ) {
							$("em").text( "*" );
							$.each( data.errors, function(key, errormsg ) {
								$("#" + key).next("em").text( errormsg );
							});
						} else {
							/*
							$("#henkilotiedot").hide();
							$("#kiitos").show(); 
							
							*/
							
							window.location = "index.php?t=kiitos&p=kiitos";
							 
							
						}
					},
					error:	function(){
						alert("Tapahtui virhe! Yritä myöhemmin uudelleen!");
					}
				})
				
				return false;
		});

		/* kalenterilinkin klikkaus */
		$('#calendar td.sunday a,td.thursday a').click( function(){
				$("#henkilotiedot").hide();
				$("#kiitos").hide();
				$("#henk_tiedot").show(); 	//n�ytet��n tee varaus -nappi
				link = $(this).attr("href");
				updateAvailability( link );
				return false;
		});

		$("#henk_tiedot").click( function() {
			$("#henkilotiedot").show();
			$("#henk_tiedot").hide(); //piilotetaan tee varaus -nappi
			$("#v_button").scrollTo(1000);
		});

		/* kesto radiot */
		$("input[@name='varaus[kesto]']").click(function(){
			updateForm( $("input[@name='varaus[kesto]']:checked").val() );
		});

		/* varaustyppi radiot */
		$("input[@name='varaus[tyyppi]']").click(function(){
			updateForm( $("input[@name='varaus[kesto]']:checked").val() );
		});

		function updateAvailability( link ) {
				
				link = link.replace("?","");
				
				$.getJSON("varausJSON.php", link + "&length=" + length, function(data) {
						options = data;
						updateForm( null );
						$("#lomake em").text( "*" ); //tyhjennet��n virheilmot
						$('#tiedot:visible').scrollTo(1000);
				});
		}
		
		function updateForm( kesto  ) {
				
				if ( options['to_su'] == false && options['su_to'] == false && options['week'] == false ) {
					$("#tiedot_incomplete").show();
					$("#tiedot").hide();
				} else {
					$("#tiedot_incomplete").hide();
					$("#tiedot").show();
				
					if ( options['to_su'] == false ) $("#to_su").hide();
					else $("#to_su").show();
	
					if ( options['su_to'] == false ) $("#su_to").hide();
					else $("#su_to").show();
					
					if ( options['week'] == false ) $("#week").hide();
					else $("#week").show();
	
					if ( ! kesto ) {
						if ( $("#to_su").is(":visible") )  $("#kesto_to_su").attr("checked", "checked");
						else if ( $("#week").is(":visible") ) $("#kesto_viikko").attr("checked", "checked");
						else if ( $("#su_to").is(":visible") ) $("#kesto_su_to").attr("checked", "checked");
					
						kesto = $("input[@name='varaus[kesto]']:checked").val()
					}
					
					$("#start_date").text( options['start_date']);
					$("#d_start").val( options['start_date']);
					
					switch ( kesto ) {
					
						case "week":
					
							switch ( options['week'] ) {
								case 1:		$("#puolet").show();
											$("#koko").hide();
											break;
								case 2:		$("#puolet").show();
											$("#koko").show();
											break;
								default:	$("#puolet").hide();
											$("#koko").hide();
											break;
							}
							$("#end_date").text( options['end_date']['week'] );
							$("#d_end").val( options['end_date']['week'] );
							break;
	
						case "su_to":
						
							switch ( options['su_to'] ) {
								case 1:		$("#puolet").show();
											$("#koko").hide();
											break;
								case 2:		$("#puolet").show();
											$("#koko").show();
											break;
								default:	$("#puolet").hide();
											$("#koko").hide();
											break;
							}
							$("#end_date").text( options['end_date']['su_to']);
							$("#d_end").val( options['end_date']['su_to'] );
							break;
							
						case "to_su":
						
							switch ( options['to_su'] ) {
								case 1:		$("#puolet").show();
											$("#koko").hide();
											break;
								case 2:		$("#puolet").show();
											$("#koko").show();
											break;
								default:	$("#puolet").hide();
											$("#koko").hide();
											break;
							}
							$("#end_date").text( options['end_date']['to_su']);
							$("#d_end").val( options['end_date']['to_su'] );
							break;
						default: 			$("#puolet").show();
											$("#koko").show();
											break;
				}
	
				
				if ( ! $("input[@name='varaus[tyyppi]']:checked:visible").val() ) {
						if ( $("#puolet").is(":visible") ) $("#tyyppi1").attr("checked", "checked"); 
						else if ( $("#koko").is(":visible") ) $("#tyyppi2").attr("checked", "checked"); 
				}
				
				setPrice();
				}

		}

		function setPrice() {
			tyyppi = $("input[@name='varaus[tyyppi]']:checked:visible").val();
			kesto = $("input[@name='varaus[kesto]']:checked:visible").val();
			
			if ( isNaN( options['price'][kesto] ) || isNaN ( tyyppi) )  {
				$("#total").text("---");
			} else {
			
				price = ( options['price'][kesto]  ) /* * parseInt(tyyppi) */ ;
				if ( tyyppi == 3 ) price = price * 2 * 0.9;
				$("#total").text( price.toFixed(2) );
				$("#hinta").val( price.toFixed(2) );
			}
		}
});