jQuery(document).ready(function() {
	jQuery("input[id=naam]").autocomplete(
		"/wp-content/plugins/zvs-zwemtijden/zvs-zwemtijden-ajax.php",
		{
			delay:100,
			minChars:2,
			matchSubset:1,
			matchContains:1,
			cacheLength:10,
			onItemSelect:selectItem,
			formatItem:formatItem,
			onItemSelect:SelectItem,
			autoFill:false
		}
	);
	
	jQuery('a.herstel').click(function() {
	  clearForm();
	});

	/*
	//
	//jQuery to setcookie on select checkbox on personlijke tijden pagina
	//
	*/	
	jQuery("input[name=onthoud]").click(function() {

		//with the start of the function, start showing the loading button...
		jQuery("#loading-onthoud").html('<img src="/wp-content/plugins/zvs-zwemtijden/css/loading.gif" alt="loading" title="loading" />');
		jQuery("#loading-onthoud").show();
		
		if (jQuery("input[name=onthoud]").attr("checked")) {
			var naam = jQuery("input[name=naam_onthoud]").val();
			var nonce = jQuery("input[name=nonce]").val();
			jQuery.ajax({
				url: "/wp-content/plugins/zvs-zwemtijden/zvs-zwemtijden-ajax.php",
				type: "POST",
				data: "naam="+naam+"&nonce="+nonce+"&type=setcookie",
				success: function(msg) {
					jQuery("#loading-onthoud")
					.html('<div class="fade">Onthouden...</div>')
					.fadeIn('slow')
					.animate({opacity: 1.0}, 1000)
					.fadeOut('slow')
				}
			});
		} else {
			jQuery.ajax({
				url: "/wp-content/plugins/zvs-zwemtijden/zvs-zwemtijden-ajax.php",
				type: "POST",
				data: "type=setcookie",
				success: function(msg) {
					jQuery("#loading-onthoud")
					.html('<div class="fade">Vergeten...</div>')
					.fadeIn('slow')
					.animate({opacity: 1.0}, 1000)
					.fadeOut('slow')
				}
			});
		}
	});
	
	/*
	//
	//jQuery to fire pChart graph change
	//
	*/	
	jQuery("select[name=slag]").change(function() {
		var height = jQuery("#gezwommentijden").css('height');
		jQuery("#gezwommentijden")
		.html('<img id="loading-large" src="/wp-content/plugins/zvs-zwemtijden/css/loading-large.gif" title="loading" />')
		.css("min-height", height); 
		
		var slag = jQuery("select[name=slag]").val();
		var naam = jQuery("input[name=naam_onthoud]").val();
		jQuery.ajax({
			url: "/wp-content/plugins/zvs-zwemtijden/zvs-zwemtijden-ajax.php",
			type: "POST",
			data: "slag="+slag+"&naam="+naam+"&type=changegraph",
			success: function (data) {
				jQuery("#gezwommentijden")
				.html(data); 
			}
		}); 
	});

/*
	//
	//jQuery to fire pChart graph change
	//
	*/	
	jQuery("select[name=slagvergelijk]").change(function() {
		var height = jQuery("#vergelijktijden").css('height');
		jQuery("#vergelijktijden")
		.html('<img id="loading-large" src="/wp-content/plugins/zvs-zwemtijden/css/loading-large.gif" title="loading" />')
		.css("min-height", height); 
		
		var aant = jQuery("input[name=aantal_vergelijk]").val();
		var namen = "";
		for(t=1;t<=aant;t++) {
			var naam = jQuery("input[name=naam"+t+"]").val();
			namen += "&naam"+t+"="+naam;
		}
		var slag = jQuery("select[name=slagvergelijk]").val();
		
		var naam1 = jQuery("input[name=naam1]").val();
		var naam2 = jQuery("input[name=naam2]").val();
		var naam3 = jQuery("input[name=naam3]").val();
		var naam4 = jQuery("input[name=naam4]").val();
		
		jQuery.ajax({
			url: "/wp-content/plugins/zvs-zwemtijden/zvs-zwemtijden-ajax.php",
			type: "POST",
			data: "slag="+slag+namen+"&type=changegraphvergelijk",
			success: function (data) {
				jQuery("#vergelijktijden")
				.html(data); 
			}
		}); 
	});		
});

function selectItem(li) {
	jQuery("input#naam").focus();
}

function SelectItem() {}
function formatItem(row) {return row[0];}

function clearForm(form) {
  // iterate over all of the inputs for the form
  // element that was passed in
  jQuery(':input', form).each(function() {
 var type = this.type;
 var tag = this.tagName.toLowerCase(); // normalize case
 // it's ok to reset the value attr of text inputs,
 // password inputs, and textareas
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = "";
 // checkboxes and radios need to have their checked state cleared
 // but should *not* have their 'value' changed
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 // select elements need to have their 'selectedIndex' property set to -1
 // (this works for both single and multiple select elements)
 else if (tag == 'select')
   this.selectedIndex = 0;
  });
}