$(function(){
  $('#editable-overlay').overlay({
    effect: 'apple'  
  });
   
  $('#cancel-editable').click(function(){
    $('#editable-overlay').overlay().close();  
    return false;
  });


  $('.editable-container').hover(function(){
    $(this).addClass('editable-active');
        
      link = '<a class="editable" style="float:right" href="#' + this.id + '">'
            + '<img src="/img/actions/editable.png" /></a>'; 
      $(this).prepend(link);
    }, function(){
      $(this).removeClass('editable-active');
      $('.editable',this).remove();
    });

  $('.editable').live('click',function(){
    path = $(this).attr('href').replace(/^#/,'');
    k = '?iekludge=' + (new Date).getTime().toString() + Math.floor(Math.random()*1000000).toString();
    $.get(path + k, function(ret){
      $('#update-editable').attr('action',path).find('textarea').val(ret);
      $('#editable-overlay').overlay().load();
    });
    return false;
  });

  $('#update-editable').submit(function(){
    path = $(this).attr('action');
    new_content = $(this).find('textarea').val();
    $.post(path, {'_method':'put', 'content': new_content},function(ret){
      path = path.replace(/\//g,'\\/');
      $('#' + path).html(ret);
      $('#editable-overlay').overlay().close(); 
    });
    return false;
  });
});


