
var Responder = function (container, historyCount, animationTime) { 

        'use strict';

        var responseContainer = $(container);


        return function (command) {


            responseContainer.queue(function () {
                var commandtxt, heightAdjust;

                commandtxt = $("<div style='opacity: 1;'></div>").html(command);
                heightAdjust = commandtxt.outerHeight();						

                responseContainer.append(commandtxt);
                        
                //debug.log( "[RESPONDER] " + container + " adding - " + command + " - " + heightAdjust ); 
                responseContainer.animate({opacity: 1,
                                   top: '-=' + heightAdjust
                                 }, animationTime);
                     
                responseContainer.dequeue();
            });


            responseContainer.queue(function () { 
                var children, responsecount;
   
                children = responseContainer.children();
                responsecount = children.length;
                //debug.log( "[RESPONDER] " + container + " fading elements " ); 
                children.each(function (index, elem) {
                    var cmdopacity, target; 
                    if (index > responsecount - historyCount) {
                        cmdopacity = (historyCount - responsecount + index) / (historyCount - 1); 
                    } else { 
                        cmdopacity = 0.01;
                    }
                    target = $(elem);
                    if (target.css('opacity') !== cmdopacity) {
                        target.animate({opacity: cmdopacity}, animationTime);
                    }
                });	
                responseContainer.dequeue();						
                    
            });

            responseContainer.queue(function () { 
                  
                if (responseContainer.children().length > historyCount) {
                    var pushdown, newHeight;                     
                    pushdown = responseContainer.children().height();
                    newHeight = parseInt(responseContainer.css('top'), 10) + pushdown;
                     
                    //debug.log( "[RESPONDER] " + container + " removing " + responseContainer.children().first().html() + " - " + pushdown  ); 
                    responseContainer.children().first().remove();
                    responseContainer.css('top', newHeight + "px");
                }
                responseContainer.dequeue();
            });

        };
    };

