/*
 *
 * Base Library Functions
 * ==================================================================================================================*/
(function($) { //---- scope wrapper, encapsulates jQuery
tbrfrontlib = { //---- name of library
	test : function(){ $('body').prepend('tbrfrontlib library present'); }			
	, adopterFrontHandler : {
		adopterApplyInit : function () {
			if ($('#ApplicationCommitmentAgreement').is(':checked')) {
				$( "#adopter-application").show();
			} else {
				$( "#adopter-application").hide();
			}
			
			$( "input[type=radio]" ).fix_radios();
			if ($("input[name='data[Application][house_children]']:checked").val() == 'Yes') {
	 			$('.children-detail').addClass('required');
	 			$('.children-detail').attr('required', 'required');
	 			$('.children-details').show();
	 		} else {
	 			$('.children-detail').removeClass('required');
	 			$('.children-detail').removeAttr('required', 'required');
	 			$('textarea.children-detail').val('');
	 			$('.children-details').hide();
	 		}
			
			if ($("input[name='data[Application][house_fenced]']:checked").val() == 'Yes') {
	 			$('.fence-detail').addClass('required');
	 			$('.fence-detail').attr('required', 'required');
	 			$('.fence-details').show();
	 		} else {
	 			$('.fence-detail').removeClass('required');
	 			$('.fence-detail').removeAttr('required', 'required');
	 			$('.fence-detail').val('');
	 			$('.fence-details').hide();
	 		}
			
			if ($("input[name='data[Application][house_rent]']:checked").val() == 'Rent') {
	 			$('text.rent-detail').addClass('required');
	 			$('text.rent-detail').attr('required', 'required');
	 			$('.rent-details').show();
	 		} else {
	 			$('.rent-detail').removeClass('required');
	 			$('.rent-detail').removeAttr('required', 'required');
	 			$('input.rent-detail:radio').removeAttr('checked');
	 			$('.rent-detail').val('');
	 			$('.rent-details').hide();
	 		}
			
			var returnIndicator = $("input[name='data[Application][newbeagle_reasonsforreturn][]']:checked");
			if (returnIndicator.length > 0) {
	 			$('.return-detail').addClass('required');
	 			$('.return-detail').attr('required', 'required');
	 			$('.return-details').show();
	 		} else {
	 			$('.return-detail').removeClass('required');
	 			$('.return-detail').removeAttr('required', 'required');
	 			$('.return-detail').val('');
	 			$('.return-details').hide();
	 		}
			
			if ($("input[name='data[Application][interest_tbrhistory]']:checked").val() == 'Yes') {
	 			$('.tbr-detail').addClass('required');
	 			$('.tbr-detail').attr('required', 'required');
	 			$('.tbr-details').show();
	 		} else {
	 			$('.tbr-detail').removeClass('required');
	 			$('.tbr-detail').removeAttr('required', 'required');
	 			$('.tbr-detail').val('');
	 			$('.tbr-details').hide();
	 		}
			
			var puppyIndicator = $('#ApplicationInterestDogAge8-16Weeks-Puppy');
	 		if (puppyIndicator.is(':checked')) {
	 			$('input.puppy-question:radio').addClass('required');
	 			$('input.puppy-question:radio').attr('required', 'required');
	 			$('#puppy-specific-questions').show();
	 		} else {
	 			$('.puppy-question').removeClass('required');
	 			$('.puppy-question').removeAttr('required', 'required');
	 			$('input.puppy-question:radio').removeAttr('checked');
	 			$('textarea.puppy-question').val('');
	 			$('#puppy-specific-questions').hide();
	 		}
			
 			$('#ApplicationCommitmentAgreement').change(function(){
				var commitmentIndicator = $(this);
				if (commitmentIndicator.is(':checked')) {
					$( "#adopter-application").show();
				} else {
					$( "#adopter-application").hide();
				}
			});
			$('#ApplicationInterestDogAge8-16Weeks-Puppy').change(tbrfrontlib.adopterFrontHandler.togglePuppySection);
			$("input[name='data[Application][house_children]']").change(tbrfrontlib.adopterFrontHandler.toggleChildrenSection);
			$("input[name='data[Application][house_fenced]']").change(tbrfrontlib.adopterFrontHandler.toggleFenceSection);
			$("input[name='data[Application][house_rent]']").change(tbrfrontlib.adopterFrontHandler.toggleRentSection);
			$("input[name='data[Application][interest_tbrhistory]']").change(tbrfrontlib.adopterFrontHandler.toggleTBRSection);
			$("input[name='data[Application][newbeagle_reasonsforreturn][]']").change(tbrfrontlib.adopterFrontHandler.toggleReturnSection);
		}
	 	, togglePuppySection : function () {
	 		var puppyIndicator = $(this);
	 		if (puppyIndicator.is(':checked')) {
	 			$('input.puppy-question:radio').addClass('required');
	 			$('input.puppy-question:radio').attr('required', 'required');
	 			$('#puppy-specific-questions').show();
	 		} else {
	 			$('.puppy-question').removeClass('required');
	 			$('.puppy-question').removeAttr('required', 'required');
	 			$('input.puppy-question:radio').removeAttr('checked');
	 			$('textarea.puppy-question').val('');
	 			$('#puppy-specific-questions').hide();
	 		}
	 	}
	 	, toggleChildrenSection : function () {
	 		if ($("input[name='data[Application][house_children]']:checked").val() == 'Yes') {
	 			$('.children-detail').addClass('required');
	 			$('.children-detail').attr('required', 'required');
	 			$('.children-details').show();
	 		} else {
	 			$('.children-detail').removeClass('required');
	 			$('.children-detail').removeAttr('required', 'required');
	 			$('textarea.children-detail').val('');
	 			$('.children-details').hide();
	 		}
	 	}
	 	, toggleFenceSection : function () {
	 		if ($("input[name='data[Application][house_fenced]']:checked").val() == 'Yes') {
	 			$('.fence-detail').addClass('required');
	 			$('.fence-detail').attr('required', 'required');
	 			$('.fence-details').show();
	 		} else {
	 			$('.fence-detail').removeClass('required');
	 			$('.fence-detail').removeAttr('required', 'required');
	 			$('.fence-detail').val('');
	 			$('.fence-details').hide();
	 		}
	 	}
	 	, toggleRentSection : function () {
	 		if ($("input[name='data[Application][house_rent]']:checked").val() == 'Rent') {
	 			$('text.rent-detail').addClass('required');
	 			$('text.rent-detail').attr('required', 'required');
	 			$('.rent-details').show();
	 		} else {
	 			$('.rent-detail').removeClass('required');
	 			$('.rent-detail').removeAttr('required', 'required');
	 			$('input.rent-detail:radio').removeAttr('checked');
	 			$('.rent-detail').val('');
	 			$('.rent-details').hide();
	 		}
	 	}
	 	, toggleTBRSection : function () {
	 		if ($("input[name='data[Application][interest_tbrhistory]']:checked").val() == 'Yes') {
	 			$('.tbr-detail').addClass('required');
	 			$('.tbr-detail').attr('required', 'required');
	 			$('.tbr-details').show();
	 		} else {
	 			$('.tbr-detail').removeClass('required');
	 			$('.tbr-detail').removeAttr('required', 'required');
	 			$('.tbr-detail').val('');
	 			$('.tbr-details').hide();
	 		}
	 	}
	 	, toggleReturnSection : function () {
	 		var returnIndicator = $("input[name='data[Application][newbeagle_reasonsforreturn][]']:checked");
			if (returnIndicator.length > 0) {
	 			$('.return-detail').addClass('required');
	 			$('.return-detail').attr('required', 'required');
	 			$('.return-details').show();
	 		} else {
	 			$('.return-detail').removeClass('required');
	 			$('.return-detail').removeAttr('required', 'required');
	 			$('.return-detail').val('');
	 			$('.return-details').hide();
	 		}
	 	}
	}
	, general : {
		
	}
}; //---- lib
})(jQuery); //---- scope wrapper, encapsulates jQuery

$.fn.fix_radios = function() {
	  function focus() {
	    if ( !this.checked ) return;
	    if ( !this.was_checked ) {
	      $( this ).change();
	    }
	  }

	  function change( e ) {
	    if ( this.was_checked ) {
	      e.stopImmediatePropagation();
	      return;
	    }
	    $( "input[name=" + this.name + "]" ).each( function() {
	      this.was_checked = this.checked;
	    } );
	  }
	  return this.focus( focus ).change( change );
	}

