
(function($) {

  $.fn.simpledit = function(conf) {
  
    var conf= conf;  
    var click_text = 'Для редактирования дважды кликните мышкой';
   
    return $.each(this, function() {
    
      if ($(this).text() == '') $(this).text(click_text);
    
      var settings = conf;
      
      $(this).bind('dblclick', function() {
      
        var $this = $(this);
        var prev;
        
        if ($this.find('textarea').size() > 0) return;
        
        prev = $this.html();
        $this.html('<div style="height: 200px; text-align: center; padding: 90px 0 0 0;"><img src="/i/lightbox/lightbox-ico-loading.gif"></div>');

        $.post(settings['source'], {id:$this.attr('id')}, function(response) {
          $this.html('<div style="margin: 10px 0 10px 0">'+
                         '<textarea style="width:100%; height: '+settings['height']+'px; display: block;">'+response+'</textarea>'+
                         '<input class="simple-submit" type="button" value="'+settings['submit']+'">&nbsp;'+
                         '<input class="simple-cancel" type="button" value="'+settings['cancel']+'">'+
                         '</div>');
                         
          $this.find('.simple-cancel').click(function() {
            $(this).parent().parent().html(prev);
          });
          
          $this.find('.simple-submit').click(function() {
          
            var $parent = $(this).parent().parent();
            var value = $parent.find('textarea').val();
            var id = $parent.attr('id');

            $parent.html('<div style="height: 200px; text-align: center; padding: 90px 0 0 0;"><img src="/i/lightbox/lightbox-ico-loading.gif"></div>');
            
            $.post(settings['save'], {id:id, value:value}, function(response) {
              $parent.html(response);
            });
          });         
          
          
        });
        


        
      });
      

      
      
      return this;
    });
    
  };

})(jQuery);