(function($) {
    $(function() {

        var updateMsg = function(type, length, max) {
            var caption = '';
            switch (type) {
                case 'title':
                    caption = 'Page Title';
                    break;
                case 'description':
                    caption = 'Meta Description';
                    break;
            }
            if (length === 0) {
                return 'You are using 0 characters of ' + max + ' recommended by Google for your ' + caption + '.';
            } else if (length > max) {
                return '<strong>You are using more than ' + max + ' Characters. Characters above the suggested count will be capped by Google, we suggest to revise your entry. </strong>';
            } else {
                return length + " characters of " + max + " recommended";
            }
        };

        var updateSeoMsg = function(seomsg, item) {
            seomsg.html(updateMsg(item.attr('data-seo-type'), item.val().length, item.attr('data-seo-length')));
        };

        $('*[data-seo-length]').each(function(k, v) {
            (function(item) {
                var parent = item.parent();
                parent.append('<span class="seomsg"></span>');
                var seomsg = parent.find('.seomsg');
                item.keyup(function() {
                    updateSeoMsg(seomsg, item);
                });
                updateSeoMsg(seomsg, item);
            })($(v));
        });

    });
})(jQuery);