/* 
 * Created by David Galvis
 */

/**
 * Instanciates ta new comment item on feed
 * @construct
 * @param int id Id of the switch
 * @param array location Locations the autocomplete will search
 */
function Comment(id, settings)
{
   var _this = this;
   this.id = id;
   jQuery.extend(this, settings);

   this.toggle = jQuery("#comment_" + id);
   this.parent = this.toggle.parents(this.parentClass);

   this.input = jQuery("<textarea id='comment_" + this.id + "' class='comment' >" + this.defaultText + "</textarea>");
   this.send = jQuery('<button style="display:inline">' + trad_comment + '</button>');
   this.textareaMini = jQuery('<div class="feed-mini-comment"><textarea>' + Dico.write_a_comment + '</textarea></div>');
   jQuery(document).ready(function(){
       _this.createContainer();
   });
   /**
    * Events
    */
   // Show/hide container
   this.toggle.click(function(){
      //if (!_this.isCreated)
      //    _this.createContainer();
      _this.textareaMini.toggle();
      _this.container.toggle();
      _this.input.focus();
   });

   this.textareaMini.mousedown(function(e){
       e.preventDefault();
       setTimeout(function(){
       _this.toggle.click();
       },20);
   });

   this.input.blur(function(){
      if (_this.input.val() == '')
          _this.toggle.click();
   });
}

Comment.prototype =
{
    target : '/application/ajax/commentFeed.php',

    responseContainer : null,

    success : null,

    label : null,

    defaultText : '',

    parentClass : 'div',

    isCreated : false,

    showMiniTextarea : false,
    showResponse: true,
    /**
     * CSS of the container
     */
    css : null,

    hide : function(){
        this.container.hide();
    },
    
    reset : function(){
        this.send.attr({disabled : false});
        this.input.attr({disabled : false});
        this.send.text(trad_comment);
        this.input.val('');
    },

    setTarget : function(target){
        this.target = target;
    },

    /**
     * Modifies send text value to the given param
     * @param string txt
     */
    setSendText : function (txt){
        this.send.html(txt);
    },

    setContainerHtml : function (html){
        this.container.html(html);
    },

    /**
     * Customize on succes function (when the comment is posted)
     * @param function fnc
     */
    onSuccess : function(fnc){
        if (typeof fnc == 'function')
            this.success = fnc;
    },

    createContainer : function(){
       var _this = this;
       _this.parent.append("<div class='recommendationFeed comment_container' id='comment_container_" + _this.id + "'></div>");
       _this.container = jQuery("#comment_container_" + _this.id);
       if (_this.label != null)
           _this.container.append("<p>" + _this.label + "</p>");
       _this.container.append(_this.input);
       //this.container.append(this.selection);
       _this.container.append(_this.send);
       //Container css
       if (_this.css != null)
           _this.container.css(_this.css);

       if (_this.showMiniTextarea)
           _this.container.after(_this.textareaMini);

       this.send.click(function()
       {
            if (jQuery.trim(_this.input.val()) == '')
                return false;
            _this.send.attr({disabled : true});
            _this.input.attr({disabled : true});
            _this.send.text(trad_sending);

                jQuery.post(_this.target,
                    {
                        data : jQuery.toJSON(_this.data),
                        comment : _this.input.val()
                    },
                    function(response){
                        _this.reset();
                        if(_this.showResponse)
                        {
                            if (_this.responseContainer != null)
                                jQuery( _this.responseContainer).append(response);
                            else
                                _this.container.before(response);
                        }
                        else
                            jQuery(_this.container).html("").hide();
                        if (_this.success != null)
                            _this.success();
                    }
                );
        });

       this.isCreated = true;
    }
};



