var StatusPoster;

(function($) {
    StatusPoster = function() {
        return this.init.apply(this, arguments);
    }

    StatusPoster.prototype = {
        init: function(form) {
            var self = this;
            var form = $(form),
                subject_input = form.find('#statussubject'),
                status_input = form.find('#statusinput'),
                posts = form.parent().find('.updates');

            
            form.submit(function() {
                var subject_val = subject_input.val();
                var status_val = status_input.val();
                var csrf_token = form.find('input[name*="csrfmiddlewaretoken"]').val();

                if (! (status_val && subject_val)) {
                    return false;
                }

                $.post(form.attr('action'), {
                    'subject': subject_val,
                    'status': status_val,
                    'csrfmiddlewaretoken': csrf_token
                }, function(resp) {
                    var div = posts.find('.status-update').eq(0).clone(false);
                    div.find('.blogsubject a').html(resp.subject).attr('href', 'status/'+resp.pk);
                    div.find('.blogtime').html(resp.posted_on + ' ago');
                    div.prependTo(posts);
                    div.css('opacity', 0).show().animate({opacity:1}, 'fast');

                    subject_input.val('');
                    status_input.val('');
                    
                    form.animate({'opacity': 1}, 'fast');
                });

                form.css('opacity', 0.5);


                return false;
            });

            var size_timer = setInterval(function() {
                var input_h = status_input.height(),
                    scroll_h = status_input[0].scrollHeight;
                    
                if (scroll_h > input_h) {
                    status_input.animate({
                        height: 130
                    }, 'fast'); 
                    clearInterval(size_timer);
                }
            }, 300);

        }
    }

    $.fn.StatusPoster = function() {
        return this.each(function() {
            new StatusPoster(this);
        });
    }

})(jQuery);

