function r_editor(tgetUrl,tpostUrl,tfilename,toption){
  if(!toption){ toption =new Array();}
  if(!toption['width']){toption['width']=400;}
  if(!toption['height']){toption['height']=300;}
  var editor = this;
  editor.filename = tfilename;
  editor.getUrl = tgetUrl;
  editor.postUrl = tpostUrl;
  editor.nodeToBeChanged=""
  editor.width = toption['width'];
  editor.height = toption['height'];
  var html='';
  editor.init = function(){
    code = "<span id='file_editor' style='border:solid; width:"+editor.width+"px;height:"+editor.height+"px; float:right;' ><textarea style='height:87%;width:97%;resize:none;border:none;' ></textarea><input type='button' class='send'  value='Send' ><input type='button' class='discard' value='Discard' ></span>";
    html = $(code);
    html.children("textarea").load(editor.getUrl);
    html.children("input.discard").click(function(){
      editor.destroy();
    });
    html.children("input.send").click(function(){
      editor.send();
    });
    return html;
  };
  editor.reload = function(){
    $.get(editor.getUrl,function(data){
      html.children("textarea").text(data);
    });
  };
  editor.send = function(){
    $.post(editor.postUrl,{filename:editor.filename,context:html.children("textarea").val(),authenticity_token:$('meta[name=csrf-token]').attr('content')},null,"script");
  };
  editor.destroy = function(){
    html.remove();
  };
}

$(".r_edit").live("click",function(){
  new_editor = new r_editor($(this).attr("path"),"edit_file",$(this).attr("filename"));
  $(this).after(new_editor.init());
});


$(".r_snapshot").live({
  mouseenter:
    function(){
      $(this).append($("<span></span>").html('<p class="z-preview"><img src="'+$(this).attr("path")+'"width="220" height="19" /></p>'));
    },
  mouseleave:
    function(){
      $(this).find("span:last").remove();
    }
  }
);

$('input.multi_files').live("change", function(){
  $(this).parent().prev("ul").append("<li>" + $(this).val() + "</li>");
  new_node = $(this).parent().clone();
  $(this).parent().css("display","none");
  new_node.children('input.multi_files').val("");
  $(this).parent().before(new_node);
});

$('a.remove_mark').live("click",function(){
  $(this).prev("input").val("true");
  $(this).parent().css("text-decoration","line-through")
  $(this).parent().removeClass("r_snapshot");
  $(this).next().remove();
  $(this).remove();
  return false;
});