var HBRT = {};

$(document).ready(function(){
    $("#nav li").hover(
        function(){ $("ul", this).fadeIn('fast'); }, 
        function() { $("ul", this).fadeOut('fast'); } 
    );
    if (document.all) {
        $("#nav li").hoverClass ("sfHover");
    }
});


$.fn.hoverClass = function(c) {
    return this.each(function(){
        $(this).hover( 
            function() { $(this).addClass(c);  },
            function() { $(this).removeClass(c); }
        );
    });
};

HBRT.Filter = function() {
    var filter_form, programs, filters, cmi;
    return {
        init: function() {
            this.filters = {'locations': []};
            this.filter_form = $('#filter_form');
            this.filter_form.submit(this.check_submit);
        },
        check_submit: function(e) {
            var filters = HBRT.Filter.filters;
            // clear errors
            var errors = [];
            $('.field_with_errors').removeClass('field_with_errors');
            $('#program_errors').hide();
            
            // counties
            filters['locations'] = [];
            $('p.locations input:checked').each(function(){
                filters['locations'].push($(this).val());
            });
            if(filters['locations'].length == 0) {
                errors.push('Please select a location.');
                $('p.locations').addClass('field_with_errors');                
            }
            
            // income
            filters['income'] = parseInt($('#f_income').val().replace(/[^0-9\.]+/g,''));
            if(!filters['income'] > 0) {
                errors.push('Please enter your total income.');
                $('#f_income').addClass('field_with_errors');
            }
            
            // household size
            filters['household'] = $('#f_household').val();
            
            // first-time
            filters['first_time'] = $('input[name=first_time]:checked').val();

            if(errors.length > 0) {
                jQuery.each(errors, function(idx,val) {
                    errors[idx] = '<li>' + val + '</li>';
                })
                $('#program_errors').html('<ul>' + errors.join('') + '</ul>').show();
            }
            else {
                HBRT.Filter.run();
            }
            return false;
        },
        run: function() {
            var filters = HBRT.Filter.filters;
            
            var selected_programs = jQuery.grep(HBRT.Filter.programs, function(program) {
              // First-time
              if(filters['first_time'] == 'no' && program['first-time-boolean'] == 'Yes') {
                  return false;
              }

              // Income
              if(filters['income'] >= HBRT.Filter.cmi[filters['household']]) {
                  return false;
              }              
              
              // Locations
              var locations_overlap = HBRT.Filter.filter_has_locations(program);
              if(!locations_overlap) {
                  return false;
              }
              return true;
            });
            if(selected_programs.length != 0) {
                var p_disp = ['<h3>Search results</h3>'];
                p_disp.push('<ul class="program_list">');
                jQuery.each(selected_programs,function(idx,program){
                    p_disp.push('<li>');
                    p_disp.push('<h4><a href="/program/' + program.key + '">' + program['program-name'] + '</a></h4>');
                    p_disp.push('<p>' + program['program-description'] + '</p>');
                    p_disp.push('</li>');
                });
                p_disp.push('</ul>');
                $('#filter_results').html(p_disp.join(''));
            }
            else {
                $('#filter_results').html('<p>No programs were available according to the information selected. Please contact <a href="mailto:info@homebuyersroundtable.org">info@homebuyersroundtable.org</a> if you have questions. You will also find more information on down payment assistance programs by attending a Homebuyer Education Seminar. Please check our calendar for upcoming classes.</p>');
            }
            $('#filter_results').show();
        },
        filter_has_locations: function(program) {
            for(var i=0;i< program['county-list'].length; i++) {
                var county = program['county-list'][i];
                if(jQuery.inArray(county, HBRT.Filter.filters['locations']) != -1) {
                    return true;
                }
            }
           return false;
        }
    };
}();

