(function($){
    $.fn.memory = function(params)
    {
        var $this = $(this),
            count=0,
            complete = 0,
            temp = [],
            cantcards = 0;

        var settings = $.extend({
            messageWell:null,
            messageWrong:null,
            messageComplete:null
        },params);

        main.call(this);

        function main()
        {
            cantcards =  $('.fn_card', $this).length / 2;
            $this.on('click','.fn_card',effectFlip);
            checkSizeScreen();
            $(window).resize(function() {
                checkSizeScreen();
            });
        }


        function effectFlip() {
            temp[count] = $(this).data('info');
            $(this,$this).removeClass('fn_card');
            count++;
            $(this).addClass('animation-flip');

            if (count == 2) {
                console.log(temp);
                var element1 = temp[0];
                var element2 = temp[1];
                $this.off('click', '.fn_card', effectFlip);

                if (element1 === element2) {
                    complete++;
                    //$('#retro > .content-retro').html($('#'+settings.messageWell).html());
                    //$('#retro').fadeIn();
                }
                else {
                    setTimeout(function(){
                        //$('#retro > .content-retro').html($('#'+settings.messageWrong).html());
                        //$('#retro').fadeIn();
                        $('.card[data-info|="'+temp[0]+'"]',$this).addClass('fn_card').removeClass('animation-flip');
                        $('.card[data-info|="'+temp[1]+'"]',$this).addClass('fn_card').removeClass('animation-flip');
                    }, 3000);
                }

                if(complete == cantcards){
                    $('#retro > .content-retro').html($('#'+settings.messageWell).html());
                    $('#retro').fadeIn();
                }


                setTimeout(function () {
                    $this.on('click', '.fn_card', effectFlip);
                    count = 0;
                    temp = [];
                }, 3000);
            }
        }

        function checkSizeScreen() {
            if(window.innerWidth < 768)
                openModal('size_screen');
            else{
                $('#modal').removeClass().addClass('zoomOut animated').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){             $(this).removeClass().hide();         });}
        }

    }
})(jQuery);