function cufon() {
    Cufon.replace('.time, .section-name, #section-name, h2, #tally, .html p, .html', { fontFamily: 'Knockout2' });    
}

/*
 * Check at least one box is "checked"
 * @return bool
 */  
function isChecked() {
    if ($("#questions input:enabled:checked").length < 1) {
        $('.unchecked').show();
        return false;
    } else {
        $('.unchecked').hide();
        return true;
    }
}

/*
 * Take the Test process
 */
$(document).ready(function() {
    
    var total = 0;
    var count = 0;
    var section = 0;
    
    window.resetTest = function() {
        total = 0;
        count = 0;
        section = 0;
    };
   
    // Alert if user attempts to leave during test
    $('.popup').closest('a.nyroModalCloseButton').live('click', function() {
        if (!confirm('Warning: Navigating away from this page will lose your Test progress. Click Cancel to continue with the test.')) {
            return false;
        }
        return true;
    });
    
    // Start the Test
    $('#test_form #submit_start').live('click', function() {
        $('#introduction').hide(); // Hide initial Test Introduction
        $('#questions').show(); // Show Section introduction
        $('#section_0').show();        
        $('#time').show();
        return false;
    });
    
    // Continue test
    $('#test_form #submit_next').live('click', function(){
        btn = $(this);
        
        if(btn.hasClass('loading-test')) { return false; }
        btn.addClass('loading-test');
        
        if (count == 0) {
            $('#timer').countdown('resume');
            
            // Section start
            $('#section_' + section + ' .intro').hide();
            $('#section_' + section + ' .question_' + count).show();
            $('#section_' + section + ' .submit').show();
            $('.tally').html(total);
            count++;
            
            // Record test start time
            if(section == 0) {
                $.post('/popup/recordStartTime', { category: $('#question_category').val(), count: $('#question_count').val() });
            }
            // start the countdown
            $('#timer').countdown({
                until: +300,
                compact: true,
                format: 'mS',
                description: '',
                onExpiry: function() {
                    $('#questions').hide();
                    $('#timeup').show();
                }
            });
            cufon();           
            return false;
        }  
        if(!isChecked())
        {

            btn.removeClass('loading-test');
            return false;
        }
        
        // Record answer
        $.post('/popup/recordAnswer', $('#test_form').serialize(), function(data) {
            // If succesful, next question
            if (data.success) {
                // Highlight bar
                $($('.bar').get(total)).addClass('complete');

                // Check if last question - stop timer
                if(!$('#section_'+section+' .question_'+count).length && !$('#section_'+(section+1)).length) {
                    $('#timer').countdown('pause');
                }
                // If no html returned (finish message), show next question
                if (!data.html) {
                    // Hide answered question and increase count
                    $('#section_'+section+' .question_'+(count-1)).empty().hide();

                    // If section finished, move to next section
                    if(!$('#section_'+section+' .question_'+count).length) {
                        $('#section_'+section).hide();
                        section++;
                        $('#section_'+section).show();
                        
                        count = 0;
                        $('#timer').countdown('pause');
                    } else {
                        // Show next question
                        $('#section_'+section+' .question_'+count).show();
                        count++;
                    }
                    total++;
                    $('.tally').html(total);
                    cufon();

                } else {
                    // Show finish message
                    $('#questions').html(data.html);
                }
                
            } else {
                //handle error?
            }
            
            btn.removeClass('loading-test');
            
            return false;
        }, "json");
        
        return false;
    });
    cufon();
});
    
/*
 * Quiz process
 */
$(document).ready(function() {

    var count = 0;
    
    window.resetQuiz = function() {
        count = 0;
    };

    // Start the quiz
    $('#quiz_form #submit_start').live('click', function() {
        $('#introduction').hide(); // Hide initial Test Introduction
        $('#questions').show(); // Show Section introduction
        resetQuiz();
        return false;
    });

    // Continue quiz
    $('#quiz_form #submit_continue, #quiz_form #submit_next').live('click', function() {
        if (count == 0) {
            // Section start
            $('.intro').hide();
            $('.question_'+count).show();
            $('#submit_next').show();
            
            // Record test start time
            if(count == 0) {
                $.post('/popup/recordStartTime', { category: $('#question_category').val(), count: $('#question_count').val() })
            }
            count++;
            $('.tally').html(count);
            cufon();
            
            return false;
        } 
        
        // If no reaction is displayed, submit needs to record answer
        if(!$('.question_'+(count-1) + ' .reaction').length) {
            if(!isChecked()) return false;

            // Record answer
            $.post('/popup/recordAnswer', $('#quiz_form').serialize(), function(data) {
                // If succesful, next question
                if (data.success) {
                    // Highlight bar
                    $($('.bar').get(count-1)).addClass(data.responseClass);

                    // Display question response
                    var resClass = data.responseClass;
                    if(resClass != 'green') {
                        if(resClass == 'red') {
                            $('#submit_next').remove();
                        }
                        $('.question_'+(count-1)).html($('<div></div>', { className: resClass + ' reaction', html: data.response } ));
                        return false;
                    }
                    // If no html returned (finish message), show next question
                    if (!data.html) {
                        // Show next question
                        $('.question_'+(count-1)).hide().find('input').attr('disabled', 'disabled');
                        $('.question_'+count).show();

                        count++; 
                        $('.tally').html(count);
                        cufon();

                    } else {
                        // Show finish message
                        $('#questions').html(data.html);

                        // Show correct message
                        if($('#progress .row').find('.orange').length) {
                            $('#green_message').hide();
                            $('#orange_message').show();
                        }
                        cufon();
                    }
                } else {
                    //handle error?
                }
                return false;
            }, "json");

        } else {
            
            if($('.question_'+count).length){
                // Show next question
                $('.question_'+(count-1)).hide();
                $('.question_'+count).show();

                count++; 
                $('.tally').html(count);
            } else {
                
                // NO QUESTIONS LEFT
                
               $.post('/popup/forceQuizSuccess', null, function(data){
                        // Show finish message
                        $('#questions').html(data);

                        // Show correct message
                        if($('#progress .row').find('.orange').length) {
                            $('#green_message').hide();
                            $('#orange_message').show();
                        }
                        cufon();
                    });
            }
        }
        return false;
    });
});
