(function ($) {
	$(function () {
		$('.page_fieldsets').each(function () {
			var paged = $(this);

			paged.find('h2').each(function () {
				$(this).click(function () {
					$(this).find('+ fieldset').slideToggle();
				});

				$(this).find('a').show().click(function () {
					$(this).parent().click();
					return false;
				});
			});

			paged.find('h2 + fieldset').each(function () {
				var fieldset                 = $(this);
				fieldset.find('input[type    = text]:last, textarea:last').focus(function () {
					fieldset.parent().next().find('fieldset').slideDown();
				});
				/* This will slide up and down the previous fieldset based on
				focus, and navigation from current fieldset. */
				// fieldset.find('input[type = text]:first, textarea:first').focus(function () {
				//	 	fieldset.parent().prev().find('fieldset').slideUp();
				//	 }).keydown(function (ev) {
				//		 	if (ev.shiftKey && ((ev.which && ev.which == 9) || (ev.which && ev.keyCode == 9))) {
				//			 		fieldset.parent().prev().find('fieldset').slideDown();
				//			 		console.log();
				//			 	}
				//			 });
						});
						paged.find('h2 + fieldset:not(:first)').each(function () {
							var fieldset                  = $(this);
							if (fieldset.find('.error').size() <= 0) {
								fieldset.hide();
							}
						});
					});
				});
				})(jQuery);

