$(document).ready(function(){
    $('#calc_btn').click(function(){
        var doc_height = $(document).height();
        var scrolltop = $(window).scrollTop();
        scrolltop += 100;
        $('#calc_bg').css('height', doc_height + 'px');
        $('#calc_container').css('top', scrolltop + 'px')
        $('#calc_bg').fadeIn(function(){
            $('#calc_container').fadeIn();
        }); 
    });
    
    $('#calc_exit, #calc_bg').click(function(){
        $('#calc_bg').fadeOut();
        $('#calc_container').fadeOut(); 
    });
            
            $('#zillow_submit').click(function(){
               var property_value = $('#calc-property-value').val();
               var dollarsdown = $('#calc-dollarsdown').val();
               var zip = $('#calc-zip').val();
               var loan_type = $('#loan_type').val();
               var query = "price="+property_value+"&dollarsdown="+dollarsdown+"&zip="+zip;
               $('#calc_result').slideDown();
               $('#calc_nums').hide();
               $('#calc_loader').show();
               
               $.ajax({
                    url: '/forms/mortgage/mortgage-calculator/zillow_call.php?callback=?&'+query,
                    dataType: "jsonp",
                    contentType: "application/json",
                    method: "GET",
                    success: function(data){
                        
                                    $('#calc_loader').fadeOut(function(){
                                         $('#calc_nums').fadeIn();
                                     });
                                     
                                     if(loan_type == 'thirty_fixed'){
                                         $('#calc_apr').html($.parseJSON(data.response.thirtyYearFixed.rate)+'%');
                                         $('#calc_emp').html('$'+$.parseJSON(data.response.thirtyYearFixed.monthlyPrincipalAndInterest));
                                     }else if(loan_type == 'fifteen_fixed'){
                                         $('#calc_apr').html($.parseJSON(data.response.fifteenYearFixed.rate)+'%');
                                         $('#calc_emp').html('$'+$.parseJSON(data.response.fifteenYearFixed.monthlyPrincipalAndInterest));
                                     }else {
                                         $('#calc_apr').html($.parseJSON(data.response.fiveOneARM.rate)+'%');
                                         $('#calc_emp').html('$'+$.parseJSON(data.response.fiveOneARM.monthlyPrincipalAndInterest));
                                     }   
                    }
               });
            
                /*
                 $.getJSON('/forms/mortgage/mortgage-calculator/zillow_call.php?'+query, function(data){
                                     $('#calc_loader').fadeOut(function(){
                                         $('#calc_nums').fadeIn();
                                     });
                             
                                     if(loan_type == 'thirty_fixed'){
                                         $('#calc_apr').html($.parseJSON(data.response.thirtyYearFixed.rate)+'%');
                                         $('#calc_emp').html('$'+data.response.thirtyYearFixed.monthlyPrincipalAndInterest);
                                     }else if(loan_type == 'fifteen_fixed'){
                                         $('#calc_apr').html(data.response.fifteenYearFixed.rate+'%');
                                         $('#calc_emp').html('$'+data.response.fifteenYearFixed.monthlyPrincipalAndInterest);
                                     }else {
                                         $('#calc_apr').html(data.response.fiveOneARM.rate+'%');
                                         $('#calc_emp').html('$'+data.response.fiveOneARM.monthlyPrincipalAndInterest);
                                     } 
                                 });
                 */
            return false;
            });
            
            $('#calc_container input:text').focus(function(){
                $(this).addClass('calc_focus');
            });
            $('#calc_container input:text').blur(function(){
                $(this).removeClass('calc_focus');
            });
            
            
            $('#calc_container input:text').keypress(function(){
                //$(this).removeClass('calc_focus');
                var allInputs = $('#calc_container :input');
                var complete = false;
                for(i=0; i<allInputs.length; i++){
                    if(allInputs[i].value == ''){
                        complete = false;
                        break;
                    }else{
                        complete = true;
                    }
                }
                
                if(complete == true){
                   $("#zillow_submit").addClass('submit_ready');
                   $("#zillow_submit").removeAttr('disabled');
                }
                 
            });
            
});
