$(document).ready(function (event) {
    
    function handleChange(selector, associatedRow, useEffects)
    {
        if (selector.is(':checked')) {
            if (useEffects) {
                associatedRow.slideDown('slow');
            } else {
                associatedRow.show();
            }
        } else {
            if (useEffects) {
                associatedRow.slideUp('fast');
            } else {
                associatedRow.hide();
            }
        }
    }
    
    $('input[name*="minimum"]').defaultValue('Min');
    $('input[name*="maximum"]').defaultValue('Max');
    $('input[name*="phone"]').defaultValue('###-###-####');
    
    childrenToggle = $('input[name$="children"]');
    childrenConsiderations = $('textarea[name*="children"]');
    childrenConsiderationsRow = childrenConsiderations.closest('.field');
    childrenToggle.bind('change', function (event) {
        handleChange(childrenToggle, childrenConsiderationsRow, true);
    });
    handleChange(childrenToggle, childrenConsiderationsRow, false);
    
    petsToggle = $('input[name$="pets"]');
    petsConsiderations = $('textarea[name*="pets"]');
    petsConsiderationsRow = petsConsiderations.closest('.field');
    petsToggle.bind('change', function (event) {
        handleChange(petsToggle, petsConsiderationsRow, true);
    });
    handleChange(petsToggle, petsConsiderationsRow, false);
    
    var handleProjectTypeChange = function (selector, useEffects) {
        var projectTypes = ['Purchasing a Plan', 'Custom Home Plan', 'Remodeling'],
            projectSection = $('#project-information');
        
        if ($.inArray(selector.find('option:selected').text(), projectTypes) > -1) {
            if (useEffects) {
                projectSection.slideDown();
            }
            else {
                projectSection.show();
            }
        }
        else {
            if (useEffects) {
                projectSection.slideUp();
            }
            else {
                projectSection.hide();
            }
        }
        
    }
    
    var projectTypeSelectors = $('select[name$="project_type"]');
    projectTypeSelectors.each(function (i) {
        $(this).bind('change', function (event) { 
            handleProjectTypeChange($(this), true); 
        });
        handleProjectTypeChange($(this), false);
    });
    
});