var JLearn =
{
    input: null,
    question: null,
    history: null,
    goodCount: 0,
    badCount: 0,
    lettersCount: null,
    quizzDate : {'date du debarquement:2':'1938,1945,1990'},
    quizzJapanese : {
    'あ':'a',
    'い':'i',
    'う':'u',
    'え':'e',
    'お':'o',        
    'か':'ka',
    'き':'ki',
    'く':'ku',
    'け':'ke',
    'こ':'ko',
    'が':'ga',
    'ぎ':'gi',
    'ぐ':'gu',
    'げ':'ge',
    'ご':'go',
    'さ':'sa',
    'し':'shi',
    'す':'su',
    'せ':'se',
    'そ':'so',
    'ざ':'za',
    'じ':'ji',
    'ず':'zu',
    'ぜ':'ze',
    'ぞ':'zo'
    },
    quizzCollection : 
    {
        'monnaie': 'numismate',
        'papillons': 'lipidopterophile',
        'boutons': 'fibulanomiste',
        'coquillages': 'conchyliophile'        
    },
    cards : null,
    getAnswer: function()
    {
        var question = JLearn.question.html();
        if(!question.match(/:/))
        {
            return JLearn.cards[JLearn.question.html()];
        }
        else
        {
            var answerIndex = question.split(':')[1];            
            return JLearn.cards[JLearn.question.html()].split(',')[answerIndex-1];
        }
    },
    showAnswer: function()
    {
      JLearn.answer.val(JLearn.cards[JLearn.question.html()]);
      setTimeout(JLearn.newLetter, 1000);
    },
    check: function(value)
    {
        var goodValue = JLearn.getAnswer();

        if(value.length < goodValue.length)
        {
            if(value.length > 0 && value[0] != goodValue[0])
            {
                JLearn.bad();
            }
            else
            {
                JLearn.reset();            
            }
        }
        else if(value == goodValue)
        {
            JLearn.good();
        }
        else
        {
            JLearn.bad();
        }
    },
    good: function()
    {
        JLearn.goodCount++;
        JLearn.answer.removeClass('bad');
        JLearn.answer.addClass('good');        
        JLearn.updateHistory('good');
    },
    bad: function()
    {
        JLearn.answer.removeClass('good');
        JLearn.answer.addClass('bad');
        if(JLearn.answer.val().length >1)
        {
            JLearn.badCount++;
            JLearn.updateHistory('bad');
        }
    },
    reset: function()
    {
        JLearn.answer.removeClass('bad');
        JLearn.answer.removeClass('good');        
    },
    updateHistory: function(status)
    {
        var percent = Math.floor(JLearn.goodCount/(JLearn.goodCount + JLearn.badCount) * 100);
        $('#stats').html('Accuracy: ' + percent + '%');
        JLearn.history.prepend('<div class="' + status + '" title="correct answer: ' + JLearn.cards[JLearn.question.html()] + '"><span class="to_learn">' + JLearn.question.html() + '</span><span class="answer">' + JLearn.answer.val() + '</span></div>');
        setTimeout(JLearn.newLetter, 400);
    },
    newLetter: function()
    {
        // TBD FIX THIS BAD COUNT
        var stop = Math.floor(Math.random() * JLearn.cardsCount);
        var index = 0;
        jQuery.each(JLearn.cards, function(key, val) {
            if(index++ == stop)
            {
                JLearn.question.html(key).hide().slideDown('slow');
                JLearn.reset();
                JLearn.answer.val('');
                console.log(JLearn.question.val());
                if(JLearn.question.html().length > 3 || JLearn.question.html().match(/:/))
                {
                    $('#learnbox').addClass('maxi');
                }
                else
                {
                    $('#learnbox').removeClass('maxi');
                }
                return;
            }
        });
    },
    initialize: function()
    {
        $(document).ready(function() {
            JLearn.cards = JLearn.quizzJapanese;
            JLearn.question = $('#question');
            JLearn.answer = $('#answer');
            JLearn.history = $('#history');
                        
            $('#help').click(function(){
                  $('#info').slideToggle('slow');
            });

            $('#show_question_box').click(function(){
                  JLearn.question.toggleClass('highlight');
            });
            $('#show_answer_box').click(function(){
                  JLearn.answer.toggleClass('highlight');
            });
                        
            var index = 0;
            jQuery.each(JLearn.cards, function(key, val) {
                index++;
            });
            JLearn.cardsCount = index;
            
            JLearn.newLetter();
            
            JLearn.answer.keyup(function(event){
                console.log('type');
                
                if(this.value == ' ')
                {
                    alert('ok');
                    JLearn.showAnswer();
                }
                else
                {
                    this.value = jQuery.trim(this.value);
                    JLearn.check(this.value);
                }
            });
        });
    }
};

JLearn.initialize();


//jQuery.each(letters, function(i, val) {
   //alert( i + " - " + val);
// });