$(document).ready( function(){
	
	// liity sivun javascriptit	
	
	// field check on submit
	$('#liity').submit( function(){
		$ok = true;
		$(this).children('input[class*="must"]').each( function(){
			// loop through all input fields with class "must" and validate that they have input
			if ( $(this).val() == '' ){
				// if there is input missing, give that input tag a "missing" class for visuals
				$(this).addClass('missing');
				$(this).blur( function(){
					// if a input was empty, give that field a eventlistener for losing focus
					// upon looing focus, if the value is no longer empty, remove the "missing" class
					if ( $(this).val() != '' )
						$(this).removeClass('missing');
				});
				$ok = false;
			}
		});
		
		// submit if all "must" input fields are filled, else abort submit
		if ( $ok )
			return true;
		else
			return false;
	});	
	
	
	
	// adding textarea on check
	// all checkboxes on the page are relevant, so :checkbox is fine
	
	$('input:checkbox').click( function (){		
		var any_checked = false;
		$('input:checked').each( function(){
			// if any input element on the page is checked set flag
			any_checked = true;
		});	
		
		if ( any_checked ){
			// if the box is still hidden, now is the time to fade it in
			if ( $('#details').css("display") == 'none' )
		 		$('#details').fadeIn();
		} else {
			// if none are checked, but the box is still show, fade it out
			if ( $('#details').css("display") == 'block' )
		 		$('#details').fadeOut();
		}
		
	});
	
	
	// kalenteri
	// "lueLisaa" fade in
	$('a[href*="#lueLisaa"]').click(function (){
		if ($(this).attr('class') == 'isHidden') {
			$(this).parentsUntil('tr').parent('tr').next('.lueLisaaTapahtumasta').fadeIn();
			$(this).removeClass('isHidden'); 
		} else {
			$(this).parentsUntil('tr').parent('tr').next('.lueLisaaTapahtumasta').fadeOut();
			$(this).addClass('isHidden');
		}
	});
	
});
